-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupdate.py
More file actions
116 lines (98 loc) · 4.17 KB
/
update.py
File metadata and controls
116 lines (98 loc) · 4.17 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import os
import subprocess
import zipfile
import shutil
import time
import const
from shutil import copyfile
def GPIO_setup():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(const.READY,GPIO.OUT)
GPIO.setup(const.SEND,GPIO.OUT)
try:
time.sleep(10) #sleep 10 sec. for better usb modem init ... but i think it doesn't help
GPIO_setup()
count = 20
timenow = time.strftime("%Y-%m-%d %H:%M:%S")
print(timenow)
print("Finding internet... ")
while not const.internet_on() and count > 0:
GPIO.output(const.READY, True)
time.sleep(1)
GPIO.output(const.READY, False)
time.sleep(5)
count -=1
print("No internet... retry "+str(count))
if (const.internet_on()):
#send logs to the server
filename="log.zip"
#copy umtskeeper.log
if (os.path.isfile("/var/log/umtskeeper.log")):
copyfile("/var/log/umtskeeper.log", const.PATH_LOG + "umtskeeper.log")
#remove old zip
if (os.path.isfile(const.PATH_BASE + "log.zip")):
os.remove(const.PATH_BASE + "log.zip")
cmd = ("zip -rq " + const.PATH_BASE + filename + " " + const.PATH_LOG + "*")
print(cmd)
try:
update = subprocess.call(cmd, shell=True)
except:
print("Zip logs failed")
GPIO.output(const.SEND, False)
if (os.path.isfile(const.PATH_BASE + filename)):
try:
print("Upload logs")
cmd = ("curl --connect-timeout 15 --max-time 300 --silent " +
"-F id='" + str(const.getserial()) + "' " +
"-F filename='" + filename + "' " +
"-F filedata=@" + const.PATH_BASE + filename + " " + const.URL_UPDATE + "")
print(cmd)
update = subprocess.call(cmd, shell=True)
#for file in os.listdir(const.PATH_LOG):
# os.remove(os.path.join(const.PATH_LOG,file))
os.remove(const.PATH_BASE + "log.zip")
except:
print("Upload logs error")
#get update
print("Try update...")
cmd = ("curl -L --connect-timeout 15 --max-time 30 " +
"-F id='" + str(const.getserial()) + "' " +
const.URL_UPDATE + " > " + const.PATH_BASE + "update.zip")
print(cmd)
update = subprocess.call(cmd, shell=True)
if (os.path.isfile(const.PATH_BASE + "update.zip")):
try:
with zipfile.ZipFile(const.PATH_BASE + "update.zip", "r") as myzip:
for f in myzip.namelist():
myzip.extract(f, path=const.PATH_UPDATE)
for name in os.listdir(const.PATH_UPDATE):
if (os.path.isfile(const.PATH_UPDATE+name)):
print("copyfile("+const.PATH_UPDATE+name+","+const.PATH_VR+name+")")
copyfile(const.PATH_UPDATE+name, const.PATH_VR+name)
except:
print("Update failed")
for file in os.listdir(const.PATH_UPDATE):
os.remove(os.path.join(const.PATH_UPDATE,file))
#os.remove(const.PATH_BASE + "update.zip")
GPIO.cleanup()
cmd_resend = "stdbuf -oL /usr/bin/python " + const.PATH_VR + "resending.py" + " >> " + const.PATH_LOG + "resending.log"
cmd_vr = "stdbuf -oL /usr/bin/python " + const.PATH_VR + "vr.py" + " >> " + const.PATH_LOG + "vr.log"
print("Run resend: "+cmd_resend)
#proc_resend = subprocess.Popen(cmd_resend.split(), shell=False)
proc_resend = subprocess.Popen(cmd_resend, shell=True)
time.sleep(3)
print("Run vr: "+cmd_vr)
#proc_vr = subprocess.Popen(cmd_vr.split(), shell=False)
proc_vr = subprocess.Popen(cmd_vr, shell=True)
except KeyboardInterrupt:
print("Ctrl-C pressed")
GPIO.cleanup()
#except Exception, e:
# print("Exception: "+ str(e))
# GPIO.cleanup()
#finally:
# GPIO.cleanup()