Skip to content

Commit

Permalink
Replace TPM script with source using tpm-multicast-client
Browse files Browse the repository at this point in the history
  • Loading branch information
albireox committed May 15, 2024
1 parent b0d6187 commit fcb9645
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 24 deletions.
3 changes: 3 additions & 0 deletions cerebro/etc/cerebro.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ profiles:
config: ${JAEGER_DIR}/src/jaeger/etc/chiller_APO.yaml
delay: 30
measure_timeout: 30.0
- tpm_data:
type: tpm
bucket: TPM
- tron:
type: tron
bucket: actors
Expand Down
43 changes: 20 additions & 23 deletions cerebro/sources/tpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
import asyncio
from contextlib import suppress

from tpm_multicast_client import TPMClient

from cerebro import log

from .source import DataPoints, Source


try:
import tpmdata # type: ignore
except ImportError:
tpmdata = None


__all__ = ["TPMSource"]


Expand All @@ -40,18 +36,17 @@ class TPMSource(Source):
source_type = "tpm"

def __init__(self, name: str, **kwargs):
if tpmdata is None:
raise RuntimeError("tpmdata cannot be imported.")

tpmdata.tinit()
self.tpm_client = TPMClient()

super().__init__(name, **kwargs)

self._running: asyncio.Task | None = None
self._listen_task: asyncio.Task | None = None

async def start(self):
"""Connects to the socket."""

self._listen_task = asyncio.create_task(self.tpm_client.listen())
self._runner = asyncio.create_task(self.read())

await super().start()
Expand All @@ -70,24 +65,26 @@ async def stop(self):
await self._runner
self._runner = None

if self._listen_task:
self._listen_task.cancel()
await self._listen_task
self._listen_task = None

super().stop()

async def read(self):
"""Reads a packet from the TPM."""

assert tpmdata is not None

while True:
loop = asyncio.get_running_loop()
dd = await loop.run_in_executor(None, tpmdata.packet, 0, 0)

if len(dd) > 0:
tags = self.tags.copy()
data_points = DataPoints(
data=[{"measurement": "tpm", "fields": dd, "tags": tags}],
bucket=self.bucket,
)

self.on_next(data_points)
if self.tpm_client.data is not None:
data = self.tpm_client.data
if len(data) > 0:
tags = self.tags.copy()
data_points = DataPoints(
data=[{"measurement": "tpm", "fields": data, "tags": tags}],
bucket=self.bucket,
)

self.on_next(data_points)

await asyncio.sleep(1)
17 changes: 16 additions & 1 deletion poetry.lock

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 @@ -42,6 +42,7 @@ pymysql = "^1.0.2"
peewee = "^3.15.4"
asyncudp = "^0.11.0"
lvmopstools = "^0.1.0"
sdss-tpm-multicast-client = "^0.3.0"

[tool.poetry.dev-dependencies]
ipython = ">=8.0.0"
Expand Down

0 comments on commit fcb9645

Please sign in to comment.