Example #1
0
func Setup(url, deviceTopic string) {
	opts := mqtt.NewClientOptions().AddBroker(url).SetClientID("ClientID")
	opts.SetKeepAlive(20 * time.Second)
	opts.SetPingTimeout(1 * time.Second)

	c := mqtt.NewClient(opts)
	if token := c.Connect(); token.Wait() && token.Error() != nil {
		panic(token.Error())
	}

	deviceOutTopic := str.Concat(deviceTopic, "/o")
	deviceInTopic := str.Concat(deviceTopic, "/i")
	token2 := c.Subscribe(deviceInTopic, 0, func(client mqtt.Client, m mqtt.Message) {
		log.Info("MQTT Dummy - Message received on: ", deviceOutTopic, " data: ", string(m.Payload()))
	})

	if token2.Wait() && token2.Error() != nil {
		os.Exit(1)
	}
}
Example #2
0
File: mqtt_1.go Project: conas/tno2
func (mb *MQTT_1) setup(ctxPath string, wos *server.WotServer) {
	deviceTopic := str.Concat(ctxPath, "/#")
	token2 := mb.client.Subscribe(deviceTopic, 0, mb.eventHandler(ctxPath, wos))
	if token2.Wait() && token2.Error() != nil {
		log.Fatal(token2.Error)
		os.Exit(1)
	}
	log.Info("MQTT_1 Backend: subscribed to device topic -> ", deviceTopic)

	for _, p := range wos.GetDescription().Properties {
		propPath := str.Concat(ctxPath, "/", p.Name)
		wos.OnGetProperty(*p.Name, func() interface{} {
			return mb.values[propPath]
		})

		if *p.Writable {
			wos.OnUpdateProperty(*p.Name, func(payload interface{}) {
				mb.publish(propPath, payload)
			})
		}
	}
}
Example #3
0
func (c *JsonEncoder) Decode(r io.Reader, t interface{}) error {
	data, err := ioutil.ReadAll(r)

	if err != nil {
		return err
	}

	err = json.Unmarshal(data, t)

	if err != nil {
		return errors.New(str.Concat("Error unmarshaling input using ", c.Info(), " codec."))
	}

	return nil
}
Example #4
0
func (sc *SimpleUrlEncoder) Encode(msgType int8, conversationID string, msgName string, data interface{}) []byte {
	d := data.(map[string]interface{})
	ds := str.Concat(msgType, ":", conversationID, ":", msgName, ":", toUrlQ(d))
	return []byte(ds)
}