Skip to content

Commit

Permalink
Read and parse input as strings
Browse files Browse the repository at this point in the history
  • Loading branch information
kjy5 committed Dec 15, 2023
1 parent 929fa49 commit 6765cd0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/ephys_link/platform_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def register_manipulator(self, manipulator_id: str) -> str:
try:
# Register manipulator
self._register_manipulator(manipulator_id)
com.dprint(f"[SUCCESS]\t Registered manipulator: {manipulator_id}\n")
except ValueError as ve:
# Manipulator not found in UMP
print(f"[ERROR]\t\t Manipulator not found: {manipulator_id}: {ve}\n")
Expand All @@ -109,6 +108,7 @@ def register_manipulator(self, manipulator_id: str) -> str:
print(f"{type(e)}: {e}\n")
return "Error registering manipulator"
else:
com.dprint(f"[SUCCESS]\t Registered manipulator: {manipulator_id}\n")
return ""

def unregister_manipulator(self, manipulator_id: str) -> str:
Expand Down
55 changes: 27 additions & 28 deletions src/ephys_link/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""

import importlib
import json
import sys
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -194,7 +195,7 @@ async def get_shank_count(self, _, manipulator_id: str) -> str:

return self.platform.get_shank_count(manipulator_id).json()

async def goto_pos(self, _, data: com.GotoPositionInputDataFormat) -> str:
async def goto_pos(self, _, data: str) -> str:
"""Move manipulator to position
:param _: Socket session ID (unused)
Expand All @@ -205,12 +206,12 @@ async def goto_pos(self, _, data: com.GotoPositionInputDataFormat) -> str:
:rtype: str
"""
try:
manipulator_id = data["manipulator_id"]
pos = data["pos"]
speed = data["speed"]
parsed_data: com.GotoPositionInputDataFormat = json.loads(data)
manipulator_id = parsed_data["manipulator_id"]
pos = parsed_data["pos"]
speed = parsed_data["speed"]
except KeyError:
manipulator_id = data["manipulator_id"] if "manipulator_id" in data else -1
print(f"[ERROR]\t\t Invalid data for manipulator {manipulator_id}\n")
print(f"[ERROR]\t\t Invalid goto_pos data: {data}\n")
return com.PositionalOutputData([], "Invalid data format").json()
except Exception as e:
print(f"[ERROR]\t\t Error in goto_pos: {e}\n")
Expand All @@ -220,7 +221,7 @@ async def goto_pos(self, _, data: com.GotoPositionInputDataFormat) -> str:
goto_result = await self.platform.goto_pos(manipulator_id, pos, speed)
return goto_result.json()

async def drive_to_depth(self, _, data: com.DriveToDepthInputDataFormat) -> str:
async def drive_to_depth(self, _, data: str) -> str:
"""Drive to depth
:param _: Socket session ID (unused)
Expand All @@ -231,12 +232,12 @@ async def drive_to_depth(self, _, data: com.DriveToDepthInputDataFormat) -> str:
:rtype: str
"""
try:
manipulator_id = data["manipulator_id"]
depth = data["depth"]
speed = data["speed"]
parsed_data: com.DriveToDepthInputDataFormat = json.loads(data)
manipulator_id = parsed_data["manipulator_id"]
depth = parsed_data["depth"]
speed = parsed_data["speed"]
except KeyError:
manipulator_id = data["manipulator_id"] if "manipulator_id" in data else -1
print(f"[ERROR]\t\t Invalid data for manipulator {manipulator_id}\n")
print(f"[ERROR]\t\t Invalid drive_to_depth data: {data}\n")
return com.DriveToDepthOutputData(-1, "Invalid data " "format").json()
except Exception as e:
print(f"[ERROR]\t\t Error in drive_to_depth: {e}\n")
Expand All @@ -246,7 +247,7 @@ async def drive_to_depth(self, _, data: com.DriveToDepthInputDataFormat) -> str:
drive_result = await self.platform.drive_to_depth(manipulator_id, depth, speed)
return drive_result.json()

async def set_inside_brain(self, _, data: com.InsideBrainInputDataFormat) -> str:
async def set_inside_brain(self, _, data: str) -> str:
"""Set the inside brain state
:param _: Socket session ID (unused)
Expand All @@ -257,11 +258,11 @@ async def set_inside_brain(self, _, data: com.InsideBrainInputDataFormat) -> str
:rtype: str
"""
try:
manipulator_id = data["manipulator_id"]
inside = data["inside"]
parsed_data: com.InsideBrainInputDataFormat = json.loads(data)
manipulator_id = parsed_data["manipulator_id"]
inside = parsed_data["inside"]
except KeyError:
manipulator_id = data["manipulator_id"] if "manipulator_id" in data else -1
print(f"[ERROR]\t\t Invalid data for manipulator {manipulator_id}\n")
print(f"[ERROR]\t\t Invalid set_inside_brain data: {data}\n")
return com.StateOutputData(False, "Invalid data format").json()
except Exception as e:
print(f"[ERROR]\t\t Error in inside_brain: {e}\n")
Expand Down Expand Up @@ -298,7 +299,7 @@ async def bypass_calibration(self, _, manipulator_id: str) -> str:

return self.platform.bypass_calibration(manipulator_id)

async def set_can_write(self, _, data: com.CanWriteInputDataFormat) -> com.StateOutputData:
async def set_can_write(self, _, data: str) -> str:
"""Set manipulator can_write state
:param _: Socket session ID (unused)
Expand All @@ -309,23 +310,21 @@ async def set_can_write(self, _, data: com.CanWriteInputDataFormat) -> com.State
:rtype: str
"""
try:
manipulator_id = data["manipulator_id"]
can_write = data["can_write"]
hours = data["hours"]

parsed_data: com.CanWriteInputDataFormat = json.loads(data)
manipulator_id = parsed_data["manipulator_id"]
can_write = parsed_data["can_write"]
hours = parsed_data["hours"]
except KeyError:
manipulator_id = data["manipulator_id"] if "manipulator_id" in data else -1
print(f"[ERROR]\t\t Invalid data for manipulator {manipulator_id}\n")
return com.StateOutputData(False, "Invalid data " "format")

print(f"[ERROR]\t\t Invalid set_can_write data: {data}\n")
return com.StateOutputData(False, "Invalid data " "format").json()
except Exception as e:
print(f"[ERROR]\t\t Error in inside_brain: {e}\n")
return com.StateOutputData(False, "Error in set_can_write")
return com.StateOutputData(False, "Error in set_can_write").json()
else:
com.dprint(
f"[EVENT]\t\t Set manipulator {manipulator_id} can_write state to {"true" if can_write else "false"}"
)
return self.platform.set_can_write(manipulator_id, can_write, hours, self.sio)
return self.platform.set_can_write(manipulator_id, can_write, hours, self.sio).json()

def stop(self, _) -> bool:
"""Stop all manipulators
Expand Down

0 comments on commit 6765cd0

Please sign in to comment.