Esempio n. 1
0
File: mqtt_1.go Progetto: 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)
			})
		}
	}
}
Esempio n. 2
0
File: main.go Progetto: conas/tno2
//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)
		}
	}()
}