forked from rcdrones/UPSPACK_V3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathups_cli.py
57 lines (45 loc) · 1.38 KB
/
ups_cli.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
#!/usr/bin/python3
from upspackv2 import *
import time
import os
import sys
test = UPS2("/dev/ttyAMA0")
def reflash_data():
try:
version, vin, batcap, vout = test.decode_uart()
except Exception as e:
print(f"Error decoding UART: {e}")
return
cur_time = time.time()
cur_time = cur_time - load_time
print("Running: " + str(int(cur_time)) + "s")
batcap_int = int(batcap)
if vin == "NG":
print("Power NOT connected!")
else:
print("Power connected!")
if batcap_int < 30:
if batcap_int == 1:
cur_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
stop_time = "\nHalt time :" + cur_time
with open("log.txt", "a+") as f:
f.write(stop_time)
os.system("sudo shutdown -t now")
sys.exit()
else:
print("Battery Capacity: " + str(batcap) + "%")
print("Output Voltage: " + vout + " mV")
if __name__ == "__main__":
load_time = time.time()
try:
version, vin, batcap, vout = test.decode_uart()
print("Smart UPS " + version)
except Exception as e:
print(f"Error initializing UPS: {e}")
sys.exit(1)
try:
while True:
reflash_data()
time.sleep(15)
except KeyboardInterrupt:
print("Exiting...")