Пример #1
0
func (cm *CodeMatchProtocolHandler) OnRead(data api.ReadRequest) {
	parsed, err := utils.ParseThingFuSerialData(data.GetPayload().(string))

	// Probably not the right thing we're looking for
	if parsed["Protocol"] != "1" {
		return
	}

	if err != nil {
		log.Println(err)
		return
	}
	ser := parsed["Data"].(string)
	if ser == "" {
		log.Println("Something is wrong. Code is nil. Skipping..")
		return
	}

	thing, service, ok := cm.getThing(ser)

	if ok != nil {
		log.Println("Unknown Thing ", ser)
	} else {
		thingManager := cm.GetThingManager()
		t, err := thingManager.GetThingType(thing.Type)
		if err != nil {
			log.Println(err)
		}

		drv := cm.GetFactory().CreateThingAdapter(t.TypeId)
		if drv == nil {
			log.Println("No adapter for thing type " + thing.Type)
			return
		}

		// Sense and Handle Thing Event
		go func() {
			handler := thingManager.GetProtocolHandlerForThing(thing)
			state := drv.OnRead(thing, service, data, handler)

			// We don't want to run rules or fire events too frequently,
			// so check against thing descriptor's Event Update Buffer
			// if we should go ahead
			lastEvent := service.LastEvent
			desc := thing.Descriptor
			if utils.TimeWithinThreshold(lastEvent, desc.EventUpdateBuffer, 5000) {
				service.UpdateLastEvent(time.Now())
				thing.UpdateService(service)
				thingManager.SaveThing(*thing)

				thingManager.Handle(thing, service, state)
			}
		}()
	}
}
func (d *AdapterDigitalHumidityTemperature433) OnRead(dev *api.Thing, service *api.ThingService, data api.ReadRequest, handler api.ProtocolHandler) (state api.State) {
	dec := data.GetAsInt("dhtdata")
	mask := 0x7f
	humidity := mask & (dec >> 16)

	mask = 0xff
	tempHigh := mask & (dec >> 8)
	tempLow := mask & dec

	state = make(map[string]interface{})

	state["h"] = humidity
	state["tH"] = tempHigh - 50
	state["tL"] = tempLow

	return
}
Пример #3
0
func (h *WT450ProtocolHandler) OnRead(data api.ReadRequest) {
	parsed, err := utils.ParseThingFuSerialData(data.GetPayload().(string))
	if err != nil {
		log.Println(err)
		return
	}

	val, _ := strconv.Atoi(parsed["Data"].(string))
	ch := val >> 26
	thing, service, err := h.getThing(strconv.Itoa(ch))

	if err == nil {
		factory := h.GetFactory()
		thingManager := h.GetThingManager()

		drv := factory.CreateThingAdapter("433mhz-wt450")

		go func() {
			data.Put("channel", ch)
			data.Put("dhtdata", val)

			handler := thingManager.GetProtocolHandlerForThing(thing)
			state := drv.OnRead(thing, service, data, handler)

			lastEvent := service.LastEvent
			desc := thing.Descriptor
			if utils.TimeWithinThreshold(lastEvent, desc.EventUpdateBuffer, 5000) {
				service.UpdateLastEvent(time.Now())
				thingManager.SaveThing(*thing)

				thingManager.Handle(thing, service, state)
			}
		}()
	}
}