Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,15 @@ also has **Customer portal** access controls.
All of these require an administrator, except the route planner, which needs
`view_all_routes`.

## Water testers

Readings can come straight from digital testers instead of being typed:
LabCOM-connected photometers (PoolLab, PoolWaterLAB, PrimeLab), Ondilo ICO and
iopool in-pool monitors, and CSV exports from eXact iDip, Hanna Lab, LaMotte
WaterLink Connect or any other tester app. A reading is only tied to a pool by
an explicit link, by the tech choosing it on that pool's visit, or by the office
in the water-tests inbox. See [supported testers and setup](docs/water-testers.md).

## Visual smoke check

Proves every screen renders with the design system applied (catches stale
Expand All @@ -509,6 +518,7 @@ Requires `pip install playwright && playwright install chromium`.
- [CI/CD & releases](docs/ci-cd.md) — pipeline, versioning, image tags, deploy
- [QuickBooks billing](docs/quickbooks-billing.md) — monthly reviewed-billing routine
- [QuickBooks transport contract](docs/quickbooks-api-contract.md) — API client guarantees
- [Water testers](docs/water-testers.md) — supported testers and monitors, and setup

## License

Expand Down
9 changes: 8 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
routes,
settings,
sign,
testers,
today,
users,
visits,
Expand All @@ -60,7 +61,7 @@
console.router, users.router, account.router, audit.router,
analytics.router, inventory.router, invoices.router, routes.router,
contracts.router, contract_template.router, sign.router,
improvements.router, portal.router, qbo_billing.router,
improvements.router, portal.router, qbo_billing.router, testers.router,
)


Expand Down Expand Up @@ -502,6 +503,12 @@ def sw():
app.state.scheduler.add_job(observability.logged_job(qbo_billing.sync_job),
"interval", minutes=5, args=[app.state.engine],
id="qbo_billing_sync", max_instances=1, coalesce=True)
from app import tester_clients
# Ondilo is further throttled to hourly inside sync_all (its quota is
# 30 requests/hour); LabCOM and iopool are cheap single calls.
app.state.scheduler.add_job(observability.logged_job(tester_clients.sync_job),
"interval", minutes=15, args=[app.state.engine],
id="tester_sync", max_instances=1, coalesce=True)
# The scheduler is in-process and non-persistent: a box that was down at
# 01:00 (night deploy, Unraid reboot) silently skips that night and
# nothing notices, so "14 daily generations" can quietly become 14
Expand Down
77 changes: 77 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,8 @@ class Setting(Base):
"qbo_client_secret", # QuickBooks app secret
"qbo_refresh_token", # QuickBooks refresh token
"geoapify_key", # Geoapify API key
"labcom_token", # LabCOM cloud API token (PoolLab testers)
"ondilo_refresh_token", # Ondilo ICO OAuth refresh token
})


Expand Down Expand Up @@ -849,3 +851,78 @@ class QBOBillingSource(Base):
__tablename__ = "qbo_billing_sources"
source_key: Mapped[str] = mapped_column(String(80), primary_key=True)
draft_id: Mapped[int] = mapped_column(ForeignKey("qbo_billing_drafts.id"), index=True)


# --- Water testers ------------------------------------------------------------
# Readings arrive from handheld testers (LabCOM cloud: PoolLab / PoolWaterLAB /
# PrimeLab), in-pool monitors (Ondilo ICO, iopool) and exported files (eXact
# iDip, Hanna Lab, LaMotte WaterLink Connect, anything CSV). None of them know
# which customer they belong to, so every reading lands here first and is tied
# to a pool only by an explicit TesterLink, a tech choosing it on that pool's
# visit, or an office user assigning it. See app/water_readings.py.

class TesterDevice(Base):
"""A physical tester, auto-registered the first time its serial is seen.
Assigning it to a tech is what lets that tech pull its unassigned readings
onto a visit — nobody else's tester shows up on their visit form."""
__tablename__ = "tester_devices"
__table_args__ = (UniqueConstraint("source", "serial", name="uq_tester_devices_source_serial"),)
id: Mapped[int] = mapped_column(primary_key=True)
source: Mapped[str] = mapped_column(String(16))
serial: Mapped[str] = mapped_column(String(80))
user_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
first_seen_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
user: Mapped["User | None"] = relationship()


