Skip to content

Commit

Permalink
267 change communication to use pure json strings (#274)
Browse files Browse the repository at this point in the history
* Update python version in env

* Converted server into a class

* Fixed server class

* Respond with JSON strings

* Fix documentation

* Make catch all return "UNKNOWN_EVENT"

* Default configure for sensapex

* Read and parse input as strings
  • Loading branch information
kjy5 authored Dec 15, 2023
1 parent 4cae767 commit a9c8927
Show file tree
Hide file tree
Showing 9 changed files with 418 additions and 411 deletions.
4 changes: 2 additions & 2 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ path = "src/ephys_link/__about__.py"
exclude = ["/.github", "/.idea"]

[tool.hatch.envs.default]
python = "3.12"
dependencies = [
"coverage[toml]>=6.5",
"pytest",
Expand Down
9 changes: 6 additions & 3 deletions src/ephys_link/__main__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import argparse
import signal
import time
from importlib import metadata
from threading import Event, Thread

import serial
import serial.tools.list_ports as ports

from ephys_link import common as com
from ephys_link import server
from ephys_link.__about__ import __version__ as version
from ephys_link.server import Server

# Setup Arduino serial port (emergency stop)
poll_rate = 0.05
kill_serial_event = Event()
poll_serial_thread: Thread

# Create Server
server = Server()


def poll_serial(kill_event: Event, serial_port: str) -> None:
"""Continuously poll serial port for data
Expand Down Expand Up @@ -100,7 +103,7 @@ def close_serial(_, __) -> None:
"-v",
"--version",
action="version",
version=f"Electrophysiology Manipulator Link v{metadata.version('ephys_link')}",
version=f"Electrophysiology Manipulator Link v{version}",
help="Print version and exit",
)

Expand Down
25 changes: 25 additions & 0 deletions src/ephys_link/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import json
from typing import TypedDict

# Debugging flag
Expand Down Expand Up @@ -81,6 +82,10 @@ def __init__(self, manipulators: list, num_axes: int, dimensions: list, error: s
error=error,
)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)


class PositionalOutputData(dict):
"""Output format for (position, error)
Expand All @@ -98,6 +103,10 @@ def __init__(self, position: list, error: str) -> None:
"""Constructor"""
super().__init__(position=position, error=error)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)


class AngularOutputData(dict):
"""Output format for (angles, error)
Expand All @@ -112,6 +121,10 @@ def __init__(self, angles: list, error: str) -> None:
"""Constructor"""
super().__init__(angles=angles, error=error)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)


class ShankCountOutputData(dict):
"""Output format for (num_shanks, error)
Expand All @@ -126,6 +139,10 @@ def __init__(self, shank_count: int, error: str) -> None:
"""Constructor"""
super().__init__(shank_count=shank_count, error=error)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)


class DriveToDepthOutputData(dict):
"""Output format for depth driving (depth, error)
Expand All @@ -142,6 +159,10 @@ def __init__(self, depth: float, error: str) -> None:
"""Create drive to depth output data dictionary"""
super().__init__(depth=depth, error=error)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)


class StateOutputData(dict):
"""Output format for (state, error)
Expand All @@ -157,3 +178,7 @@ class StateOutputData(dict):
def __init__(self, state: bool, error: str) -> None:
"""Create state output data dictionary"""
super().__init__(state=state, error=error)

def json(self) -> str:
"""Return JSON string"""
return json.dumps(self)
1 change: 0 additions & 1 deletion src/ephys_link/gui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# pylama:skip=1
import socket
from argparse import Namespace
from threading import Event, Thread
Expand Down
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
Loading

0 comments on commit a9c8927

Please sign in to comment.