-
Notifications
You must be signed in to change notification settings - Fork 0
/
ups.py
95 lines (83 loc) · 2.72 KB
/
ups.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
"""
by Denexapp
"""
import threading
import usb.core
import usb.util
import denexapp_config as dconfig
import time
import sys
class ups():
def __init__(self, gsm_object):
self.warning_sent = False
self.ready_to_work = True
self.stop = False
self.gsm_object = gsm_object
self.online = True
def start_monitoring(self):
thread = threading.Thread(target=self.__start_monitoring_action)
thread.daemon = True
thread.start()
def stop_monitoring(self):
self.stop = True
def __start_monitoring_action(self):
self.stop = False
time.sleep(13)
print "started_monitor"
while True:
self.battery_status_update()
# print "Warning sent ==", self.warning_sent, "self.online ==", self.online
if self.online is False:
if self.warning_sent is False:
print "sms sending started"
self.gsm_object.send_no_power()
self.ready_to_work = False
self.warning_sent = True
print "sms sent"
else:
if self.warning_sent is True:
print "sms sending started"
self.gsm_object.send_power_on()
self.ready_to_work = True
self.warning_sent = False
print "sms sent"
time.sleep(dconfig.check_interval/1000)
if self.stop:
break
def able_to_work(self):
return self.ready_to_work
def attach(self):
self._had_driver = False
self._dev = usb.core.find(idVendor=dconfig.vendor_id, idProduct=dconfig.product_id)
if self._dev is None:
raise ValueError("Device not found")
if self._dev.is_kernel_driver_active(0):
self._dev.detach_kernel_driver(0)
self._had_driver = True
self._dev.set_configuration()
def release(self):
usb.util.release_interface(self._dev, 0)
if self._had_driver:
self._dev.attach_kernel_driver(0)
def battery_status_update(self):
try:
self.attach()
except:
print sys.exc_info()
try:
ret = usb.util.get_string(self._dev, 0x03, langid=0x0409)
# print ret
if ret[0] == "(":
if ret[38] == "0":
self.online = True
# print "online"
elif ret[38] == "1":
self.online = False
# print "offline"
except:
print sys.exc_info()
try:
self.release()
except:
print sys.exc_info()
time.sleep(0.5)