"""One-shot setup for the live Pi probe. Does three things: 1. Rotates fusion_plating_iot.ingest_token to a real random secret. 2. Picks (or creates) a test tank + temperature parameter. 3. Creates the fp.tank.sensor record mapped to the real probe serial (28-000000b276e4), with plating-realistic alert thresholds. Run: cat fp_iot_setup_live_sensor.py | odoo shell -c /etc/odoo/odoo.conf -d admin --no-http Prints the new token at the end — copy it into the Pi poller script. """ import secrets env = env # noqa — odoo shell SERIAL = '28-000000b276e4' SENSOR_NAME = 'Pi IoT pilot — DS18B20 #1' # ----------------------------------------------------------- # 1. Rotate token # ----------------------------------------------------------- new_token = f'fp-iot-{secrets.token_urlsafe(24)}' env['ir.config_parameter'].sudo().set_param( 'fusion_plating_iot.ingest_token', new_token, ) print(f'\n>>> Token rotated to: {new_token}') print(' (use this in the Pi poller X-FP-IOT-Token header)\n') # ----------------------------------------------------------- # 2. Pick / create test tank + temperature parameter # ----------------------------------------------------------- tank = env['fusion.plating.tank'].search( [('code', '=', 'SMOKE-TEST')], limit=1, ) or env['fusion.plating.tank'].search([], limit=1) if not tank: facility = env['fusion.plating.facility'].search([], limit=1) tank = env['fusion.plating.tank'].create({ 'name': 'IoT Pilot Tank', 'code': 'IOT-PILOT-01', 'facility_id': facility.id if facility else False, }) print(f'Tank: {tank.name} (id={tank.id}, code={tank.code})') param = env['fusion.plating.bath.parameter'].search( [('parameter_type', '=', 'temperature')], limit=1, ) or env['fusion.plating.bath.parameter'].search([], limit=1) print(f'Parameter: {param.name} ' f'(target range {param.target_min}..{param.target_max} {param.uom or ""})') # ----------------------------------------------------------- # 3. Create / update fp.tank.sensor # ----------------------------------------------------------- # Alert thresholds chosen for bench testing — 15°C to 35°C will stay # in-spec at room temp, goes out if you squeeze the probe. sensor = env['fp.tank.sensor'].search([('device_serial', '=', SERIAL)], limit=1) vals = { 'name': SENSOR_NAME, 'device_serial': SERIAL, 'device_kind': 'ds18b20', 'tank_id': tank.id, 'parameter_id': param.id, 'alert_min_override': 15.0, 'alert_max_override': 35.0, 'alert_on_out_of_spec': True, } if sensor: sensor.write(vals) print(f'Updated existing sensor id={sensor.id}') else: sensor = env['fp.tank.sensor'].create(vals) print(f'Created sensor id={sensor.id}') print(f' alert range: {sensor.alert_min_override}°C .. {sensor.alert_max_override}°C') env.cr.commit() print('\n✓ Setup committed.\n') print('Next step — deploy the Pi poller with token:') print(f' {new_token}')