#!/bin/bash

################################################################################
# @file		openems-heatpump-config
# @author	A. Pietsch <a.pietsch@onsolinno.de>
# @brief	Applies mender-configuration for openems heatpump
# @version	1.0.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"

CONFIG_FOLDER="/data/openems.d/data/openems/MqttTelemetryComponent"
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

# generate uuid
UUID=$(cat /proc/sys/kernel/random/uuid)

# create folder and remove old configs
if [[ ! -d $CONFIG_FOLDER ]]; then

    mkdir -p $CONFIG_FOLDER
fi

# remove old config
ls ${CONFIG_FOLDER} | grep .config
if [[ $? == 0 ]]; then
    echo "removing old config"
    rm ${CONFIG_FOLDER}/*.config
fi

# generate mqtt.config
CONFIG_FILE="${CONFIG_FOLDER}/${UUID}.config"
echo "generating $CONFIG_FOLDER $CONFIG_FILE"

cat >${CONFIG_FILE} <<EOF
:org.apache.felix.configadmin.revision:=L"16"
alias=""
channelIdList=[ \
  "Ir11RoomRemoteAdj1Temp", \
  "Ir23OverheatSetpoint", \
  "ReturnTemperature", \
  "Coil4Zup", \
  "Hr12Modbus", \
  "Coil0Errorreset", \
  "Hr8WaterOperationMode", \
  "Coil12Vsk", \
  "Hr9Modbus", \
  "Ir8HotGasTemp", \
  "Hr7CircuitHeatingOperationMode", \
  "SetDefaultMinimumPowerValue", \
  "Hr10Mc3OperationMode", \
  "Ir17SupplyAirTemp", \
  "Ir44EnergyTotal", \
  "Di2PoolThermostat", \
  "Hr18CurveMc1Shift", \
  "Hr6BlockRelease", \
  "Hr16CurveCircuitHeatingShift", \
  "Ir19CompressorIntakeTemp", \
  "Di3Compressor1", \
  "Ir30HoursAux1", \
  "Hr19CurveMc2EndPoint", \
  "Di7Aux3", \
  "WarningMessage", \
  "Coil13Frh", \
  "Hr21CurveMc3EndPoint", \
  "Coil10Slp", \
  "Hr7Modbus", \
  "Ir38EnergyCircuitHeating", \
  "Ir14SolarCollectorTemp", \
  "SetDefaultActivePowerValue", \
  "Ir37Status", \
  "Hr11Modbus", \
  "Coil2Hup", \
  "Ir7FlowTempMc3", \
  "Ir32HoursAux3", \
  "Coil0Modbus", \
  "Hr13PoolOperationMode", \
  "Coil3Ven", \
  "Coil7Zip", \
  "Hr14SmartGrid", \
  "Di5Aux1", \
  "Ir3ReturnTempExt", \
  "Ir40EnergyWater", \
  "Hr10Modbus", \
  "Di0DmsBlock", \
  "Ir25RbeRoomTempSetpoint", \
  "Ir5FlowTempMc1", \
  "Ir20EvaporatorIntakeTemp", \
  "ErrorMessage", \
  "SmartGridState", \
  "Ir6FlowTempMc2", \
  "Ir46Error", \
  "Ir12RoomRemoteAdj2Temp", \
  "Di1DmsBlockSg", \
  "Ir10HeatSourceOutletTemp", \
  "Ir22Overheat", \
  "Ir21CompressorHeaterTemp", \
  "Ir16ExtEnergySourceTemp", \
  "Ir9HeatSourceInletTemp", \
  "Hr1ReturnTempSetpoint", \
  "Hr12VentilationOperationMode", \
  "Coil8Fup2", \
  "Ir35HoursWaterHeating", \
  "Hr23TempPm", \
  "Coil11Sup", \
  "Hr5WaterTempSetpoint", \
  "EnableSignal", \
  "HeaterState", \
  "State", \
  "SetPointHeatingPower", \
  "Hr20CurveMc2Shift", \
  "Ir29HoursComp2", \
  "Hr8Modbus", \
  "Ir24RbeRoomTemp", \
  "Ir0AverageTemp", \
  "Hr9Mc2OperationMode", \
  "Hr22CurveMc3Shift", \
  "FlowTemperature", \
  "Coil5Bup", \
  "EffectiveHeatingPower", \
  "Ir42EnergyPool", \
  "EffectiveHeatingPowerPercent", \
  "Di6Aux2", \
  "UseSmartGridState", \
  "Ir28HoursComp1", \
  "Coil9Fup3", \
  "Ir4WaterTemp", \
  "Ir18ExtractAirTemp", \
  "Ir31HoursAux2", \
  "Hr17CurveMc1EndPoint", \
  "Ir26HighPressure", \
  "SetPointHeatingPowerPercent", \
  "Di4Compressor2", \
  "Hr11CoolingOperationMode", \
  "Hr13Modbus", \
  "Ir33HoursHeatPump", \
  "Ir15SolarStorageTankTemp", \
  "Ir27LowPressure", \
  "Coil6Bosup", \
  "Ir34HoursCircuitHeating", \
  "SetPointTemperature", \
  "Ir36HoursPoolSolar", \
  "Ir13RoomRemoteAdj3Temp", \
  "Hr3FlowTempSetpointMc2", \
  "Hr2FlowTempSetpointMc1", \
  "Hr15CurveCircuitHeatingEndPoint", \
  "Hr0Outsidetemp", \
  "Hr4FlowTempSetpointMc3", \
  "Configuration", \
  "State", \
  ]
configurationDone=B"true"
createdByOsgi=B"true"
enabled=B"true"
id="TelemetryMqttComponentAlphaInnotech"
mqttBridgeId="MqttBridge"
mqttId="${HEATPUMP_ID}"
otherComponentId="HeatPump0"
pathForJson=""
payloadStyle="STANDARD"
payloads=[ \
  "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", \
  ]
publishList=[ \
  "LOW!telemetry/${HEATPUMP_ID}!0!true!true!0!2", \
  ]
service.factoryPid="MqttTelemetryComponent"
service.pid="MqttTelemetryComponent.eeb2f60f-1836-4a12-b3e7-ed471c5f85a3"
subscriptionList=[ \
  "LOW!control/${HEATPUMP_ID}/setPower!1!true!true!1!1", \
  ]

EOF
