forked from inboxsgk/Balert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.pyw
153 lines (128 loc) · 4.78 KB
/
main.pyw
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
152
153
from notifypy import Notify
import psutil
import time
import pystray
from PIL import Image
import os
import threading
appIsRunning = True
class Const:
CONFIG_FILE_PATH = "./config.txt"
APP_DEFAULT_ENABLE = True
APP_DEFAULT_ALERT_TIME_INTERVAL = 15
BATTERY_DEFAULT_MIN = 25
BATTERY_DEFAULT_MAX = 85
CONFIG_KEY_EN = "en"
CONFIG_KEY_MAX = "max"
CONFIG_KEY_MIN = "min"
CONFIG_KEY_ALERT_TIME_INTERVAL = "alert_time_interval"
STRAY_ENABLE = "Enable"
STRAY_DISABLE = "Disable"
STRAY_OPEN_CONFIG = "Open config.txt"
STRAY_EXIT = "Exit"
class Config:
en: bool
min: int
max: int
alertTimeInterval: int
configPath: str
def __init__(self):
self.configPath = Const.CONFIG_FILE_PATH
self.en = Const.APP_DEFAULT_ENABLE
self.min = Const.BATTERY_DEFAULT_MIN
self.max = Const.BATTERY_DEFAULT_MAX
self.alertTimeInterval = Const.APP_DEFAULT_ALERT_TIME_INTERVAL
self.readConfig()
def readConfig(self):
with open(self.configPath, 'r') as fp:
for line in fp.readlines():
line = line.strip()
if line == "":
continue
key, value = line.split()
if key == Const.CONFIG_KEY_EN:
self.en = int(value) == 1
elif key == Const.CONFIG_KEY_MAX:
self.max = int(value)
elif key == Const.CONFIG_KEY_MIN:
self.min = int(value)
elif key == Const.CONFIG_KEY_ALERT_TIME_INTERVAL:
self.alertTimeInterval = int(value)
fp.close()
def writeConfig(self):
en = 0
if self.en:
en = 1
with open(self.configPath, 'w') as fp:
fp.write(f"{Const.CONFIG_KEY_EN} {en}\n")
fp.write(f"{Const.CONFIG_KEY_MAX} {self.max}\n")
fp.write(f"{Const.CONFIG_KEY_MIN} {self.min}\n")
fp.write(f"{Const.CONFIG_KEY_ALERT_TIME_INTERVAL} {self.alertTimeInterval}")
fp.close()
def sendNotification(title: str, text: str):
notification = Notify()
notification.title = title
notification.message = text
notification.send(block=False)
def onIconRightClicked(stray, query):
query = str(query)
config = Config()
if query == Const.STRAY_ENABLE:
config.en = True
sendNotification("Alert Enabled", "You can turn it off from the system tray icon.")
elif query == Const.STRAY_DISABLE:
config.en = False
sendNotification("Alert Disabled", "You can turn it on from the system tray icon.")
elif query == Const.STRAY_OPEN_CONFIG:
openConfigFile()
elif query == Const.STRAY_EXIT:
# config.en = False
# config.writeConfig()
global appIsRunning
appIsRunning = False
stray.stop()
# exit(0)
config.writeConfig()
# left click on icon is currently not supported by pystray library
def onIconLeftClicked(icon, item):
openConfigFile()
def openConfigFile():
os.startfile(str(os.path.dirname(os.path.abspath(__file__))) + '\\config.txt')
def setupStray():
image = Image.open("./assets/icon.png")
stray = pystray.Icon("Battery-Percentage-Alert", image,
"Battery-Percentage-Alert",
menu=pystray.Menu(
pystray.MenuItem(Const.STRAY_ENABLE, onIconRightClicked),
pystray.MenuItem(Const.STRAY_DISABLE, onIconRightClicked),
pystray.MenuItem(Const.STRAY_OPEN_CONFIG, onIconRightClicked),
pystray.MenuItem(Const.STRAY_EXIT, onIconRightClicked)))
stray.run()
def main():
config = Config()
showSystemStatus(config)
while appIsRunning:
battery = psutil.sensors_battery()
isPluggedIn = battery.power_plugged
if isPluggedIn:
if int(battery.percent) > int(config.max) and config.en:
sendNotification("Unplug charger", ("The Power is at " + str(battery.percent)))
else:
if int(battery.percent) < int(config.min) and config.en:
sendNotification("Low Battery, Plug-in charger", ("The Power is at " + str(battery.percent)))
time.sleep(config.alertTimeInterval)
config.readConfig()
def showSystemStatus(config: Config):
if config.en:
sendNotification("Battery Percentage Alert", "Alert is enable")
else:
sendNotification("Battery Percentage Alert", "Alert is disable")
if __name__ == "__main__":
try:
mainFunctionThread = threading.Thread(target=main)
mainFunctionThread.start()
setupStray()
except Exception as e:
with open("./error.txt", 'w') as errorFile:
errorFile.write(str(e))
errorFile.close()