// Copyright (C) 2020-2022 Fredrik Öhrström (gpl-3.0-or-later)
driver {
    name           = q400
    meter_type     = WaterMeter
    default_fields = name,id,total_m3,timestamp
    detect {
        mvt = AXI,01,07
        mvt = AXI,10,07
        mvt = AXI,20,07
    }
    library {
        use = meter_datetime
        use = on_time_h
        use = total_m3
        use = total_forward_m3
        use = total_backward_m3
        use = flow_temperature_c
        use = volume_flow_m3h
    }
    fields {
        field {
            name       = status
            quantity   = Text
            info       = 'Status and error flags.'
            attributes = STATUS,INCLUDE_TPL_STATUS
        }
        field {
            name         = set
            quantity     = PointInTime
            info         = 'Date and time when the previous billing period ended.'
            display_unit = datetime
            match {
                measurement_type = Instantaneous
                vif_range        = DateTime
                storage_nr       = 1
            }
        }
        field {
            name     = consumption_at_set_date
            quantity = Volume
            info     = 'The total water consumption at the end of the previous billing period.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1
            }
        }
        field {
            name     = forward_at_set_date
            quantity = Volume
            info     = 'The total media volume flowing forward at the end of previous billing period.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1
                add_combinable   = ForwardFlow
            }
        }
        field {
            name     = backward_at_set_date
            quantity = Volume
            info     = 'The total media volume flowing backward at the end of the previous billing period.'
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 1
                add_combinable   = BackwardFlow
            }
        }
        field {
            name     = error_free_time
            quantity = Time
            info     = 'How long the meter has been running without recording an error.'
            match {
                measurement_type = Instantaneous
                vif_range        = OperatingTime
            }
        }
        field {
            name        = error_code
            quantity    = Dimensionless
            info        = 'Instantaneous error code, as documented by Axioma. Zero means no error.'
            vif_scaling = None
            match {
                difvifkey = 34FD17
            }
        }
        field {
            name         = battery
            quantity     = Dimensionless
            info         = 'Percentage of battery remaining.'
            vif_scaling  = None
            display_unit = pct
            match {
                difvifkey = 01FD74
            }
        }
    }
    tests {
        test {
            args     = 'Q400Water q400 72727272 NOKEY'
            telegram = 2E4409077272727210077AD71020052F2F_046D040D742C041377000000446D0000612C4413000000002F2F2F2F2F2F
            json     = '{"_":"telegram","media":"water","driver":"q400","name":"Q400Water","id":"72727272","meter_datetime":"2019-12-20 13:04","total_m3":0.119,"status":"TEMPORARY_ERROR","set_datetime":"2019-12-01 00:00","consumption_at_set_date_m3":0,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Q400Water;72727272;0.119;1111-11-11 11:11.11'
        }
        test {
            args     = 'AxiomaWater q400 72727273 NOKEY'
            comment  = 'Test Axioma W1 telegram with additional fields compared to the older q400 meter.'
            telegram = 5E4409077372727210077A710050052F2F_046D0110A92704130022000004933B0000000004933C00000000023B000002592A0A446D0000A12744130000000044933B0000000044933C0000000001FD74622F2F2F2F2F2F2F2F2F2F2F2F2F2F
            json     = '{"_":"telegram","media":"water","driver":"q400","name":"AxiomaWater","id":"72727273","meter_datetime":"2021-07-09 16:01","total_m3":8.704,"total_forward_m3":0,"total_backward_m3":0,"flow_temperature_c":26.02,"volume_flow_m3h":0,"status":"OK","set_datetime":"2021-07-01 00:00","consumption_at_set_date_m3":0,"forward_at_set_date_m3":0,"backward_at_set_date_m3":0,"battery_pct":98,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'AxiomaWater;72727273;8.704;1111-11-11 11:11.11'
        }
        test {
            args     = 'Mm q400 05829163 NOKEY'
            telegram = 544409076391820510077ABF100000046D2A0DC62C0420E80F430104130000000004933B0000000004933C00000000023B00000259F0D8446D0000C12C44130000000044933B0000000044933C0000000001FD7461
            json     = '{"_":"telegram","backward_at_set_date_m3":0,"battery_pct":97,"consumption_at_set_date_m3":0,"flow_temperature_c":-100,"forward_at_set_date_m3":0,"id":"05829163","media":"water","driver":"q400","meter_datetime":"2022-12-06 13:42","name":"Mm","on_time_h":5881.166667,"set_datetime":"2022-12-01 00:00","status":"TEMPORARY_ERROR","timestamp":"1111-11-11T11:11:11Z","total_backward_m3":0,"total_forward_m3":0,"total_m3":0,"volume_flow_m3h":0}'
            fields   = 'Mm;05829163;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'AxiomaW1v20 q400 10221910 NOKEY'
            comment  = 'Test Axioma W1 telegram with dll-version 0x20, dry/empty spool status.'
            telegram = 8C4409071019221020077A56100000046D3008423804209081A20004130000000004933B0000000004933C00000000023B00000259F0D8446D0000413844130000000044933B0000000044933C00000000C401130000000034FD17010000000424407DA20001FD74630F0200000000000000000000000041380000000000000000000000000000000000000000
            json     = '{"_":"telegram","media":"water","driver":"q400","name":"AxiomaW1v20","id":"10221910","backward_at_set_date_m3":0,"battery_pct":99,"consumption_at_set_date_m3":0,"error_code_counter":1,"error_free_time_h":2958.026667,"flow_temperature_c":-100,"forward_at_set_date_m3":0,"on_time_h":2958.333333,"set_datetime":"2026-08-01 00:00","total_m3":0,"total_backward_m3":0,"total_forward_m3":0,"volume_flow_m3h":0,"meter_datetime":"2026-08-02 08:48","status":"TEMPORARY_ERROR","timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'AxiomaW1v20;10221910;0;1111-11-11 11:11.11'
        }
        test {
            args     = 'MM q400 06289748 NOKEY'
            telegram = 614409074897280601077AD1000000046D2C173B370420580193040413FB94000004933BFB94000004933C01000000023B02000259E306446D000021374413C08A000044933BC08A000044933C0100000034FD17000000000424BA01930401FD745B
            json     = '{"_":"telegram","backward_at_set_date_m3":0.001,"battery_pct":91,"consumption_at_set_date_m3":35.52,"error_code_counter":0,"error_free_time_h":21317.527222,"flow_temperature_c":17.63,"forward_at_set_date_m3":35.52,"id":"06289748","media":"water","driver":"q400","meter_datetime":"2025-07-27 23:44","name":"MM","on_time_h":21317.5,"set_datetime":"2025-07-01 00:00","status":"OK","timestamp":"1111-11-11T11:11:11Z","total_backward_m3":0.001,"total_forward_m3":38.139,"total_m3":38.139,"volume_flow_m3h":0.002}'
            fields   = 'MM;06289748;38.139;1111-11-11 11:11.11'
        }
    }
}