func updateMPD(comp *backend.Component) error { mpd := comp.Internal.(*mpdClientComponent) if !mpd.connected { defer mpd.connect() return nil } status, err := mpd.client.Status() if err != nil { mpd.connected = false return err } song, err := mpd.client.CurrentSong() if err != nil { mpd.connected = false return err } res := map[string]interface{}{ "song": song, "status": status, } json, err := config.RenderJson(res) if err != nil { return err } if json != mpd.lastJSONString { mpd.lastJSONString = json comp.StateSink <- res } return nil }
func updateWeather(component *backend.Component) error { weather := component.Internal.(*weatherTracker) nextCheck := weather.lastCheck.Add(time.Duration(weather.delay) * time.Minute) if time.Now().Before(nextCheck) { logrus.Debugf("Component: %s skipping update. Next check at %s", component.URN(), nextCheck.Local()) return nil } coord := owm.Coordinates{ Longitude: core.Global.Longitude, Latitude: core.Global.Latitude, } if err := weather.current.CurrentByCoordinates(&coord); err != nil { logrus.Warnf("Component: %s failed to update current weather conditions: %s", component.URN(), err) return err } json, err := config.RenderJson(weather.current) if err != nil { logrus.Warnf("Component: %s failed to update current weather conditions: %s", component.URN(), err) return err } if json != weather.lastJSON { weather.lastJSON = json component.StateSink <- weather.current } weather.lastCheck = time.Now() return nil }
func updateNetgear(comp *backend.Component) error { net := comp.Internal.(*netgearComponent) start := time.Now() if net.isUpdating { logrus.Infof("Component: %s ignoring polling request. update still running", comp.URN()) return nil } net.isUpdating = true defer func() { net.isUpdating = false }() logrus.Debugf("Component: %s Updating attached devices", comp.URN()) res, err := net.router.Login() if err != nil { return err } if res { device, err := net.router.GetAttachedDevices() var preparedDevices []netgear.AttachedDevice if err != nil { return err } for i, dev := range device { if net.knownDevices[dev.Mac] != "" { if net.trackOnlyKnown { dev.Name = net.knownDevices[dev.Mac] preparedDevices = append(preparedDevices, dev) } else { device[i].Name = net.knownDevices[dev.Mac] } } } if net.trackOnlyKnown { device = preparedDevices } str, err := config.RenderJson(device) if err != nil { return err } net.last = str comp.StateSink <- device } else { return fmt.Errorf("Failed to login") } logrus.Debugf("Component: %s updated connected devices in %s", comp.URN(), time.Now().Sub(start)) return nil }
func apiRule(call otto.FunctionCall) otto.Value { obj := call.Argument(0).Object() nameValue, err := obj.Get("name") if err != nil { logrus.Errorf("Error: %v", err) return otto.Value{} } name := nameValue.String() rule := &JsRule{ Name: name, VM: call.Otto, } obj.Call("init") call.Otto.Set(rule.Name, obj) logrus.Infof("Registered javascript: %#v", rule) eventbus.EventBus.Subscribe(eventbus.STATE_CHANGED, func(event eventbus.Event) { data, _ := config.RenderJson(event) o, err := call.Otto.Object(rule.Name) if err != nil { logrus.Warnf("JavaScript-Error: %s: %s error=%s", rule.Name, o, err) } val, err := call.Otto.Object("(" + data + ")") if err != nil { logrus.Warnf("JavaScript-Error: %s: %s error=%s", rule.Name, val, err) } resultValue, err := o.Call("onChange", val) if err != nil { logrus.Warnf("JavaScript-Error: %s: %s error=%s", rule.Name, resultValue, err) } result := resultValue.String() if rule.result != result { logrus.Infof("Sending update %s -> %s (old: %s)", rule.Name, result, rule.result) event := eventbus.Event{ Topic: eventbus.STATE_CHANGED, Origin: "rule." + rule.Name, Error: nil, Data: result, } eventbus.EventBus.Publish(event) rule.result = result } }) return otto.Value{} }
func apiSubscribe(call otto.FunctionCall) otto.Value { topic := call.Argument(0).String() func_name := call.Argument(1).String() logrus.Infof("[Plugins/rules] javascript vm subscibed to %s", topic, func_name) eventbus.EventBus.Subscribe(topic, func(event eventbus.Event) { data, _ := config.RenderJson(event) jsCmd := fmt.Sprintf("(%s.bind(Home))(%s)", func_name, data) result, err := call.Otto.Run(jsCmd) if err != nil { logrus.Warnf("JavaScript-Error: %s error=%s", result, err) } }) return otto.Value{} }