Beispiel #1
0
func (mb *MQTT_1) eventHandler(ctxPath string, wos *server.WotServer) func(mqtt.Client, mqtt.Message) {
	return func(client mqtt.Client, m mqtt.Message) {
		topic := m.Topic()

		p := PropertyChange{
			Name:  topic[len(ctxPath)+1 : len(topic)],
			Value: string(m.Payload()),
		}

		mb.values[topic] = p.Value
		wos.EmitEvent("property-change", p)
	}
}
Beispiel #2
0
//Implementation of interaction with Web Device
func setupWotServer(s *server.WotServer) {
	s.OnGetProperty("relay", func() interface{} {
		log.Info("OnGetProperty: relay")
		return db["relay"]
	}).OnUpdateProperty("relay", func(newValue interface{}) {
		log.Info("OnUpdateProperty: relay")
		db["relay"] = newValue
	}).OnInvokeAction("throtle-move", func(args interface{}, ph async.ProgressHandler) interface{} {
		log.Info("OnInvokeAction: throtle-move, position: ", args)
		m := args.(map[string]interface{})
		targetPos := int(m["value"].(float64))

		if targetPos < 0 || targetPos > 50 {
			ph.Fail("Invalid throtle position.")
			return nil
		}

		steps := 4
		step := targetPos / steps

		for i := 0; i < steps; i++ {
			ph.Update(&Throtle{ThrotlePosition: i * step})
			time.Sleep(time.Second * 5)
		}

		return Throtle{ThrotlePosition: targetPos}
	})

	//Event generator. For demo purposes event geneator generates some sample events, clients  can subscribe to.
	go func() {
		for {
			s.EmitEvent("critical-temperature-event", &CriticalEvent{EventData: "temperature -> 192°C and rasing."})
			time.Sleep(time.Second * 5)
		}
	}()
}
Beispiel #3
0
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)
			})
		}
	}
}