Skip to content

Commit

Permalink
send function now takes name and timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
leroxyl committed Oct 23, 2019
1 parent 26b7c20 commit 5be0aba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
10 changes: 6 additions & 4 deletions c8y/c8y_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,17 @@ func GetClient(uuid string, tenant string, bootstrapPW string) (mqtt.Client, err
}
}

func Send(c mqtt.Client, valueToSend bool) error {
func Send(c mqtt.Client, name string, value bool, timestamp time.Time) error {
log.Println("sending...")
const timeFormat = "2006-01-02T15:04:05.000Z"
log.Println(timestamp.Format(timeFormat))
var message string
if valueToSend {
if value {
// send true (1)
message = "200,c8y_Bool,B,1"
message = "200,c8y_Switch," + name + ",1,," + timestamp.Format(timeFormat)
} else {
// send false (0)
message = "200,c8y_Bool,B,0"
message = "200,c8y_Switch," + name + ",0,," + timestamp.Format(timeFormat)
}

if token := c.Publish("s/us", 0, false, message); token.Wait() && token.Error() != nil {
Expand Down
6 changes: 5 additions & 1 deletion main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ func main() {
}
defer client.Disconnect(0)

switchName1 := "switch1"
switchName2 := "switch2"
value := true
for {
err = c8y.Send(client, value)
now := time.Now().UTC()
err = c8y.Send(client, switchName1, value, now)
err = c8y.Send(client, switchName2, !value, now)
if err != nil {
log.Fatalf("error: %v", err)
}
Expand Down

0 comments on commit 5be0aba

Please sign in to comment.