before refactoring

This commit is contained in:
2025-09-28 23:14:23 -04:00
parent 8964203f09
commit fd27e36fa6
11 changed files with 105 additions and 95 deletions

2
.gitignore vendored
View File

@@ -1 +1,3 @@
.venv/ .venv/
__pycache__
*.egg-info

52
Makefile Normal file
View File

@@ -0,0 +1,52 @@
# Makefile for medtrace-synth
VENV := .venv
PYTHON := $(VENV)/bin/python
PIP := $(PYTHON) -m pip
POJAGI_DSP_PATH ?= ../pojagi-dsp
help:
@echo "Targets:"
@echo " venv - Create virtualenv in .venv"
@echo " install - Install deps and this package"
@echo " run - Run the server via 'python -m medtrace_synth'"
@echo " install-dev - Install deps (and this package) in dev mode"
@echo " dev - Run using PYTHONPATH=src (no install)"
@echo " build - Build sdist and wheel into dist/"
@echo " clean - Remove build artifacts"
@echo " nuke - Clean artifacts and remove .venv"
$(VENV):
python3 -m venv $(VENV)
$(PIP) -q install -U pip
install: $(VENV)
$(PIP) install .[live]
install-dev: $(VENV)
$(PIP) install -e .[dev]
$(PIP) install -e $(POJAGI_DSP_PATH)
run: install
$(PYTHON) -m medtrace_synth
dev: install-dev
PYTHONPATH=src $(PYTHON) -m watchfiles \
--filter=python \
--target-type=command \
"$(PYTHON) -m medtrace_synth" \
src $(POJAGI_DSP_PATH)
build: install
$(PIP) install -U hatch
$(PYTHON) -m hatch build
clean:
rm -rf build dist .eggs *.egg-info
nuke: clean
rm -rf $(VENV)
.PHONY: help install install-dev run dev build clean nuke

1
README.md Normal file
View File

@@ -0,0 +1 @@
# Back-end

View File

