Files
Odoo-Modules/fusion_iot/iot/models/iot_device.py
gsinghpal 6e964c230f 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>
2026-04-19 10:46:45 -04:00

112 lines
3.8 KiB
Python

from odoo import api, fields, models
class IotDevice(models.Model):
_name = 'iot.device'
_description = 'IOT Device'
iot_id = fields.Many2one('iot.box', string='IoT Box', required=True, index=True, ondelete='cascade')
name = fields.Char('Name')
identifier = fields.Char(string='Identifier', readonly=True)
type = fields.Selection([
('printer', 'Printer'),
('camera', 'Camera'),
('keyboard', 'Keyboard'),
('scanner', 'Barcode Scanner'),
('device', 'Device'),
('payment', 'Payment Terminal'),
('scale', 'Scale'),
('display', 'Display'),
('fiscal_data_module', 'Fiscal Data Module'),
('unsupported', 'Unsupported'),
],
readonly=True,
default='device',
string='Type',
help="Type of device.",
)
manufacturer = fields.Char(string='Manufacturer', readonly=True)
connection = fields.Selection([
('network', 'Network'),
('direct', 'USB'),
('bluetooth', 'Bluetooth'),
('serial', 'Serial'),
('hdmi', 'HDMI'),
],
readonly=True,
string="Connection",
help="Type of connection.",
)
report_ids = fields.Many2many('ir.actions.report', string='Reports')
iot_ip = fields.Char(related="iot_id.ip")
company_id = fields.Many2one('res.company', 'Company', related="iot_id.company_id")
connected_status = fields.Selection([
('disconnected', 'Disconnected'),
('connected', 'Connected'),
],
default='disconnected',
readonly=True
)
keyboard_layout = fields.Many2one('iot.keyboard.layout', string='Keyboard Layout')
display_url = fields.Char(
'Display URL',
help=(
"URL of the page that will be displayed by the device, "
"leave empty to use the customer facing display of the POS."
)
)
manual_measurement = fields.Boolean(
'Manual Measurement',
compute="_compute_manual_measurement",
help="Manually read the measurement from the device"
)
is_scanner = fields.Boolean(
string='Is Scanner',
compute="_compute_is_scanner",
inverse="_set_scanner",
help="Manually switch the device type between keyboard and scanner"
)
subtype = fields.Selection([
('receipt_printer', 'Receipt Printer'),
('label_printer', 'Label Printer'),
('office_printer', 'Office Printer'),
('', '')
],
default='',
help='Subtype of device.',
)
@api.depends('name', 'iot_id', 'connection')
@api.depends_context('formatted_display_name')
def _compute_display_name(self):
connection_display_values = dict(self._fields['connection'].selection)
for device in self:
if device.env.context.get("formatted_display_name"):
connection = connection_display_values.get(device.connection, device.connection) if device.connection else ''
device.display_name = f"{device.name} \t --{connection}-- \t --{device.iot_id.name}--"
else:
device.display_name = f"{device.name}"
@api.depends('type')
def _compute_is_scanner(self):
for device in self:
device.is_scanner = device.type == 'scanner'
def _set_scanner(self):
for device in self:
device.type = 'scanner' if device.is_scanner else 'keyboard'
@api.depends('manufacturer')
def _compute_manual_measurement(self):
for device in self:
device.manual_measurement = device.manufacturer == 'Adam'
class IotKeyboardLayout(models.Model):
_name = 'iot.keyboard.layout'
_description = 'Keyboard Layout'
name = fields.Char('Name')
layout = fields.Char('Layout')
variant = fields.Char('Variant')