-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaster_main.py
261 lines (214 loc) · 9.16 KB
/
master_main.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
from data import *
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
import argparse
import json
import sys
from pydantic import BaseModel
from master import Master
from datetime import datetime
from config_file import ConfigFile, ConfigSchema, CfgSectionWrapper, makeNewSection
serverArgParser = argparse.ArgumentParser(description='SBC Rest API Server',
prog=sys.argv[0],
usage='%(prog)s [OPTIONS]',
epilog='\n')
# Argument used to specify a server configuration json file
serverArgParser.add_argument('-p', '--port',
type=str,
default=8005,
help='used to specify a server port')
serverArgParser.add_argument('-c', '--config',
type=str,
default='master_config.ini',
help='path to config file')
class MasterLogger:
def __init__(self, maxSize: int):
self._maxSize = maxSize
self._logs = list[str]()
def push(self, msg: str):
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
self._logs.append(f'[{current_time}]: {msg}')
if len(self._logs) > self._maxSize:
self._logs.pop(0)
def getLogs(self) -> list[str]:
return self._logs
class MasterServer:
def __init__(self):
_, *params = sys.argv
args = serverArgParser.parse_args()
self.configFile = ConfigFile.read(args.config)
self.masterClient = Master()
self.is_active = False
self._logger = MasterLogger(7)
def isActive(self):
return self.is_active
def pushSlaveLog(self, mqtt, msg: str):
self._logger.push(f'{mqtt.slave_name}: {msg}')
def start(self):
print("Server started")
def on_slave_message(msg):
if msg.msg_type == MessageType.SLAVE_REQUEST_CONFIG:
self.pushSlaveLog(msg, f'config request for [{msg.msg_payload}]')
if self.is_active == True:
cfg = CfgSectionWrapper(self.configFile, msg.msg_payload)
out = Data_ConfigSection(name=cfg.getName(),
pwd=cfg.getPassword(),
remote_accepted=cfg.getAllowed())
self.pushSlaveLog(msg, f'reply to config [{msg.msg_payload}] with {str(cfg.getAllowed())}')
self.masterClient.sendConfigRequestReply(reqMessage=msg, cfg_section=out)
elif msg.msg_type == MessageType.SLAVE_REQUEST_LOG_MESSAGE:
self.pushSlaveLog(msg, f'log-> {msg.msg_payload}')
# Special case for master requests
elif msg.msg_type == MessageType.MASTER_REPLY_MASTER_ROLE:
self.pushSlaveLog(msg, f' master role ACK reply')
elif msg.msg_type == MessageType.MASTER_REQUEST_MASTER_ROLE:
if self.masterClient.getName() != msg.msg_payload:
self.masterClient.sendMasterRoleRequestReply(reqMessage=msg)
self.masterClient.stop()
self.masterClient.start(on_slave_message)
def stop(self):
print("Server stopped")
self.masterClient.stop()
self.is_active = False
def resume(self):
print("Server resumed")
self.is_active = True
def suspend(self):
print("Server suspended")
self.is_active = False
class MasterApp:
server: MasterServer
def master_app_onstart():
print('master_app_onstart called')
MasterApp.server = MasterServer()
MasterApp.server.start()
def master_app_onstop():
print('master_app_onstop called')
MasterApp.server.stop()
class ConfigSection(BaseModel):
name: str
pwd: str
allowed: bool
class ConfigRequest(BaseModel):
sections: list[ConfigSection]
class ConfigResponse(BaseModel):
sections: list[ConfigSection]
class MasterStatusResponse(BaseModel):
is_active: bool
name: str
class MasterLogsResponse(BaseModel):
logs: list[str]
class MasterStatusRequest(BaseModel):
is_active: bool
class BaseResponse(BaseModel):
status: str
masterapi = FastAPI(title='ParentControl REST API',
description="Game launcher control",
license_info={'name': 'Copyright by Alexander Shimgaev. All rights reserved'},
on_startup=[master_app_onstart],
on_shutdown=[master_app_onstop])
masterapi.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@masterapi.get("/master/app/{resource}")
def get_demo(resource: str):
out = ''
with open(f'./html/{resource}', 'r') as resFile:
out = resFile.read()
return HTMLResponse(content=out, status_code=200)
@masterapi.get("/master/send-message", response_model=BaseResponse)
def master_send_message(msg: str):
MasterApp.server.masterClient.sendShowMessageRequest(msg=msg)
return BaseResponse(status='OK')
@masterapi.get("/master/slave-shutdown", response_model=BaseResponse)
def master_send_slave_shutdown(name: str):
MasterApp.server.masterClient.sendSlaveShutdownRequest(name=name)
return BaseResponse(status='OK')
@masterapi.get("/master/logs", response_model=MasterLogsResponse)
def master_get_logs():
return MasterLogsResponse(logs=MasterApp.server._logger.getLogs())
@masterapi.get("/master/statistic", response_model=BaseResponse)
def master_get_statistic():
def onReply(msg):
if msg.msg_type == MessageType.SLAVE_REPLY_STATISTIC:
MasterApp.server.pushSlaveLog(msg, f'statistic reply-> {msg.games}')
MasterApp.server._logger.push(f'Master statistic request')
MasterApp.server.masterClient.sendStatisticRequest(onReply)
return BaseResponse(status='OK')
@masterapi.get("/master/master-role", response_model=BaseResponse)
def master_get_master_role():
MasterApp.server._logger.push(f'Master request: MASTER ROLE')
MasterApp.server.masterClient.sendMasterRoleRequest()
return BaseResponse(status='OK')
@masterapi.get("/master/status", response_model=MasterStatusResponse)
def master_get_status():
return MasterStatusResponse(is_active=MasterApp.server.isActive(), name=MasterApp.server.masterClient.getName())
@masterapi.post("/master/status", response_model=MasterStatusResponse)
def master_set_status(reqBody: MasterStatusRequest):
if reqBody.is_active:
MasterApp.server.resume()
else:
MasterApp.server.suspend()
return MasterStatusResponse(is_active=MasterApp.server.isActive())
@masterapi.get("/master/config", response_model=ConfigResponse)
def get_config():
sections = list[ConfigSection]()
cfgFile = MasterApp.server.configFile
for name in cfgFile.getSections():
cfg = CfgSectionWrapper(cfgFile, name)
sections.append(ConfigSection(name=cfg.getName(),
pwd=cfg.getPassword(),
allowed=cfg.getAllowed()))
response = ConfigResponse(sections=sections)
return response
@masterapi.get("/master/config/sync", response_model=BaseResponse)
def sync_config():
def onReply(msg):
if msg.msg_type == MessageType.SLAVE_REPLY_CONFIG_LIST:
MasterApp.server.pushSlaveLog(msg, f'sync config reply with {len(msg.cfg_sections)} items')
if msg.cfg_sections and len(msg.cfg_sections) > 0:
MasterApp.server.configFile.clear()
for s in msg.cfg_sections:
cfg = makeNewSection(MasterApp.server.configFile, s.name)
cfg.setPassword(s.pwd)
MasterApp.server.configFile.write()
MasterApp.server._logger.push(f'Master sync config request')
MasterApp.server.masterClient.sendConfigListRequest(on_reply_cb=onReply)
return BaseResponse(status='OK')
@masterapi.post("/master/config", response_model=BaseResponse)
def set_config(reqBody: ConfigRequest):
needUpdate = False
cfgFile = MasterApp.server.configFile
for newCfg in reqBody.sections:
cfg = CfgSectionWrapper(cfgFile, newCfg.name)
if cfg.getPassword() != newCfg.pwd:
cfg.setPassword(val=newCfg.pwd)
needUpdate = True
cfg.setAllowed(val=newCfg.allowed)
# Notify slave
msgCfg = Data_ConfigSection(name=cfg.getName(),
pwd=cfg.getPassword(),
remote_accepted=cfg.getAllowed())
def on_reply(msg):
MasterApp.server.pushSlaveLog(msg, f'config ACK reply for [{cfg.getName()}]')
MasterApp.server._logger.push(f'Master send config update request [{cfg.getName()}]')
MasterApp.server.masterClient.sendConfigUpdateRequest(msgCfg, on_reply)
if needUpdate:
cfgFile.write()
return BaseResponse(status='OK')
MASTER_APP = masterapi
def main():
_, *params = sys.argv
print("Starting REST API with parameters:", params)
args = serverArgParser.parse_args()
print("serving at port", args.port)
import uvicorn
uvicorn.run('master_main:MASTER_APP', host="127.0.0.1", port=args.port)
if __name__ == "__main__":
main()