Esempio n. 1
0
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
}
Esempio n. 2
0
func updateSunTracker(component *backend.Component) error {
	sun := component.Internal.(*sunTracker)

	if sun.lastUpdate.Add(1 * time.Hour).After(time.Now()) {
		logrus.Debugf("Component: %s skipping update", component.URN())
		return nil
	}
	sun.lastUpdate = time.Now()

	url := fmt.Sprintf("http://api.sunrise-sunset.org/json?lat=%f&lng=%f&formatted=0", sun.lat, sun.lng)

	timeRise, timeSet, err := sun.getSunState(url)
	if err != nil {
		return err
	}

	// We are 'below_horizon' and need to get the information for the next day
	if time.Now().After(timeSet) {
		url = fmt.Sprintf("http://api.sunrise-sunset.org/json?lat=%f&lng=%f&formatted=0&date=tomorrow", core.Global.Latitude, core.Global.Longitude)

		timeRise, timeSet, err = sun.getSunState(url)
		if err != nil {
			return err
		}
		sun.currentState = "below_horizon"
	} else
	// Its before sunset of today
	if time.Now().Before(timeRise) {
		sun.currentState = "below_horizon"
	} else {
		sun.currentState = "above_horizon"
	}
	sun.nextSet = timeSet.Local()
	sun.nextRise = timeRise.Local()

	return updateState(component)
}
Esempio n. 3
0
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
}