MCPcopy Create free account
hub / github.com/domoticz/domoticz / CUnitEx_refresh

Function CUnitEx_refresh

hardware/plugins/PythonObjectEx.cpp:452–546  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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()));

Callers 2

CUnitEx_insertFunction · 0.85
CUnitEx_updateFunction · 0.85

Calls 8

PyBorrowedRefClass · 0.85
_tColorClass · 0.85
safe_queryMethod · 0.80
c_strMethod · 0.80
BuildDeviceOptionsMethod · 0.80
toJSONStringMethod · 0.80
LogMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected