#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# snmp_scan_function
# .1.3.6.1.2.1.1.4.0 = STRING: x.name@green-cooling.de < green-cooling match
# .1.3.6.1.2.1.1.5.0 = STRING: pCOWeb                    < pcoweb match
# .1.3.6.1.4.1.9839.1                                    < exists

# snmp_info
# .1.3.6.1.4.1.9839.2.1.1.31.0 = INTEGER: 0   < Waterloss
# .1.3.6.1.4.1.9839.2.1.1.51.0 = INTEGER: 1   < Global
# .1.3.6.1.4.1.9839.2.1.1.67.0 = INTEGER: 0   < Unit in Emergeny operation
# .1.3.6.1.4.1.9839.2.1.2.6.0 = INTEGER:  246 < Humidifier: Relative Humidity


def inventory_carel_uniflair_cooling(info):
    return [(None, None)]


def check_carel_uniflair_cooling(item, _no_params, info):
    waterloss, global_status, ermergency_op, humidity = info[0]

    err_waterloss = waterloss != "0"
    err_global_status = global_status != "1"
    err_emergency_op = ermergency_op != "0"
    humidity = float(humidity) / 10

    output = ""
    output = output + ("Global Status: %s" % (err_global_status and "Error(!!), " or "OK, "))
    output = output + (
        "Emergency Operation: %s" % (err_emergency_op and "Active(!!), " or "Inactive, ")
    )
    output = output + (
        "Humidifier: %s" % (err_waterloss and "Water Loss(!!), " or "No Water Loss, ")
    )
    output = output + "Humidity: %3.1f%%" % humidity

    perfdata = [("humidity", humidity)]
    if err_waterloss or err_global_status or err_emergency_op:
        return (2, output, perfdata)
    return (0, output, perfdata)


check_info["carel_uniflair_cooling"] = {
    "check_function": check_carel_uniflair_cooling,
    "inventory_function": inventory_carel_uniflair_cooling,
    "service_description": "Carel uniflair cooling",
    "has_perfdata": True,
    # All the OIDs of this checks seems to be wrong for the current version
    # of this device, so the scan function is disbaled until we have better information
    "snmp_scan_function": lambda oid: False,
    # "snmp_scan_function"   : lambda oid: ("pCO" in oid(".1.3.6.1.2.1.1.1.0")
    #                                      or "Linux" in oid(".1.3.6.1.2.1.1.1.0")) \
    #                                     and oid(".1.3.6.1.4.1.9839.1.2.0"),
    "snmp_info": (
        ".1.3.6.1.4.1.9839.2.1",
        [
            "1.31.0",  # Waterloss
            "1.51.0",  # Global
            "1.67.0",  # Unit in Emergency operation
            "2.6.0",  # Relative Humidity
        ],
    ),
}
