void MQTT::SendDeviceInfo(const int m_HwdID, const unsigned long long DeviceRowIdx, const std::string &DeviceName, const unsigned char *pRXCommand) { boost::lock_guard<boost::mutex> l(m_mqtt_mutex); if (!m_IsConnected) return; std::vector<std::vector<std::string> > result; result = m_sql.safe_query("SELECT DeviceID, Unit, Name, [Type], SubType, nValue, sValue, SwitchType, SignalLevel, BatteryLevel, Options FROM DeviceStatus WHERE (HardwareID==%d) AND (ID==%llu)", m_HwdID, DeviceRowIdx); if (result.size() > 0) { std::vector<std::string> sd = result[0]; std::string did = sd[0]; int dunit = atoi(sd[1].c_str()); std::string name = sd[2]; int dType = atoi(sd[3].c_str()); int dSubType = atoi(sd[4].c_str()); int nvalue = atoi(sd[5].c_str()); std::string svalue = sd[6]; _eSwitchType switchType = (_eSwitchType)atoi(sd[7].c_str()); int RSSI = atoi(sd[8].c_str()); int BatteryLevel = atoi(sd[9].c_str()); std::map<std::string, std::string> options = m_sql.BuildDeviceOptions(sd[10]); Json::Value root; root["idx"] = DeviceRowIdx; root["id"] = did; root["unit"] = dunit; root["name"] = name; root["dtype"] = RFX_Type_Desc(dType,1); root["stype"] = RFX_Type_SubType_Desc(dType, dSubType); if (IsLightOrSwitch(dType, dSubType) == true) { root["switchType"] = Switch_Type_Desc(switchType); } // Add device options std::map<std::string, std::string>::const_iterator ittOptions; for (ittOptions = options.begin(); ittOptions != options.end(); ++ittOptions) { std::string optionName = ittOptions->first.c_str(); std::string optionValue = ittOptions->second.c_str(); root[optionName] = optionValue; } root["RSSI"] = RSSI; root["Battery"] = BatteryLevel; root["nvalue"] = nvalue; //give all svalues separate std::vector<std::string> strarray; StringSplit(svalue, ";", strarray); std::vector<std::string>::const_iterator itt; int sIndex = 1; for (itt = strarray.begin(); itt != strarray.end(); ++itt) { std::stringstream szQuery(""); szQuery << "svalue" << sIndex; root[szQuery.str()] = *itt; sIndex++; } std::string message = root.toStyledString(); if (m_publish_topics & PT_out) { SendMessage(TOPIC_OUT, message); } if (m_publish_topics & PT_floor_room) { result = m_sql.safe_query("SELECT F.Name, P.Name, M.DeviceRowID FROM Plans as P, Floorplans as F, DeviceToPlansMap as M WHERE P.FloorplanID=F.ID and M.PlanID=P.ID and M.DeviceRowID=='%llu'", DeviceRowIdx); for(size_t i=0 ; i<result.size(); i++) { std::vector<std::string> sd = result[i]; std::string floor = sd[0]; std::string room = sd[1]; std::stringstream topic(""); topic << TOPIC_OUT << "/" << floor << "/" + room; SendMessage(topic.str() , message); } } } }
void CHttpPush::DoHttpPush() { std::string httpUrl = ""; std::string httpData = ""; std::string httpHeaders = ""; int httpMethodInt = 0; int httpAuthInt = 0; std::string httpAuthBasicLogin = ""; std::string httpAuthBasicPassword = ""; m_sql.GetPreferencesVar("HttpMethod", httpMethodInt); m_sql.GetPreferencesVar("HttpAuth", httpAuthInt); m_sql.GetPreferencesVar("HttpAuthBasicLogin", httpAuthBasicLogin); m_sql.GetPreferencesVar("HttpAuthBasicPassword", httpAuthBasicPassword); int httpDebugActiveInt = 0; bool httpDebugActive = false; m_sql.GetPreferencesVar("HttpDebug", httpDebugActiveInt); if (httpDebugActiveInt == 1) { httpDebugActive = true; } std::vector<std::vector<std::string> > result; result = m_sql.safe_query( "SELECT A.DeviceID, A.DelimitedValue, B.ID, B.Type, B.SubType, B.nValue, B.sValue, A.TargetType, A.TargetVariable, A.TargetDeviceID, A.TargetProperty, A.IncludeUnit, B.SwitchType, strftime('%%s', B.LastUpdate), B.Name FROM HttpLink as A, DeviceStatus as B " "WHERE (A.DeviceID == '%" PRIu64 "' AND A.Enabled = '1' AND A.DeviceID==B.ID)", m_DeviceRowIdx); if (!result.empty()) { std::string sendValue; std::vector<std::vector<std::string> >::const_iterator itt; for (itt = result.begin(); itt != result.end(); ++itt) { m_sql.GetPreferencesVar("HttpUrl", httpUrl); m_sql.GetPreferencesVar("HttpData", httpData); m_sql.GetPreferencesVar("HttpHeaders", httpHeaders); if (httpUrl == "") return; std::vector<std::string> sd = *itt; //unsigned int deviceId = atoi(sd[0].c_str()); std::string sdeviceId = sd[0].c_str(); std::string ldelpos = sd[1].c_str(); int delpos = atoi(sd[1].c_str()); int dType = atoi(sd[3].c_str()); int dSubType = atoi(sd[4].c_str()); int nValue = atoi(sd[5].c_str()); std::string sValue = sd[6].c_str(); //int targetType = atoi(sd[7].c_str()); std::string targetVariable = sd[8].c_str(); //int targetDeviceID = atoi(sd[9].c_str()); //std::string targetProperty = sd[10].c_str(); int includeUnit = atoi(sd[11].c_str()); int metertype = atoi(sd[12].c_str()); int lastUpdate = atoi(sd[13].c_str()); std::string ltargetVariable = sd[8].c_str(); std::string ltargetDeviceId = sd[9].c_str(); std::string lname = sd[14].c_str(); sendValue = sValue; unsigned long tzoffset = get_tzoffset(); #ifdef WIN32 unsigned __int64 localTime = lastUpdate; unsigned __int64 localTimeUtc = lastUpdate - tzoffset; #else unsigned long long int localTime = lastUpdate; unsigned long long int localTimeUtc = lastUpdate - tzoffset; #endif char szLocalTime[21]; sprintf(szLocalTime, "%llu", localTime); char szLocalTimeUtc[21]; sprintf(szLocalTimeUtc, "%llu", localTimeUtc); char szLocalTimeMs[21]; sprintf(szLocalTimeMs, "%llu", localTime * 1000); char szLocalTimeUtcMs[21]; sprintf(szLocalTimeUtcMs, "%llu", localTimeUtc * 1000); std::string llastUpdate = get_lastUpdate(localTimeUtc); // Replace keywords /* %v : Value %t0 : Timestamp (epoc time localtime) %t1 : Timestamp (epoc ms localtime) %t2 : Timestamp (epoc time UTC) %t3 : Timestamp (epoc ms UTC) %t4 : Timestamp : "2015-01-29T21:50:44Z" %D : Target Device id %V : Target Variable %u : Unit %n : Device name %T0 : Type %T1 : SubType %h : hostname %idx : 'Original device' id (idx) */ std::string lunit = getUnit(delpos, metertype); std::string lType = RFX_Type_Desc(dType, 1); std::string lSubType = RFX_Type_SubType_Desc(dType, dSubType); char hostname[256]; gethostname(hostname, sizeof(hostname)); std::vector<std::string> strarray; if (sendValue.find(";") != std::string::npos) { StringSplit(sendValue, ";", strarray); if (int(strarray.size()) >= delpos && delpos > 0) { std::string rawsendValue = strarray[delpos - 1].c_str(); sendValue = ProcessSendValue(rawsendValue, delpos, nValue, false, dType, dSubType, metertype); } } else { sendValue = ProcessSendValue(sendValue, delpos, nValue, false, dType, dSubType, metertype); } ltargetDeviceId += "_"; ltargetDeviceId += ldelpos; replaceAll(httpUrl, "%v", sendValue); replaceAll(httpUrl, "%u", includeUnit ? lunit : ""); replaceAll(httpUrl, "%D", ltargetDeviceId); replaceAll(httpUrl, "%V", ltargetVariable); replaceAll(httpUrl, "%t0", std::string(szLocalTime)); replaceAll(httpUrl, "%t1", std::string(szLocalTimeMs)); replaceAll(httpUrl, "%t2", std::string(szLocalTimeUtc)); replaceAll(httpUrl, "%t3", std::string(szLocalTimeUtcMs)); replaceAll(httpUrl, "%t4", llastUpdate); replaceAll(httpUrl, "%n", lname); replaceAll(httpUrl, "%T0", lType); replaceAll(httpUrl, "%T1", lSubType); replaceAll(httpUrl, "%h", std::string(hostname)); replaceAll(httpUrl, "%idx", sdeviceId); replaceAll(httpData, "%v", sendValue); replaceAll(httpData, "%u", includeUnit ? lunit : ""); replaceAll(httpData, "%D", ltargetDeviceId); replaceAll(httpData, "%V", ltargetVariable); replaceAll(httpData, "%t0", std::string(szLocalTime)); replaceAll(httpData, "%t1", std::string(szLocalTimeMs)); replaceAll(httpData, "%t2", std::string(szLocalTimeUtc)); replaceAll(httpData, "%t3", std::string(szLocalTimeUtcMs)); replaceAll(httpData, "%t4", llastUpdate); replaceAll(httpData, "%n", lname); replaceAll(httpData, "%T0", lType); replaceAll(httpData, "%T1", lSubType); replaceAll(httpData, "%h", std::string(hostname)); replaceAll(httpData, "%idx", sdeviceId); if (sendValue != "") { std::string sResult; std::vector<std::string> ExtraHeaders; if (httpAuthInt == 1) { // BASIC authentication std::stringstream sstr; sstr << httpAuthBasicLogin << ":" << httpAuthBasicPassword; std::string m_AccessToken = base64_encode((const unsigned char *)(sstr.str().c_str()), strlen(sstr.str().c_str())); ExtraHeaders.push_back("Authorization:Basic " + m_AccessToken); } sendValue = CURLEncode::URLEncode(sendValue); // data if (httpDebugActive) { _log.Log(LOG_NORM, "HttpLink: sending global variable %s with value: %s", targetVariable.c_str(), sendValue.c_str()); } if (httpMethodInt == 0) { // GET if (!HTTPClient::GET(httpUrl, ExtraHeaders, sResult, true)) { _log.Log(LOG_ERROR, "HttpLink: Error sending data to http with GET!"); } } else if (httpMethodInt == 1) { // POST if (httpHeaders.size() > 0) { // Add additional headers std::vector<std::string> ExtraHeaders2; StringSplit(httpHeaders, "\r\n", ExtraHeaders2); for (size_t i = 0; i < ExtraHeaders2.size(); i++) { ExtraHeaders.push_back(ExtraHeaders2[i]); } } if (!HTTPClient::POST(httpUrl, httpData, ExtraHeaders, sResult, true, true)) { _log.Log(LOG_ERROR, "HttpLink: Error sending data to http with POST!"); } } else if (httpMethodInt == 2) { // PUT if (!HTTPClient::PUT(httpUrl, httpData, ExtraHeaders, sResult, true)) { _log.Log(LOG_ERROR, "HttpLink: Error sending data to http with PUT!"); } } // debug if (httpDebugActive) { _log.Log(LOG_NORM, "HttpLink: response %s", sResult.c_str()); } } } } }