-
Notifications
You must be signed in to change notification settings - Fork 0
/
py_bat_control.py
executable file
·170 lines (141 loc) · 3.83 KB
/
py_bat_control.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
"""
See:
https://stackoverflow.com/questions/38828578/python-threading-interrupt-sleep
https://stackoverflow.com/questions/20044559/how-to-pip-or-easy-install-tkinter-on-windows
You can modify the parameters ( recommended_values # test_values ):
TIME_TO_WAIT = 5000 # 2000 ## in milliseconds
LOWER_THRESHOLD = 40 # 100
UPPER_THRESHOLD = 80
SEND_MESSAGE_TIME = 20.0 # 5.0
Requirements:
threading
tk
psutil
"""
TIME_TO_WAIT = 5000 # in milliseconds
LOWER_THRESHOLD = 30
UPPER_THRESHOLD = 80
LOW_BATTERY = 20
FULL_BATTERY = 100
MAX_COUNTER = 8
SEND_MESSAGE_TIME = 20.0
SHUTDOWN_TIME = 300
from threading import Timer, Thread, Semaphore, Event
from time import time, sleep
import psutil
from tkinter import Tk, messagebox
import os, socket
## environment variables
from environ import server_address
def client(client_address=('localhost',8001)):
"""
The client must receive in his own IP
otherwise it will be not able to bind
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(client_address)
data, addr = sock.recvfrom(4096)
sock.close()
return str(data.decode('ascii'))
def server(data, server_address=('localhost',8001)):
"""
The server can send anywhere
"""
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(data.encode('ascii'), server_address)
sock.close()
def read_key(quit, quit_sem, event):
value = input("To quit press 'q' + ENTER: ")
if value == 'q' or value == 'Q':
quit_sem.acquire(blocking=True, timeout=None)
quit[0] = True
event.set()
quit_sem.release()
def check_battery():
return int(psutil.sensors_battery().percent)
def send_message(state,server_address):
if state == 0:
server('on',server_address)
else:
server('off',server_address)
def ask_for_answer():
window = Tk()
try:
window.wm_withdraw()
window.after(TIME_TO_WAIT, window.destroy)
answer = messagebox.askyesno(title="¡¡¡ALERTA!!!", message="¡Algo va mal! ¿Quiere evitar que se apague el ordenador? [No]")
return answer
except:
pass
finally:
try:
window.destroy()
except:
pass
def th_send_message(quit, quit_sem, event, server_address):
quit_sem.acquire(blocking=True, timeout=None)
quit_l = quit[0]
quit_sem.release()
low_trigger = False
high_trigger = False
try:
counter = 0
while not quit_l:
percent = check_battery()
print("\n%i"%percent) # borrar
if counter < MAX_COUNTER:
if percent <= LOWER_THRESHOLD:
if not low_trigger:
send_message(0,server_address)
low_trigger = True
high_trigger = False
elif percent >= UPPER_THRESHOLD:
if not high_trigger:
send_message(1,server_address)
low_trigger = False
high_trigger = True
counter += 1
else:
if percent <= LOW_BATTERY:
if not low_trigger:
send_message(0,server_address)
low_trigger = True
high_trigger = False
elif percent >= FULL_BATTERY:
if not high_trigger:
send_message(1,server_address)
low_trigger = False
high_trigger = True
counter = 0
event.wait(SEND_MESSAGE_TIME)
quit_sem.acquire(blocking=True, timeout=None)
quit_l = quit[0]
quit_sem.release()
server('off',server_address)
except:
server('off',server_address)
answer=False
t_ini = time()
while t < SHUTDOWN_TIME and not answer:
t = time()-t_ini
answer = ask_for_answer()
if t > SHUTDOWN_TIME and not answer:
os.system('shutdown /s')
elif answer:
break
if __name__ == "__main__":
event = Event()
quit = [False]
quit_sem = Semaphore(value=1)
main_thread = Thread( target=read_key, args=(quit, quit_sem, event) )
main_thread.start()
threads = []
threads.append(
Thread( target=th_send_message, args=(quit, quit_sem, event, server_address) )
)
for t in threads:
t.start()
for t in threads:
t.join()
main_thread.join()