Skip to content

Commit a1cdb9f

Browse files
committed
[BleBox] refactor and new method for useful all hardwares
1 parent 2f69eea commit a1cdb9f

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

hardware/BleBox.cpp

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -422,20 +422,15 @@ namespace http {
422422
}
423423

424424
std::string hwid = request::findValue(&req, "idx");
425-
if (hwid == "")
426-
return;
427-
int iHardwareID = atoi(hwid.c_str());
428-
CDomoticzHardwareBase *pHardware = m_mainworker.GetHardware(iHardwareID);
429-
if (pHardware == NULL)
430-
return;
431-
if (pHardware->HwdType != HTYPE_BleBox)
425+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
426+
if (pBaseHardware == NULL)
432427
return;
433428

434429
root["status"] = "OK";
435430
root["title"] = "BleBoxGetNodes";
436431

437432
std::vector<std::vector<std::string> > result;
438-
result = m_sql.safe_query("SELECT ID,Name,DeviceID FROM DeviceStatus WHERE (HardwareID==%d)", iHardwareID);
433+
result = m_sql.safe_query("SELECT ID,Name,DeviceID FROM DeviceStatus WHERE (HardwareID=='%d')", pBaseHardware->m_HwdID);
439434
if (result.size() > 0)
440435
{
441436
std::vector<std::vector<std::string> >::const_iterator itt;
@@ -469,12 +464,9 @@ namespace http {
469464
(mode2 == "")
470465
)
471466
return;
472-
int iHardwareID = atoi(hwid.c_str());
473-
CDomoticzHardwareBase *pBaseHardware = m_mainworker.GetHardware(iHardwareID);
467+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
474468
if (pBaseHardware == NULL)
475469
return;
476-
if (pBaseHardware->HwdType != HTYPE_BleBox)
477-
return;
478470
BleBox *pHardware = reinterpret_cast<BleBox*>(pBaseHardware);
479471

480472
root["status"] = "OK";
@@ -505,12 +497,9 @@ namespace http {
505497
(ip == "")
506498
)
507499
return;
508-
int iHardwareID = atoi(hwid.c_str());
509-
CDomoticzHardwareBase *pBaseHardware = m_mainworker.GetHardware(iHardwareID);
500+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
510501
if (pBaseHardware == NULL)
511502
return;
512-
if (pBaseHardware->HwdType != HTYPE_BleBox)
513-
return;
514503
BleBox *pHardware = reinterpret_cast<BleBox*>(pBaseHardware);
515504

516505
root["status"] = "OK";
@@ -536,12 +525,9 @@ namespace http {
536525
(ip == "")
537526
)
538527
return;
539-
int iHardwareID = atoi(hwid.c_str());
540-
CDomoticzHardwareBase *pBaseHardware = m_mainworker.GetHardware(iHardwareID);
528+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
541529
if (pBaseHardware == NULL)
542530
return;
543-
if (pBaseHardware->HwdType != HTYPE_BleBox)
544-
return;
545531
BleBox *pHardware = reinterpret_cast<BleBox*>(pBaseHardware);
546532

547533
root["status"] = "OK";
@@ -564,12 +550,9 @@ namespace http {
564550
(nodeid == "")
565551
)
566552
return;
567-
int iHardwareID = atoi(hwid.c_str());
568-
CDomoticzHardwareBase *pBaseHardware = m_mainworker.GetHardware(iHardwareID);
553+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
569554
if (pBaseHardware == NULL)
570555
return;
571-
if (pBaseHardware->HwdType != HTYPE_BleBox)
572-
return;
573556
BleBox *pHardware = reinterpret_cast<BleBox*>(pBaseHardware);
574557

575558
root["status"] = "OK";
@@ -586,14 +569,9 @@ namespace http {
586569
}
587570

588571
std::string hwid = request::findValue(&req, "idx");
589-
if (hwid == "")
590-
return;
591-
int iHardwareID = atoi(hwid.c_str());
592-
CDomoticzHardwareBase *pBaseHardware = m_mainworker.GetHardware(iHardwareID);
572+
CDomoticzHardwareBase *pBaseHardware = GetHardwareBase(hwid, HTYPE_BleBox);
593573
if (pBaseHardware == NULL)
594574
return;
595-
if (pBaseHardware->HwdType != HTYPE_BleBox)
596-
return;
597575
BleBox *pHardware = reinterpret_cast<BleBox*>(pBaseHardware);
598576

599577
root["status"] = "OK";

main/WebServer.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15748,5 +15748,19 @@ namespace http {
1574815748
"DELETE FROM UserSessions WHERE ExpirationDate < datetime('now', 'localtime')");
1574915749
}
1575015750

15751+
15752+
CDomoticzHardwareBase* CWebServer::GetHardwareBase(const std::string &HwId, const int Type)
15753+
{
15754+
if (HwId == "")
15755+
return NULL;
15756+
int iHardwareID = atoi(HwId.c_str());
15757+
CDomoticzHardwareBase *pHardware = m_mainworker.GetHardware(iHardwareID);
15758+
if (pHardware == NULL)
15759+
return NULL;
15760+
if (pHardware->HwdType != Type)
15761+
return NULL;
15762+
return pHardware;
15763+
}
15764+
1575115765
} //server
1575215766
}//http

main/WebServer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "../webserver/cWebem.h"
66
#include "../webserver/request.hpp"
77
#include "../webserver/session_store.hpp"
8+
#include "../DomoticzHardware.h"
89

910
struct lua_State;
1011
struct lua_Debug;
@@ -62,6 +63,8 @@ class CWebServer : public session_store, public boost::enable_shared_from_this<C
6263
void SBFSpotImportOldData(WebEmSession & session, const request& req, std::string & redirect_uri);
6364
void SetCurrentCostUSBType(WebEmSession & session, const request& req, std::string & redirect_uri);
6465

66+
CDomoticzHardwareBase* GetHardwareBase(const std::string &HwId, const int Type);
67+
6568
cWebem *m_pWebEm;
6669

6770
void ReloadCustomSwitchIcons();

0 commit comments

Comments
 (0)