-- map register -> value_verified z modbus_command (poslední verified řádek per register)

create or replace function ems.fn_modbus_last_verified_map(
  p_site_id int,
  p_asset_id int
)
returns jsonb
language sql
stable
as $fn$
  select coalesce(
    jsonb_object_agg(register::text, to_jsonb(value_verified)),
    '{}'::jsonb
  )
  from (
    select
      v.register,
      v.value_verified
    from ems.vw_modbus_last_verified v
    where v.site_id = p_site_id
      and v.asset_type = 'inverter'
      and v.asset_id = p_asset_id
  ) t;
$fn$;
