-
Notifications
You must be signed in to change notification settings - Fork 0
/
patcher.py
204 lines (187 loc) · 7.08 KB
/
patcher.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
import os
import time
import os.path
from os import path
import datetime
import socket
import json
import sys
import subprocess
import requests
try:
import psutil
except:
os.system('pip3 install psutil')
import psutil
myname = socket.gethostname()
myip = subprocess.check_output(['hostname', '-I'])
webserver = myname == "rpi4-web-server"
whitenoise = myname == "whitenoisepi"
twilled = False
mqtted = False
f = open('/home/pi/config.json')
config = json.load(f)
try:
from twilio.rest import Client
#twilled = True # off for a reason. will remove later
print('we have twilio')
except:
os.system('pip3 install twilio')
twilled = False
print('we do not have twilio')
try:
import paho.mqtt.client as mqtt
mqtted = True
print('we have paho')
except:
os.system('pip3 install paho-mqtt')
mqtted = False
print('we do not have paho')
lookup = {
"rpi4-web-server": "app",
"whitenoisepi": "command_whitenoise",
"addresspi": "mqtt_rgb",
"windowpi": "mqtt_rgb",
"garagecontrolpi": "new_interface",
"kitchenpi": "new_interface",
"dayroompi": "new_interface",
"tablepi": "new_interface",
"hallwaypi": "new_interface",
"thermopi": "thermostat",
"rollerpi": "roller"
}
whatiuse = "command_light"
if myname in lookup.keys():
whatiuse = lookup[myname]
else:
try:
xstart = open('/etc/xdg/lxsession/LXDE-pi/autostart')
if "new_interface" in xstart:
whatiuse = "new_interface"
except:
whatiuse = "command_light"
try:
xstart = open('/etc/rc.local')
if "mqtt_rgb" in xstart:
whatiuse = "mqtt_rgb"
if "command_garage" in xstart:
whatiuse = "command_garage"
except:
whatiuse = "command_light"
def findProcessIdByName(processName):
'''
Get a list of all the PIDs of a all the running process whose name contains
the given string processName
'''
listOfProcessObjects = []
#Iterate over the all the running process
for proc in psutil.process_iter():
try:
pinfo = proc.as_dict(attrs=['pid', 'name', 'create_time'])
# Check if process name contains the given name string.
if processName.lower() in pinfo['name'].lower() :
listOfProcessObjects.append(pinfo)
except (psutil.NoSuchProcess, psutil.AccessDenied , psutil.ZombieProcess) :
pass
return listOfProcessObjects
def mosquittoDo(topic, command):
global received
global result
if mqtted is False:
return
try:
client = mqtt.Client()
client.connect("192.168.2.200")
client.publish(topic,command)
client.disconnect()
except:
print('failed')
return 'OK'
def sendCommand(command):
print("sending command: "+command)
try:
r =requests.get('https://api.idkline.com/pireport/'+command)
print(str(r.status_code))
except:
print('failed to send command')
def heartbeat():
sendCommand(myname + " "+str(myip).split(' ')[0].replace("b'",""))
def log(message):
with open('/home/pi/SMS.log','a') as write_file:
write_file.write(message+'\n')
def sms(message):
log('sms: '+message)
if twilled is False:
return
try:
account_sid = config["account_sid"]
auth_token = config["auth_token"]
client = Client(account_sid, auth_token)
client.messages.create(
messaging_service_sid=config["messaging_service_sid"],
body=message,
to='+19377166465'
)
except:
log("Unexpected error:" + sys.exc_info()[0])
def doCheck():
os.system('cd /home/pi/rpi && git pull --all')
if myname == "mosquitto":
os.system('cd /home/pi/rpi && sudo python3 new_automation.py')
time.sleep(9)
repo_version_file = '/home/pi/rpi/version.json'
local_version_file = '/home/pi/version.json'
with open(repo_version_file, "r") as read_file:
repo_version = json.load(read_file)
print('loaded repo version file')
if path.exists(local_version_file) == False:
with open(local_version_file, "w") as write_file:
write_file.write(json.dumps(repo_version))
print('created new local version file')
with open(local_version_file, "r") as read_file:
local_version = None
try:
local_version = json.load(read_file)
print('loaded local version file')
except:
with open(local_version_file, "w") as write_file:
write_file.write(json.dumps(repo_version))
print('error loading. created local version file')
if webserver is True:
print('local command_center: '+local_version["command_center"]+' repo command_center: '+repo_version["command_center"])
if local_version["command_center"] != repo_version["command_center"]:
with open(local_version_file, "w") as write_file:
write_file.write(json.dumps(repo_version))
print('updated local version file')
sms('building command center on '+myname+' because version updated from '+local_version["command_center"]+' to '+repo_version["command_center"])
os.system('cd /home/pi/rpi/command-center && sudo ng build && sudo mv /home/pi/rpi/command-center/dist/command-center/* /var/www/idkline.com/public_html')
sms('built command center')
if "system_monitor" not in local_version.keys() or local_version["system_monitor"] != repo_version["system_monitor"]:
with open(local_version_file, "w") as write_file:
write_file.write(json.dumps(repo_version))
print('updated local version file')
sms('restarting system_monitor on '+myname+' because version updated from '+local_version["system_monitor"]+' to '+repo_version["system_monitor"])
os.system('cd /home/pi/rpi && sudo killall python3 && sudo python3 system_monitor.py')
sms('system_monitor restarted')
if whatiuse not in local_version.keys() or local_version[whatiuse] != repo_version[whatiuse]:
with open(local_version_file, "w") as write_file:
write_file.write(json.dumps(repo_version))
print('updated local version file')
time.sleep(1)
if webserver is True:
print('copying app')
os.system('sudo cp /home/pi/rpi/new_app.py /var/www/api/app.py && sudo systemctl restart flaskrest.service')
sms('restarting flask on '+myname+' because '+whatiuse+' updated from '+local_version[whatiuse]+' to '+repo_version[whatiuse])
if webserver is False:
try:
mosquittoDo("pi/"+myname+"/status",myname + " "+str(myip).split(' ')[0].replace("b'","")+" restarting at "+datetime.datetime.now().strftime("%m/%d/%Y, %H:%M:%S"))
except:
print('nada')
os.system('sudo reboot now')
try:
sms('restarting '+myname+' because '+whatiuse+' updated from '+local_version[whatiuse]+' to '+repo_version[whatiuse])
except:
print('neine')
exit()
heartbeat()
doCheck()