Ejemplo n.º 1
0
def set_lights_by_weather_for_key_and_time_delta(key, time_delta_into_future):
    ip = urlopen("http://ip.42.pl/raw").read()

    url = "https://freegeoip.net/json/{}".format(ip)

    location_json = json.loads(urllib.urlopen(url).read())

    lat = location_json["latitude"]
    lng = location_json["longitude"]

    weather_json = json.loads(
        urlopen("https://api.forecast.io/forecast/2beeada32fb06ea94f0790895259d23c/{},{}".format(lat, lng)).read()
    )

    daily_forecasts = weather_json[key]["data"]

    tomorrow = (datetime.now() + time_delta_into_future).date()

    for day_forecast in daily_forecasts:
        if (datetime.utcfromtimestamp(day_forecast["time"])).date() == tomorrow:
            temperature = (day_forecast["temperatureMax"] + day_forecast["temperatureMin"]) / 2
            precip_intensity = day_forecast["precipIntensity"]
            icon = day_forecast["icon"]

            temp_color_degrees = int((100 - temperature) * 2.4)

            if temp_color_degrees > 240:
                temp_color_degrees = 240
            elif temp_color_degrees < 0:
                temp_color_degrees = 0

            precip_brightness = int(ceil(255 * float(precip_intensity) / 75))

            temp_color = colours.colourForHueDegrees(temp_color_degrees)
            icon_color = filter(lambda weather_color: weather_color.icon == icon, weather_colors)[0].color
            precip_color = colours.colourDict["blue"]
            precip_color["bri"] = precip_brightness

            sendColorRequest(lights[0], temp_color)
            sendColorRequest(lights[1], icon_color)
            sendColorRequest(lights[2], precip_color)
Ejemplo n.º 2
0
def circleAction(lightNumbers, colourList, t):
    for lightNumber in lightNumbers:
        timeStep = int(lightNumber) + t
        index = timeStep % len(colourList)
        bridge_request.sendColorRequest(lightNumber, colourList[index])