Implement cable_details call

This commit is contained in:
2024-03-26 18:41:59 -05:00
parent 6d6c2030a9
commit ad216f21fa
4 changed files with 109 additions and 92 deletions

186
run.py
View File

@@ -90,7 +90,15 @@ def wait_for(val, name):
while val is False:
sleep(0.1)
def send_data(type, call, data, client_id="*"):
out = dict()
out["type"] = type
out["call"] = call
out["data"] = data
to_server_queue.put((client_id, json.dumps(out)))
def start_server_socket():
global jbs
"""app = Flask(__name__)
@app.route('/report_ip', methods=['POST'])
@@ -122,92 +130,99 @@ def start_server_socket():
# Message handler
try:
decoded = json.loads(message)
if "type" not in decoded:
fprint("Missing \"type\" field.")
continue
if "call" not in decoded:
fprint("Missing \"call\" field.")
continue
if "data" not in decoded:
fprint("Missing \"data\" field.")
continue
# if we get here, we have a "valid" data packet
data = decoded["data"]
call = decoded["call"]
match decoded["type"]:
case "log":
fprint("log message")
if call == "send":
fprint("webapp: " + str(data), sendqueue=to_server_queue)
elif call == "request":
fprint("")
case "cable_map":
fprint("cable_map message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case "ping":
fprint("Pong!!!")
# Lucas' notes
# Add a ping pong :) response/handler
# Add a get cable response/handler
# this will tell the robot arm to move
# Call for turning off everything
# TODO Helper for converting Python Dictionaries to JSON
# make function: pythonData --> { { "type": "...", "call": "...", "data": pythonData } }
# to send: to_server_queue.put(("*", "JSON STRING HERE")) # replace * with UUID of client to send to one specific location
case "cable_details":
fprint("cable_details message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case "cable_search":
fprint("cable_search message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case "keyboard":
fprint("keyboard message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
if data["enabled"] == True:
# todo : send this to client
p = Process(target=run_cmd, args=("./keyboard-up.ps1",))
p.start()
elif data["enabled"] == False:
p = Process(target=run_cmd, args=("./keyboard-down.ps1",))
p.start()
case "machine_settings":
fprint("machine_settings message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case _:
fprint("Unknown/unimplemented data type: " + decoded["type"])
except:
fprint("Non-JSON message recieved")
continue
if "type" not in decoded:
fprint("Missing \"type\" field.")
continue
if "call" not in decoded:
fprint("Missing \"call\" field.")
continue
if "data" not in decoded:
fprint("Missing \"data\" field.")
continue
# if we get here, we have a "valid" data packet
data = decoded["data"]
call = decoded["call"]
match decoded["type"]:
case "log":
fprint("log message")
if call == "send":
fprint("webapp: " + str(data), sendqueue=to_server_queue)
elif call == "request":
fprint("")
case "cable_map":
fprint("cable_map message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case "ping":
fprint("Pong!!!")
# Lucas' notes
# Add a ping pong :) response/handler
# Add a get cable response/handler
# this will tell the robot arm to move
# Call for turning off everything
# TODO Helper for converting Python Dictionaries to JSON
# make function: pythonData --> { { "type": "...", "call": "...", "data": pythonData } }
# to send: to_server_queue.put(("*", "JSON STRING HERE")) # replace * with UUID of client to send to one specific location
case "cable_details":
fprint("cable_details message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
dataout = dict()
dataout["cables"] = list()
print(data)
if "part_number" in data:
for part in data["part_number"]:
print(part)
print(jbs.get_partnum(part))
dataout["cables"].append(jbs.get_partnum(part)["fullspecs"])
if "position" in data:
for pos in data["position"]:
print(pos)
print(jbs.get_position(str(pos)))
dataout["cables"].append(jbs.get_position(str(pos))["fullspecs"])
send_data(decoded["type"], "send", dataout, client_id)
case "cable_search":
fprint("cable_search message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case "keyboard":
fprint("keyboard message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
if data["enabled"] == True:
# todo : send this to client
p = Process(target=run_cmd, args=("./keyboard-up.ps1",))
p.start()
elif data["enabled"] == False:
p = Process(target=run_cmd, args=("./keyboard-down.ps1",))
p.start()
case "machine_settings":
fprint("machine_settings message")
if call == "send":
fprint("")
elif call == "request":
fprint("")
case _:
fprint("Unknown/unimplemented data type: " + decoded["type"])
sleep(0.001) # Sleep to prevent tight loop
@@ -262,6 +277,7 @@ def setup_server(pool):
ledsys = LEDSystem()
pool.apply_async(ledsys.init, callback=led_start_callback)
#pool.apply_async(sensor_control.init, callback=sensor_start_callback)
jbs = JukeboxSearch()
serverproc = Process(target=start_server_socket)
serverproc.start()
@@ -293,7 +309,7 @@ def setup_server(pool):
fprint("Arm initialized.", sendqueue=to_server_queue)
jbs = JukeboxSearch()
return True
@@ -391,7 +407,7 @@ def mainloop_server(pool):
"BLRA500P"
]
cable_list = tmp
pool.apply_async(get_specs.get_multi, (tmp, 0.5), callback=cable_search_callback)
pool.apply_async(get_specs.get_multi, (tmp, 0.3), callback=cable_search_callback)
mode = "Parsing"
fprint("All cables scanned. Finding & parsing datasheets...")
if mode == "Parsing":
@@ -423,6 +439,8 @@ def mainloop_server(pool):
specs = json.load(f)
searchdata["fullspecs"] = specs
jbs.add_document(searchdata)
#sleep(0.5)
#print(jbs.get_position("1"))
fprint("All cables added to database.")
mode = "Idle"