diff --git a/schrack/README.md b/schrack/README.md new file mode 100644 index 0000000..751d42a --- /dev/null +++ b/schrack/README.md @@ -0,0 +1,12 @@ +# Schrack CION + +This plugin adds support for the Schrack CION wallbox to nymea. + +The wallbox needs to be connected to the nymea system using an RS485 interface which needs to be configured as a Modbus RTU interface with the following parameters: +* Baudrate: 57600 +* Data bits: 8 +* Stop bit: 1 +* Parity: None + +Once the Modbus RTU interface is configured, the Wallbox can be added to nymea. nymea will try to discover the wallbox on the first 10 slave ids. This means, the DIP switches on the Wallbox are required to be configred for a modbus Slave ID from 1 to 10. + diff --git a/schrack/ciondiscovery.cpp b/schrack/ciondiscovery.cpp new file mode 100644 index 0000000..63e1619 --- /dev/null +++ b/schrack/ciondiscovery.cpp @@ -0,0 +1,76 @@ +#include "ciondiscovery.h" + +#include"extern-plugininfo.h" + +#include + + +CionDiscovery::CionDiscovery(ModbusRtuHardwareResource *modbusRtuResource, QObject *parent) + : QObject{parent}, + m_modbusRtuResource{modbusRtuResource} +{ + +} + +void CionDiscovery::startDiscovery() +{ + qCInfo(dcSchrack()) << "Discovery: Searching for Schrack i-CHARGE wallboxes on modbus RTU..."; + + QList candidateMasters; + foreach (ModbusRtuMaster *master, m_modbusRtuResource->modbusRtuMasters()) { + if (master->baudrate() == 57600 && master->dataBits() == 8 && master->stopBits() == 1 && master->parity() == QSerialPort::NoParity) { + candidateMasters.append(master); + } + } + + if (candidateMasters.isEmpty()) { + qCWarning(dcSchrack()) << "No usable modbus RTU master found."; + emit discoveryFinished(false); + return; + } + + foreach (ModbusRtuMaster *master, candidateMasters) { + if (master->connected()) { + tryConnect(master, 1); + } else { + qCWarning(dcSchrack()) << "Modbus RTU master" << master->modbusUuid().toString() << "is not connected."; + } + } +} + +QList CionDiscovery::discoveryResults() const +{ + return m_discoveryResults; +} + +void CionDiscovery::tryConnect(ModbusRtuMaster *master, quint16 slaveId) +{ + qCDebug(dcSchrack()) << "Scanning modbus RTU master" << master->modbusUuid() << "Slave ID:" << slaveId; + + ModbusRtuReply *reply = master->readHoldingRegister(slaveId, 832, 16); + connect(reply, &ModbusRtuReply::finished, this, [=](){ + + if (reply->error() == ModbusRtuReply::NoError) { + + QString firmwareVersion = ModbusDataUtils::convertToString(reply->result()); + qCDebug(dcSchrack()) << "Test reply finished!" << reply->error() << firmwareVersion; + + // Version numbers seem to be wild west... We can't really understand what's in there... + // So let's assume this is a schrack if reading alone succeeded and it is a valid string and 18 to 32 chars long... + // Examples of how this looks like: + // EBE 1.2: "V1.2 15.02.2021" + // ICC: "003090056-01 20220913" + QRegExp re = QRegExp("[A-Z0-9\\.- ]{18,32}"); + if (re.exactMatch(firmwareVersion)) { + Result result {master->modbusUuid(), firmwareVersion, slaveId}; + m_discoveryResults.append(result); + } + } + + if (slaveId < 10) { + tryConnect(master, slaveId+1); + } else { + emit discoveryFinished(true); + } + }); +} diff --git a/schrack/ciondiscovery.h b/schrack/ciondiscovery.h new file mode 100644 index 0000000..6b3f80a --- /dev/null +++ b/schrack/ciondiscovery.h @@ -0,0 +1,64 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * +* +* Copyright 2013 - 2023, nymea GmbH +* Contact: contact@nymea.io +* +* This file is part of nymea. +* This project including source code and documentation is protected by +* copyright law, and remains the property of nymea GmbH. All rights, including +* reproduction, publication, editing and translation, are reserved. The use of +* this project is subject to the terms of a license agreement to be concluded +* with nymea GmbH in accordance with the terms of use of nymea GmbH, available +* under https://nymea.io/license +* +* GNU Lesser General Public License Usage +* Alternatively, this project may be redistributed and/or modified under the +* terms of the GNU Lesser General Public License as published by the Free +* Software Foundation; version 3. This project 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 +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public License +* along with this project. If not, see . +* +* For any further details and any questions please contact us under +* contact@nymea.io or see our FAQ/Licensing Information on +* https://nymea.io/license/faq +* +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef CIONDISCOVERY_H +#define CIONDISCOVERY_H + +#include +#include + +class CionDiscovery : public QObject +{ + Q_OBJECT +public: + explicit CionDiscovery(ModbusRtuHardwareResource *modbusRtuResource, QObject *parent = nullptr); + struct Result { + QUuid modbusRtuMasterId; + QString firmwareVersion; + quint16 slaveId; + }; + + void startDiscovery(); + + QList discoveryResults() const; + +signals: + void discoveryFinished(bool modbusRtuMasterAvailable); + +private slots: + void tryConnect(ModbusRtuMaster *master, quint16 slaveId); + +private: + ModbusRtuHardwareResource *m_modbusRtuResource = nullptr; + + QList m_discoveryResults; +}; + +#endif // CIONDISCOVERY_H diff --git a/schrack/integrationpluginschrack.cpp b/schrack/integrationpluginschrack.cpp index aa5877b..9be9271 100644 --- a/schrack/integrationpluginschrack.cpp +++ b/schrack/integrationpluginschrack.cpp @@ -31,6 +31,8 @@ #include "integrationpluginschrack.h" #include "plugininfo.h" +#include "ciondiscovery.h" + IntegrationPluginSchrack::IntegrationPluginSchrack() { } @@ -42,32 +44,36 @@ void IntegrationPluginSchrack::init() void IntegrationPluginSchrack::discoverThings(ThingDiscoveryInfo *info) { - qCDebug(dcSchrack()) << "Discovering modbus RTU resources..."; - if (hardwareManager()->modbusRtuResource()->modbusRtuMasters().isEmpty()) { - info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("No Modbus RTU interface available. Please set up a Modbus RTU interface first.")); - return; - } + CionDiscovery *discovery = new CionDiscovery(hardwareManager()->modbusRtuResource(), info); - uint slaveAddress = info->params().paramValue(cionDiscoverySlaveAddressParamTypeId).toUInt(); - if (slaveAddress > 254 || slaveAddress == 0) { - info->finish(Thing::ThingErrorInvalidParameter, QT_TR_NOOP("The Modbus slave address must be a value between 1 and 254.")); - return; - } + connect(discovery, &CionDiscovery::discoveryFinished, info, [this, info, discovery](bool modbusMasterAvailable){ + if (!modbusMasterAvailable) { + info->finish(Thing::ThingErrorHardwareNotAvailable, QT_TR_NOOP("No modbus RTU master with appropriate settings found. Please set up a modbus RTU master with a baudrate of 57600, 8 data bis, 1 stop bit and no parity first.")); + return; + } - foreach (ModbusRtuMaster *modbusMaster, hardwareManager()->modbusRtuResource()->modbusRtuMasters()) { - qCDebug(dcSchrack()) << "Found RTU master resource" << modbusMaster << "connected" << modbusMaster->connected(); - if (!modbusMaster->connected()) - continue; + qCInfo(dcSchrack()) << "Discovery results:" << discovery->discoveryResults().count(); - ThingDescriptor descriptor(info->thingClassId(), "i-CHARGE CION", QString::number(slaveAddress) + " " + modbusMaster->serialPort()); - ParamList params; - params << Param(cionThingSlaveAddressParamTypeId, slaveAddress); - params << Param(cionThingModbusMasterUuidParamTypeId, modbusMaster->modbusUuid()); - descriptor.setParams(params); - info->addThingDescriptor(descriptor); - } + foreach (const CionDiscovery::Result &result, discovery->discoveryResults()) { + ThingDescriptor descriptor(cionThingClassId, "Schrack CION", QString("Slave ID: %1, Version: %2").arg(result.slaveId).arg(result.firmwareVersion)); - info->finish(Thing::ThingErrorNoError); + ParamList params{ + {cionThingModbusMasterUuidParamTypeId, result.modbusRtuMasterId}, + {cionThingSlaveAddressParamTypeId, result.slaveId} + }; + descriptor.setParams(params); + + Thing *existingThing = myThings().findByParams(params); + if (existingThing) { + descriptor.setThingId(existingThing->id()); + } + info->addThingDescriptor(descriptor); + } + + info->finish(Thing::ThingErrorNoError); + }); + + discovery->startDiscovery(); } void IntegrationPluginSchrack::setupThing(ThingSetupInfo *info) @@ -130,6 +136,12 @@ void IntegrationPluginSchrack::setupThing(ThingSetupInfo *info) // Note: This register really only tells us if we can control anything... i.e. if the wallbox is unlocked via RFID connect(cionConnection, &CionModbusRtuConnection::chargingEnabledChanged, thing, [=](quint16 charging){ qCDebug(dcSchrack()) << "Charge control enabled changed:" << charging; + // If this register goes 0->1 it means charging has been started. This could be because of an RFID tag. + // As we have may set charging current to 0 ourselves, we'll want to activate it again here + uint maxSetPoint = thing->stateValue(cionMaxChargingCurrentStateTypeId).toUInt(); + if (cionConnection->chargingCurrentSetpoint() != maxSetPoint) { + cionConnection->setChargingCurrentSetpoint(maxSetPoint); + } }); // We can write chargingCurrentSetpoint to the preferred charging we want, and the wallbox will take it, @@ -147,8 +159,11 @@ void IntegrationPluginSchrack::setupThing(ThingSetupInfo *info) connect(cionConnection, &CionModbusRtuConnection::currentChargingCurrentE3Changed, thing, [=](quint16 currentChargingCurrentE3){ qCDebug(dcSchrack()) << "Current charging current E3 current changed:" << currentChargingCurrentE3; - if (cionConnection->chargingCurrentSetpoint() > 0) { + if (cionConnection->chargingEnabled() == 1 && cionConnection->chargingCurrentSetpoint() > 0) { thing->setStateValue(cionMaxChargingCurrentStateTypeId, currentChargingCurrentE3); + thing->setStateValue(cionPowerStateTypeId, true); + } else { + thing->setStateValue(cionPowerStateTypeId, false); } }); diff --git a/schrack/integrationpluginschrack.json b/schrack/integrationpluginschrack.json index 341a700..7210a3d 100644 --- a/schrack/integrationpluginschrack.json +++ b/schrack/integrationpluginschrack.json @@ -13,17 +13,8 @@ "name": "cion", "displayName": "i-CHARGE CION", "id": "075d389d-3330-4d0b-9649-9f085120ca40", - "createMethods": ["discovery"], + "createMethods": ["discovery", "user"], "interfaces": ["evcharger", "connectable"], - "discoveryParamTypes": [ - { - "id": "8004705f-0e13-4713-b75e-49d115cd9517", - "name": "slaveAddress", - "displayName": "Slave address", - "type": "int", - "defaultValue": 1 - } - ], "paramTypes": [ { "id": "dac10e08-734c-4e71-a5d6-0d2a1f416ca6", diff --git a/schrack/schrack.pro b/schrack/schrack.pro index 94fae58..2b8023c 100644 --- a/schrack/schrack.pro +++ b/schrack/schrack.pro @@ -6,8 +6,10 @@ MODBUS_CONNECTIONS += cion-registers.json include(../modbus.pri) SOURCES += \ + ciondiscovery.cpp \ integrationpluginschrack.cpp HEADERS += \ + ciondiscovery.h \ integrationpluginschrack.h diff --git a/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-de.ts b/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-de.ts new file mode 100644 index 0000000..df66743 --- /dev/null +++ b/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-de.ts @@ -0,0 +1,109 @@ + + + + + IntegrationPluginSchrack + + + No modbus RTU master with appropriate settings found. Please set up a modbus RTU master with a baudrate of 57600, 8 data bis, 1 stop bit and no parity first. + Es wurde mein Modbus RTU Master mit passenden Einstellungen gefunden. Bitte richte einen Modbus RTU Master mit einer Baudrate von 57600, 8 Daten-Bits, 1 Stop-Bit und keiner Parität ein. + + + + The Modbus address not valid. It must be a value between 1 and 254. + Die Modbus Addresse ist ungültig. Sie muss zwischen 1 und 254 sein. + + + + The Modbus RTU resource is not available. + Die Modbus RTU Ressource ist nicht verfügbar. + + + + schrack + + + Charging + The name of the StateType ({b59b4b4d-2cdb-4534-bf86-90123ae9bb1a}) of ThingClass cion + Ladend + + + + + Charging enabled + The name of the ParamType (ThingClass: cion, ActionType: power, ID: {61aea53a-bf8d-4fe6-859a-f1687e15d190}) +---------- +The name of the StateType ({61aea53a-bf8d-4fe6-859a-f1687e15d190}) of ThingClass cion + Laden freigegeben + + + + Connected + The name of the StateType ({b7aa8e49-a6c0-4b48-b65e-47259686185f}) of ThingClass cion + Verbunden + + + + Enable or disable charging + The name of the ActionType ({61aea53a-bf8d-4fe6-859a-f1687e15d190}) of ThingClass cion + Ladefreigabe erteilen/entfernen + + + + Firmware version + The name of the StateType ({33780b19-4855-4b5c-bd84-cec8400309be}) of ThingClass cion + Firmware-Version + + + + + Maximum charging current + The name of the ParamType (ThingClass: cion, ActionType: maxChargingCurrent, ID: {221af869-a796-46c2-a5e0-27c8972a0bf2}) +---------- +The name of the StateType ({221af869-a796-46c2-a5e0-27c8972a0bf2}) of ThingClass cion + Maximaler Ladestrom + + + + Modbus RTU master + The name of the ParamType (ThingClass: cion, Type: thing, ID: {636241a9-4838-48ae-bcc8-3427ac3fc102}) + Modbus RTU Master + + + + Modbus slave address + The name of the ParamType (ThingClass: cion, Type: thing, ID: {dac10e08-734c-4e71-a5d6-0d2a1f416ca6}) + Modbus Slave Adresse + + + + Plugged in + The name of the StateType ({13423618-4314-49be-b48c-42d9415199a8}) of ThingClass cion + Eingesteckt + + + + Schrack + The name of the plugin schrack ({600beeb5-5c34-49fc-b2af-8f83c1b11eab}) + Schrack + + + + Schrack GmbH + The name of the vendor ({cadbbbc5-216f-4d25-a8ad-ccf653d1518f}) + Schrack GmbH + + + + Set maximum charging current + The name of the ActionType ({221af869-a796-46c2-a5e0-27c8972a0bf2}) of ThingClass cion + Setze maximalen Ladestrom + + + + i-CHARGE CION + The name of the ThingClass ({075d389d-3330-4d0b-9649-9f085120ca40}) + i-CHARGE CION + + + diff --git a/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-en_US.ts b/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-en_US.ts index da42dcd..105ddf4 100644 --- a/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-en_US.ts +++ b/schrack/translations/600beeb5-5c34-49fc-b2af-8f83c1b11eab-en_US.ts @@ -4,22 +4,17 @@ IntegrationPluginSchrack - - No Modbus RTU interface available. Please set up a Modbus RTU interface first. + + No modbus RTU master with appropriate settings found. Please set up a modbus RTU master with a baudrate of 57600, 8 data bis, 1 stop bit and no parity first. - - The Modbus slave address must be a value between 1 and 254. - - - - + The Modbus address not valid. It must be a value between 1 and 254. - + The Modbus RTU resource is not available. @@ -27,160 +22,85 @@ schrack - - + Charging - The name of the ParamType (ThingClass: cion, EventType: charging, ID: {b59b4b4d-2cdb-4534-bf86-90123ae9bb1a}) ----------- -The name of the StateType ({b59b4b4d-2cdb-4534-bf86-90123ae9bb1a}) of ThingClass cion + The name of the StateType ({b59b4b4d-2cdb-4534-bf86-90123ae9bb1a}) of ThingClass cion - - - + + Charging enabled The name of the ParamType (ThingClass: cion, ActionType: power, ID: {61aea53a-bf8d-4fe6-859a-f1687e15d190}) ---------- -The name of the ParamType (ThingClass: cion, EventType: power, ID: {61aea53a-bf8d-4fe6-859a-f1687e15d190}) ----------- The name of the StateType ({61aea53a-bf8d-4fe6-859a-f1687e15d190}) of ThingClass cion - - Charging enabled or disabled - The name of the EventType ({61aea53a-bf8d-4fe6-859a-f1687e15d190}) of ThingClass cion - - - - - Charging started or stopped - The name of the EventType ({b59b4b4d-2cdb-4534-bf86-90123ae9bb1a}) of ThingClass cion - - - - - + Connected - The name of the ParamType (ThingClass: cion, EventType: connected, ID: {b7aa8e49-a6c0-4b48-b65e-47259686185f}) ----------- -The name of the StateType ({b7aa8e49-a6c0-4b48-b65e-47259686185f}) of ThingClass cion + The name of the StateType ({b7aa8e49-a6c0-4b48-b65e-47259686185f}) of ThingClass cion - - Connected changed - The name of the EventType ({b7aa8e49-a6c0-4b48-b65e-47259686185f}) of ThingClass cion - - - - - - Current power consumption - The name of the ParamType (ThingClass: cion, EventType: currentPower, ID: {db5c951f-47e0-4cdc-8b8b-285f7ed95285}) ----------- -The name of the StateType ({db5c951f-47e0-4cdc-8b8b-285f7ed95285}) of ThingClass cion - - - - - Current power consumption changed - The name of the EventType ({db5c951f-47e0-4cdc-8b8b-285f7ed95285}) of ThingClass cion - - - - + Enable or disable charging The name of the ActionType ({61aea53a-bf8d-4fe6-859a-f1687e15d190}) of ThingClass cion - - - + + Firmware version + The name of the StateType ({33780b19-4855-4b5c-bd84-cec8400309be}) of ThingClass cion + + + + + Maximum charging current The name of the ParamType (ThingClass: cion, ActionType: maxChargingCurrent, ID: {221af869-a796-46c2-a5e0-27c8972a0bf2}) ---------- -The name of the ParamType (ThingClass: cion, EventType: maxChargingCurrent, ID: {221af869-a796-46c2-a5e0-27c8972a0bf2}) ----------- The name of the StateType ({221af869-a796-46c2-a5e0-27c8972a0bf2}) of ThingClass cion - - Maximum charging current changed - The name of the EventType ({221af869-a796-46c2-a5e0-27c8972a0bf2}) of ThingClass cion - - - - + Modbus RTU master The name of the ParamType (ThingClass: cion, Type: thing, ID: {636241a9-4838-48ae-bcc8-3427ac3fc102}) - + Modbus slave address The name of the ParamType (ThingClass: cion, Type: thing, ID: {dac10e08-734c-4e71-a5d6-0d2a1f416ca6}) - - + Plugged in - The name of the ParamType (ThingClass: cion, EventType: pluggedIn, ID: {13423618-4314-49be-b48c-42d9415199a8}) ----------- -The name of the StateType ({13423618-4314-49be-b48c-42d9415199a8}) of ThingClass cion + The name of the StateType ({13423618-4314-49be-b48c-42d9415199a8}) of ThingClass cion - - Plugged or unplugged - The name of the EventType ({13423618-4314-49be-b48c-42d9415199a8}) of ThingClass cion - - - - + Schrack The name of the plugin schrack ({600beeb5-5c34-49fc-b2af-8f83c1b11eab}) - + Schrack GmbH The name of the vendor ({cadbbbc5-216f-4d25-a8ad-ccf653d1518f}) - + Set maximum charging current The name of the ActionType ({221af869-a796-46c2-a5e0-27c8972a0bf2}) of ThingClass cion - - Slave address - The name of the ParamType (ThingClass: cion, Type: discovery, ID: {8004705f-0e13-4713-b75e-49d115cd9517}) - - - - - - Total consumed energy - The name of the ParamType (ThingClass: cion, EventType: totalEnergyConsumed, ID: {8390c005-a8ba-4db6-bb94-8f765ddcc784}) ----------- -The name of the StateType ({8390c005-a8ba-4db6-bb94-8f765ddcc784}) of ThingClass cion - - - - - Total consumed energy changed - The name of the EventType ({8390c005-a8ba-4db6-bb94-8f765ddcc784}) of ThingClass cion - - - - + i-CHARGE CION The name of the ThingClass ({075d389d-3330-4d0b-9649-9f085120ca40})