-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerosa.bak.py
227 lines (183 loc) · 6.54 KB
/
Serosa.bak.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
from pyfladesk import init_gui
import flask
from flask import request, jsonify
app = flask.Flask(__name__)
app.config["DEBUG"] = True
################################
## STATE COUNTDOWN THREADER
################################
import _thread, time
def actuatorCountdown(fakeArg):
while True:
time.sleep(1)
for actuator in actuators:
if (actuator['countdown'].isnumeric()) and actuator['state'] != '0':
print(actuator['id'])
actuator['countdown'] = str(int(actuator['countdown']) - 1)
if int(actuator['countdown']) <= 0:
actuator['countdown'] == '0'
actuator['state'] = '0'
_thread.start_new_thread(actuatorCountdown, ('fake',))
################################
## SUB STATE ARRAYS
################################
import json
# Opening JSON file...
f = open('SerosaConfig.json')
# returns JSON object as a dictionary...
data = json.load(f)
# reads in each given path of the json file...
actuators = data['actuators']
sensors = data['sensors']
commands = data['commands']
# closing the file.
f.close()
################################
## MACRO COMMANDS AND
## SUPPORT FUNCTIONS
################################
def allStop():
for actuator in actuators:
if actuator['class'] == 'thruster':
SetSingleActuator(actuator['id'], '0')
return "All Stop"
def aheadFull():
setPortStarboardThrusters(100, 0)
return "Ahead Full"
def asternFull():
setPortStarboardThrusters(-100, 0)
return "Astern Full"
def setPortStarboardThrusters(portStarboardState, elseState):
for actuator in actuators:
if actuator['class'] == 'thruster':
if ('port' in actuator['id']) or ('starboard' in actuator['id']):
SetSingleActuator(actuator['id'], portStarboardState)
else:
SetSingleActuator(actuator['id'], elseState)
def fullDive():
setAllVerticalThrusters('-100', '0')
return 'Full Dive'
def fullAscent():
setAllVerticalThrusters('100', '0')
return 'Full Ascent'
def setAllVerticalThrusters(verticalState, elseState):
for actuator in actuators:
if actuator['class'] == 'thruster':
if 'vertical' not in actuator['id']:
SetSingleActuator(actuator['id'], elseState)
else:
SetSingleActuator(actuator['id'], verticalState)
def emergencyAscent():
SetSingleActuator('globalThrottle', '100')
print(fullAscent())
return "Emergency Ascent! Blowing ballast!"
def SetSingleActuator(id, state):
for actuator in actuators:
if actuator['id'] == id:
actuator['state'] = state
actuator['countdown'] = actuator['defaultCountdown']
def allLightsOff():
setAllLightsToState('off')
return "All Lights Off"
def allLightsOn():
setAllLightsToState('on')
return "All Lights On"
def setAllLightsToState(state):
for actuator in actuators:
if actuator['class'] == 'light':
SetSingleActuator(actuator['id'], state)
def lowPower():
print(setPortStarboardThrusters('0', '0'))
print(allLightsOff())
print(SetSingleActuator('internalFan', 'off'))
return 'Low Power'
################################
## ROUTING
################################
@app.route('/', methods=['GET'])
def home():
return '''<h1>Serosa Class ROV Web Service</h1>
<p>API for controlling the movement and recording sensor readings of any Serosa class ROV.</p>'''
@app.route('/commands/list', methods=['GET'])
def api_commandsList():
return jsonify(commands)
@app.route('/commands', methods=['POST'])
def api_commands():
if not request.json or not 'id' in request.json:
return "Error: Please format request in JSON and specify an ID."
else:
id = request.json.get('id')
for command in commands:
if command['id'] == id:
functionList = globals().copy()
functionList.update(locals())
for cmd in functionList:
if cmd == command['id']:
execCmd = functionList.get(cmd)
print(execCmd())
break
return jsonify(actuators)
@app.route('/actuators/list', methods=['GET'])
def api_actuatorsList():
return jsonify(actuators)
@app.route('/actuators', methods=['POST'])
def api_setActuatorState():
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
if not request.json or not 'id' in request.json:
return "Error: Please format request in JSON and specify an ID."
else:
id = request.json.get('id')
# Check if an STATE was provided as part of the URL.
# If STATE is provided, assign it to a variable.
# If no STATE is provided, display an error in the browser.
if not request.json or not 'state' in request.json:
return "Error: Please format request in JSON and specify a STATE."
else:
state = request.json.get('state')
for actuator in actuators:
if actuator['id'] == id:
actuator['state'] = state
actuator['countdown'] = '10'
break
return jsonify(actuators)
@app.route('/sensors/list', methods=['GET'])
def api_sensorsList():
return jsonify(sensors)
@app.route('/sensors', methods=['GET'])
def api_sensors():
# Check if an ID was provided as part of the URL.
# If ID is provided, assign it to a variable.
# If no ID is provided, display an error in the browser.
if 'id' in request.args:
id = request.args['id']
else:
return "Error: No ID field provided. Please specify an ID."
# Create an empty list for our results
results = []
# Loop through the data and match results that fit the requested ID.
# IDs are unique, but other fields might return many results
for sensor in sensors:
if sensor['id'] == id:
results.append(sensor)
break
# Use the jsonify function from Flask to convert our list of
# Python dictionaries to the JSON format.
return jsonify(results)
@app.route('/sensors/all', methods=['GET'])
def api_sensorsAll():
# Create an empty list for our results...
results = []
# Loop through the full sensor list...
for sensor in sensors:
results.append(sensor)
# Return the results...
return jsonify(results)
################################
## APP.RUN()
###############################
app.run()
# if __name__ == '__main__':
# init_gui(app)
#init_gui(app)