This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrainlinkWeb.py
179 lines (145 loc) · 6.53 KB
/
trainlinkWeb.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
'''
Handles the websocket connections
Copyright (C) 2020 TrainLink Organisation (matt-hu)
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/>
'''
#imports the sub-modules
import webUtils as utils
import trainlinkObjects
#imports the required external modules
import websockets, asyncio, json
class web:
""" Serves websocket users """
# The trainlinkSerial instance
serialUtils = None
# The variables needed for configuration
address = ""
port = ""
websocket = None
mode = "normal"
debug = False
# Arrays used for storing runtime data
power = 0
users = set()
cabs = {}
'''
cabID = {}
cabSpeeds = {}
cabDirections = {}
cabFunctions = {}
'''
logfile = None
# Assigns config variables from arguments
def __init__ (self, address, port, logfile, debug, cabIDxml, serialUtils):
self.serialUtils = serialUtils
self.debug =debug
self.address = address
self.port = port
self.cabID = cabIDxml
self.logfile = logfile
for cab in cabIDxml:
self.cabs[cabIDxml[cab]] = trainlinkObjects.cab(name=cab,address=cabIDxml[cab],functions=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
def start(self, mode):
self.mode = mode
logfile = self.logfile
logfile.log("Starting server at %s:%s" %(self.address,self.port))
logfile.log("Debug enabled", "d")
start_server = websockets.serve(self.main, self.address, self.port)
if self.mode == "test":
logfile.log("Test mode", "dw")
raise KeyboardInterrupt
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
async def notifyState(self, websocket):
if self.users:
for user in self.users:
await self.stateEvent(user)
async def main (self, websocket, path):
logfile = self.logfile
self.websocket = websocket
await self.register(websocket)
try:
await self.stateEvent(websocket)
try:
async for message in websocket:
data = json.loads(message)
if data["class"] == "cabControl":
self.cabControl(data)
await self.notifyState(websocket)
elif data["class"] == "directCommand":
await self.directCommand(data["command"])
elif data["class"] == "power":
await self.setPower(data["state"])
await self.notifyState(websocket)
elif data["class"] == "cabFunction":
await self.cabFunction(data)
await self.notifyState(websocket)
except websockets.exceptions.ConnectionClosedError:
logfile.log("Websocket closed", "d")
finally:
await self.unregister(websocket)
async def register(self, websocket):
self.logfile.log("New user connected", "d")
self.users.add(websocket)
await websocket.send(json.dumps({"type": "config", "cabs": self.cabID,"debug": self.debug}))
async def unregister(self, websocket):
self.logfile.log("User disconnected", "d")
web.users.remove(websocket)
async def stateEvent(self, websocket):
for cab in self.cabs:
await websocket.send(json.dumps({"type": "state", "updateType": "cab", "cab": cab, "speed": self.cabs[cab].getSpeed(), "direction": self.cabs[cab].getDirection(), "functions": self.cabs[cab].getFunctions()}))
await websocket.send(json.dumps({"type": "state", "updateType": "power", "state": self.power}))
def cabControl(self, data):
logfile = self.logfile
try:
if data["action"] == "setSpeed":
address = utils.obtainAddress(data["cabAddress"], self.cabID)
self.cabs[address].setSpeed(data["cabSpeed"])
self.cabs[address].setDirection(data["cabDirection"])
#self.cabSpeeds[address] = data["cabSpeed"]
#self.cabDirections[address] = data["cabDirection"]
elif data["action"] == "stop":
address = utils.obtainAddress(data["cabAddress"], self.cabID)
self.cabs[address].setSpeed("0")
self.cabs[address].setDirection("0")
#self.cabSpeeds[address] = "0"
#self.cabDirections[address] = "0"
elif data["action"] == "estop":
address = utils.obtainAddress(data["cabAddress"], self.cabID)
self.cabs[address].setSpeed("0")
self.cabs[address].setDirection("0")
except UnboundLocalError:
logfile.log("Unknowen Address!", "ed")
async def directCommand(self, packet):
await self.serialUtils.directCommand(packet)
async def setPower(self, powerState):
await self.serialUtils.setPower(powerState)
self.power = powerState
async def cabFunction(self, data):
logfile = self.logfile
print(data)
try:
address = utils.obtainAddress(data["cab"], self.cabID)
if data["state"] != -1:
self.cabs[address].setFunction(data["func"],data["state"])
else:
print("switching")
self.cabs[address].setFunction(data["func"],int(not self.cabs[address].getFunction(data["func"])))
legacyMode = True
if legacyMode:
await self.serialUtils.setFunction(address, functionStates=self.cabs[address].getFunctions())
else:
await self.serialUtils.setFunction(address, function=data["func"], state=data["state"])
except KeyError:
logfile.log("Received bad data! (Probably a cab address)", "de")
def update(self):
asyncio.run(self.notifyState(self.websocket))