fixed wallbe plugin

This commit is contained in:
Boernsman 2021-02-22 20:31:44 +01:00
parent ed3d8ef258
commit 71e51e8337
9 changed files with 225 additions and 107 deletions

View File

@ -38,7 +38,7 @@ ModbusTCPMaster::ModbusTCPMaster(const QHostAddress &hostAddress, uint port, QOb
m_modbusTcpClient = new QModbusTcpClient(this); m_modbusTcpClient = new QModbusTcpClient(this);
m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkPortParameter, port); m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkPortParameter, port);
m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkAddressParameter, hostAddress.toString()); m_modbusTcpClient->setConnectionParameter(QModbusDevice::NetworkAddressParameter, hostAddress.toString());
m_modbusTcpClient->setTimeout(100); m_modbusTcpClient->setTimeout(1000);
m_modbusTcpClient->setNumberOfRetries(3); m_modbusTcpClient->setNumberOfRetries(3);
connect(m_modbusTcpClient, &QModbusTcpClient::stateChanged, this, &ModbusTCPMaster::onModbusStateChanged); connect(m_modbusTcpClient, &QModbusTcpClient::stateChanged, this, &ModbusTCPMaster::onModbusStateChanged);
@ -190,7 +190,7 @@ QUuid ModbusTCPMaster::writeHoldingRegisters(uint slaveAddress, uint registerAdd
emit writeRequestError(requestId, reply->errorString()); emit writeRequestError(requestId, reply->errorString());
reply->finished(); // To make sure it will be deleted reply->finished(); // To make sure it will be deleted
}); });
QTimer::singleShot(200, reply, &QModbusReply::deleteLater); QTimer::singleShot(2000, reply, &QModbusReply::deleteLater);
} else { } else {
delete reply; // broadcast replies return immediately delete reply; // broadcast replies return immediately
return ""; return "";
@ -234,7 +234,7 @@ QUuid ModbusTCPMaster::readDiscreteInput(uint slaveAddress, uint registerAddress
emit writeRequestError(requestId, reply->errorString()); emit writeRequestError(requestId, reply->errorString());
reply->finished(); // To make sure it will be deleted reply->finished(); // To make sure it will be deleted
}); });
QTimer::singleShot(200, reply, &QModbusReply::deleteLater); QTimer::singleShot(2000, reply, &QModbusReply::deleteLater);
} else { } else {
delete reply; // broadcast replies return immediately delete reply; // broadcast replies return immediately
return ""; return "";
@ -277,7 +277,7 @@ QUuid ModbusTCPMaster::readInputRegister(uint slaveAddress, uint registerAddress
emit writeRequestError(requestId, reply->errorString()); emit writeRequestError(requestId, reply->errorString());
reply->finished(); // To make sure it will be deleted reply->finished(); // To make sure it will be deleted
}); });
QTimer::singleShot(200, reply, &QModbusReply::deleteLater); QTimer::singleShot(2000, reply, &QModbusReply::deleteLater);
} else { } else {
delete reply; // broadcast replies return immediately delete reply; // broadcast replies return immediately
return ""; return "";
@ -321,7 +321,7 @@ QUuid ModbusTCPMaster::readHoldingRegister(uint slaveAddress, uint registerAddre
emit writeRequestError(requestId, reply->errorString()); emit writeRequestError(requestId, reply->errorString());
reply->finished(); // To make sure it will be deleted reply->finished(); // To make sure it will be deleted
}); });
QTimer::singleShot(200, reply, &QModbusReply::deleteLater); QTimer::singleShot(2000, reply, &QModbusReply::deleteLater);
} else { } else {
delete reply; // broadcast replies return immediately delete reply; // broadcast replies return immediately
return ""; return "";
@ -371,7 +371,7 @@ QUuid ModbusTCPMaster::writeCoils(uint slaveAddress, uint registerAddress, const
emit writeRequestError(requestId, reply->errorString()); emit writeRequestError(requestId, reply->errorString());
reply->finished(); // To make sure it will be deleted reply->finished(); // To make sure it will be deleted
}); });
QTimer::singleShot(200, reply, &QModbusReply::deleteLater); QTimer::singleShot(2000, reply, &QModbusReply::deleteLater);
} else { } else {
delete reply; // broadcast replies return immediately delete reply; // broadcast replies return immediately
return ""; return "";
@ -397,7 +397,7 @@ void ModbusTCPMaster::onModbusErrorOccurred(QModbusDevice::Error error)
void ModbusTCPMaster::onModbusStateChanged(QModbusDevice::State state) void ModbusTCPMaster::onModbusStateChanged(QModbusDevice::State state)
{ {
bool connected = (state != QModbusDevice::UnconnectedState); bool connected = (state == QModbusDevice::ConnectedState);
if (!connected) { if (!connected) {
//try to reconnect in 10 seconds //try to reconnect in 10 seconds
m_reconnectTimer->start(10000); m_reconnectTimer->start(10000);

View File

@ -143,7 +143,7 @@ void Discovery::discoveryFinished(int exitCode, QProcess::ExitStatus exitStatus)
if (isUp) { if (isUp) {
foundHosts++; foundHosts++;
qCDebug(dcWallbe()) << " - host:" << address; qCDebug(dcWallbe()) << " - host:" << vendor << address << macAddress;
Host *host = new Host(); Host *host = new Host();
host->setAddress(address); host->setAddress(address);
@ -157,7 +157,7 @@ void Discovery::discoveryFinished(int exitCode, QProcess::ExitStatus exitStatus)
arpLookup->start("arp", {"-vn"}); arpLookup->start("arp", {"-vn"});
} }
host->setHostName(vendor); host->setVendor(vendor);
QHostInfo::lookupHost(address, this, SLOT(hostLookupDone(QHostInfo))); QHostInfo::lookupHost(address, this, SLOT(hostLookupDone(QHostInfo)));
m_pendingNameLookups.insert(address, host); m_pendingNameLookups.insert(address, host);

View File

@ -32,26 +32,8 @@
Host::Host() Host::Host()
{ {
qRegisterMetaType<Host>();
} qRegisterMetaType<QList<Host> >();
Host::Host(const QString &hostName, const QString &address, const QString &macAddress, const bool &reachable):
m_hostName(hostName),
m_address(address),
m_macAddress(macAddress),
m_reachable(reachable)
{
}
QString Host::hostName() const
{
return m_hostName;
}
QString Host::address() const
{
return m_address;
} }
QString Host::macAddress() const QString Host::macAddress() const
@ -59,18 +41,64 @@ QString Host::macAddress() const
return m_macAddress; return m_macAddress;
} }
void Host::setMacAddress(const QString &macAddress)
{
m_macAddress = macAddress;
}
QString Host::hostName() const
{
return m_hostName;
}
void Host::setHostName(const QString &hostName)
{
m_hostName = hostName;
}
QString Host::vendor() const
{
return m_vendor;
}
void Host::setVendor(const QString &vendor)
{
m_vendor = vendor;
}
QString Host::address() const
{
return m_address;
}
void Host::setAddress(const QString &address)
{
m_address = address;
}
void Host::seen()
{
m_lastSeenTime = QDateTime::currentDateTime();
}
QDateTime Host::lastSeenTime() const
{
return m_lastSeenTime;
}
bool Host::reachable() const bool Host::reachable() const
{ {
return m_reachable; return m_reachable;
} }
bool Host::isValid() const void Host::setReachable(bool reachable)
{ {
return !m_hostName.isEmpty() && !m_address.isEmpty(); m_reachable = reachable;
} }
QDebug operator<<(QDebug dbg, const Host &host) QDebug operator<<(QDebug dbg, const Host &host)
{ {
dbg.nospace() << "Host(" << host.hostName() << ", " << host.address() << ", " << (host.reachable() ? "up" : "down") << ")"; dbg.nospace() << "Host(" << host.macAddress() << "," << host.hostName() << ", " << host.address() << ", " << (host.reachable() ? "up" : "down") << ")";
return dbg.space(); return dbg.space();
} }

View File

@ -33,27 +33,42 @@
#include <QString> #include <QString>
#include <QDebug> #include <QDebug>
#include <QDateTime>
class Host class Host
{ {
public: public:
Host(); Host();
Host(const QString &hostName, const QString &address, const QString &macAddress, const bool &reachable);
QString macAddress() const;
void setMacAddress(const QString &macAddress);
QString hostName() const; QString hostName() const;
QString address() const; void setHostName(const QString &hostName);
QString macAddress() const;
bool reachable() const;
bool isValid() const; QString vendor() const;
void setVendor(const QString &vendor);
QString address() const;
void setAddress(const QString &address);
void seen();
QDateTime lastSeenTime() const;
bool reachable() const;
void setReachable(bool reachable);
private: private:
QString m_macAddress;
QString m_hostName; QString m_hostName;
QString m_address; QString m_address;
QString m_macAddress; QString m_vendor;
QDateTime m_lastSeenTime;
bool m_reachable; bool m_reachable;
}; };
Q_DECLARE_METATYPE(Host)
QDebug operator<<(QDebug dbg, const Host &host); QDebug operator<<(QDebug dbg, const Host &host);
#endif // HOST_H #endif // HOST_H

View File

@ -45,26 +45,30 @@ IntegrationPluginWallbe::IntegrationPluginWallbe()
void IntegrationPluginWallbe::init() { void IntegrationPluginWallbe::init() {
m_discovery = new Discover(QStringList("-xO" "-p 502")); m_discovery = new Discovery();
connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) { connect(m_discovery, &Discovery::finished, this, [this](const QList<Host> &hosts) {
foreach (const Host &host, hosts) {
if (!host.vendor().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
continue;
Q_FOREACH(Thing *existingThing, myThings()) { Q_FOREACH(Thing *existingThing, myThings()) {
if (existingThing->paramValue(wallbeEcoThingMacAddressParamTypeId).toString().isEmpty()) { if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString().isEmpty()) {
//This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup //This device got probably manually setup, to enable auto rediscovery the MAC address needs to setup
if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() == host.address()) { if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() == host.address()) {
qCDebug(dcWallbe()) << "Wallbe Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress(); qCDebug(dcWallbe()) << "Wallbe Wallbox MAC Address has been discovered" << existingThing->name() << host.macAddress();
existingThing->setParamValue(wallbeEcoThingMacParamTypeId, host.macAddress()); existingThing->setParamValue(wallbeEcoThingMacParamTypeId, host.macAddress());
}
} else if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() != host.address()) {
qCDebug(dcWallbe()) << "Wallbe Wallbox IP Address has changed, from" << existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() << "to" << host.address();
existingThing->setParamValue(wallbeEcoThingIpParamTypeId, host.address());
} else {
qCDebug(dcWallbe()) << "Wallbe Wallbox" << existingThing->name() << "IP address has not changed" << host.address();
}
break;
} }
} else if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
if (existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() != host.address()) {
qCDebug(dcWallbe()) << "Wallbe Wallbox IP Address has changed, from" << existingThing->paramValue(wallbeEcoThingIpParamTypeId).toString() << "to" << host.address();
existingThing->setParamValue(wallbeEcoThingIpParamTypeId, host.address());
} else {
qCDebug(dcWallbe()) << "Wallbe Wallbox" << existingThing->name() << "IP address has not changed" << host.address();
}
break;
} }
} }
}); });
@ -76,15 +80,24 @@ void IntegrationPluginWallbe::discoverThings(ThingDiscoveryInfo *info)
if (info->thingClassId() == wallbeEcoThingClassId){ if (info->thingClassId() == wallbeEcoThingClassId){
qCDebug(dcWallbe) << "Start Wallbe eco discovery"; qCDebug(dcWallbe) << "Start Wallbe eco discovery";
m_discovery->discoverHosts(25); m_discovery->discoverHosts(50);
connect(discover, &Discover::devicesDiscovered, this, [info, this](QList<Host> hosts){ connect(m_discovery, &Discovery::finished, info, [this, info] (const QList<Host> &hosts) {
foreach (Host host, hosts) {
ThingDescriptor descriptor; foreach (const Host &host, hosts) {
foreach(Thing *thing, myThings().filterByParam(wallbeEcoThingMacParamTypeId, host.macAddress())) { if (!host.vendor().contains("Phoenix", Qt::CaseSensitivity::CaseInsensitive))
descriptor.setThingId(thing->id()); continue;
break;
ThingDescriptor descriptor(wallbeEcoThingClassId);
// Rediscovery
foreach (Thing *existingThing, myThings()) {
if (existingThing->paramValue(wallbeEcoThingMacParamTypeId).toString() == host.macAddress()) {
qCDebug(dcWallbe()) << " - Device is already added";
descriptor.setThingId(existingThing->id());
break;
}
} }
descriptor.setTitle(host.hostName()); descriptor.setTitle(host.hostName().remove(".localdomain"));
descriptor.setDescription(host.address());
ParamList params; ParamList params;
params.append(Param(wallbeEcoThingIpParamTypeId, host.address())); params.append(Param(wallbeEcoThingIpParamTypeId, host.address()));
params.append(Param(wallbeEcoThingMacParamTypeId, host.macAddress())); params.append(Param(wallbeEcoThingMacParamTypeId, host.macAddress()));
@ -104,12 +117,12 @@ void IntegrationPluginWallbe::setupThing(ThingSetupInfo *info)
Thing *thing = info->thing(); Thing *thing = info->thing();
qCDebug(dcWallbe) << "Setting up a new device:" << thing->params(); qCDebug(dcWallbe) << "Setting up a new device:" << thing->params();
if (info->thingClassId() == wallbeEcoThingClassId) { if (thing->thingClassId() == wallbeEcoThingClassId) {
QHostAddress address(thing->paramValue(wallbeEcoThingIpParamTypeId).toString()); QHostAddress address(thing->paramValue(wallbeEcoThingIpParamTypeId).toString());
if (m_connections.contains(thing)) { if (m_connections.contains(thing)) {
qCDebug(dcWallbe()) << "Setup after reconfiguration, cleaning up ..."; qCDebug(dcWallbe()) << "Setup after reconfiguration, cleaning up ...";
m_connnections.take(thing).deleteLater();) m_connections.take(thing)->deleteLater();
} }
if (address.isNull()){ if (address.isNull()){
qCWarning(dcWallbe) << "IP address is not valid"; qCWarning(dcWallbe) << "IP address is not valid";
@ -118,34 +131,43 @@ void IntegrationPluginWallbe::setupThing(ThingSetupInfo *info)
} }
ModbusTCPMaster *modbusTcpMaster = new ModbusTCPMaster(address, 502, this); ModbusTCPMaster *modbusTcpMaster = new ModbusTCPMaster(address, 502, this);
connect(modbusTcpMaster, &ModbusTCPMaster::connectionStateChanged, this, &IntegrationPluginWallbe::onConnectionStateChanged); connect(modbusTcpMaster, &ModbusTCPMaster::connectionStateChanged, this, &IntegrationPluginWallbe::onConnectionStateChanged);
connect(modbusTcpMaster, &ModbusTCPMaster::receivedCoil, this, &IntegrationPluginWallbe::onReceivedCoil);
connect(modbusTcpMaster, &ModbusTCPMaster::receivedInputRegister, this, &IntegrationPluginWallbe::onReceivedInputRegister);
connect(modbusTcpMaster, &ModbusTCPMaster::receivedHoldingRegister, this, &IntegrationPluginWallbe::onReceivedHoldingRegister); connect(modbusTcpMaster, &ModbusTCPMaster::receivedHoldingRegister, this, &IntegrationPluginWallbe::onReceivedHoldingRegister);
connect(modbusTcpMaster, &ModbusTCPMaster::writeRequestExecuted, this, &IntegrationPluginWallbe::onWriteRequestExecuted); connect(modbusTcpMaster, &ModbusTCPMaster::writeRequestExecuted, this, &IntegrationPluginWallbe::onWriteRequestExecuted);
connect(modbusTcpMaster, &ModbusTCPMaster::writeRequestError, this, &IntegrationPluginWallbe::onWriteRequestError); connect(modbusTcpMaster, &ModbusTCPMaster::writeRequestError, this, &IntegrationPluginWallbe::onWriteRequestError);
connect(info, &ThingSetupInfo::aborted, modbusTcpMaster, &ModbusTCPMaster::deleteLater);
m_connections.insert(thing, modbusTcpMaster);
connect(modbusTcpMaster, &ModbusTCPMaster::connectionStateChanged, info, [this, info, modbusTcpMaster](bool connected) { connect(modbusTcpMaster, &ModbusTCPMaster::connectionStateChanged, info, [this, info, modbusTcpMaster](bool connected) {
qCDebug(dcWallbe()) << "Modbus TCP connection changed, connected" << connected;
if(connected) { if(connected) {
m_connections.insert(info->thing(), modbusTcpMaster);
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);
} else {
info->finish(Thing::ThingErrorHardwareNotAvailable);
} }
}); });
if (!modbusTcpMaster->connectDevice()) {
qCWarning(dcWallbe()) << "Could not connect device";
return info->finish(Thing::ThingErrorHardwareNotAvailable);
}
} }
} }
void IntegrationPluginWallbe::postSetupThing(Thing *thing) void IntegrationPluginWallbe::postSetupThing(Thing *thing)
{ {
if (!m_pluginTimer) { qCDebug(dcWallbe()) << "Post setup thing" << thing->name();
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(120);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this] {
foreach(Thing *thing, m_connections.keys()) {
update(thing);
}
});
}
if (thing->thingClassId() == wallbeEcoThingClassId){ if (thing->thingClassId() == wallbeEcoThingClassId){
if (!m_pluginTimer) {
qCDebug(dcWallbe()) << "Starting plugin timer";
m_pluginTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
connect(m_pluginTimer, &PluginTimer::timeout, this, [this] {
foreach(Thing *thing, m_connections.keys()) {
update(thing);
}
});
}
thing->setStateValue(wallbeEcoConnectedStateTypeId, true);
update(thing); update(thing);
} else { } else {
Q_ASSERT_X(false, "postSetupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8()); Q_ASSERT_X(false, "postSetupThing", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
@ -172,10 +194,8 @@ void IntegrationPluginWallbe::executeAction(ThingActionInfo *info)
// get the param value of the charging action // get the param value of the charging action
bool charging = action.param(wallbeEcoPowerActionPowerParamTypeId).value().toBool(); bool charging = action.param(wallbeEcoPowerActionPowerParamTypeId).value().toBool();
qCDebug(dcWallbe) << "start Charging button" << thing->name() << "set power to" << charging; qCDebug(dcWallbe) << "Start Charging button" << thing->name() << "set power to" << charging;
QUuid requestId = modbusTcpMaster->writeCoil(0xff, WallbeRegisterAddress::ChargingStatus, charging); QUuid requestId = modbusTcpMaster->writeCoil(m_slaveAddress, WallbeRegisterAddress::EnableCharging, charging);
// Set the "power" state
thing->setStateValue(wallbeEcoPowerStateTypeId, charging);
m_asyncActions.insert(requestId, info); m_asyncActions.insert(requestId, info);
connect(info, &ThingActionInfo::aborted, this, [this, requestId] {m_asyncActions.remove(requestId);}); connect(info, &ThingActionInfo::aborted, this, [this, requestId] {m_asyncActions.remove(requestId);});
@ -183,8 +203,7 @@ void IntegrationPluginWallbe::executeAction(ThingActionInfo *info)
uint16_t current = action.param(wallbeEcoMaxChargingCurrentEventMaxChargingCurrentParamTypeId).value().toUInt(); uint16_t current = action.param(wallbeEcoMaxChargingCurrentEventMaxChargingCurrentParamTypeId).value().toUInt();
qCDebug(dcWallbe) << "Charging power set to" << current; qCDebug(dcWallbe) << "Charging power set to" << current;
QUuid requestId = modbusTcpMaster->writeCoil(0xff, WallbeRegisterAddress::ChargingCurrent, current); QUuid requestId = modbusTcpMaster->writeHoldingRegister(m_slaveAddress, WallbeRegisterAddress::ChargingCurrent, current);
thing->setStateValue(wallbeEcoMaxChargingCurrentStateTypeId, current);
m_asyncActions.insert(requestId, info); m_asyncActions.insert(requestId, info);
connect(info, &ThingActionInfo::aborted, this, [this, requestId] {m_asyncActions.remove(requestId);}); connect(info, &ThingActionInfo::aborted, this, [this, requestId] {m_asyncActions.remove(requestId);});
@ -215,12 +234,16 @@ void IntegrationPluginWallbe::thingRemoved(Thing *thing)
void IntegrationPluginWallbe::update(Thing *thing) void IntegrationPluginWallbe::update(Thing *thing)
{ {
ModbusTCPMaster *modbusTCPMaster = m_connections.value(thing); ModbusTCPMaster *modbusTCPMaster = m_connections.value(thing);
if(!modbusTCPMaster) if(!modbusTCPMaster) {
qCWarning(dcWallbe()) << "Modbus TCP connection not found for" << thing->name();
return; return;
modbusTCPMaster->readHoldingRegister(0xff, WallbeRegisterAddress::EVStatus); }
modbusTCPMaster->readHoldingRegister(0xff, WallbeRegisterAddress::ChargingCurrent); modbusTCPMaster->readInputRegister(m_slaveAddress, WallbeRegisterAddress::EVStatus, 1);
modbusTCPMaster->readHoldingRegister(0xff, WallbeRegisterAddress::ChargingStatus); modbusTCPMaster->readInputRegister(m_slaveAddress, WallbeRegisterAddress::FirmwareVersion, 2);
modbusTCPMaster->readHoldingRegister(0xff, WallbeRegisterAddress::ChargingTime); modbusTCPMaster->readInputRegister(m_slaveAddress, WallbeRegisterAddress::ChargingTime, 2);
modbusTCPMaster->readHoldingRegister(m_slaveAddress, WallbeRegisterAddress::ChargingCurrent, 1);
modbusTCPMaster->readCoil(m_slaveAddress, WallbeRegisterAddress::EnableCharging, 1);
} }
void IntegrationPluginWallbe::onConnectionStateChanged(bool status) void IntegrationPluginWallbe::onConnectionStateChanged(bool status)
@ -232,16 +255,14 @@ void IntegrationPluginWallbe::onConnectionStateChanged(bool status)
thing->setStateValue(wallbeEcoConnectedStateTypeId, status); thing->setStateValue(wallbeEcoConnectedStateTypeId, status);
} }
void IntegrationPluginWallbe::onReceivedHoldingRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value) void IntegrationPluginWallbe::onReceivedInputRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value)
{ {
Q_UNUSED(slaveAddress) Q_UNUSED(slaveAddress)
ModbusTCPMaster *modbusTCPMaster = static_cast<ModbusTCPMaster *>(sender()); ModbusTCPMaster *modbusTCPMaster = static_cast<ModbusTCPMaster *>(sender());
Thing *thing = m_connections.key(modbusTCPMaster); Thing *thing = m_connections.key(modbusTCPMaster);
if (!thing) if (!thing)
return; return;
if (WallbeRegisterAddress(modbusRegister) == WallbeRegisterAddress::EVStatus) {
switch (WallbeRegisterAddress(modbusRegister)) {
case WallbeRegisterAddress::EVStatus:
//EV state - 16 bit ASCII (8bit) //EV state - 16 bit ASCII (8bit)
switch (value[0]) { switch (value[0]) {
case 65: case 65:
@ -265,28 +286,64 @@ void IntegrationPluginWallbe::onReceivedHoldingRegister(int slaveAddress, int mo
default: default:
thing->setStateValue(wallbeEcoEvStatusStateTypeId, "F - Supply equipment not available"); thing->setStateValue(wallbeEcoEvStatusStateTypeId, "F - Supply equipment not available");
} }
break; } else if (WallbeRegisterAddress(modbusRegister) == WallbeRegisterAddress::ChargingTime) {
case WallbeRegisterAddress::ChargingStatus:
thing->setStateValue(wallbeEcoPowerStateTypeId, value[0]);
break;
case WallbeRegisterAddress::ChargingTime: {
// Extract Input Register 102 - load time - 32bit integer // Extract Input Register 102 - load time - 32bit integer
int minutes = (((uint32_t)(value[0]<<16)|(uint32_t)(value[1]))/60); //Converts to minutes
thing->setStateValue(wallbeEcoChargeTimeStateTypeId, minutes); if (value.length() >= 2) {
int minutes = (((uint32_t)(value[1]<<16)|(uint32_t)(value[0]))/60); //Converts to minutes
qCDebug(dcWallbe()) << " - Charging time:" << minutes << "[min]";
thing->setStateValue(wallbeEcoChargeTimeStateTypeId, minutes);
}
} else if (WallbeRegisterAddress(modbusRegister) == WallbeRegisterAddress::FirmwareVersion) {
int firmware = (uint32_t)(value[1]<<16)|(uint32_t)(value[0]);
uint major = firmware/10000;
uint minor = (firmware%10000)/100;
uint patch = firmware%100;
QString firmwarestring = QString::number(major)+'.'+QString::number(minor)+'.'+QString::number(patch);
thing->setStateValue(wallbeEcoFirmwareVersionStateTypeId, firmwarestring);
} }
break; }
case WallbeRegisterAddress::ChargingCurrent:
void IntegrationPluginWallbe::onReceivedCoil(int slaveAddress, int modbusRegister, const QVector<quint16> &value)
{
Q_UNUSED(slaveAddress)
ModbusTCPMaster *modbusTCPMaster = static_cast<ModbusTCPMaster *>(sender());
Thing *thing = m_connections.key(modbusTCPMaster);
if (!thing)
return;
if (WallbeRegisterAddress(modbusRegister) == WallbeRegisterAddress::EnableCharging) {
qCDebug(dcWallbe()) << " - Enable charging:" << (value[0] != 0);
thing->setStateValue(wallbeEcoPowerStateTypeId, (value[0] != 0));
}
}
void IntegrationPluginWallbe::onReceivedHoldingRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value)
{
Q_UNUSED(slaveAddress)
ModbusTCPMaster *modbusTCPMaster = static_cast<ModbusTCPMaster *>(sender());
Thing *thing = m_connections.key(modbusTCPMaster);
if (!thing)
return;
switch (WallbeRegisterAddress(modbusRegister)) {
case WallbeRegisterAddress::ChargingCurrent: {
qCDebug(dcWallbe()) << " - Charging current:" << value[0] << "[A]";
thing->setStateValue(wallbeEcoMaxChargingCurrentStateTypeId, value[0]); thing->setStateValue(wallbeEcoMaxChargingCurrentStateTypeId, value[0]);
break; } break;
case WallbeRegisterAddress::ErrorCode: case WallbeRegisterAddress::ErrorCode: {
qCDebug(dcWallbe()) << "Received Error Code modbus register" << value[0]; qCDebug(dcWallbe()) << "Received Error Code modbus register" << value[0];
break; break;
} }
default:
break;
}
} }
void IntegrationPluginWallbe::onWriteRequestExecuted(const QUuid &requestId, bool success) void IntegrationPluginWallbe::onWriteRequestExecuted(const QUuid &requestId, bool success)
{ {
if (m_asyncActions.contains(requestId)) { if (m_asyncActions.contains(requestId)) {
qCDebug(dcWallbe()) << "Write request executed" << requestId << success;
ThingActionInfo *info = m_asyncActions.value(requestId); ThingActionInfo *info = m_asyncActions.value(requestId);
if (success) { if (success) {
info->finish(Thing::ThingErrorNoError); info->finish(Thing::ThingErrorNoError);

View File

@ -35,7 +35,7 @@
#include "plugintimer.h" #include "plugintimer.h"
#include "host.h" #include "host.h"
#include "discover.h" #include "discovery.h"
#include "../modbus/modbustcpmaster.h" #include "../modbus/modbustcpmaster.h"
#include <QObject> #include <QObject>
@ -52,10 +52,16 @@ public:
enum WallbeRegisterAddress { enum WallbeRegisterAddress {
EVStatus = 100, EVStatus = 100,
ChargingTime = 102, ChargingTime = 102,
FirmwareVersion = 105,
ErrorCode = 107, ErrorCode = 107,
ChargingCurrent = 300, ChargingCurrent = 300,
ChargingStatus = 400, EnableCharging = 400,
Output1 = 405,
Output2 = 406,
Output3 = 407,
Output4 = 408
}; };
Q_ENUM(WallbeRegisterAddress)
explicit IntegrationPluginWallbe(); explicit IntegrationPluginWallbe();
void init() override; void init() override;
@ -66,14 +72,18 @@ public:
void thingRemoved(Thing *thing) override; void thingRemoved(Thing *thing) override;
private: private:
Discovery *m_discovery = nullptr;
QHash<Thing *, ModbusTCPMaster *> m_connections; QHash<Thing *, ModbusTCPMaster *> m_connections;
PluginTimer *m_pluginTimer = nullptr; PluginTimer *m_pluginTimer = nullptr;
QHash<QUuid, ThingActionInfo *> m_asyncActions; QHash<QUuid, ThingActionInfo *> m_asyncActions;
int m_slaveAddress = 180;
void update(Thing *thing); void update(Thing *thing);
private slots: private slots:
void onConnectionStateChanged(bool status); void onConnectionStateChanged(bool status);
void onReceivedInputRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
void onReceivedCoil(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
void onReceivedHoldingRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value); void onReceivedHoldingRegister(int slaveAddress, int modbusRegister, const QVector<quint16> &value);
void onWriteRequestExecuted(const QUuid &requestId, bool success); void onWriteRequestExecuted(const QUuid &requestId, bool success);

View File

@ -88,6 +88,14 @@
"maxValue": 80, "maxValue": 80,
"defaultValue": 6, "defaultValue": 6,
"writable": true "writable": true
},
{
"id": "f4c822a0-454b-4782-85d2-8c60bacb4fe8",
"displayName": "Firmware version",
"displayNameEvent": "Firmware version changed",
"name": "firmwareVersion",
"type": "QString",
"defaultValue": ""
} }
] ]
} }

View File

@ -7,11 +7,11 @@ QT += \
SOURCES += \ SOURCES += \
integrationpluginwallbe.cpp \ integrationpluginwallbe.cpp \
../modbus/modbustcpmaster.cpp \ ../modbus/modbustcpmaster.cpp \
host.cpp \ discovery.cpp \
discover.cpp host.cpp
HEADERS += \ HEADERS += \
integrationpluginwallbe.h \ integrationpluginwallbe.h \
../modbus/modbustcpmaster.h \ ../modbus/modbustcpmaster.h \
host.h \ discovery.h \
discover.h host.h