Initial commit: astrape-ems split out of the combined Astrape tree

Everything that talks to the energy backend: the Sigenergy Modbus
client, its register map, the plant store, and the polling daemon.

Sits at the base of the Astrape dependency chain, because the plant
store is what satisfies gibil's PlantHistoryPort -- the oracle cannot
forecast without observed plant history. EnvLoader lives here for the
same reason: it is the only place all three repos can see it without a
fourth repo for 37 lines.

EnvLoader no longer resolves env/ from __file__. Where a package is
installed says nothing about where the deployment keeps its secrets;
it now uses the working directory, or ASTRAPE_ENV_DIR.

Extracted from a single repository where the forecasting engine, the
database plumbing, the hardware client, the ingest daemons and the web
UI all imported each other freely. Verified that every module imports
with gibil resolved from its own installed checkout.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
rpotter6298
2026-08-30 14:19:01 +02:00
commit 5b562338ab
15 changed files with 2187 additions and 0 deletions
@@ -0,0 +1,95 @@
from __future__ import annotations
import argparse
from dataclasses import dataclass
from os import environ
from sys import stderr
from time import sleep
from astrape_ems.classes.env import EnvLoader
from astrape_ems.classes.client import SigenPlantClient
from astrape_ems.classes.store import SigenStore
@dataclass(frozen=True)
class SigenDaemonConfig:
poll_seconds: float
@classmethod
def from_env(cls) -> "SigenDaemonConfig":
return cls(
poll_seconds=float(environ.get("SIGEN_POLL_SECONDS", "5")),
)
class SigenDaemon:
"""Polls Sigenergy plant metrics and stores normalized snapshots."""
def __init__(
self,
config: SigenDaemonConfig,
plant_client: SigenPlantClient,
sigen_store: SigenStore,
) -> None:
self.config = config
self.plant_client = plant_client
self.sigen_store = sigen_store
@classmethod
def from_env(cls) -> "SigenDaemon":
return cls(
config=SigenDaemonConfig.from_env(),
plant_client=SigenPlantClient.from_env(),
sigen_store=SigenStore.from_env(),
)
def initialize(self) -> None:
self.sigen_store.initialize()
def run_once(self) -> int:
snapshot = self.plant_client.fetch_snapshot()
return self.sigen_store.save_snapshot(snapshot)
def run_forever(self) -> None:
self.initialize()
while True:
try:
saved_count = self.run_once()
print(f"stored_sigen_plant_snapshots={saved_count}", flush=True)
except Exception as error:
print(f"sigen_poll_error={error}", file=stderr, flush=True)
sleep(self.config.poll_seconds)
def main() -> None:
try:
EnvLoader().load()
daemon = SigenDaemon.from_env()
args = parse_args()
if args.once:
daemon.initialize()
saved_count = daemon.run_once()
print(f"stored_sigen_plant_snapshots={saved_count}", flush=True)
return
daemon.run_forever()
except Exception as error:
print(f"sigen_daemon_startup_error={error}", file=stderr)
raise SystemExit(1) from error
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Poll Sigenergy plant metrics into Astrape's database."
)
parser.add_argument(
"--once",
action="store_true",
help="Initialize storage, save one snapshot, and exit.",
)
return parser.parse_args()
if __name__ == "__main__":
main()