feat(iot): repackaged Odoo iot modules + Fusion Plating sensor wrapper
Phase A of the IoT initiative — gets the server-side infrastructure
in place before the Raspberry Pi hardware arrives, so the iot admin
UI + /fp/iot/ingest endpoint are ready to accept the first real
temperature reading as soon as the Pi is wired up.
New top-level folder: fusion_iot/
1. **iot_base/** — Odoo S.A. iot_base module, copied from
RePackaged-Odoo verbatim. LGPL-3 upstream, no changes needed.
2. **iot/** — Odoo S.A. iot module, repackaged:
- `models/update.py` neutralised (removed the publisher_warranty
IoT-Box-counting report that phones home to odoo.com for
enterprise licence enforcement)
- `iot_handlers/lib/load_worldline_library.sh` deleted (proprietary
Worldline payment lib fetch from download.odoo.com, not needed)
- `wizard/add_iot_box.py._connect_iot_box_with_pairing_code` —
upstream called odoo.com's iot-proxy to resolve pairing codes;
replaced with a no-op. Pi-side iot_drivers proxy registers
directly with this Odoo server instead.
- Manifest rebranded with an explicit changelog preamble.
3. **fusion_plating_iot/** — new plating-specific wrapper:
- `fp.tank.sensor` — maps an iot.device (or a direct-HTTP-ingest
sensor) to a fusion.plating.tank + fusion.plating.bath.parameter.
Supports DS18B20, PT100/1000, pH, conductivity, level. Per-sensor
alert_min/max overrides.
- `fp.tank.reading` — append-only time-series. On create, evaluates
against sensor's alert range. On in-spec → out-of-spec TRANSITION,
auto-raises a fusion.plating.quality.hold (once per excursion,
no spam during sustained out-of-spec).
- `POST /fp/iot/ingest` — shared-secret HTTP endpoint for sensors
bypassing the Pi proxy. Token via X-FP-IOT-Token header OR body.
Accepts single-reading or batch payloads.
- Menu under Plating → Operations → Sensors & Readings.
- Tank form inherits get a Sensors tab inline.
Deployed to entech. Verified end-to-end:
- Install: iot_base + iot + fusion_plating_iot all 'installed'
- Smoke test: in-spec → out-of-spec → hold raised (HOLD-0010);
continued excursion → NO duplicate hold; back-in-spec → NEW
excursion → NEW hold (HOLD-0011) ✓
- HTTP endpoint: correct token → 200 accepted; wrong token → 401;
unknown device_serial → 404; batch payload → 200 accepted=N ✓
Phase B (when Raspberry Pi hardware arrives): DS18B20 iot_handler
driver for the Pi-side iot_drivers proxy + systemd service on
vanilla Raspberry Pi OS + first live reading from physical probe.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
4
fusion_iot/fusion_plating_iot/models/__init__.py
Normal file
4
fusion_iot/fusion_plating_iot/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from . import fp_tank_sensor
|
||||
from . import fp_tank_reading
|
||||
from . import fusion_plating_tank
|
||||
189
fusion_iot/fusion_plating_iot/models/fp_tank_reading.py
Normal file
189
fusion_iot/fusion_plating_iot/models/fp_tank_reading.py
Normal file
@@ -0,0 +1,189 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
# Part of the Fusion Plating product family.
|
||||
"""Time-series of sensor readings.
|
||||
|
||||
Every POST to /fp/iot/ingest (or every broadcast from the iot proxy)
|
||||
lands as a new row here. Kept intentionally append-only — we never
|
||||
update or delete readings, which makes this the compliance log for
|
||||
bath-temperature history.
|
||||
|
||||
Auto-creates a fusion.plating.quality.hold when a reading falls
|
||||
outside the sensor's alert range AND the sensor has
|
||||
`alert_on_out_of_spec` enabled. The hold is created once per
|
||||
excursion (we don't spam a new hold for every reading during a
|
||||
sustained excursion) — tracked via the sensor's most-recent
|
||||
`last_reading_in_spec` flag.
|
||||
"""
|
||||
|
||||
import logging
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class FpTankReading(models.Model):
|
||||
_name = 'fp.tank.reading'
|
||||
_description = 'Fusion Plating — Tank Sensor Reading'
|
||||
_order = 'reading_at desc, id desc'
|
||||
_rec_name = 'display_name'
|
||||
|
||||
sensor_id = fields.Many2one(
|
||||
'fp.tank.sensor', string='Sensor', required=True,
|
||||
ondelete='cascade', index=True,
|
||||
)
|
||||
# Denormalised for fast list views + kpi queries — auto-filled at
|
||||
# create time from sensor_id. Indexed for historical trending.
|
||||
tank_id = fields.Many2one(
|
||||
'fusion.plating.tank', string='Tank',
|
||||
related='sensor_id.tank_id', store=True, index=True,
|
||||
)
|
||||
bath_id = fields.Many2one(
|
||||
'fusion.plating.bath', string='Bath',
|
||||
related='sensor_id.bath_id', store=True,
|
||||
)
|
||||
parameter_id = fields.Many2one(
|
||||
'fusion.plating.bath.parameter', string='Parameter',
|
||||
related='sensor_id.parameter_id', store=True,
|
||||
)
|
||||
|
||||
reading_at = fields.Datetime(
|
||||
string='Read At', required=True,
|
||||
default=fields.Datetime.now, index=True,
|
||||
)
|
||||
value = fields.Float(
|
||||
string='Value', required=True, digits=(12, 4),
|
||||
help='Numeric reading in the parameter\'s native unit (°C, pH, '
|
||||
'µS/cm, etc.).',
|
||||
)
|
||||
unit = fields.Char(
|
||||
string='Unit', related='parameter_id.uom', store=True,
|
||||
)
|
||||
|
||||
source = fields.Selection(
|
||||
[
|
||||
('iot_proxy', 'IoT Proxy (Pi)'),
|
||||
('http_ingest', 'HTTP Ingest (direct)'),
|
||||
('manual', 'Manual Entry'),
|
||||
],
|
||||
string='Source', default='http_ingest', required=True,
|
||||
)
|
||||
in_spec = fields.Boolean(
|
||||
string='In Spec', readonly=True,
|
||||
help='Whether this reading fell within the sensor\'s alert range.',
|
||||
)
|
||||
hold_id = fields.Many2one(
|
||||
'fusion.plating.quality.hold', string='Resulting Hold',
|
||||
ondelete='set null', readonly=True,
|
||||
help='The quality hold auto-created by this reading, if any. '
|
||||
'Only the FIRST out-of-spec reading in an excursion creates '
|
||||
'a hold; subsequent readings during the same excursion do '
|
||||
'not duplicate.',
|
||||
)
|
||||
display_name = fields.Char(
|
||||
string='Display', compute='_compute_display_name', store=True,
|
||||
)
|
||||
|
||||
@api.depends('sensor_id', 'value', 'reading_at')
|
||||
def _compute_display_name(self):
|
||||
for r in self:
|
||||
sensor = r.sensor_id.name or 'sensor'
|
||||
at = fields.Datetime.to_string(r.reading_at) if r.reading_at else ''
|
||||
unit = r.unit or ''
|
||||
r.display_name = f'{sensor} — {r.value:.2f} {unit} @ {at}'
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Create hook — evaluate against spec + raise a quality hold if we
|
||||
# just crossed INTO an out-of-spec state.
|
||||
# ------------------------------------------------------------------
|
||||
@api.model_create_multi
|
||||
def create(self, vals_list):
|
||||
records = super().create(vals_list)
|
||||
for rec in records:
|
||||
try:
|
||||
rec._evaluate_spec()
|
||||
except Exception:
|
||||
# Never let alert-logic break the ingest path — the
|
||||
# reading itself is what matters for compliance. Log
|
||||
# and carry on.
|
||||
_logger.exception(
|
||||
'fp.tank.reading alert eval failed for reading %s', rec.id,
|
||||
)
|
||||
return records
|
||||
|
||||
def _evaluate_spec(self):
|
||||
"""Set `in_spec`, update sensor cache, raise hold if this reading
|
||||
is the first out-of-spec reading of a new excursion.
|
||||
"""
|
||||
self.ensure_one()
|
||||
sensor = self.sensor_id
|
||||
lo, hi = sensor._get_alert_range()
|
||||
# Zero-bounded checks: a 0 value means "no bound defined"
|
||||
ok_lo = (lo == 0.0) or (self.value >= lo)
|
||||
ok_hi = (hi == 0.0) or (self.value <= hi)
|
||||
in_spec = ok_lo and ok_hi
|
||||
self.in_spec = in_spec
|
||||
|
||||
# Track excursion transitions on the sensor so we only fire ONE
|
||||
# hold per out-of-spec episode, not one per reading.
|
||||
previously_in_spec = sensor.last_reading_in_spec
|
||||
sensor.sudo().write({
|
||||
'last_reading_value': self.value,
|
||||
'last_reading_at': self.reading_at,
|
||||
'last_reading_in_spec': in_spec,
|
||||
})
|
||||
|
||||
# Crossed from in-spec → out-of-spec on this reading
|
||||
newly_excursion = (previously_in_spec and not in_spec)
|
||||
first_reading_and_bad = (sensor.reading_count == 1 and not in_spec)
|
||||
if (newly_excursion or first_reading_and_bad) and sensor.alert_on_out_of_spec:
|
||||
self._raise_quality_hold()
|
||||
|
||||
def _raise_quality_hold(self):
|
||||
"""Create a quality hold describing the out-of-spec reading."""
|
||||
self.ensure_one()
|
||||
Hold = self.env.get('fusion.plating.quality.hold')
|
||||
if Hold is None:
|
||||
return # quality module not installed
|
||||
sensor = self.sensor_id
|
||||
lo, hi = sensor._get_alert_range()
|
||||
parts = [
|
||||
f'Sensor {sensor.name!r} reading {self.value:.2f} '
|
||||
f'{self.unit or ""} is out of spec.',
|
||||
f'Target range: {lo:.2f} .. {hi:.2f}.',
|
||||
]
|
||||
if sensor.tank_id:
|
||||
parts.append(f'Tank: {sensor.tank_id.name}.')
|
||||
if sensor.bath_id:
|
||||
parts.append(f'Bath: {sensor.bath_id.name}.')
|
||||
description = ' '.join(parts)
|
||||
|
||||
hold_vals = {
|
||||
'hold_reason': 'out_of_spec',
|
||||
'description': description,
|
||||
'qty_on_hold': 1,
|
||||
# state defaults to 'on_hold' — leave it
|
||||
}
|
||||
# Attach facility + work-centre context if the tank has them,
|
||||
# so the hold is actionable from the shop floor (operator can
|
||||
# navigate back to the tank from the hold record).
|
||||
if sensor.tank_id:
|
||||
if 'facility_id' in Hold._fields:
|
||||
tank_facility = getattr(sensor.tank_id, 'facility_id', None)
|
||||
if tank_facility:
|
||||
hold_vals['facility_id'] = tank_facility.id
|
||||
if 'part_ref' in Hold._fields:
|
||||
hold_vals['part_ref'] = f'Tank {sensor.tank_id.name} bath'
|
||||
try:
|
||||
hold = Hold.sudo().create(hold_vals)
|
||||
self.hold_id = hold.id
|
||||
_logger.info(
|
||||
'fp.tank.reading %s triggered quality hold %s (%.2f %s out of %.2f..%.2f)',
|
||||
self.id, hold.id, self.value, self.unit or '', lo, hi,
|
||||
)
|
||||
except Exception:
|
||||
_logger.exception(
|
||||
'Could not create quality hold for reading %s', self.id,
|
||||
)
|
||||
151
fusion_iot/fusion_plating_iot/models/fp_tank_sensor.py
Normal file
151
fusion_iot/fusion_plating_iot/models/fp_tank_sensor.py
Normal file
@@ -0,0 +1,151 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
# Part of the Fusion Plating product family.
|
||||
"""Sensor → tank mapping.
|
||||
|
||||
One physical sensor (a DS18B20 probe, a MAX31865 RTD, or any future
|
||||
device registered via the iot_drivers proxy) is mapped to exactly one
|
||||
tank or bath and measures ONE bath parameter (temperature, pH,
|
||||
conductivity, etc.).
|
||||
|
||||
The same tank can carry multiple sensors — e.g. a temp probe and a pH
|
||||
probe. Each is its own fp.tank.sensor row.
|
||||
"""
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class FpTankSensor(models.Model):
|
||||
_name = 'fp.tank.sensor'
|
||||
_description = 'Fusion Plating — Tank Sensor'
|
||||
_order = 'tank_id, parameter_id'
|
||||
|
||||
name = fields.Char(
|
||||
string='Sensor Name', required=True,
|
||||
help='Human label (e.g. "Tank 3 — ENP temp").',
|
||||
)
|
||||
active = fields.Boolean(default=True)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Physical device — either an Odoo iot.device (proxied through the Pi)
|
||||
# OR a direct-ingest sensor (skipping the proxy, posting straight to
|
||||
# /fp/iot/ingest with the shared secret + device_serial).
|
||||
# ------------------------------------------------------------------
|
||||
iot_device_id = fields.Many2one(
|
||||
'iot.device', string='IoT Device', ondelete='set null',
|
||||
help='The iot.device record as registered by the Pi proxy. '
|
||||
'Leave empty for direct-HTTP-ingest sensors.',
|
||||
)
|
||||
device_serial = fields.Char(
|
||||
string='Device Serial', index=True,
|
||||
help='Hardware unique ID (e.g. DS18B20 1-Wire address '
|
||||
'"28-abc123def456"). Used by /fp/iot/ingest to route '
|
||||
'posted readings to the right sensor.',
|
||||
)
|
||||
device_kind = fields.Selection(
|
||||
[
|
||||
('ds18b20', 'DS18B20 temperature'),
|
||||
('pt100', 'PT100 RTD temperature'),
|
||||
('pt1000', 'PT1000 RTD temperature'),
|
||||
('ph', 'pH probe'),
|
||||
('conductivity','Conductivity probe'),
|
||||
('level', 'Level sensor'),
|
||||
('other', 'Other'),
|
||||
],
|
||||
string='Sensor Type', default='ds18b20', required=True,
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Where this sensor lives + what it measures
|
||||
# ------------------------------------------------------------------
|
||||
tank_id = fields.Many2one(
|
||||
'fusion.plating.tank', string='Tank', ondelete='cascade',
|
||||
)
|
||||
bath_id = fields.Many2one(
|
||||
'fusion.plating.bath', string='Bath',
|
||||
help='Optional — if the sensor is bound to a specific bath '
|
||||
'chemistry rather than a physical tank.',
|
||||
)
|
||||
parameter_id = fields.Many2one(
|
||||
'fusion.plating.bath.parameter', string='Parameter Measured',
|
||||
required=True,
|
||||
help='Which bath parameter this sensor reports (temperature, pH, '
|
||||
'etc.). Drives unit labelling + out-of-spec alerting against '
|
||||
'the parameter\'s target_min / target_max.',
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Alerting behaviour
|
||||
# ------------------------------------------------------------------
|
||||
alert_on_out_of_spec = fields.Boolean(
|
||||
string='Alert on Out-of-Spec', default=True,
|
||||
help='If checked, a fusion.plating.quality.hold is auto-created '
|
||||
'when a reading falls outside the parameter target range.',
|
||||
)
|
||||
alert_min_override = fields.Float(
|
||||
string='Alert Min (override)', digits=(10, 4),
|
||||
help='Optional override of the parameter\'s target_min for this '
|
||||
'specific sensor. Leave 0 to inherit from bath.parameter.',
|
||||
)
|
||||
alert_max_override = fields.Float(
|
||||
string='Alert Max (override)', digits=(10, 4),
|
||||
help='Optional override of the parameter\'s target_max for this '
|
||||
'specific sensor.',
|
||||
)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Cached latest-reading fields (for quick display in list views)
|
||||
# ------------------------------------------------------------------
|
||||
last_reading_value = fields.Float(
|
||||
string='Latest Value', readonly=True, digits=(12, 4),
|
||||
)
|
||||
last_reading_at = fields.Datetime(string='Latest Reading', readonly=True)
|
||||
last_reading_in_spec = fields.Boolean(
|
||||
string='In Spec?', readonly=True,
|
||||
help='Computed from the last reading vs alert_min/alert_max.',
|
||||
)
|
||||
|
||||
reading_ids = fields.One2many(
|
||||
'fp.tank.reading', 'sensor_id', string='Reading History',
|
||||
)
|
||||
reading_count = fields.Integer(
|
||||
string='Readings', compute='_compute_reading_count',
|
||||
)
|
||||
|
||||
_sql_constraints = [
|
||||
('fp_tank_sensor_serial_uniq',
|
||||
'unique(device_serial)',
|
||||
'Each hardware serial can only be mapped to one sensor.'),
|
||||
]
|
||||
|
||||
@api.depends('reading_ids')
|
||||
def _compute_reading_count(self):
|
||||
for rec in self:
|
||||
rec.reading_count = len(rec.reading_ids)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Resolve effective alert range — override wins, else bath.parameter
|
||||
# ------------------------------------------------------------------
|
||||
def _get_alert_range(self):
|
||||
"""Return (min, max) floats. Zero means 'no bound'."""
|
||||
self.ensure_one()
|
||||
lo = self.alert_min_override or (
|
||||
self.parameter_id.target_min if self.parameter_id else 0.0
|
||||
)
|
||||
hi = self.alert_max_override or (
|
||||
self.parameter_id.target_max if self.parameter_id else 0.0
|
||||
)
|
||||
return (lo or 0.0, hi or 0.0)
|
||||
|
||||
def action_view_readings(self):
|
||||
self.ensure_one()
|
||||
return {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': f'Readings — {self.name}',
|
||||
'res_model': 'fp.tank.reading',
|
||||
'view_mode': 'list,form,graph',
|
||||
'domain': [('sensor_id', '=', self.id)],
|
||||
'context': {'default_sensor_id': self.id},
|
||||
'target': 'current',
|
||||
}
|
||||
33
fusion_iot/fusion_plating_iot/models/fusion_plating_tank.py
Normal file
33
fusion_iot/fusion_plating_iot/models/fusion_plating_tank.py
Normal file
@@ -0,0 +1,33 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# Copyright 2026 Nexa Systems Inc.
|
||||
# License OPL-1 (Odoo Proprietary License v1.0)
|
||||
"""Lightweight extension of fusion.plating.tank to surface its mapped
|
||||
sensors + latest reading state inline on the tank form.
|
||||
"""
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class FusionPlatingTank(models.Model):
|
||||
_inherit = 'fusion.plating.tank'
|
||||
|
||||
x_fc_sensor_ids = fields.One2many(
|
||||
'fp.tank.sensor', 'tank_id', string='Sensors',
|
||||
)
|
||||
x_fc_sensor_count = fields.Integer(
|
||||
string='Sensor Count', compute='_compute_sensor_stats',
|
||||
)
|
||||
x_fc_has_out_of_spec = fields.Boolean(
|
||||
string='Any Sensor Out of Spec', compute='_compute_sensor_stats',
|
||||
help='True if ANY mapped sensor\'s latest reading is out of spec.',
|
||||
)
|
||||
|
||||
@api.depends('x_fc_sensor_ids.last_reading_in_spec',
|
||||
'x_fc_sensor_ids.last_reading_at')
|
||||
def _compute_sensor_stats(self):
|
||||
for tank in self:
|
||||
live = tank.x_fc_sensor_ids.filtered(lambda s: s.last_reading_at)
|
||||
tank.x_fc_sensor_count = len(tank.x_fc_sensor_ids)
|
||||
tank.x_fc_has_out_of_spec = any(
|
||||
not s.last_reading_in_spec for s in live
|
||||
)
|
||||
Reference in New Issue
Block a user