This repository was archived by the owner on Dec 23, 2025. It is now read-only.
forked from nebhead/garage-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
162 lines (131 loc) · 4.2 KB
/
common.py
File metadata and controls
162 lines (131 loc) · 4.2 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
import time
import datetime
import os
import json
def DefaultSettings():
settings = {}
settings['misc'] = {
'PublicURL': '',
'CameraURL': '',
}
settings['email'] = {
'ToEmail': 'your_to_email', # E-mail address to send notification to
'FromEmail': 'your_from_email', # E-mail address to log into system
'Username': 'your_username', # Username
'Password' : 'your_password', # Password
'SMTPServer' : 'smtp.gmail.com', # SMTP Server Name
'SMTPPort' : 587, # SMTP Port
'UseTLS': True,
}
settings['notification'] = {
'minutes': 0, # Once-off notification that the door is open
'reminder': 0, # Repeated notification that the door is still open
}
settings['ifttt'] = {
'APIKey': "0", # API Key for WebMaker IFTTT App notification
'notify_on_open': "off",
}
settings['pushover'] = {
'APIKey': '', # API Key for Pushover notifications
'UserKeys': '', # Comma-separated list of user keys
}
return settings
def DefaultStates():
states = {}
states['inputs'] = {
'switch': False # Magnetic Switch
}
states['outputs'] = {
'button': False # Relay Button
}
return states
def ReadSettings():
# *****************************************
# Read Switch States from File
# *****************************************
# Default settings
settings = DefaultSettings()
# Read all lines of states.json into an list(array)
try:
json_data_file = open("settings.json", "r")
json_data_string = json_data_file.read()
user_settings = json.loads(json_data_string)
json_data_file.close()
# Merge each section of the user settings over the defaults
for key in settings.keys():
settings[key].update(user_settings.get(key, {}))
except(IOError, OSError):
# Issue with reading states JSON, so create one/write new one
WriteSettings(settings)
return(settings)
def WriteSettings(settings):
# *****************************************
# Write all control states to JSON file
# *****************************************
json_data_string = json.dumps(settings)
with open("settings.json", 'w') as settings_file:
settings_file.write(json_data_string)
def ReadStates():
# *****************************************
# Read Switch States from File
# *****************************************
# Read all lines of states.json into an list(array)
try:
json_data_file = open("states.json", "r")
json_data_string = json_data_file.read()
states = json.loads(json_data_string)
json_data_file.close()
except(IOError, OSError):
# Issue with reading states JSON, so create one/write new one
states = DefaultStates()
WriteStates(states)
return(states)
def WriteStates(states):
# *****************************************
# Write all control states to JSON file
# *****************************************
json_data_string = json.dumps(states)
with open("states.json", 'w') as settings_file:
settings_file.write(json_data_string)
def ReadLog():
# *****************************************
# Function: ReadLog
# Input: none
# Output: event_list, num_events
# Description: Read event.log and populate
# an array of events.
# *****************************************
# Read all lines of events.log into an list(array)
try:
with open('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('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 20, 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("events.log", "a")
logfile.write(now + ' ' + event + '\n')
logfile.close()