Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

Commit

Permalink
cmd: address linter issues
Browse files Browse the repository at this point in the history
Signed-off-by: Paweł Krupa (paulfantom) <[email protected]>
  • Loading branch information
paulfantom committed May 9, 2022
1 parent da68c52 commit 4697810
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,41 +95,39 @@ func onMQTTMessage(client mqtt.Client, message mqtt.Message) {
}

func synchronizer(evok string, interval int) {
for {
response, err := http.Get(evok)
if err != nil {
log.Fatalf("Couldn't connect to EVOK: %v", err)
}
response, err := http.Get(evok)
if err != nil {
log.Fatalf("Couldn't connect to EVOK: %v", err)
}

defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Couldn't read EVOK data: %v", err)
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatalf("Couldn't read EVOK data: %v", err)
}

data := types.GPIOStates{}
err = json.Unmarshal([]byte(contents), &data)
if err != nil {
log.Printf("Failed to unmarshal JSON data from EVOK message: %v\n", err)
}
data := types.GPIOStates{}
err = json.Unmarshal([]byte(contents), &data)
if err != nil {
log.Printf("Failed to unmarshal JSON data from EVOK message: %v\n", err)
}

log.Printf("Got data from evok: %v", data)

for _, sensor := range data.Data {
if sensor.Dev != "temp" && sensor.Dev != "relay" && sensor.Dev != "ai" {
continue
}
topic := topicMapper(sensor.Dev, sensor.Circuit)
value := applyOffset(sensor.Value, topic)
token := MQTTClient.Publish(topic, 0, false, value)
token.Wait()
if token.Error() != nil {
log.Printf("Failed to publish packet: %s", token.Error())
}
}
log.Printf("Got data from evok: %v", data)

time.Sleep(time.Duration(interval) * time.Second)
for _, sensor := range data.Data {
if sensor.Dev != "temp" && sensor.Dev != "relay" && sensor.Dev != "ai" {
continue
}
topic := topicMapper(sensor.Dev, sensor.Circuit)
value := applyOffset(sensor.Value, topic)
token := MQTTClient.Publish(topic, 0, false, value)
token.Wait()
if token.Error() != nil {
log.Printf("Failed to publish packet: %s", token.Error())
}
}

time.Sleep(time.Duration(interval) * time.Second)
}

func main() {
Expand Down Expand Up @@ -179,20 +177,14 @@ func main() {

EvokClient.OnDisconnected = func(err error, socket gowebsocket.Socket) {
log.Println("Disconnected from EVOK server ")
return
}

EvokClient.Connect()
log.Printf("Connected to EVOK on %s\n", *evok)
defer EvokClient.Close()

go synchronizer("http://"+*evok+"/json/all", config.Interval)
log.Printf("Connected to EVOK on %s\n", *evok)

for {
select {
case <-interrupt:
log.Println("interrupt")
EvokClient.Close()
return
}
synchronizer("http://"+*evok+"/json/all", config.Interval)
}
}

0 comments on commit 4697810

Please sign in to comment.