-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
202 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package mqttclient | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
MQTT "github.com/eclipse/paho.mqtt.golang" | ||
) | ||
|
||
//Heartbeat for MQTT Client | ||
//Will Subscribe and Publish to MQTT | ||
type Heartbeat struct { | ||
Topic string | ||
Client *MQTTClient | ||
OnHeartbeatMissed func(error) | ||
LastHeartbeat time.Time | ||
stopped bool | ||
MaxDurationMs int64 | ||
} | ||
|
||
func (h *Heartbeat) receivedHeartbeat(client MQTT.Client, msg MQTT.Message) { | ||
h.LastHeartbeat = time.Now() | ||
} | ||
|
||
//Start the heartbeat | ||
func (h *Heartbeat) Start() error { | ||
if h.MaxDurationMs == 0 { | ||
h.MaxDurationMs = 5000 | ||
} | ||
h.stopped = false | ||
client := h.Client.MQTTClient | ||
if !client.IsConnected() { | ||
return fmt.Errorf("Can't start heartbeat. MQTT Client is not connected!") | ||
} | ||
if token := client.Subscribe(h.Topic, uint8(0), h.receivedHeartbeat); token.Wait() && token.Error() != nil { | ||
return token.Error() | ||
} | ||
h.LastHeartbeat = time.Now() | ||
|
||
go func() { | ||
for !h.stopped { | ||
token := h.Client.MQTTClient.Publish(h.Topic, uint8(2), false, "OK") | ||
token.Wait() | ||
err := token.Error() | ||
if err != nil { | ||
h.OnHeartbeatMissed(err) | ||
h.stopped = true | ||
return | ||
} | ||
time.Sleep(1000 * time.Millisecond) | ||
} | ||
}() | ||
|
||
go func() { | ||
for !h.stopped { | ||
duration := time.Now().Sub(h.LastHeartbeat).Nanoseconds() / 1000000 | ||
if duration > h.MaxDurationMs { | ||
h.stopped = true | ||
h.OnHeartbeatMissed(fmt.Errorf("Timeout in heartbeat: %d.", duration)) | ||
return | ||
} | ||
time.Sleep(500 * time.Millisecond) | ||
} | ||
|
||
}() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.