#!/bin/bash

################################################################################
# @file		openems-heatpump-config
# @author	A. Pietsch <a.pietsch@onsolinno.de>
# @brief	Applies mender-configuration for openems heatpump
# @version	1.1.0
# @date		07.03.2023
# 
# @copyright GPLv3 (c) 2023 A. Pietsch <a.pietsch@onsolinno.de>
# This program 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, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
################################################################################



if [ $# -ne 1 ]; then
    echo "Must be invoked with exactly one argument: The JSON configuration." 1>&2
    exit 2
fi

CONFIG="$1"

INVENTORY_FILE="/data/leaflet/Inventory.list"

if ! [ -e "$CONFIG" ]; then
    echo "Error: $CONFIG does not exist." 1>&2
    exit 0
fi

# get HEATPUMP ID Data
HEATPUMP_ID="$(jq -r -e .HEATPUMP_ID <"$CONFIG")"

# verify HEATPUMP_ID Data
if [[ "$HEATPUMP_ID" == "null" ]]; then
    echo "HEATPUMP_ID not found"
    exit 0
fi

INVENTORY_FOLDER=$(basename $INVENTORY_FILE)
# create Inventory Folder
if [[ ! -d $INVENTORY_FOLDER ]]; then
    mkdir -p $INVENTORY_FOLDER
fi

echo "Add ID to Inventory"
echo "heat_pump_id=${HEATPUMP_ID}" | tee -a $INVENTORY_FILE

# Set Config (ToDo: update config if exists)
echo "Add Openems Config"

curl -X POST -H 'Authorization: Basic QWRtaW46YWRtaW4=' \
     -H 'Content-Type: application/json' \
     -d '{"jsonrpc":"2.0","method":"createComponentConfig","params":{
      "factoryPid": MqttTelemetryComponent,
      "properties": [{
        "name": "id",
        "value": "TelemetryMqttComponentAlphaInnotech"
      },{
        "name": "otherComponentId",
        "value": "HeatPump0"
      },{
        "name": "mqttBridgeId",
        "value": "MqttBridge"
      },{
        "name": "mqttId",
        "value": "'${HEATPUMP_ID}'"
      },{
        "name": "payloads",
        "value": [ "bh-1:Ir33HoursHeatPump:bh-2:Ir34HoursCircuitHeating:bh-3:Ir35HoursWaterHeating:bh-4:Ir28HoursComp1:bh-5:Ir29HoursComp2:bh-6:Ir30HoursAux1:bh-7:Ir31HoursAux2:err-1:Ir46Error:err-2:Coil0Errorreset:heatcurve-1:Hr15CurveCircuitHeatingEndPoint:heatcurve-2:Hr16CurveCircuitHeatingShift:heatcurve-3:Hr17CurveMc1EndPoint:heatcurve-4:Hr18CurveMc1Shift:heatcurve-5:Hr19CurveMc2EndPoint:heatcurve-6:Hr20CurveMc2Shift:heatcurve-7:Hr21CurveMc3EndPoint:heatcurve-8:Hr22CurveMc3Shift:lock-1:Hr6BlockRelease:lock-2:Di0DmsBlock:lock-3:Di1DmsBlockSg:mode-1:Hr7CircuitHeatingOperationMode:mode-2:Hr8WaterOperationMode:mode-3:Hr11CoolingOperationMode:status-1:Di3Compressor1:status-2:Di4Compressor2:status-3:Di5Aux1:status-4:Di6Aux2:switch-1:Coil2Hup:switch-2:Coil4Zup:switch-3:Coil7Zip:switch-4:Coil8Fup2:switch-5:Coil9Fup3:switch-6:Coil13Frh:thermal_power_plus-1:Ir44EnergyTotal:thermal_power_plus-2:Ir40EnergyWater:thermal_power_plus-3:Ir38EnergyCircuitHeating:tp-1:FlowTemperature:tp-10:Hr0Outsidetemp:tp-11:Ir5FlowTempMc1:tp-12:Hr2FlowTempSetpointMc1:tp-13:Ir6FlowTempMc2:tp-14:Hr3FlowTempSetpointMc2:tp-15:Ir7FlowTempMc3:tp-16:Hr4FlowTempSetpointMc3:tp-17:Ir18ExtractAirTemp:tp-18:Ir0AverageTemp:tp-19:Hr23TempPm:tp-2:ReturnTemperature:tp-3:Hr1ReturnTempSetpoint:tp-4:Ir9HeatSourceInletTemp:tp-5:Ir10HeatSourceOutletTemp:tp-6:Ir4WaterTemp:tp-7:Hr5WaterTempSetpoint:tp-8:Ir24RbeRoomTemp:tp-9:Ir25RbeRoomTempSetpoint","heatcurve-1:Hr15CurveCircuitHeatingEndPoint:heatcurve-2:Hr16CurveCircuitHeatingShift:heatcurve-3:Hr17CurveMc1EndPoint:heatcurve-4:Hr18CurveMc1Shift:heatcurve-5:Hr19CurveMc2EndPoint:heatcurve-6:Hr20CurveMc2Shift:heatcurve-7:Hr21CurveMc3EndPoint:heatcurve-8:Hr22CurveMc3Shift:lock-1:Hr6BlockRelease:mode-1:Hr7CircuitHeatingOperationMode:mode-2:Hr8WaterOperationMode:tp-7:Hr5WaterTempSetpoint:err-2:Coil0Errorreset"]
      },{
        "name": "publishList",
        "value": ["LOW!telemetry/'${HEATPUMP_ID}'!0!true!true!0!2"]
      },{
        "name": "subscriptionList",
        "value": ["LOW!control/'${HEATPUMP_ID}'/setPower!1!true!true!1!1"]
      },{
        "name": "configurationDone",
        "value": "true"
      }
    ]}}' \
    http://localhost:8086/jsonrpc

