void CDataPush::DoFibaroPush() { std::string fibaroIP = ""; std::string fibaroUsername = ""; std::string fibaroPassword = ""; int iIsVersion4 = 0; m_sql.GetPreferencesVar("FibaroIP", fibaroIP); m_sql.GetPreferencesVar("FibaroUsername", fibaroUsername); m_sql.GetPreferencesVar("FibaroPassword", fibaroPassword); m_sql.GetPreferencesVar("FibaroVersion4", iIsVersion4); int fibaroDebugActiveInt; bool fibaroDebugActive = false; m_sql.GetPreferencesVar("FibaroDebug", fibaroDebugActiveInt); if (fibaroDebugActiveInt == 1) { fibaroDebugActive = true; } bool bIsV4 = (iIsVersion4 != 0); if ( (fibaroIP == "") || (fibaroUsername == "") || (fibaroPassword == "") ) return; 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 FROM FibaroLink as A, DeviceStatus as B " "WHERE (A.DeviceID == '%llu' AND A.Enabled = '1' AND A.DeviceID==B.ID)", m_DeviceRowIdx); if (result.size()>0) { std::string sendValue; std::vector<std::vector<std::string> >::const_iterator itt; for (itt=result.begin(); itt!=result.end(); ++itt) { std::vector<std::string> sd=*itt; 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()); std::string lstatus=""; if ((targetType==0)||(targetType==1)) { if (delpos == 0) { int llevel=0; bool bHaveDimmer=false; bool bHaveGroupCmd=false; int maxDimLevel=0; GetLightStatus(dType,dSubType,(_eSwitchType)metertype,nValue,sValue,lstatus,llevel,bHaveDimmer,maxDimLevel,bHaveGroupCmd); sendValue = lstatus; } else if (delpos>0) { std::vector<std::string> strarray; if (sValue.find(";")!=std::string::npos) { StringSplit(sValue, ";", strarray); if (int(strarray.size())>=delpos) { std::string rawsendValue = strarray[delpos-1].c_str(); sendValue = ProcessSendValue(rawsendValue,delpos,nValue,includeUnit,metertype); } } } } else { // scenes/reboot, only on/off int llevel=0; bool bHaveDimmer=false; bool bHaveGroupCmd=false; int maxDimLevel=0; GetLightStatus(dType,dSubType, STYPE_OnOff,nValue,sValue,lstatus,llevel,bHaveDimmer,maxDimLevel,bHaveGroupCmd); sendValue = lstatus; } if (sendValue !="") { std::string sResult; std::stringstream sPostData; std::stringstream Url; std::vector<std::string> ExtraHeaders; sendValue = CURLEncode::URLEncode(sendValue); if (targetType==0) { Url << "http://" << fibaroUsername << ":" << fibaroPassword << "@" << fibaroIP << "/api/globalVariables"; if (bIsV4) Url << "/" << targetVariable; sPostData << "{\"name\": \"" << targetVariable << "\", \"value\": \"" << sendValue << "\"}"; if (fibaroDebugActive) { _log.Log(LOG_NORM,"FibaroLink: sending global variable %s with value: %s",targetVariable.c_str(),sendValue.c_str()); } if (!HTTPClient::PUT(Url.str(),sPostData.str(),ExtraHeaders,sResult)) { _log.Log(LOG_ERROR,"Error sending data to Fibaro!"); } } else if (targetType==1) { Url << "http://" << fibaroUsername << ":" << fibaroPassword << "@" << fibaroIP << "/api/callAction?deviceid=" << targetDeviceID << "&name=setProperty&arg1=" << targetProperty << "&arg2=" << sendValue; if (fibaroDebugActive) { _log.Log(LOG_NORM,"FibaroLink: sending value %s to property %s of virtual device id %d",sendValue.c_str(),targetProperty.c_str(),targetDeviceID); } if (!HTTPClient::GET(Url.str(),sResult)) { _log.Log(LOG_ERROR,"Error sending data to Fibaro!"); } } else if (targetType==2) { if (((delpos==0)&&(lstatus=="Off"))||((delpos==1)&&(lstatus=="On"))) { Url << "http://" << fibaroUsername << ":" << fibaroPassword << "@" << fibaroIP << "/api/sceneControl?id=" << targetDeviceID << "&action=start"; if (fibaroDebugActive) { _log.Log(LOG_NORM,"FibaroLink: activating scene %d",targetDeviceID); } if (!HTTPClient::GET(Url.str(),sResult)) { _log.Log(LOG_ERROR,"Error sending data to Fibaro!"); } } } else if (targetType==3) { if (((delpos==0)&&(lstatus=="Off"))||((delpos==1)&&(lstatus=="On"))) { Url << "http://" << fibaroUsername << ":" << fibaroPassword << "@" << fibaroIP << "/api/settings/reboot"; if (fibaroDebugActive) { _log.Log(LOG_NORM,"FibaroLink: reboot"); } if (!HTTPClient::POST(Url.str(),sPostData.str(),ExtraHeaders,sResult)) { _log.Log(LOG_ERROR,"Error sending data to Fibaro!"); } } } } } } }
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()); } } } } }