class TesterLink(Base):
"""An external account/pool/label that always means one of our pools:
a LabCOM account, an Ondilo or iopool pool, or a customer name as it
appears in an imported file."""
__tablename__ = "tester_links"
__table_args__ = (UniqueConstraint("source", "external_key", name="uq_tester_links_source_key"),)
id: Mapped[int] = mapped_column(primary_key=True)
source: Mapped[str] = mapped_column(String(16))
external_key: Mapped[str] = mapped_column(String(160))
external_label: Mapped[str] = mapped_column(String(200), default="")
property_id: Mapped[int] = mapped_column(ForeignKey("properties.id"), index=True)
# iopool issues one API key per homeowner account; it is stored with the
# pool it reads, encrypted like every other credential (app/secretbox).
secret: Mapped[str] = mapped_column(Text, default="")
last_polled_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
created_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
property: Mapped[Property] = relationship()


class DeviceReading(Base):
"""One water test: every parameter measured in one sitting.

property_id is None until the reading is tied to a pool; visit_id is set
once a completed visit used it (UNIQUE: a reading fills one visit, ever).
`edited` records that the tech changed a value before saving, so the
visit's provenance never claims a number the tester did not produce."""
__tablename__ = "device_readings"
__table_args__ = (
UniqueConstraint("source", "external_id", name="uq_device_readings_source_ext"),
UniqueConstraint("visit_id", name="uq_device_readings_visit"),
)
id: Mapped[int] = mapped_column(primary_key=True)
source: Mapped[str] = mapped_column(String(16)) # labcom|ondilo|iopool|file
kind: Mapped[str] = mapped_column(String(8), default="handheld") # handheld|monitor
external_id: Mapped[str] = mapped_column(String(200))
device_serial: Mapped[str] = mapped_column(String(80), default="")
external_key: Mapped[str] = mapped_column(String(160), default="")
external_label: Mapped[str] = mapped_column(String(200), default="")
measured_at: Mapped[datetime] = mapped_column(DateTime, index=True)
values: Mapped[dict] = mapped_column(JSON, default=dict)
property_id: Mapped[int | None] = mapped_column(ForeignKey("properties.id"), nullable=True, index=True)
visit_id: Mapped[int | None] = mapped_column(ForeignKey("visits.id"), nullable=True)
match_method: Mapped[str] = mapped_column(String(8), default="") # link|visit|manual
status: Mapped[str] = mapped_column(String(8), default="new") # new|ignored
edited: Mapped[bool] = mapped_column(Boolean, default=False)
uploaded_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
assigned_by_id: Mapped[int | None] = mapped_column(ForeignKey("users.id"), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime, default=utcnow)
property: Mapped["Property | None"] = relationship()
visit: Mapped["Visit | None"] = relationship()
19 changes: 19 additions & 0 deletions app/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,24 @@ def overview(session, app):
"mail_configured": mailer.smtp_configured(session), "o365_connected": msoauth.connected(session),
"qbo_connected": bool(models.get_setting(session, "qbo_refresh_token", "")),
"qbo_error": bool(models.get_setting(session, "qbo_last_error", "")),
**_testers(session),
"maps_configured": bool(models.get_setting(session, "geoapify_key", "")),
"runs": runs}


def _testers(session) -> dict:
from app import tester_clients, water_readings
st = tester_clients.status(session)
waiting = session.scalar(select(func.count()).select_from(models.DeviceReading).where(
models.DeviceReading.property_id.is_(None), models.DeviceReading.status == "new")) or 0
configured = {
"labcom": bool(models.get_setting(session, "labcom_token", "")),
"ondilo": bool(models.get_setting(session, "ondilo_refresh_token", "")),
"iopool": session.scalar(select(models.TesterLink.id).where(
models.TesterLink.source == "iopool").limit(1)) is not None,
}
# A removed token leaves its last error behind; only sources still
# configured can need attention.
return {"tester_errors": [water_readings.SOURCES[s].split(" (")[0]
for s, v in st.items() if v["error"] and configured[s]],
"testers_connected": any(configured.values()), "tests_waiting": waiting}
Loading
Loading