From eab94cda96024f73985a911580960551d29a648b Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 28 Oct 2020 19:25:28 +0100 Subject: [PATCH 1/4] fixed w1 temperature sensor setup --- onewire/integrationpluginonewire.cpp | 35 +++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/onewire/integrationpluginonewire.cpp b/onewire/integrationpluginonewire.cpp index d3520d23..dc4757da 100644 --- a/onewire/integrationpluginonewire.cpp +++ b/onewire/integrationpluginonewire.cpp @@ -143,26 +143,31 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) } else if (thing->thingClassId() == temperatureSensorThingClassId) { qCDebug(dcOneWire) << "Setup one wire temperature sensor" << thing->params(); - QString address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray(); - if (myThings().findById(thing->parentId())->thingClassId() == oneWireInterfaceThingClassId) { - if (m_owfsInterface) { - thing->setStateValue(temperatureSensorConnectedStateTypeId, m_owfsInterface->isConnected(address.toUtf8())); - thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_owfsInterface->getTemperature(address.toUtf8())); - return info->finish(Thing::ThingErrorNoError); + if (!thing->parentId().isNull()) { + // If there is a parent then it is from an OWFS interface + Thing *parentThing = myThings().findById(thing->parentId()); + if (!parentThing) { + qCWarning(dcOneWire()) << "Could not find parent thing for" << thing->name(); + } + if (parentThing->setupComplete()) { + setupOwfsTemperatureSensor(info); } else { - //OWFS Interface is not yet initialized try a setup in 3 seconds - QTimer::singleShot(3000, this, [this, info]{setupThing(info);}); + connect(parentThing, &Thing::setupStatusChanged, [info, this] { + setupOwfsTemperatureSensor(info); + }); } } else { if (!m_w1Interface) { m_w1Interface = new W1(this); } if (m_w1Interface->interfaceIsAvailable()) { + QString address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toString(); thing->setStateValue(temperatureSensorConnectedStateTypeId, m_w1Interface->deviceAvailable(address)); thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_w1Interface->getTemperature(address)); return info->finish(Thing::ThingErrorNoError); } else { qCWarning(dcOneWire()) << "W1 interface is not available"; + return info->finish(Thing::ThingErrorHardwareNotAvailable); } } @@ -303,6 +308,20 @@ void IntegrationPluginOneWire::thingRemoved(Thing *thing) } } +void IntegrationPluginOneWire::setupOwfsTemperatureSensor(ThingSetupInfo *info) +{ + Thing *thing = info->thing(); + QByteArray address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray(); + if (m_owfsInterface) { + thing->setStateValue(temperatureSensorConnectedStateTypeId, m_owfsInterface->isConnected(address)); + thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_owfsInterface->getTemperature(address)); + return info->finish(Thing::ThingErrorNoError); + } else { + qCWarning(dcOneWire()) << "OWFS interface is not available"; + return info->finish(Thing::ThingErrorHardwareNotAvailable); + } +} + void IntegrationPluginOneWire::onPluginTimer() { From 256593f83c5fb30b680dd79816ec34c6f4717442 Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 28 Oct 2020 19:30:00 +0100 Subject: [PATCH 2/4] fixed w1 temperature sensor setup --- onewire/integrationpluginonewire.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onewire/integrationpluginonewire.h b/onewire/integrationpluginonewire.h index 6237bf31..19c525be 100644 --- a/onewire/integrationpluginonewire.h +++ b/onewire/integrationpluginonewire.h @@ -60,6 +60,8 @@ private: QHash m_runningDiscoveries; + void setupOwfsTemperatureSensor(ThingSetupInfo *info); + private slots: void onPluginTimer(); void onOneWireDevicesDiscovered(QList devices); From 27d1861886490a6ae2a5e3904373259ed7368e5f Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 28 Oct 2020 19:46:20 +0100 Subject: [PATCH 3/4] fixed w1 temperature sensor --- onewire/integrationpluginonewire.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/onewire/integrationpluginonewire.cpp b/onewire/integrationpluginonewire.cpp index dc4757da..c34e34a0 100644 --- a/onewire/integrationpluginonewire.cpp +++ b/onewire/integrationpluginonewire.cpp @@ -292,14 +292,17 @@ void IntegrationPluginOneWire::executeAction(ThingActionInfo *info) void IntegrationPluginOneWire::thingRemoved(Thing *thing) { if (thing->thingClassId() == oneWireInterfaceThingClassId) { - m_owfsInterface->deleteLater(); - m_owfsInterface = nullptr; - return; + if (m_owfsInterface) { + m_owfsInterface->deleteLater(); + m_owfsInterface = nullptr; + } } if (myThings().filterByThingClassId(temperatureSensorThingClassId).isEmpty()) { - m_w1Interface->deleteLater(); - m_w1Interface = nullptr; + if(m_w1Interface) { + m_w1Interface->deleteLater(); + m_w1Interface = nullptr; + } } if (myThings().empty()) { @@ -337,12 +340,12 @@ void IntegrationPluginOneWire::onPluginTimer() double temperature = 0; bool connected = false; - if (myThings().findById(thing->parentId())->thingClassId() == oneWireInterfaceThingClassId) { + if (!thing->parentId().isNull()) { if (m_owfsInterface) { temperature = m_owfsInterface->getTemperature(address); connected = m_owfsInterface->isConnected(address); } else { - qCWarning(dcOneWire()) << "onPlugInTimer: OWFS interface not setup yet for thing" << thing->name(); + qCWarning(dcOneWire()) << "onPlugInTimer: OWFS interface not setup for thing" << thing->name(); } } else { From c21791bcdacd149fe2db99d1b38cabfd32d4b18f Mon Sep 17 00:00:00 2001 From: Boernsman Date: Wed, 28 Oct 2020 20:09:26 +0100 Subject: [PATCH 4/4] only setup temperature sensor is owfs setup was successfull --- onewire/integrationpluginonewire.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/onewire/integrationpluginonewire.cpp b/onewire/integrationpluginonewire.cpp index c34e34a0..48fbdd44 100644 --- a/onewire/integrationpluginonewire.cpp +++ b/onewire/integrationpluginonewire.cpp @@ -152,8 +152,10 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info) if (parentThing->setupComplete()) { setupOwfsTemperatureSensor(info); } else { - connect(parentThing, &Thing::setupStatusChanged, [info, this] { - setupOwfsTemperatureSensor(info); + connect(parentThing, &Thing::setupStatusChanged, info, [info, parentThing, this] { + if (parentThing->setupComplete()) { + setupOwfsTemperatureSensor(info); + } }); } } else {