Merge PR #81: fix handling DeviceChanged notification

This commit is contained in:
Jenkins 2018-12-01 12:40:03 +01:00
commit 7ad2ae0c5d
4 changed files with 29 additions and 9 deletions

View File

@ -121,7 +121,7 @@ void DeviceManager::notificationReceived(const QVariantMap &data)
device->deleteLater();
} else if (notification == "Devices.DeviceChanged") {
QUuid deviceId = data.value("params").toMap().value("device").toMap().value("id").toUuid();
qDebug() << "Device changed notification" << deviceId;
qDebug() << "Device changed notification" << deviceId << data.value("params").toMap();
Device *oldDevice = m_devices->getDevice(deviceId);
if (!oldDevice) {
qWarning() << "Received a device changed notification for a device we don't know";
@ -131,6 +131,7 @@ void DeviceManager::notificationReceived(const QVariantMap &data)
qWarning() << "Error parsing device changed notification";
return;
}
qDebug() << "*** device unpacked" << oldDevice->stateValue("98e4476f-e745-4a7f-b795-19269cb70c40");
} else {
qWarning() << "DeviceManager unhandled device notification received" << notification;
}

View File

@ -232,10 +232,18 @@ Device* JsonTypes::unpackDevice(const QVariantMap &deviceMap, DeviceClasses *dev
}
device->setParams(params);
States *states = new States(device);
foreach (StateType *stateType, deviceClass->stateTypes()->stateTypes()) {
State *state = new State(device->id(), stateType->id(), stateType->defaultValue(), states);
states->addState(state);
States *states = device->states();
if (!states) {
states = new States(device);
}
foreach (const QVariant &stateVariant, deviceMap.value("states").toList()) {
State *state = states->getState(stateVariant.toMap().value("stateTypeId").toUuid());
if (!state) {
state = new State(device->id(), stateVariant.toMap().value("stateTypeId").toUuid(), stateVariant.toMap().value("value"), states);
states->addState(state);
} else {
state->setValue(stateVariant.toMap().value("value"));
}
}
device->setStates(states);

View File

@ -75,8 +75,13 @@ Params *Device::params() const
void Device::setParams(Params *params)
{
m_params = params;
emit paramsChanged();
if (m_params != params) {
if (m_params) {
m_params->deleteLater();
}
m_params = params;
emit paramsChanged();
}
}
States *Device::states() const
@ -86,8 +91,13 @@ States *Device::states() const
void Device::setStates(States *states)
{
m_states = states;
emit statesChanged();
if (m_states != states) {
if (m_states) {
m_states->deleteLater();
}
m_states = states;
emit statesChanged();
}
}
DeviceClass *Device::deviceClass() const

View File

@ -76,6 +76,7 @@ QVariant States::data(const QModelIndex &index, int role) const
void States::addState(State *state)
{
state->setParent(this);
beginInsertRows(QModelIndex(), m_states.count(), m_states.count());
//qDebug() << "States: loaded state" << state->stateTypeId();
m_states.append(state);