name: water_storage_tank__lwm2m
description: >
  Virtual water storage tank with a numerically evolving level and remotely
  writable analog inlet valve over LwM2M/CoAP.

meta_parameters:
  lwm2m_local_port:
    type: int
    default: 0
  lwm2m_endpoint:
    type: str
    default: "spx-water-tank-001"
  lwm2m_server_host:
    type: str
    default: leshan-server
  lwm2m_server_port:
    type: int
    default: 5683

attributes:
  tank_level_pct:
    type: float
    default: 42.0
    unit: percent
  k__valve_opening_ratio:
    type: float
    default: 0.4
    unit: ratio
  inflow_gain_pct_per_step:
    type: float
    default: 2.5
    unit: percent
  outlet_loss_pct_per_step:
    type: float
    default: 0.6
    unit: percent
  tank_level_min_pct:
    type: float
    default: 42.0
    unit: percent
  tank_level_max_pct:
    type: float
    default: 42.0
    unit: percent
  lwm2m_connected:
    type: int
    default: 0
  _tank_level_units:
    type: str
    default: "%"
  _lwm2m_manufacturer:
    type: str
    default: "SPX Virtual Devices"
  _lwm2m_model_number:
    type: str
    default: "water-storage-tank-lwm2m"

actions:
  - function: $in(tank_level_pct)
    name: update_tank_level
    description: >
      Integrate inlet flow controlled by valve opening and a fixed outlet loss
      for one model execution step, clamped to the tank range.
    call: |
      max(
        0.0,
        min(
          100.0,
          $in(tank_level_pct)
          + $in(k__valve_opening_ratio) * $in(inflow_gain_pct_per_step)
          - $in(outlet_loss_pct_per_step)
        )
      )
  - function: $in(tank_level_min_pct)
    name: capture_minimum_tank_level
    call: min($in(tank_level_min_pct), $in(tank_level_pct))
  - function: $in(tank_level_max_pct)
    name: capture_maximum_tank_level
    call: max($in(tank_level_max_pct), $in(tank_level_pct))
  - function: $in(lwm2m_connected)
    name: read_registration_diagnostic
    call: int($attr(communication.lwm2m.registered))

communication:
  - lwm2m:
      description: Register and serve the virtual tank resources through CoAP.
      port: $param(lwm2m_local_port)
      server:
        host: $param(lwm2m_server_host)
        port: $param(lwm2m_server_port)
        endpoint: $param(lwm2m_endpoint)
        lifetime: 120
      bindings:
        - name: query_manufacturer
          path: 3/0/0
          operation: read
          read_attribute: '#attr(_lwm2m_manufacturer)'
          content_format: text/plain
        - name: query_model_number
          path: 3/0/1
          operation: read
          read_attribute: '#attr(_lwm2m_model_number)'
          content_format: text/plain
        - name: query_tank_level
          path: 3300/0/5700
          operation: read
          read_attribute: '#attr(tank_level_pct)'
          content_format: text/plain
        - name: query_tank_level_units
          path: 3300/0/5701
          operation: read
          read_attribute: '#attr(_tank_level_units)'
          content_format: text/plain
        - name: query_tank_minimum
          path: 3300/0/5601
          operation: read
          read_attribute: '#attr(tank_level_min_pct)'
          content_format: text/plain
        - name: query_tank_maximum
          path: 3300/0/5602
          operation: read
          read_attribute: '#attr(tank_level_max_pct)'
          content_format: text/plain
        - name: query_valve_opening
          path: 3203/0/5650
          operation: read
          read_attribute: '#attr(k__valve_opening_ratio)'
          content_format: text/plain
        - name: set_valve_opening
          path: 3203/0/5650
          operation: write
          write_attribute: '#attr(k__valve_opening_ratio)'
          content_format: text/plain
