-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtopless.py
More file actions
190 lines (168 loc) · 6.37 KB
/
Copy pathtopless.py
File metadata and controls
190 lines (168 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Sign up for a free API key at https://home.openweathermap.org/users/sign_up
import argparse
import array
import datetime
import itertools
import os
import requests
import simplejson as json
import sys
import time
from blinkt import set_all, set_pixel, set_brightness, show, clear, get_pixel
try:
while True:
try:
set_pixel(0, 0, 0, 0)
print("Going topless...")
# CONFIGURATION
config = open(os.path.dirname(os.path.abspath(__file__)) + '/config.json').read()
configJson = json.loads(config)
apiKey = configJson["apiKey"]
thresholdTemperatureLow = float(configJson["thresholds"]["temperature"]["low"])
thresholdTemperatureHigh = float(configJson["thresholds"]["temperature"]["high"])
units = configJson["units"].lower().strip()
# ============================================================================
parser = argparse.ArgumentParser()
parser.add_argument('--location', dest="location", help='ZIP Code')
args = parser.parse_args()
# base URL
url = ""
if (args.location):
currentLocation = args.location
url = "https://api.openweathermap.org/data/2.5/forecast?zip=" + currentLocation
else:
currentIP = requests.get("https://ipapi.co/ip").text
# print(currentIP)
currentLocation = requests.get("https://ipapi.co/"+currentIP+"/postal/").text
# print(currentLocation)
url = "https://api.openweathermap.org/data/2.5/forecast?zip=" + currentLocation
# Metric or Imperial?
if (units.startswith("m") or units.startswith("c")):
url = url + "&units=metric"
else:
url = url + "&units=imperial"
# add API key
url = url + "&appid=" + apiKey
#print(url)
while True:
now = datetime.datetime.now()
datestamp = now.strftime("%Y-%m-%d %-I:%M%p")
print("Updating forecast for " + currentLocation + " at: " + datestamp)
timer = time.time()
ledEffect = []
# get forecast
response = requests.get(url).text
clear()
#print('----------')
#print(response)
#print('----------')
ledID = 0
for forecast in (json.loads(response)["list"])[:8]:
forecastAttributes = json.loads(json.dumps(forecast))
forecastDate = forecastAttributes["dt_txt"]
forecastTemp = float(forecastAttributes["main"]["temp"])
forecastTempMinimum = float(forecastAttributes["main"]["temp_min"])
forecastTempMaximum = float(forecastAttributes["main"]["temp_max"])
forecastPressure = float(forecastAttributes["main"]["pressure"]) # hPa
forecastPressureSeaLevel = float(forecastAttributes["main"]["sea_level"]) # hPa
forecastPressureGroundLevel = float(forecastAttributes["main"]["grnd_level"]) # hPa
forecastHumidity = float(forecastAttributes["main"]["humidity"]) # %
for weather in forecastAttributes["weather"]:
forecastWeatherCondition = weather["id"]
forecastWeather = weather["main"]
forecastWeatherDescription = weather["description"]
forecastWeatherIcon = weather["icon"]
try:
forecastClouds = float(forecastAttributes["clouds"]["all"]) # %
except KeyError:
forecastClouds = 0
try:
forecastWindSpeed = float(forecastAttributes["wind"]["speed"])
forecastWindDegrees = float(forecastAttributes["wind"]["deg"])
except KeyError:
forecastWindSpeed = 0
forecastWindDegrees = 0
try:
forecastRain = float(forecastAttributes["rain"]["3h"]) # mm
except KeyError:
forecastRain = 0
try:
forecastSnow = float(forecastAttributes["snow"]["3h"]) # mm
except KeyError:
forecastSnow = 0
ledSet = False
print('Rain: ' + str(forecastRain))
if (forecastRain > 3):
set_pixel(ledID, 0, 0, 255, 0.6)
ledSet = True
ledEffect.append('flash')
elif (forecastRain > 1):
set_pixel(ledID, 0, 0, 255, 0.4)
ledSet = True
ledEffect.append('pulse')
elif (forecastRain > 0.05):
set_pixel(ledID, 0, 0, 128, 0.2)
ledSet = True
ledEffect.append('none')
print('Snow: ' + str(forecastSnow))
print('Temp (Min): ' + str(forecastTempMinimum))
if (forecastSnow > 1 or forecastTempMinimum <= (thresholdTemperatureLow - (thresholdTemperatureLow * 0.20))):
set_pixel(ledID, 255, 255, 255, 0.6)
ledSet = True
ledEffect.append('flash')
elif (forecastSnow > 1 or forecastTempMinimum <= (thresholdTemperatureLow - (thresholdTemperatureLow * 0.10))):
set_pixel(ledID, 255, 255, 255, 0.4)
ledSet = True
ledEffect.append('pulse')
elif (forecastSnow > 0.5 or forecastTempMinimum <= thresholdTemperatureLow):
set_pixel(ledID, 128, 128, 128, 0.2)
ledSet = True
ledEffect.append('none')
print('Temp (Max): ' + str(forecastTempMaximum))
if (forecastTempMaximum >= (thresholdTemperatureHigh + (thresholdTemperatureHigh * 0.20))):
set_pixel(ledID, 255, 128, 0, 0.6)
ledSet = True
ledEffect.append('flash')
elif (forecastTempMaximum >= (thresholdTemperatureHigh + (thresholdTemperatureHigh * 0.10))):
set_pixel(ledID, 255, 128, 0, 0.4)
ledSet = True
ledEffect.append('pulse')
elif (forecastTempMaximum >= thresholdTemperatureHigh):
set_pixel(ledID, 255, 180, 0, 0.2)
ledSet = True
ledEffect.append('none')
if (ledSet == False):
set_pixel(ledID, 0, 128, 0, 0.2)
ledSet = True
ledEffect.append('none')
show()
ledID += 1
print('----------')
while time.time() - timer < 300:
ledToManipulate = 0
while ledToManipulate < 8:
priorPixelValue = (get_pixel(ledToManipulate))
if (ledEffect[ledToManipulate] == 'flash'):
for flash in itertools.repeat(None, 6):
set_pixel(ledToManipulate, 255, 0, 0, 1.0)
show()
time.sleep(0.05)
set_pixel(ledToManipulate, priorPixelValue[0], priorPixelValue[1], priorPixelValue[2], priorPixelValue[3])
show()
elif (ledEffect[ledToManipulate] == 'pulse'):
for flash in itertools.repeat(None, 4):
set_pixel(ledToManipulate, priorPixelValue[0], priorPixelValue[1], priorPixelValue[2], 1.0)
show()
time.sleep(0.1)
set_pixel(ledToManipulate, priorPixelValue[0], priorPixelValue[1], priorPixelValue[2], priorPixelValue[3])
show()
else:
time.sleep(0.05)
ledToManipulate += 1
except Exception as ex:
print("Please wait...")
print(ex)
time.sleep(300)
except KeyboardInterrupt:
set_pixel(0, 0, 0, 0)
sys.exit(1)