-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.py
More file actions
163 lines (128 loc) · 4.66 KB
/
clock.py
File metadata and controls
163 lines (128 loc) · 4.66 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
#!/usr/bin/python
import os
import logging
import math
import requests
import signal
import sys
import time
import datetime
from Adafruit_7Segment import SevenSegment
from astral import Astral
segment = SevenSegment(address=0x77)
APP_HOME = os.path.dirname(os.path.realpath(__file__))
FORMAT = '%(asctime)-15s %(levelname)s:%(message)s'
logging.basicConfig(
format=FORMAT, filename=APP_HOME + '/log/clock.err', level=logging.ERROR)
# format=FORMAT, filename=APP_HOME + '/log/clock.log', level=logging.DEBUG)
def receive_signal(signal, frame):
logging.debug('received signal: %s', signal)
segment.clear()
sys.exit(0)
signal.signal(signal.SIGUSR1, receive_signal)
signal.signal(signal.SIGUSR2, receive_signal)
brightness = -1
BRIGHTNESS_DAY = 10
BRIGHTNESS_NIGHT = 0
brightness_checked_at = time.time() - 700
daytime = False
temp_city = 0
# forecast.io configuration
APIKey = ''
LATITUDE = ''
LONGITUDE = ''
FAHRENHEIT = False
city_name = ''
a = Astral()
a.solar_depression = 'civil'
city = a[city_name]
def is_daytime(now):
result = True
try:
sun = city.sun(date=now, local=True)
sun_start = sun['sunrise']
sun_end = sun['sunset']
logging.debug("Sunrise: %s; Sunset: %s" % (sun_start, sun_end))
b_daytime_start = now.hour > sun_start.hour or (
now.hour == sun_start.hour and now.minute > sun_start.minute)
b_daytime_end = now.hour < sun_end.hour or (
now.hour == sun_end.hour and now.minute < sun_end.minute)
logging.debug("b_daytime_start: %s; b_daytime_end: %s" %
(b_daytime_start, b_daytime_end))
result = b_daytime_start and b_daytime_end
except Exception, e:
logging.error('Exception (astral): %s', e)
return result
updated_s = -1
# Continually update the time on a 4 char, 7-segment display
while(True):
now = datetime.datetime.now()
logging.debug('now: %s', now.strftime("%s"))
hour = now.hour
minute = now.minute
second = now.second
second_09 = second % 10
dot = False
if (time.time() > brightness_checked_at + 600):
brightness_checked_at = time.time()
logging.debug('brightness_checked_at: %s', brightness_checked_at)
daytime = is_daytime(now)
if (daytime):
if (brightness != BRIGHTNESS_DAY):
brightness = BRIGHTNESS_DAY
segment.setBrightness(brightness)
else:
if (brightness != BRIGHTNESS_NIGHT):
brightness = BRIGHTNESS_NIGHT
segment.setBrightness(brightness)
if (temp_city == 0 or updated_s < 0 or (updated_s + 120) <= int(now.strftime("%s"))):
try:
URL = 'https://api.forecast.io/forecast/' + APIKey + '/' + LATITUDE + \
',' + LONGITUDE + '?exclude=minutely,hourly,daily,flags,alerts'
forecast = requests.get(URL).json()
logging.debug('forecast: %s', forecast)
temp_city = round(float(forecast["currently"]["temperature"]), 1)
if (not FAHRENHEIT):
temp_city = round((temp_city - 32) * 5 / 9, 1)
logging.debug('Living room temperature: %s %s' %
(temp_city, 'F' if FAHRENHEIT else 'C'))
# if failed requests are counted for daily limit, move this to be updated before the request is made
updated_s = int(now.strftime("%s"))
logging.debug('updated_s: %s', updated_s)
except Exception, e:
logging.error('Exception (temp): %s', e)
if (temp_city == 0 or second_09 <= 6):
dot = False
hour_tens = int(hour / 10)
hour_ones = hour % 10
min_tens = int(minute / 10)
min_ones = minute % 10
else:
dot = True
# show temp if available
negative_temp = temp_city < 0
m_floor = math.floor(abs(temp_city))
logging.debug('m_floor: %s', m_floor)
hour_tens = int(m_floor / 10)
logging.debug('hour_tens: %s', hour_tens)
hour_ones = int(m_floor % 10)
logging.debug('hour_ones: %s', hour_ones)
min_tens = int("%.0f" % ((abs(temp_city) - m_floor) * 10))
logging.debug('min_tens: %s', min_tens)
if (negative_temp):
min_ones = 0xE
else:
if (FAHRENHEIT):
min_ones = 0xF
else:
min_ones = 0xC
# Set hours
segment.writeDigit(0, hour_tens) # Tens
segment.writeDigit(1, hour_ones, dot) # Ones
# Set minutes
segment.writeDigit(3, min_tens) # Tens
segment.writeDigit(4, min_ones) # Ones
# Toggle colon
segment.setColon(not dot and second % 2) # Toggle colon at 1Hz
# Wait one second
time.sleep(1)