-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathshutdown_scr
executable file
·138 lines (124 loc) · 5.46 KB
/
shutdown_scr
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
#!/usr/bin/python3
# -*- coding:utf-8 -*-
from PIL import Image, ImageDraw, ImageFont # Font handeling
from waveshare_epd import epd2in13_V2
import os
import time
from os.path import exists
import re
import subprocess
import configparser #to handle the config file.
from ser2bt_lib import center_text, line_pos
#import configuration file:
#Read ser2bt_config.ini file
config_obj = configparser.ConfigParser()
config_obj.read("/etc/ser2bt_config.ini")
screen = config_obj["screen"]
shutdown_screen = config_obj["shutdown_screen"]
# parameters to use the keys from the ser2bt_config.ini
screen_rotate = int(screen["screen_rotate"])
foreground = int(screen["foreground"])
background = int(screen["background"])
# Variable decleration
standby_msg = 'Standby:'
title_msg = 'Serial 2 Bluetooth Bridge'
tmp_dir='/tmp/'
shut_file = 'shut'
bat_file = 'bat'
np = True #New page True or False
host_name=os.uname()[1] # define host_name
epd = epd2in13_V2.EPD()
picdir='/usr/local/lib/ser2bt-bridge/images/'
roboto_font_dir='/usr/share/fonts/truetype/roboto/unhinted/RobotoTTF/'
f_top_line_font16 = ImageFont.truetype(roboto_font_dir+'Roboto-Medium.ttf', 16)
f_title_font18 = ImageFont.truetype(roboto_font_dir+'Roboto-Bold.ttf', 18)
f_title_font24 = ImageFont.truetype(roboto_font_dir+'Roboto-Bold.ttf', 24)
if foreground == 0:
bmp = Image.open(os.path.join(picdir, 'logo.bmp'))
else:
bmp = Image.open(os.path.join(picdir, 'logo_inverted.bmp'))
exit_image = Image.new('1', (epd.height, epd.width), int(background)) #clear the frame
exit_draw = ImageDraw.Draw(exit_image)
left_margin = 1 #Left margin.
title_correction = 5 #Will start all text at the 6th pixel from the top on the shutdown screen only.
bottom_margen = epd.width - 1
def shutdown_check():
power_off = 'p' #flag to indicate that the bridge is being powered off.
exit_out = 'e' #flag to indicate that the bridge will exit out of its service(s).
reboot = 'r' #flag to indicate that the bridge will reboot.
upgrade = 'u' #flag to indicate that the bridge is upgrading.
flag = ' ' #capture contents of shut_file
msg = " " #message to send back to calling statement.
if os.path.exists(tmp_dir+shut_file):
file = open(tmp_dir+shut_file,'r')
flag = file.read()
file.close()
if exit_out in flag:
msg = 'Exit'
elif power_off in flag:
msg = 'Shutdown'
elif reboot in flag:
msg = 'Reboot'
elif upgrade in flag:
msg = 'Upgrade'
else:
msg = 'Shutdown'
else:
msg = 'Shutdown'
return (msg)
def bat_lvl():
f = open(tmp_dir+bat_file,'r')
bat = f.read()
f.close()
if bat == "":
bat="Err"
return (bat)
#Clear the Screen:
epd.init(epd.FULL_UPDATE)
#Draw out the boot screen
exit_draw.text((center_text(exit_draw, standby_msg,f_title_font24), line_pos(np,exit_draw,standby_msg,f_title_font24)), standby_msg, font=f_title_font24, fill = int(foreground))
np = False
exit_draw.text((center_text(exit_draw, title_msg,f_title_font18), line_pos(np,exit_draw,title_msg,f_title_font18)), title_msg, font=f_title_font18, fill = int(foreground))
if shutdown_check() == "Exit":
reason = "Is Exiting"
elif shutdown_check() == "Upgrade":
reason = "Is being Upgraded"
elif shutdown_check() == "Reboot":
reason = "Is Rebooting"
elif shutdown_check() == "Shutdown":
reason = "Is being Shutdown"
else:
reason = "Is Exiting"
exit_draw.text((center_text(exit_draw,reason,f_title_font18), line_pos(np,exit_draw,standby_msg,f_title_font18)), reason, font=f_title_font18, fill = int(foreground))
epd.display(epd.getbuffer(exit_image.rotate(screen_rotate)))
if shutdown_check() == "Shutdown":
#new page, reset line_position.
epd.sleep()
time.sleep(2)
np = True
#compute and format uptime
up_time = subprocess.check_output(['uptime', '-p']).decode("utf8").replace(',', '').replace("up ","")
up_time = re.sub("hours?","hrs", up_time)
up_time = re.sub("minutes?","min", up_time)
# updated parameters to use the keys from the ser2bt_config.ini
screen_rotate = int(shutdown_screen["screen_rotate"])
foreground = int(shutdown_screen["foreground"])
background = int(shutdown_screen["background"])
icon_image = Image.new('1', (epd.height, epd.width), int(background)) #clear the frame
icon_draw = ImageDraw.Draw(icon_image)
icon_image.paste(bmp, (0,20)) #Display the background image
#display the screen title"
icon_draw.text((center_text(icon_draw,title_msg,f_title_font18), line_pos(np,icon_draw,standby_msg,f_title_font18)-title_correction), title_msg, font=f_title_font18,fill = int(foreground))
np = False
#Add the discriptions to the boxes in the image
icon_draw.text((4,45), 'master', font=f_top_line_font16,fill = int(foreground)) #28 is the top of the square
icon_draw.text((122,45), host_name, font=f_top_line_font16,fill = int(foreground))
icon_draw.text((204,45), 'slave', font=f_top_line_font16,fill = int(foreground)) #75 is the bottom of the square
#Add uptime, battery level, then add uptime at the bottom of the screen
icon_draw.text((left_margin,85), 'Halted @ '+time.strftime('%H:%M')+'/'+'bat:'+bat_lvl(), font=f_top_line_font16,fill = int(foreground))
icon_draw.text((left_margin,103), 'Uptime:'+ up_time, font=f_top_line_font16,fill = int(foreground))
epd.init(epd.FULL_UPDATE)
epd.display(epd.getbuffer(icon_image.rotate(screen_rotate)))
epd2in13_V2.epdconfig.module_init()
#epd2in13_V2.epdconfig.module_exit()
exit()