// Copyright (C) 2021 Vincent Privat (gpl-3.0-or-later)
// Copyright (C) 2022 Fredrik Öhrström (gpl-3.0-or-later)
// Diehl Sharky 775 heat meter.
driver {
    name           = sharky
    meter_type     = HeatMeter
    default_fields = name,id,total_energy_consumption_kwh,total_energy_consumption_tariff1_kwh,total_volume_m3,total_volume_tariff2_m3,volume_flow_m3h,power_kw,flow_temperature_c,return_temperature_c,temperature_difference_c,timestamp
    detect {
        mvt = HYD,20,04
        mvt = DME,40,04
    }
    library {
        use = operating_time_h
    }
    fields {
        field {
            name       = status
            quantity   = Text
            info       = 'Status of meter.'
            attributes = STATUS
            match {
                measurement_type = Instantaneous
                vif_range        = ErrorFlags
            }
            lookup {
                name            = ERROR_FLAGS
                map_type        = BitToString
                mask_bits       = 0x0000
                default_message = OK
            }
        }
        field {
            name           = total_energy_consumption
            quantity       = Energy
            info           = 'The total heat energy consumption recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
            }
        }
        field {
            name           = total_energy_consumption_tariff1
            quantity       = Energy
            info           = 'The total heat energy consumption recorded by this meter on tariff 1.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                tariff_nr        = 1
            }
        }
        field {
            name           = total_volume
            quantity       = Volume
            info           = 'The total heating media volume recorded by this meter.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
            }
        }
        field {
            name           = total_volume_tariff2
            quantity       = Volume
            info           = 'The total heating media volume recorded by this meter on tariff 2.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                tariff_nr        = 2
            }
        }
        field {
            name           = volume_flow
            quantity       = Flow
            info           = 'The current heat media volume flow.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = VolumeFlow
            }
        }
        field {
            name           = power
            quantity       = Power
            info           = 'The current power consumption.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = PowerW
            }
        }
        field {
            name           = flow_temperature
            quantity       = Temperature
            info           = 'The current forward heat media temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = FlowTemperature
            }
        }
        field {
            name           = return_temperature
            quantity       = Temperature
            info           = 'The current return heat media temperature.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = ReturnTemperature
            }
        }
        field {
            name           = temperature_difference
            quantity       = Temperature
            info           = 'The current temperature difference between flow and return.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = TemperatureDifference
            }
        }
        field {
            name           = target_energy_consumption
            quantity       = Energy
            info           = 'The total heat energy consumption at the end of the previous billing period.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = AnyEnergyVIF
                storage_nr       = 5
            }
        }
        field {
            name           = target_volume
            quantity       = Volume
            info           = 'The total heating media volume at the end of the previous billing period.'
            vif_scaling    = Auto
            dif_signedness = Signed
            match {
                measurement_type = Instantaneous
                vif_range        = Volume
                storage_nr       = 5
            }
        }
        field {
            name           = target
            quantity       = PointInTime
            info           = 'The last billing period end date.'
            vif_scaling    = Auto
            dif_signedness = Signed
            display_unit   = DateLT
            match {
                measurement_type = Instantaneous
                vif_range        = Date
                storage_nr       = 5
            }
        }
    }
    tests {
        test {
            args     = 'Heat sharky 68926025 NOKEY'
            telegram = 534424232004256092687A370045752235854DEEEA5939FAD81C25FEEF5A23C38FB9168493C563F08DB10BAF87F660FBA91296BA2397E8F4220B86D3A192FB51E0BFCF24DCE72118E0C75A9E89F43BDFE370824B
            json     = '{"_":"telegram","media":"heat","meter":"sharky","name":"Heat","id":"68926025","total_energy_consumption_kwh":2651,"total_energy_consumption_tariff1_kwh":0,"total_volume_m3":150.347,"total_volume_tariff2_m3":0.018,"volume_flow_m3h":0,"power_kw":0,"flow_temperature_c":42.3,"return_temperature_c":28.1,"status":"OK","temperature_difference_c":14.1,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heat;68926025;2651;0;150.347;0.018;0;0;42.3;28.1;14.1;1111-11-11 11:11.11'
        }
        test {
            args     = 'Heat sharky 61917653 NOKEY'
            telegram = 5e44a5115376916140047a0B0050052f2f0c0e829311008c100e000000000c14014938000c2B751400000B3B2902000a5a52070a5e95060a6256000a279015cc020e92831100cc021478113800c2026cdf2c2f2f2f2f2f2f2f2f2f2f2f2f2f
            json     = '{"_":"telegram","media":"heat","meter":"sharky","name":"Heat","id":"61917653","flow_temperature_c":75.2,"operating_time_h":38160,"power_kw":1.475,"return_temperature_c":69.5,"target_date":"2022-12-31","target_energy_consumption_kwh":32886.666667,"target_volume_m3":3811.78,"temperature_difference_c":5.6,"total_energy_consumption_kwh":33161.666667,"total_energy_consumption_tariff1_kwh":0,"total_volume_m3":3849.01,"volume_flow_m3h":0.229,"timestamp":"1111-11-11T11:11:11Z"}'
            fields   = 'Heat;61917653;33161.666667;0;3849.01;null;0.229;1.475;75.2;69.5;5.6;1111-11-11 11:11.11'
        }
        test {
            args     = 'Heato sharky 69696969 NOKEY'
            telegram = 5e44a5116969696940047aBe0050052f2f0c06975100008c1006000000000c13849345000c2B000000000B3B0000000a5a06020a5e08020a6202f00B26110201cc020623500000cc021329554400c2026cdf2c2f2f2f2f2f2f2f2f2f2f2f2f
            json     = '{"_":"telegram","flow_temperature_c": 20.6,"id": "69696969","media": "heat","meter": "sharky","name": "Heato","operating_time_h": 10211,"power_kw": 0,"return_temperature_c": 20.8,"target_date": "2022-12-31","target_energy_consumption_kwh": 5023,"target_volume_m3": 445.529,"temperature_difference_c": -0.2,"timestamp": "1111-11-11T11:11:11Z","total_energy_consumption_kwh": 5197,"total_energy_consumption_tariff1_kwh": 0,"total_volume_m3": 459.384, "volume_flow_m3h": 0}'
            fields   = 'Heato;69696969;5197;0;459.384;null;0;0;20.6;20.8;-0.2;1111-11-11 11:11.11'
        }
    }
}