-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattery_alarm.py
84 lines (72 loc) · 1.91 KB
/
battery_alarm.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
import subprocess
import os
import pickle
import re
import sys
import datetime
# settings
app_name = 'battery_alarm'
base_dir = os.environ['HOME'] + '/' + app_name + '/'
state_file = base_dir + 'state'
batt_log_file = base_dir + 'batt_percent.log'
def alarm(batt_percent):
'''
alarm work
batt_percent: battery percent
'''
os.system('open ' + base_dir + app_name + '.app')
def get_batt_percent():
'''
배터리 퍼센트 가져오기
return: battery percent
'''
cmd_result = subprocess.check_output(['pmset', '-g', 'batt'], universal_newlines=True)
batt_percent = re.findall('\d*%', cmd_result)[0]
batt_percent = int(batt_percent[:-1])
return batt_percent
def read_state():
'''
Read state
* state: 배터리가 경고 범위 안일 때 알람이 매 분 울리지 않고 한 번만 울리도록 조절하는 변수(state)
return: state. default True(alarm available)
'''
if os.path.exists(state_file):
with open(state_file, 'rb') as f:
state = pickle.load(f)
return state
return True
def batt_check():
'''
배터리 잔량 체크
'''
percent = get_batt_percent()
# log_batt_percent(percent)
state = read_state()
if (percent <= 20) or (80 <= percent):
if state:
alarm(percent)
state = False
else:
state = True
save_state(state)
def log_batt_percent(batt_percent):
'''
배터리 퍼센트 로그 찍기
'''
with open(batt_log_file, 'a') as f:
f.write(str(datetime.datetime.now()) + ':\t' + str(batt_percent) + '%\n')
def save_state(state):
'''
상태 저장
'''
with open(state_file, 'wb') as f:
pickle.dump(state, f)
def main():
# 명령행 인자
what = sys.argv[1]
if what == 'check':
batt_check()
elif what == 'state':
print(read_state())
if __name__ == '__main__':
main()