-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdf_system.py
More file actions
executable file
·120 lines (94 loc) · 3.66 KB
/
df_system.py
File metadata and controls
executable file
·120 lines (94 loc) · 3.66 KB
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
#!/usr/bin/python3
"""
DigiFrame is a lightweight solution for digital picture frame.
Copyright (C) 2021 Awalon
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import os
import threading
from multiprocessing.connection import Listener
import df_util
from df_util import df_logger
# global CONFIG
CONFIG = df_util.Config()
def execute_command(command, message):
status = 500
msg = 'command failed'
if df_logger:
df_logger.debug('execute: %s' % command)
if CONFIG.SYS_DEBUG:
if os.system('echo "exec: \'' + command + '\'"') == 0:
status = 200
msg = message
else:
if os.system(command) == 0:
status = 200
msg = message
return [status, msg]
def control_interface(thread_name):
global CONFIG
listener = None
while True:
address = ('localhost', CONFIG.SYS_IPC_PORT) # family is deduced to be 'AF_INET'
try:
listener = Listener(address, authkey=bytes(CONFIG.WEB_SECRET, 'utf-8'))
conn = listener.accept()
while True:
if df_logger:
df_logger.debug('[%s] connection accepted from: %s' % (thread_name, listener.last_accepted))
msg = conn.recv()
# reboot system
if msg == 'c:system:restart':
if df_logger:
df_logger.debug('[%s] restart message received: %s' % (thread_name, msg))
conn.send(execute_command(CONFIG.SYS_CMD_RESTART, 'restarting, please wait...'))
conn.close()
break
# shutdown system
if msg == 'c:system:shutdown':
if df_logger:
df_logger.debug('[%s] shutdown message received: %s' % (thread_name, msg))
conn.send(execute_command(CONFIG.SYS_CMD_SHUTDOWN, 'powering off, please wait...'))
conn.close()
break
# close connection
elif msg == 'close':
conn.send(200)
conn.close()
break
except Exception as ex_srv:
if df_logger:
df_logger.info('[%s] server error: %s' % (thread_name, str(ex_srv)))
finally:
if listener is not None:
listener.close()
listener = None
if __name__ == "__main__":
# init logging
df_util.init_logger(CONFIG)
if CONFIG.SYS_DEBUG:
if df_logger:
df_logger.info('Debug mode, commands will be logged but not executed!')
try:
# start ipc thread
ipc = threading.Thread(target=control_interface, args=('ipc',))
ipc.start()
# start IPC server
ipc.join()
except KeyboardInterrupt:
if df_logger:
df_logger.critical('Stopping user aborted with CTRL+C')
except Exception as ex:
if df_logger:
df_logger.critical('Fatal error: %s', ex)
finally:
pass