Exemple #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)
	}
}
Exemple #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)
		}
	}()
}