Skip to content

Commit ae93fe3

Browse files
author
Corbin Munce
committed
changed to only listen to port 9898 once
1 parent 74bf66f commit ae93fe3

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

hardware/XiaomiGateway.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,9 @@ void XiaomiGateway::InsertUpdateSwitch(const std::string &nodeid, const std::str
386386

387387
//check if this switch is already in the database
388388
std::vector<std::vector<std::string> > result;
389-
//result = m_sql.safe_query("SELECT nValue FROM DeviceStatus WHERE (HardwareID==%d) AND (DeviceID=='%q') AND (Type==%d) ", m_HwdID, ID.c_str(), pTypeGeneralSwitch);
389+
result = m_sql.safe_query("SELECT nValue FROM DeviceStatus WHERE (HardwareID==%d) AND (DeviceID=='%q') AND (Type==%d) ", m_HwdID, ID.c_str(), pTypeGeneralSwitch);
390390
//ignore the hardwareid to handle deviceids over multiple gateways - this will cause a problem if another general switch device has the same deviceid
391-
result = m_sql.safe_query("SELECT nValue, LastLevel FROM DeviceStatus WHERE (DeviceID=='%q') AND (Type==%d) ", ID.c_str(), pTypeGeneralSwitch);
391+
//result = m_sql.safe_query("SELECT nValue, LastLevel FROM DeviceStatus WHERE (DeviceID=='%q') AND (Type==%d) ", ID.c_str(), pTypeGeneralSwitch);
392392
if (result.size() < 1)
393393
{
394394
_log.Log(LOG_STATUS, "XiaomiGateway: New Device Found (%s)", str.c_str());
@@ -510,9 +510,26 @@ bool XiaomiGateway::StartHardware()
510510

511511
//check there is only one instance of the Xiaomi Gateway
512512
std::vector<std::vector<std::string> > result;
513-
result = m_sql.safe_query("SELECT Password, Address, ID FROM Hardware WHERE Type=%d AND ID=%d", HTYPE_XiaomiGateway, m_HwdID);
513+
//result = m_sql.safe_query("SELECT Password, Address, ID FROM Hardware WHERE Type=%d AND ID=%d", HTYPE_XiaomiGateway, m_HwdID);
514+
result = m_sql.safe_query("SELECT Password, Address, ID FROM Hardware WHERE Type=%d", HTYPE_XiaomiGateway);
514515
if (result.size() > 0) {
516+
int lowestId = 9999;
517+
int Id = 0;
518+
for (int i = 0; i < result.size(); i++) {
519+
Id = atoi(result[i][2].c_str());
520+
//_log.Log(LOG_STATUS, "XiaomiGateway: checking hardware id %d", Id);
521+
if (Id < lowestId) {
522+
lowestId = Id;
523+
}
524+
}
515525
m_ListenPort9898 = true;
526+
if (lowestId != m_HwdID) {
527+
//_log.Log(LOG_STATUS, "XiaomiGateway: %d != %d", lowestId, m_HwdID);
528+
m_ListenPort9898 = false;
529+
}
530+
else {
531+
_log.Log(LOG_STATUS, "XiaomiGateway: will listen on 9898 for hardware id %d", m_HwdID);
532+
}
516533
//retrieve the gateway key
517534
m_GatewayPassword = result[0][0].c_str();
518535
m_GatewayIp = result[0][1].c_str();
@@ -568,7 +585,10 @@ void XiaomiGateway::Do_Work()
568585
}
569586

570587
XiaomiGateway::xiaomi_udp_server udp_server(io_service, m_HwdID, m_GatewayIp, m_LocalIp, this);
571-
boost::thread bt(boost::bind(&boost::asio::io_service::run, &io_service));
588+
boost::thread bt;
589+
if (m_ListenPort9898) {
590+
bt = boost::thread(boost::bind(&boost::asio::io_service::run, &io_service));
591+
}
572592

573593
int sec_counter = 0;
574594
while (!m_stoprequested)

0 commit comments

Comments
 (0)