Files
gsinghpal a7fd39d6f3 feat(iot): Sub 7 — per-sensor polling interval + rate-limit + entech seed
Per-sensor override on fp.tank.sensor.poll_interval_minutes with a
company-wide default (res.company.x_fc_default_poll_interval_minutes,
default 30) exposed in Settings → Fusion Plating → IoT. Single
lookup helper _fp_effective_poll_interval_minutes keeps downstream
call sites simple. Read-only poll_interval_display Char ("30 min
(default)" / "15 min (override)") keeps units unambiguous per user
request.

Ingest endpoint /fp/iot/ingest drops readings that arrive inside a
sensor's effective interval, returning {accepted, skipped} so the Pi
agent can log it. Pi-side interval stays its own concern.

Post-init hook seeds 5 small tanks + 20 big tanks (10 active, 10
inactive) with 1 temperature + 1 pH sensor each → 25 tanks, 50
sensors. Idempotent (keyed by tank.code, with_context(active_test=
False)). Opt-in via ir.config_parameter
fusion_plating_iot.seed_entech_tanks = '1' so a fresh install
elsewhere doesn't auto-seed. Flag set on entech today; 27 tanks / 52
sensors now live (2 pilot + 25 seeded).

Smoke on entech: 14/14 assertions pass including idempotency and
rate-limit conditions.

fusion_plating_iot → 19.0.2.0.0

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-22 23:29:08 -04:00

163 lines
6.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
# Copyright 2026 Nexa Systems Inc.
# License OPL-1 (Odoo Proprietary License v1.0)
# Part of the Fusion Plating product family.
"""Direct-HTTP ingest endpoint for sensors that bypass the Odoo IoT proxy.
Authentication: shared secret header `X-FP-IOT-Token` compared to the
system parameter `fusion_plating_iot.ingest_token`. The Pi proxy (via
iot_drivers) uses Odoo's built-in websocket and doesn't need this path.
Payload (JSON):
{
"device_serial": "28-abc123def456",
"value": 87.3,
"unit": "C", // informational, optional
"read_at": "2026-04-19T13:12:05Z" // optional; defaults to now
}
Or a batch form:
{
"token": "<shared secret>", // alternative to X-FP-IOT-Token
"readings": [
{"device_serial": "...", "value": ..., "read_at": "..."},
...
]
}
Returns 200 + `{ok: true, accepted: N}` on success, 401 on auth fail,
404 if any device_serial isn't mapped to a fp.tank.sensor.
"""
import hmac
import json
import logging
from datetime import datetime, timedelta, timezone
from odoo import http
from odoo.http import request, Response
_logger = logging.getLogger(__name__)
def _parse_read_at(raw):
"""Best-effort ISO-8601 parse — fall back to 'now' on garbage input."""
from odoo.fields import Datetime as OdooDatetime
if not raw:
return OdooDatetime.now()
try:
# Accept both "2026-04-19T13:12:05Z" and "2026-04-19 13:12:05"
s = raw.replace('Z', '+00:00')
dt = datetime.fromisoformat(s)
# Strip tz to store naive UTC, which is what Odoo Datetime fields store
if dt.tzinfo is not None:
dt = dt.astimezone(timezone.utc).replace(tzinfo=None)
return dt
except Exception:
return OdooDatetime.now()
class FpIotIngestController(http.Controller):
@http.route('/fp/iot/ingest', type='http', auth='public',
methods=['POST'], csrf=False, save_session=False)
def ingest(self, **_kwargs):
"""Accept one-or-many sensor readings and land them in fp.tank.reading."""
# Pull the shared secret from config — configured at install via
# data/ir_config_parameter_data.xml, but admins can rotate it
# in Settings → Technical → System Parameters.
expected = request.env['ir.config_parameter'].sudo().get_param(
'fusion_plating_iot.ingest_token', ''
)
if not expected:
_logger.warning('fp.iot.ingest: token not configured — all requests rejected')
return Response(
json.dumps({'ok': False, 'error': 'token_not_configured'}),
status=503, content_type='application/json',
)
# Accept token via either header or payload body — some simple
# sensors can't easily set custom headers.
header_token = request.httprequest.headers.get('X-FP-IOT-Token', '')
raw = request.httprequest.get_data(as_text=True) or ''
try:
body = json.loads(raw) if raw else {}
except ValueError:
return Response(
json.dumps({'ok': False, 'error': 'invalid_json'}),
status=400, content_type='application/json',
)
body_token = body.get('token', '')
presented = header_token or body_token
if not hmac.compare_digest(str(presented), str(expected)):
return Response(
json.dumps({'ok': False, 'error': 'unauthorised'}),
status=401, content_type='application/json',
)
# Normalise payload to a list of readings.
readings = body.get('readings')
if readings is None:
# Single-reading shortcut
if 'device_serial' in body and 'value' in body:
readings = [body]
else:
return Response(
json.dumps({'ok': False, 'error': 'no_readings'}),
status=400, content_type='application/json',
)
Sensor = request.env['fp.tank.sensor'].sudo()
Reading = request.env['fp.tank.reading'].sudo()
accepted = 0
skipped_interval = 0
unknown_serials = []
for r in readings:
serial = (r.get('device_serial') or '').strip()
if not serial:
continue
sensor = Sensor.search([('device_serial', '=', serial)], limit=1)
if not sensor:
unknown_serials.append(serial)
continue
try:
value = float(r.get('value'))
except (TypeError, ValueError):
continue
# Sub 7 — per-sensor rate-limit. Drop readings that arrive
# inside the sensor's effective polling interval so the Pi
# agent can happily poll every 30 s while the log only
# retains a row every 1530 min. Inactive sensors also
# dropped so disabled tanks don't clutter the log.
if not sensor.active:
skipped_interval += 1
continue
interval = sensor._fp_effective_poll_interval_minutes()
if interval > 0 and sensor.last_reading_at:
read_at = _parse_read_at(r.get('read_at'))
elapsed = read_at - sensor.last_reading_at
if elapsed < timedelta(minutes=interval):
skipped_interval += 1
continue
Reading.create({
'sensor_id': sensor.id,
'value': value,
'reading_at': _parse_read_at(r.get('read_at')),
'source': 'http_ingest',
})
accepted += 1
status = 200 if (accepted or skipped_interval) else (404 if unknown_serials else 400)
payload = {
'ok': (accepted + skipped_interval) > 0,
'accepted': accepted,
'skipped': skipped_interval,
}
if unknown_serials:
payload['unknown_serials'] = unknown_serials
return Response(
json.dumps(payload),
status=status, content_type='application/json',
)