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>
This commit is contained in:
@@ -23,6 +23,7 @@ _logger = logging.getLogger(__name__)
|
||||
def post_init_hook(env):
|
||||
_backfill_uuids(env)
|
||||
_backfill_sensor_types(env)
|
||||
_seed_entech_tanks_and_sensors(env)
|
||||
|
||||
|
||||
def _backfill_uuids(env):
|
||||
@@ -74,3 +75,98 @@ def _backfill_sensor_types(env):
|
||||
updated += 1
|
||||
_logger.info('fp.tank.sensor: set default sensor_type_id on %d records',
|
||||
updated)
|
||||
|
||||
|
||||
def _seed_entech_tanks_and_sensors(env):
|
||||
"""Sub 7 — seed 25 tanks (5 small + 20 big; 10 big inactive) with
|
||||
one temperature and one pH sensor each.
|
||||
|
||||
Idempotent: tanks keyed by `code` so re-runs skip existing rows.
|
||||
Sensors keyed by (tank_id, parameter_type) so duplicates aren't
|
||||
created if the admin added a temp/pH sensor manually after seed.
|
||||
|
||||
Opt-in via system parameter `fusion_plating_iot.seed_entech_tanks`.
|
||||
Default False so a fresh install doesn't auto-seed on anyone else.
|
||||
Admin sets it to '1' via Settings → Technical → System Parameters
|
||||
and re-runs the module upgrade (-u fusion_plating_iot) to trigger.
|
||||
"""
|
||||
Param = env['ir.config_parameter'].sudo()
|
||||
if Param.get_param('fusion_plating_iot.seed_entech_tanks', '0') != '1':
|
||||
return
|
||||
|
||||
# with_context(active_test=False) so the idempotency search sees
|
||||
# the inactive big tanks and doesn't try to recreate them on re-run.
|
||||
Tank = env['fusion.plating.tank'].with_context(active_test=False)
|
||||
Sensor = env['fp.tank.sensor'].with_context(active_test=False)
|
||||
Parameter = env['fusion.plating.bath.parameter']
|
||||
Facility = env['fusion.plating.facility']
|
||||
|
||||
facility = Facility.search([], limit=1)
|
||||
if not facility:
|
||||
_logger.warning('Sub 7 seed: no fusion.plating.facility found — '
|
||||
'skipping tank seed. Create a facility first.')
|
||||
return
|
||||
|
||||
temp_param = (
|
||||
Parameter.search([('code', '=', 'TEMP')], limit=1)
|
||||
or Parameter.search([('parameter_type', '=', 'temperature')], limit=1)
|
||||
)
|
||||
ph_param = (
|
||||
Parameter.search([('code', '=', 'PH')], limit=1)
|
||||
or Parameter.search([('parameter_type', '=', 'ph')], limit=1)
|
||||
)
|
||||
if not temp_param or not ph_param:
|
||||
_logger.warning('Sub 7 seed: temperature / pH bath parameters '
|
||||
'not found — skipping. Seed bath parameters first.')
|
||||
return
|
||||
|
||||
plan = []
|
||||
for i in range(1, 6):
|
||||
plan.append({'name': 'Small Tank #%d' % i, 'code': 'SMALL-%02d' % i,
|
||||
'active': True, 'sequence': 10 + i})
|
||||
for i in range(1, 21):
|
||||
plan.append({'name': 'Big Tank #%d' % i, 'code': 'BIG-%02d' % i,
|
||||
'active': (i <= 10), 'sequence': 20 + i})
|
||||
|
||||
tanks_created = 0
|
||||
sensors_created = 0
|
||||
for row in plan:
|
||||
tank = Tank.search([('code', '=', row['code'])], limit=1)
|
||||
if not tank:
|
||||
tank = Tank.create({
|
||||
'name': row['name'],
|
||||
'code': row['code'],
|
||||
'facility_id': facility.id,
|
||||
'sequence': row['sequence'],
|
||||
'active': row['active'],
|
||||
})
|
||||
tanks_created += 1
|
||||
# Temperature sensor
|
||||
if not Sensor.search_count([
|
||||
('tank_id', '=', tank.id),
|
||||
('parameter_id.parameter_type', '=', 'temperature'),
|
||||
]):
|
||||
Sensor.create({
|
||||
'name': '%s — Temperature' % tank.name,
|
||||
'tank_id': tank.id,
|
||||
'parameter_id': temp_param.id,
|
||||
'device_kind': 'ds18b20',
|
||||
'active': row['active'],
|
||||
})
|
||||
sensors_created += 1
|
||||
# pH sensor
|
||||
if not Sensor.search_count([
|
||||
('tank_id', '=', tank.id),
|
||||
('parameter_id.parameter_type', '=', 'ph'),
|
||||
]):
|
||||
Sensor.create({
|
||||
'name': '%s — pH' % tank.name,
|
||||
'tank_id': tank.id,
|
||||
'parameter_id': ph_param.id,
|
||||
'device_kind': 'ph',
|
||||
'active': row['active'],
|
||||
})
|
||||
sensors_created += 1
|
||||
|
||||
_logger.info('Sub 7 seed: created %d tanks + %d sensors',
|
||||
tanks_created, sensors_created)
|
||||
|
||||
Reference in New Issue
Block a user