@@ -1,46 +0,0 @@
import time
import websockets
import asyncio
from pojagi_dsp.channel.ecg.generator.wavetable import ECGWaveTableSynthesizer
from pojagi_dsp.channel.ecg.generator.wavetable.sinus import SinusWaveTable, TachycardiaWaveTable
PORT = 7890
async def consumer_handler(websocket):
async for message in websocket:
print(f"message received: {message}")
async def producer_handler(websocket):
ecg = ECGWaveTableSynthesizer(
tables={
(0,90): SinusWaveTable(),
(70, 300): TachycardiaWaveTable(),
},
heart_rate=70,
srate=50,
)
while True:
try:
message = next(ecg)
await websocket.send(str(message))
time.sleep(0.01)
except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected")
break
async def handler(websocket, path):
while True:
print(f"New connection. Path: {path}")
consumer_task = asyncio.create_task(consumer_handler(websocket))
producer_task = asyncio.create_task(producer_handler(websocket))
done, pending = await asyncio.wait(
[consumer_task, producer_task],
return_when=asyncio.FIRST_COMPLETED,
)
for task in pending:
task.cancel()
# Start the server
start_server = websockets.serve(handler, "0.0.0.0", PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

View File

@@ -1,24 +1,26 @@
[build-system]
requires = ["setuptools", "setuptools-scm"]
build-backend = "setuptools.build_meta"
[project] [project]
name = "medtrace-synth" name = "medtrace-synth"
version = "0.0.0.dev0"
description = "Synthesized biometric signals service" description = "Synthesized biometric signals service"
urls = { "gitlab" = "https://gitlab.pojagi.org/tjb1982/medtrace-synth" } readme = "README.md"
requires-python = ">=3.8"
authors = [ authors = [
{ name = "Tom Brennan", email = "tjb1982@gmail.com" }, { name = "Tom Brennan", email = "tjb1982@gmail.com" },
] ]
readme = "README.md"
requires-python = ">=3.8"
classifiers = [] classifiers = []
dependencies = [ urls = { "gitlab" = "https://git.pojagi.org/tjb1982/medtrace-synth" }
# "pydantic==1.10.2", dependencies = ["websockets==12.0"]
"pojagi-dsp==0.0.0.dev0",
# "PyYAML>=5.3.1",
"websockets==12.0", [project.optional-dependencies]
] dev = ["watchfiles"] # "pojagi-dsp @ file://{root}/../pojagi-dsp"]
live = ["pojagi-dsp @ git+https://git.pojagi.org/tjb1982/pojagi-dsp@blueberry"]
version = "0.0.0.dev0" [build-system]
# dynamic = ["version"] requires = ["hatchling"]
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/medtrace_synth"]
[tool.hatch.metadata]
allow-direct-references = true

View File

@@ -1,10 +0,0 @@
Metadata-Version: 2.1
Name: medtrace-synth
Version: 0.0.0.dev0
Summary: Synthesized biometric signals service
Author-email: Tom Brennan <tjb1982@gmail.com>
Project-URL: gitlab, https://gitlab.pojagi.org/tjb1982/medtrace-synth
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: pojagi-dsp==0.0.0.dev0
Requires-Dist: websockets==12.0

View File

@@ -1,8 +0,0 @@
.gitignore
pyproject.toml
src/medtrace_synth/__main__.py
src/medtrace_synth.egg-info/PKG-INFO
src/medtrace_synth.egg-info/SOURCES.txt
src/medtrace_synth.egg-info/dependency_links.txt
src/medtrace_synth.egg-info/requires.txt
src/medtrace_synth.egg-info/top_level.txt

View File

@@ -1,2 +0,0 @@
pojagi-dsp==0.0.0.dev0
websockets==12.0

View File

@@ -1 +0,0 @@
medtrace_synth

View File

@@ -1,33 +1,50 @@
import time
import websockets
import asyncio import asyncio
import logging
import time
import websockets
from pojagi_dsp.channel.ecg.generator.wavetable import ECGWaveTableSynthesizer from pojagi_dsp.channel.ecg.generator.wavetable import ECGWaveTableSynthesizer
from pojagi_dsp.channel.ecg.generator.wavetable.sinus import SinusWaveTable, TachycardiaWaveTable from pojagi_dsp.channel.ecg.generator.wavetable.sinus import (
SinusWaveTable, TachycardiaWaveTable)
if __name__ != "__main__":
raise ImportWarning("This script is not intended to be imported.")
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
log = logging.getLogger(__name__)
PORT = 7890 PORT = 7890
async def consumer_handler(websocket):
async def consumer_handler(websocket: websockets.WebSocketServerProtocol):
async for message in websocket: async for message in websocket:
print(f"message received: {message}") print(f"message received: {message}")
async def producer_handler(websocket):
async def producer_handler(websocket: websockets.WebSocketServerProtocol):
srate = 50
ecg = ECGWaveTableSynthesizer( ecg = ECGWaveTableSynthesizer(
tables={ tables={
(0,90): SinusWaveTable(), (0, 90): SinusWaveTable(),
(70, 300): TachycardiaWaveTable(), (70, 300): TachycardiaWaveTable(),
}, },
heart_rate=70, heart_rate=60,
srate=50, srate=srate,
) )
while True: while True:
try: try:
message = next(ecg) message = next(ecg)
await websocket.send(str(message)) await websocket.send(str(message))
time.sleep(0.01) await asyncio.sleep(1/srate)
except websockets.exceptions.ConnectionClosed as e: except websockets.exceptions.ConnectionClosed as e:
print("A client just disconnected") print("A client just disconnected")
break break
async def handler(websocket, path): async def handler(websocket, path):
while True: while True:
print(f"New connection. Path: {path}") print(f"New connection. Path: {path}")
@@ -43,4 +60,8 @@ async def handler(websocket, path):
# Start the server # Start the server
start_server = websockets.serve(handler, "0.0.0.0", PORT) start_server = websockets.serve(handler, "0.0.0.0", PORT)
asyncio.get_event_loop().run_until_complete(start_server) asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
try:
asyncio.get_event_loop().run_forever()
except KeyboardInterrupt:
log.info("exiting...")