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) } }() } }
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 (o *DefaultThingManager) Cycle() { for _, dev := range o.things { thingType := dev.Type descriptor := dev.Descriptor cycletime := descriptor.CycleTime if cycletime > 0 { if utils.TimeWithinThreshold(dev.LastCycle, cycletime, 0) { drv := o.factory.CreateThingAdapter(thingType) if drv != nil { drv.Cycle(&dev) } dev.UpdateLastCycle(time.Now()) o.SaveThing(dev) } } } }
func (r DefaultRulesManager) Trigger(triggerType uint8, facts *api.RuleFacts) { exec := make(map[string]api.Rule) thing := facts.Thing if triggerType == api.TRIGGER_THING { thingDescriptor := thing.Descriptor target := facts.Target lastEvent := thing.LastEvent if utils.TimeWithinThreshold(lastEvent, thingDescriptor.EventUpdateBuffer, 5000) { thing.UpdateLastEvent(time.Now()) r.thingManager.SaveThing(*thing) for idx, rule := range r.rules { targets := rule.Targets if len(targets) > 0 { contains := false for _, t := range targets { if t == target { contains = true } } if contains { exec[idx] = rule } } } } } else { exec = r.rules } for key_rule, rule := range exec { if !rule.Enabled { continue } if !utils.TimeWithinThreshold(rule.LastRun, rule.Buffer, 5000) { continue } whens := rule.When doAction := true for _, when := range whens { result := r.evaluateWhen(&when, facts, &rule) if result == false { doAction = false break } } if doAction { rule.LastRun = time.Now() thens := rule.Then for _, o := range thens { if rule.Async { go r.executeConsequence(o, thing) } else { r.executeConsequence(o, thing) } } r.rules[key_rule] = rule } } }