#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

# No factory default because of different defaultlevels
carel_temp_defaultlevels = {
    "Room": (30, 35),
    "Outdoor": (60, 70),
    "Delivery": (60, 70),
    "Cold Water": (60, 70),
    "Hot Water": (60, 70),
    "Cold Water Outlet": (60, 70),
    "Circuit 1 Suction": (60, 70),
    "Circuit 2 Suction": (60, 70),
    "Circuit 1 Evap": (60, 70),
    "Circuit 2 Evap": (60, 70),
    "Circuit 1 Superheat": (60, 70),
    "Circuit 2 Superheat": (60, 70),
    "Cooling Set Point": (60, 70),
    "Cooling Prop. Band": (60, 70),
    "Cooling 2nd Set Point": (60, 70),
    "Heating Set Point": (60, 70),
    "Heating 2nd Set Point": (60, 70),
    "Heating Prop. Band": (60, 70),
}


def carel_sensors_parse(info):

    oid_parse = {
        "1.0": "Room",
        "2.0": "Outdoor",
        "3.0": "Delivery",
        "4.0": "Cold Water",
        "5.0": "Hot Water",
        "7.0": "Cold Water Outlet",
        "10.0": "Circuit 1 Suction",
        "11.0": "Circuit 2 Suction",
        "12.0": "Circuit 1 Evap",
        "13.0": "Circuit 2 Evap",
        "14.0": "Circuit 1 Superheat",
        "15.0": "Circuit 2 Superheat",
        "20.0": "Cooling Set Point",
        "21.0": "Cooling Prop. Band",
        "22.0": "Cooling 2nd Set Point",
        "23.0": "Heating Set Point",
        "24.0": "Heating 2nd Set Point",
        "25.0": "Heating Prop. Band",
    }

    parsed = {}
    for oidend, value in info:
        sensor_name = oid_parse.get(oidend)
        if sensor_name is not None and value is not None \
           and value != "0" and value != "-9999":
            parsed[sensor_name] = float(value) / 10

    return parsed


def inventory_carel_sensors_temp(parsed):
    for sensor in parsed.keys():
        levels = carel_temp_defaultlevels[sensor]
        yield sensor, {"levels": levels}


def check_carel_sensors_temp(item, params, parsed):
    if item in parsed:
        return check_temperature(parsed[item], params, "carel_sensors_temp_%s" % item)


check_info["carel_sensors"] = {
    "parse_function"             : carel_sensors_parse,
    "inventory_function"         : inventory_carel_sensors_temp,
    "check_function"             : check_carel_sensors_temp,
    "service_description"        : "Temperature %s",
    "group"                      : "temperature",
    "has_perfdata"               : True,
    "snmp_info"                  : ( ".1.3.6.1.4.1.9839.2.1", [ OID_END, "2" ] ),
    "snmp_scan_function"         : lambda oid: ( "pCO" in oid(".1.3.6.1.2.1.1.1.0") or \
                                                 oid(".1.3.6.1.2.1.1.1.0").endswith("armv4l")
                                               ) and \
                                               oid(".1.3.6.1.4.1.9839.1.1.0") ,
    "includes"                   : [ "temperature.include" ],
}
