| 450 | } |
| 451 | |
| 452 | PyObject *CUnitEx_refresh(CUnitEx *self) |
| 453 | { |
| 454 | module_state *pModState = CPlugin::FindModule(); |
| 455 | if (!pModState) |
| 456 | { |
| 457 | _log.Log(LOG_ERROR, "(%s) Unable to obtain module state.", __func__); |
| 458 | Py_RETURN_NONE; |
| 459 | } |
| 460 | |
| 461 | if (!pModState->pPlugin) |
| 462 | { |
| 463 | _log.Log(LOG_ERROR, "(%s) illegal operation, Plugin has not started yet.", __func__); |
| 464 | Py_RETURN_NONE; |
| 465 | } |
| 466 | |
| 467 | if ((pModState->pPlugin) && (pModState->pPlugin->m_HwdID != -1) && (self->Unit != -1)) |
| 468 | { |
| 469 | if (!(CDeviceEx*)self->Parent) |
| 470 | { |
| 471 | _log.Log(LOG_ERROR, "(%s) Unit is not associated with a Device.", __func__); |
| 472 | Py_RETURN_NONE; |
| 473 | } |
| 474 | CDeviceEx *pDevice = (CDeviceEx*)self->Parent; |
| 475 | std::string sDevice = PyBorrowedRef(pDevice->DeviceID); |
| 476 | // load associated devices to make them available to python |
| 477 | std::vector<std::vector<std::string>> result; |
| 478 | |
| 479 | Py_BEGIN_ALLOW_THREADS |
| 480 | result = m_sql.safe_query("SELECT Unit, ID, Name, nValue, sValue, Type, SubType, SwitchType, LastLevel, CustomImage, SignalLevel, BatteryLevel, LastUpdate, Options, " |
| 481 | "Description, Color, Used, AddjValue, AddjMulti FROM DeviceStatus WHERE (HardwareID==%d) AND (DeviceID=='%s') AND (Unit==%d) ORDER BY Unit ASC", |
| 482 | pModState->pPlugin->m_HwdID, sDevice.c_str(), self->Unit); |
| 483 | Py_END_ALLOW_THREADS |
| 484 | if (!result.empty()) |
| 485 | { |
| 486 | for (const auto &sd : result) |
| 487 | { |
| 488 | self->Unit = atoi(sd[0].c_str()); |
| 489 | self->ID = atoi(sd[1].c_str()); |
| 490 | Py_XDECREF(self->Name); |
| 491 | self->Name = PyUnicode_FromString(sd[2].c_str()); |
| 492 | self->nValue = atoi(sd[3].c_str()); |
| 493 | Py_XDECREF(self->sValue); |
| 494 | self->sValue = PyUnicode_FromString(sd[4].c_str()); |
| 495 | self->Type = atoi(sd[5].c_str()); |
| 496 | self->SubType = atoi(sd[6].c_str()); |
| 497 | self->SwitchType = atoi(sd[7].c_str()); |
| 498 | self->LastLevel = atoi(sd[8].c_str()); |
| 499 | self->Image = atoi(sd[9].c_str()); |
| 500 | self->SignalLevel = atoi(sd[10].c_str()); |
| 501 | self->BatteryLevel = atoi(sd[11].c_str()); |
| 502 | Py_XDECREF(self->LastUpdate); |
| 503 | self->LastUpdate = PyUnicode_FromString(sd[12].c_str()); |
| 504 | PyDict_Clear(self->Options); |
| 505 | if (!sd[13].empty()) |
| 506 | { |
| 507 | if (self->SubType == sTypeCustom) |
| 508 | { |
| 509 | PyDict_SetItemString(self->Options, "Custom", PyUnicode_FromString(sd[13].c_str())); |
no test coverage detected