-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
233 lines (204 loc) · 6.47 KB
/
common.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import datetime
import io
import os
import json
def DefaultSettings():
settings = {}
settings['spkr_config'] = {
'spkr_00' : {
'name' : 'Living Room',
'enabled' : True,
'default' : 'nc',
'impedance' : 4,
'gpio_relay_pin' : 22,
'gpio_led_pin' : 17,
'gpio_btn_pin' : 13,
'ir_key' : 'KEY_0'
},
'spkr_01' : {
'name' : 'Kitchen',
'enabled' : True,
'default' : 'no',
'impedance' : 8,
'gpio_relay_pin' : 23,
'gpio_led_pin' : 18,
'gpio_btn_pin' : 14,
'ir_key' : 'KEY_1'
},
'spkr_02' : {
'name' : 'Dining Room',
'enabled' : True,
'default' : 'no',
'impedance' : 8,
'gpio_relay_pin' : 24,
'gpio_led_pin' : 19,
'gpio_btn_pin' : 15,
'ir_key' : 'KEY_2'
},
'spkr_03' : {
'name' : 'Unused',
'enabled' : False,
'default' : 'no',
'impedance' : 8,
'gpio_relay_pin' : 25,
'gpio_led_pin' : 20,
'gpio_btn_pin' : 16,
'ir_key' : 'KEY_3'
}
}
settings['protection'] = {
'name' : 'Speaker Protection',
'enabled' : True,
'default' : 'no', # This is actually the opposite, but needs to be set this way to work properly
'impedance' : 2.5,
'gpio_relay_pin' : 26,
'gpio_led_pin' : 21,
'gpio_btn_pin' : 0,
'ir_key' : 'KEY_P'
}
settings['ui_config'] = {
'pagetheme' : 'default',
'themes' : {
'default' : 'Default Light Theme',
'dark' : 'Dark Theme'
},
'auto_theme' : {
'enabled' : False, # True - Auto Theme Enabled, False - Static Theme Enabled
'mode' : 'sunrise_set', # timeofday, sunrise_set
'theme_night' : 'dark',
'theme_day' : 'default',
'sunset' : '',
'sunrise' : '',
'location' : '',
'latitude' : 0,
'longitude' : 0,
'lastupdate' : ''
},
'auto_LEDs' : {
'enabled' : False,
'mode' : 'timeofday', # timeofday, sunrise_set
'time_on' : '',
'time_off' : '',
'location' : '', # timezone
'lastupdate' : ''
},
}
settings['extapi_config'] = {
'enabled' : False, # Disable external API support by default
'api_key' : ''
}
settings['api_config'] = {
'enabled' : True # Enable local API support by default
}
settings['options'] = {
'ir_input' : False,
'triggerlevel' : 'high', # Relays are active 'high' enable or active 'low' enable
}
return(settings)
def DefaultStates():
states = {}
states = {
'spkr_00' : 'on',
'spkr_01' : 'off',
'spkr_02' : 'off',
'spkr_03' : 'off',
'spkr_pro' : 'off'
}
return(states)
def ReadJSON(target):
# *****************************************
# Read from JSON file
# *****************************************
# Get structure format
if (target == 'settings'):
outputstruct = DefaultSettings()
inputfile = 'settings.json'
else:
print('No Target')
return(False)
try:
json_data_file = os.fdopen(os.open(inputfile, os.O_RDONLY))
json_data_string = json_data_file.read()
json_struct = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Issue with reading states JSON, so create one/write new one
WriteJSON(outputstruct, target)
return(outputstruct)
except:
# Issue with reading states JSON, so create one/write new one
WriteJSON(outputstruct, target)
return(outputstruct)
# Overlay the read values over the top of the default settings
# This ensures that any NEW fields are captured.
for key in outputstruct.keys():
outputstruct[key].update(json_struct.get(key, {}))
return(outputstruct)
def WriteJSON(inputstruct, target):
# *****************************************
# Write all settings to JSON file
# *****************************************
json_data_string = json.dumps(inputstruct, indent=2, sort_keys=True)
# Get structure format
if (target == 'settings'):
outputstruct = DefaultSettings()
outputfile = 'settings.json'
else:
print('No Target')
return(False)
for key in outputstruct.keys():
outputstruct[key].update(inputstruct.get(key, {}))
with open(outputfile, 'w') as json_file:
json_file.write(json_data_string)
def ReadLog():
# *****************************************
# Function: ReadLog
# Input: none
# Output:
# Description: Read event.log and populate
# an array of events.
# *****************************************
# Read all lines of events.log into an list(array)
try:
with open('/logs/events.log') as event_file:
event_lines = event_file.readlines()
event_file.close()
# If file not found error, then create events.log file
except(IOError, OSError):
event_file = open('/logs/events.log', "w")
event_file.close()
event_lines = []
# Initialize event_list list
event_list = []
# Get number of events
num_events = len(event_lines)
for x in range(num_events):
event_list.append(event_lines[x].split(" ",2))
# Error handling if number of events is less than 10, fill array with empty
if (num_events < 10):
for line in range((10-num_events)):
event_list.append(["--------","--:--:--","---"])
num_events = 10
return(event_list, num_events)
def WriteLog(event):
# *****************************************
# Function: WriteLog
# Input: str event
# Description: Write event to event.log
# Event should be a string.
# *****************************************
now = str(datetime.datetime.now())
now = now[0:19] # Truncate the microseconds
logfile = open("logs/events.log", "a")
logfile.write(now + ' ' + event + '\n')
logfile.close()
'''
is_raspberrypi() function borrowed from user https://raspberrypi.stackexchange.com/users/126953/chris
in post: https://raspberrypi.stackexchange.com/questions/5100/detect-that-a-python-program-is-running-on-the-pi
'''
def is_raspberrypi():
try:
with io.open('/sys/firmware/devicetree/base/model', 'r') as m:
if 'raspberry pi' in m.read().lower(): return True
except Exception: pass
return False