Merge PR #333: OneWire: Fixed W1 temperature sensor setup

This commit is contained in:
Jenkins nymea 2020-11-02 18:48:11 +01:00
commit edc5a5adf2
2 changed files with 41 additions and 15 deletions

View File

@ -143,26 +143,33 @@ void IntegrationPluginOneWire::setupThing(ThingSetupInfo *info)
} else if (thing->thingClassId() == temperatureSensorThingClassId) { } else if (thing->thingClassId() == temperatureSensorThingClassId) {
qCDebug(dcOneWire) << "Setup one wire temperature sensor" << thing->params(); qCDebug(dcOneWire) << "Setup one wire temperature sensor" << thing->params();
QString address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toByteArray(); if (!thing->parentId().isNull()) {
if (myThings().findById(thing->parentId())->thingClassId() == oneWireInterfaceThingClassId) { // If there is a parent then it is from an OWFS interface
if (m_owfsInterface) { Thing *parentThing = myThings().findById(thing->parentId());
thing->setStateValue(temperatureSensorConnectedStateTypeId, m_owfsInterface->isConnected(address.toUtf8())); if (!parentThing) {
thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_owfsInterface->getTemperature(address.toUtf8())); qCWarning(dcOneWire()) << "Could not find parent thing for" << thing->name();
return info->finish(Thing::ThingErrorNoError); }
if (parentThing->setupComplete()) {
setupOwfsTemperatureSensor(info);
} else { } else {
//OWFS Interface is not yet initialized try a setup in 3 seconds connect(parentThing, &Thing::setupStatusChanged, info, [info, parentThing, this] {
QTimer::singleShot(3000, this, [this, info]{setupThing(info);}); if (parentThing->setupComplete()) {
setupOwfsTemperatureSensor(info);
}
});
} }
} else { } else {
if (!m_w1Interface) { if (!m_w1Interface) {
m_w1Interface = new W1(this); m_w1Interface = new W1(this);
} }
if (m_w1Interface->interfaceIsAvailable()) { if (m_w1Interface->interfaceIsAvailable()) {
QString address = thing->paramValue(temperatureSensorThingAddressParamTypeId).toString();
thing->setStateValue(temperatureSensorConnectedStateTypeId, m_w1Interface->deviceAvailable(address)); thing->setStateValue(temperatureSensorConnectedStateTypeId, m_w1Interface->deviceAvailable(address));
thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_w1Interface->getTemperature(address)); thing->setStateValue(temperatureSensorTemperatureStateTypeId, m_w1Interface->getTemperature(address));
return info->finish(Thing::ThingErrorNoError); return info->finish(Thing::ThingErrorNoError);
} else { } else {
qCWarning(dcOneWire()) << "W1 interface is not available"; qCWarning(dcOneWire()) << "W1 interface is not available";
return info->finish(Thing::ThingErrorHardwareNotAvailable);
} }
} }
@ -287,14 +294,17 @@ void IntegrationPluginOneWire::executeAction(ThingActionInfo *info)
void IntegrationPluginOneWire::thingRemoved(Thing *thing) void IntegrationPluginOneWire::thingRemoved(Thing *thing)
{ {
if (thing->thingClassId() == oneWireInterfaceThingClassId) { if (thing->thingClassId() == oneWireInterfaceThingClassId) {
m_owfsInterface->deleteLater(); if (m_owfsInterface) {
m_owfsInterface = nullptr; m_owfsInterface->deleteLater();
return; m_owfsInterface = nullptr;
}
} }
if (myThings().filterByThingClassId(temperatureSensorThingClassId).isEmpty()) { if (myThings().filterByThingClassId(temperatureSensorThingClassId).isEmpty()) {
m_w1Interface->deleteLater(); if(m_w1Interface) {
m_w1Interface = nullptr; m_w1Interface->deleteLater();
m_w1Interface = nullptr;
}
} }
if (myThings().empty()) { if (myThings().empty()) {
@ -303,6 +313,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() void IntegrationPluginOneWire::onPluginTimer()
{ {
@ -318,12 +342,12 @@ void IntegrationPluginOneWire::onPluginTimer()
double temperature = 0; double temperature = 0;
bool connected = false; bool connected = false;
if (myThings().findById(thing->parentId())->thingClassId() == oneWireInterfaceThingClassId) { if (!thing->parentId().isNull()) {
if (m_owfsInterface) { if (m_owfsInterface) {
temperature = m_owfsInterface->getTemperature(address); temperature = m_owfsInterface->getTemperature(address);
connected = m_owfsInterface->isConnected(address); connected = m_owfsInterface->isConnected(address);
} else { } 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 { } else {

View File

@ -60,6 +60,8 @@ private:
QHash<Thing*, ThingDiscoveryInfo*> m_runningDiscoveries; QHash<Thing*, ThingDiscoveryInfo*> m_runningDiscoveries;
void setupOwfsTemperatureSensor(ThingSetupInfo *info);
private slots: private slots:
void onPluginTimer(); void onPluginTimer();
void onOneWireDevicesDiscovered(QList<Owfs::OwfsDevice> devices); void onOneWireDevicesDiscovered(QList<Owfs::OwfsDevice> devices);