func (o *DefaultThingManager) Handle(thing *api.Thing, service *api.ThingService, state api.State) { facts := new(api.RuleFacts) facts.Thing = thing facts.Service = service facts.Target = thing.GetId() drv := o.factory.CreateThingAdapter(thing.Descriptor.TypeId) // Save latest state of thing if state != nil { state["lastUpdated"] = time.Now() o.dataSource.SaveState(thing, state) } // Save event to DB desc := thing.Descriptor if desc.LogEvents { evt := api.NewEvent() evt.Thing = thing.Id evt.Service = service.Name evt.ShortText, evt.LongText = drv.GetEventText(thing, service, state) evt.Event = api.EVENT_SENSE evt.Data = state o.dataSource.PutEvent(evt) } // Run Rules for this thing o.rulesService.Trigger(api.TRIGGER_THING, facts) }
func (m *MongoDataSource) SaveThing(dev api.Thing) { session := m.session.Copy() defer session.Close() c := session.DB("devices").C("devices") c.UpdateId(dev.GetDatabaseId(), &dev) }
func (o *DefaultThingManager) RegisterThing(d api.Thing) { descriptor, err := o.GetThingType(d.Type) if err != nil { log.Println(err) } d.Descriptor = descriptor o.things[d.GetId()] = d }
// POST http://localhost:8181/api/thing func (app *WebApplication) addThing(w http.ResponseWriter, req *http.Request) { body, err := ioutil.ReadAll(req.Body) if err != nil { log.Println(err) } var payload map[string]interface{} if err := json.Unmarshal(body, &payload); err != nil { panic(err) } // Create Thing Instance t := new(api.Thing) // Create ID if not assigned if payload["Id"] != nil { t.Id = payload["Id"].(string) } else { t.Id = utils.RandomString(7) } t.Name = payload["name"].(string) t.Description = payload["description"].(string) t.Group = "home" t.Type = payload["type"].(string) t.LogEvents = true t.Enabled = true // Handle Atrributes attrs := make(map[string]api.ThingAttributeValue, 0) for k, v := range payload { if strings.HasPrefix(k, "attrib_") { n := strings.Replace(k, "attrib_", "", -1) attr := api.NewThingAttributeValue(n, v) attrs[n] = attr } } t.Attributes = attrs app.thingManager.CreateThing(t) /* DatabaseId Descriptor <Auto> Attributes <attrib:name> */ // Check with Protocol Handler if // this instance already exists and return // error if exists // }
func (m *MongoDataSource) PutThing(t *api.Thing) api.Thing { session := m.session.Copy() defer session.Close() c := session.DB("devices").C("devices") t.DatabaseId = bson.NewObjectId() t.Descriptor = api.ThingType{} err := c.Insert(&t) if err != nil { log.Println(err) } return *t }
func (tv *AdapterLGTV47LS5700) initSession(dev *api.Thing) { log.Printf("initSession") host := dev.GetAttributeValue("host").Value.(string) url := "http://" + host + ":8080/roap/api/auth" pairingKey := dev.GetAttributeValue("pairingKey").Value.(string) payload := "<auth><type>AuthReq</type><value>" + pairingKey + "</value></auth>" client := &http.Client{} post_data := strings.NewReader(payload) req, _ := http.NewRequest("POST", url, post_data) req.Header.Add("Content-Type", "application/atom+xml") _, err := client.Do(req) if err != nil { log.Print(err) } }
func (o *DefaultThingManager) SaveThing(d api.Thing) { go o.dataSource.SaveThing(d) o.things[d.GetId()] = d }