2019-11-30 20:49:19 +00:00
|
|
|
package mqttdevice
|
|
|
|
|
|
|
|
import (
|
2019-12-27 13:47:55 +00:00
|
|
|
"encoding/json"
|
2019-11-30 20:49:19 +00:00
|
|
|
"fmt"
|
2019-12-21 17:13:58 +00:00
|
|
|
"github.com/cyrilix/robocar-base/types"
|
2019-11-30 20:49:19 +00:00
|
|
|
MQTT "github.com/eclipse/paho.mqtt.golang"
|
|
|
|
"log"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Publisher interface {
|
|
|
|
Publish(topic string, payload MqttValue)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Subscriber interface {
|
|
|
|
Subscribe(topic string, mh MQTT.MessageHandler)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MQTTPubSub interface {
|
|
|
|
Publisher
|
|
|
|
Subscriber
|
|
|
|
}
|
|
|
|
|
|
|
|
type pahoMqttPubSub struct {
|
|
|
|
client MQTT.Client
|
2019-12-31 13:04:55 +00:00
|
|
|
qos int
|
|
|
|
retain bool
|
2019-11-30 20:49:19 +00:00
|
|
|
}
|
|
|
|
|
2019-12-31 13:04:55 +00:00
|
|
|
func NewPahoMqttPubSub(client MQTT.Client, qos int, retain bool) MQTTPubSub {
|
|
|
|
p := pahoMqttPubSub{client: client, qos: qos, retain: retain}
|
2019-11-30 20:49:19 +00:00
|
|
|
return &p
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:04:55 +00:00
|
|
|
func Connect(uri, username, password, clientId string) (MQTT.Client, error) {
|
2019-11-30 20:49:19 +00:00
|
|
|
//create a ClientOptions struct setting the broker address, clientid, turn
|
|
|
|
//off trace output and set the default message handler
|
2019-12-31 13:04:55 +00:00
|
|
|
opts := MQTT.NewClientOptions().AddBroker(uri)
|
|
|
|
opts.SetUsername(username)
|
|
|
|
opts.SetPassword(password)
|
|
|
|
opts.SetClientID(clientId)
|
2019-11-30 20:49:19 +00:00
|
|
|
opts.SetAutoReconnect(true)
|
|
|
|
opts.SetDefaultPublishHandler(
|
|
|
|
//define a function for the default message handler
|
|
|
|
func(client MQTT.Client, msg MQTT.Message) {
|
|
|
|
fmt.Printf("TOPIC: %s\n", msg.Topic())
|
|
|
|
fmt.Printf("MSG: %s\n", msg.Payload())
|
|
|
|
})
|
|
|
|
|
|
|
|
//create and start a client using the above ClientOptions
|
2019-12-31 13:04:55 +00:00
|
|
|
client := MQTT.NewClient(opts)
|
|
|
|
if token := client.Connect(); token.Wait() && token.Error() != nil {
|
|
|
|
return nil, fmt.Errorf("unable to connect to mqtt bus: %v", token.Error())
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish message to broker
|
|
|
|
func (p *pahoMqttPubSub) Publish(topic string, payload MqttValue) {
|
|
|
|
tokenResp := p.client.Publish(topic, byte(p.qos), p.retain, string(payload))
|
|
|
|
if tokenResp.Error() != nil {
|
|
|
|
log.Fatalf("%+v\n", tokenResp.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register func to execute on message
|
|
|
|
func (p *pahoMqttPubSub) Subscribe(topic string, callback MQTT.MessageHandler) {
|
|
|
|
tokenResp := p.client.Subscribe(topic, byte(p.qos), callback)
|
|
|
|
if tokenResp.Error() != nil {
|
|
|
|
log.Fatalf("%+v\n", tokenResp.Error())
|
2019-11-30 20:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type MqttValue []byte
|
|
|
|
|
|
|
|
func NewMqttValue(v interface{}) MqttValue {
|
|
|
|
switch val := v.(type) {
|
|
|
|
case string:
|
|
|
|
return MqttValue(val)
|
|
|
|
case float32, float64:
|
|
|
|
return MqttValue(fmt.Sprintf("%0.2f", val))
|
|
|
|
case int, int8, int16, int32, int64:
|
|
|
|
return MqttValue(fmt.Sprintf("%d", val))
|
|
|
|
case bool:
|
|
|
|
if val {
|
|
|
|
return []byte("ON")
|
|
|
|
} else {
|
|
|
|
return []byte("OFF")
|
|
|
|
}
|
|
|
|
case []byte:
|
|
|
|
return val
|
|
|
|
case MqttValue:
|
|
|
|
return val
|
|
|
|
default:
|
2019-12-27 13:47:55 +00:00
|
|
|
jsonValue, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("unable to mashall to json value '%v': %v", v, err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return jsonValue
|
2019-11-30 20:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttValue) IntValue() (int, error) {
|
|
|
|
return strconv.Atoi(string(*m))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MqttValue) Float32Value() (float32, error) {
|
|
|
|
val := string(*m)
|
|
|
|
r, err := strconv.ParseFloat(val, 32)
|
|
|
|
return float32(r), err
|
|
|
|
}
|
|
|
|
func (m *MqttValue) Float64Value() (float64, error) {
|
|
|
|
val := string(*m)
|
|
|
|
return strconv.ParseFloat(val, 64)
|
|
|
|
}
|
|
|
|
func (m *MqttValue) StringValue() (string, error) {
|
|
|
|
return string(*m), nil
|
|
|
|
}
|
2019-12-21 17:13:58 +00:00
|
|
|
func (m *MqttValue) DriveModeValue() (types.DriveMode, error) {
|
2019-12-18 20:08:46 +00:00
|
|
|
val, err := m.IntValue()
|
|
|
|
if err != nil {
|
2019-12-21 17:13:58 +00:00
|
|
|
return types.DriveModeInvalid, err
|
2019-12-18 20:08:46 +00:00
|
|
|
}
|
2019-12-21 17:13:58 +00:00
|
|
|
return types.DriveMode(val), nil
|
2019-12-18 20:08:46 +00:00
|
|
|
}
|
2019-11-30 20:49:19 +00:00
|
|
|
func (m *MqttValue) ByteSliceValue() ([]byte, error) {
|
|
|
|
return *m, nil
|
|
|
|
}
|
|
|
|
func (m *MqttValue) BoolValue() (bool, error) {
|
|
|
|
val := string(*m)
|
|
|
|
switch val {
|
|
|
|
case "ON":
|
|
|
|
return true, nil
|
|
|
|
case "OFF":
|
|
|
|
return false, nil
|
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("value %v can't be converted to bool", val)
|
|
|
|
}
|
|
|
|
}
|