-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery_notifier.py
151 lines (112 loc) · 4.04 KB
/
battery_notifier.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
147
148
149
150
151
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 19 22:22:55 2022
@author: ACER
"""
import time
import psutil
import serial_connect
import serial.tools.list_ports
import error_statements as er
TURN_ON_THRESH = 45
TURN_OFF_THRESH = 90
counter = [0]
def convert_time(seconds):
""" Function to cvonvert seconds into HH:MM:S format """
minutes, seconds = divmod(seconds, 60)
hours, minutes = divmod(minutes, 60)
return "%d:%02d:%02d" % (hours, minutes, seconds)
def display_info(percent, plugged_status, time_left):
""" A simple function to display the availabel information """
if plugged_status is True:
time_left = "PC is on Charge"
if plugged_status is False and int(time_left.split(":")[0]) >= 12:
time_left = "Estimating..."
print("Battery percentage : ", percent)
print("Power plugged in : ", plugged_status)
print("Battery left : ", time_left)
print()
def get_battery_status():
""" The core function to get battery levels and charger status """
battery = psutil.sensors_battery()
percent = battery.percent
plugged_status = battery.power_plugged
time_left = convert_time(battery.secsleft)
return percent, plugged_status, time_left
def mcu_control_logic(mcu, percent, plugged_status):
""" The function to control MCU for charging purposes """
if TURN_OFF_THRESH == 100 and percent >= TURN_OFF_THRESH and plugged_status is True: # 100%
print("Waiting for full charge @ 100%")
time.sleep(300) # 5 * 60 SECS
print("Removing charger")
print()
mcu.write('0'.encode())
time.sleep(5)
while 1:
percent, plugged_status, time_left = get_battery_status()
if plugged_status is not True:
break
print("Its possible bypass switch is ON \n")
time.sleep(5)
elif percent >= TURN_OFF_THRESH and plugged_status is True:
print(f"Removing charger, Treshold reached @ {TURN_OFF_THRESH}")
print()
mcu.write('0'.encode())
time.sleep(5)
while 1:
percent, plugged_status, time_left = get_battery_status()
if plugged_status is not True:
break
print("Its possible bypass switch is ON \n")
time.sleep(5)
elif percent <= TURN_ON_THRESH and plugged_status is False:
print(f"Charging On, Treshold Reached @ {TURN_ON_THRESH}")
print()
mcu.write('1'.encode())
time.sleep(5)
counter[0] += 1
if counter[0] >= 3 and plugged_status is False:
print("It's possible power adapter is not yet turned on. \n")
if plugged_status is True:
counter[0] = 0
def thresh_valiadte(turn_on_thresh, turn_off_thresh):
""" A function to validate Turn On and Turn Off thresholds """
if 5 <= turn_on_thresh <= 100:
pass
else:
raise ValueError("Turn ON threshold should be between 5 - 100 \n")
if 5 <= turn_off_thresh <= 100:
pass
else:
raise ValueError("Turn OFF threshold should be between 5 - 100 \n")
if (turn_off_thresh - turn_on_thresh) >= 20:
pass
else:
raise ValueError(
"There should be minimium gap of 20% between Turn ON and OFF thresholds\n")
try:
thresh_valiadte(TURN_ON_THRESH, TURN_OFF_THRESH)
mcu = serial_connect.connect_port(display=True, baud=9600)
print()
while 1:
percent, plugged_status, time_left = get_battery_status()
display_info(percent, plugged_status, time_left)
mcu_control_logic(mcu, percent, plugged_status)
time.sleep(2.5)
mcu.write('9'.encode())
time.sleep(2.5)
except ValueError as e:
print(er.VAL_ERR, e)
except TypeError as e:
print(er.NO_USB, e)
except NameError as e:
print(er.PORT_ERR, e)
except serial.serialutil.SerialException as e:
print(er.SERIAL_ERR, e)
try:
mcu.close()
print("\nConnection closed, safely remove the device.")
except Exception:
pass
except Exception as e:
print(er.UNKNOWN_ERR, e)