-
Notifications
You must be signed in to change notification settings - Fork 7
/
ups.py
executable file
·148 lines (109 loc) · 3.06 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
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
#!/usr/bin/env python
#
# Uninterruptible Power Supply (UPS) Control
#
# Note:
# User must have sudo rights to be able to perform a system shutdown
#
#
# This source file is part of the follwoing repository:
# http://www.github.com/microfarad-de/pi-ups
#
# Please visit:
# http://www.microfarad.de
# http://www.github.com/microfarad-de
#
# Copyright (C) 2019 Karim Hraibi (khraibi@gmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import serial # pip install pyserial
import ilock # pip install ilock
import sys
import time
import os
# Serial device
DEVICE = "/dev/ttyUSB0"
# Serial port baud rate
BAUD_RATE = 19200
# Polling interval in seconds
INTERVAL = 5
# Print info log message
def infoLog(text):
print(text.rstrip())
# Print warning log message
def warningLog(text):
print("[WARNING] " + text.rstrip())
# Print error log message
def errorLog (text):
print("[ERROR] " + text.rstrip())
# Read the contents of the receive buffer
def read():
rx = " "
result = ""
while len(rx) > 0:
rx = ser.readline()
result = result + rx
time.sleep(0.1)
return result
# Write to the transmit buffer
def write(str):
ser.write(str)
time.sleep(0.1)
#################
#### START ####
#################
# System-wide lock ensures mutually exclusive access to the serial port
lock = ilock.ILock(DEVICE, timeout=600)
infoLog("UPS service started")
# Initialize the serial port
with lock: # Ensure exclusive access through system-wide lock
ser = serial.Serial(DEVICE, BAUD_RATE, timeout=0.1)
# A serial connection will cause the MCU to reboot
# The following will flush the initial boot message
# and wait until the MCU is up and running
time.sleep(2)
with lock:
result = read()
sys.stdout.write(result)
time.sleep(2)
lastResult = ""
lastMeasResult = ""
measCount = 0
# Main loop
while 1:
# Read the UPS status
with lock:
write("stat\n")
result = read()
# Trace the UPS status
if result != lastResult:
lastResult = result
if "BATTERY" in result:
warningLog(result)
elif "ERROR" in result:
errorLog(result)
else:
infoLog(result)
# Handle low battery condition
if "BATTERY 0" in result:
with lock:
write("halt\n")
result = read()
if "SHUTDOWN" in result:
errorLog(result)
os.popen("sudo halt")
else:
errorLog("shutdown failed")
time.sleep(INTERVAL)