-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.py
109 lines (91 loc) · 3.7 KB
/
check.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
import urllib, json, datetime, time, smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from email.MIMEBase import MIMEBase
from email import encoders
def send_email(machine_number,hall,machine_type, updated_time, message):
fromaddr = "[email protected]"
toaddr = "[email protected]"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "[ERROR] %s - %s %s" %(machine_number,hall,machine_type)
body = message + "something wrong! last update: " + updated_time.strftime('%Y-%m-%d %H:%M:%S')
msg.attach(MIMEText(body, 'plain'))
tries = 0
while True:
if (tries > 60):
exit()
try:
server = smtplib.SMTP('smtp.gmail.com', 587)
break
except Exception as e:
tries = tries + 1
time.sleep(1)
server.starttls()
server.login(fromaddr, "I_am_a_laundry")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
print('Send Report Email Successfully')
server.quit()
allow_diff_second = 3000
down_list = []
maintenance_error_list = []
while True:
for i in range(1, 9) :
if ( i == 7):
continue
now = datetime.datetime.now()
url = "https://ust.one/api/laundry/device-status-check/" + str(i)
response = urllib.urlopen(url)
data = json.loads(response.read())
print now
for washer in data['washers']['data']:
updated_time = datetime.datetime.strptime(washer["updated_at"], '%Y/%m/%d %H:%M:%S')
# print datetime.datetime.now()
diff_seconds = (now - updated_time).total_seconds()
print washer['name'] +" "+ str(diff_seconds)
if (diff_seconds>allow_diff_second):
if (washer["id"] not in down_list):
# print washer["id"] + "is down"
down_list.append(washer["id"])
# notify
send_email('ID:'+str(washer["id"]) +' - '+'#'+washer['name'], washer['hall'], washer['type'], updated_time, "device_power_error")
elif washer["id"] in down_list:
# print washer["id"] + "recovers"
down_list.remove(washer["id"])
if (washer['is_down'] == True and washer['remaining_minutes'] != 0 ):
if (washer["id"] not in maintenance_error_list):
# print washer["id"] + "'s mode needs to be updated"
maintenance_error_list.append(washer["id"])
# notify
send_email('ID:'+str(washer["id"]) +' - '+'#'+washer['name'], washer['hall'], washer['type'], updated_time, "maintenance_error")
elif washer["id"] in maintenance_error_list:
# print washer["id"] + "mode may be ok now"
maintenance_error_list.remove(washer["id"])
for dryer in data['dryers']['data']:
updated_time = datetime.datetime.strptime(dryer["updated_at"], '%Y/%m/%d %H:%M:%S')
# print datetime.datetime.now()
diff_seconds = (now - updated_time).total_seconds()
print dryer['name'] +" "+ str(diff_seconds)
if (diff_seconds>allow_diff_second):
if (dryer["id"] not in down_list):
down_list.append(dryer["id"])
# notify
send_email('ID:'+str(dryer["id"]) +' - '+'#'+dryer['name'], dryer['hall'], dryer['type'], updated_time, "device_power_error")
elif dryer["id"] in down_list:
down_list.remove(dryer["id"])
if (dryer['is_down'] == True and dryer['remaining_minutes'] != 0 ):
if (dryer["id"] not in maintenance_error_list):
# print dryer["id"] + "'s mode needs to be updated"
maintenance_error_list.append(dryer["id"])
# notify
send_email('ID:'+str(dryer["id"]) +' - '+'#'+dryer['name'], dryer['hall'], dryer['type'], updated_time, "maintenance_error")
elif dryer["id"] in maintenance_error_list:
# print dryer["id"] + "mode may be ok now"
maintenance_error_list.remove(dryer["id"])
print "down list: "
print down_list
print "maintenance error list: "
print maintenance_error_list
time.sleep(15)