Commit 2e5a7b62 authored by 杨熙's avatar 杨熙

设备基本功能完成,待完善升级,自定义topic,PIPO的topic

parent 9523bf65
......@@ -243,7 +243,7 @@ void MQ_CCU_Adapter::onRecvMsg(string nodeId, string opcode, EH::Json::Value arg
} else if (opcode == "CLOSE_NET_CHANNEL"&& status == "success") { //关闭组网
KKdiscover =false;//
} else if (opcode == "CENTRAL_AC_INDOOR_UNIT_STATUS"||opcode == "CENTRAL_AC_INDOOR_UNIT_SETTING") { //中央空调内机
kk_handleDeviceInfo::GetInstance()->handleCentralACStatusPush(hwSubDevs,indoorunitsId2AddrMap,pushTopic,nodeId, arg,opcode);
// kk_handleDeviceInfo::GetInstance()->handleCentralACStatusPush(hwSubDevs,indoorunitsId2AddrMap,pushTopic,nodeId, arg,opcode);
}
}catch (exception &e) {
......@@ -254,52 +254,63 @@ void MQ_CCU_Adapter::onRecvMsg(string nodeId, string opcode, EH::Json::Value arg
//接收云端下发的消息
void MQ_CCU_Adapter::onCloudMsgRecv(string payload,string topicName){
try{
LOGI("onCloudMsgRecv is: %s topicName: %s\n",payload.c_str(),topicName.c_str());
EH::Json::Value jsonObject;
EH::Json::Reader reader;
if (!reader.parse(payload, jsonObject) || !jsonObject.isObject()) {
LOGW("Message format is wrong.primary msg is: %s\n",payload.c_str());
return;
}
vector<string> chanMac;
Split(topicName,"/",chanMac);
string Sub_Topic = chanMac[3];
if(Sub_Topic == "publishstate"){ //获取设备信息列表
LOGI("onCloudMsgRecv is:{} topicName: %s\n",topicName.c_str());
kk_handleDeviceInfo::GetInstance()->onGetDevService(hwSubDevs,controllerModel,pushTopic,gw_id);
}else if(Sub_Topic == "commands"){ //下发控制指令
LOGI("onCloudMsgRecv is: %s topicName: %s\n",payload.c_str(),topicName.c_str());
EH::Json::Value jsonObject;
EH::Json::Reader reader;
if (!reader.parse(payload, jsonObject) || !jsonObject.isObject()) {
LOGW("Message format is wrong.primary msg is: %s\n",payload.c_str());
return;
}
//判别控制指令
if(jsonObject["method"] =="prepare_join_gw"){ //添加设备的界面下准备命令
if(Sub_Topic == "devices_public"){ //13. 获取设备列表信息
kk_handleDeviceInfo::GetInstance()->onGetDevService(hwSubDevs,controllerModel,pushTopic);
}else if(Sub_Topic == "device"){ //子设备
if(chanMac[4] == "delete"){ //3. 删除子设备
string sn = jsonObject["mac"].asString();
onDeviceRequestDelete(sn);
}else if(chanMac[4] == "search"){ //2. 搜索子设备
on_Query_GateWay_Infomation();
}else if(jsonObject["method"] =="enable_join_gw"){ //打开网关允许加网模式
onDeviceRequestJoin(true);
}else if(jsonObject["method"] =="disable_join_gw"){ //关闭网关允许加网模式
}else if(chanMac[4] == "stopsearch"){ //20. 停止搜索子设备
onDeviceRequestJoin(false);
}else if(jsonObject["method"] =="delete_device"){ //删除子设备
string sn = jsonObject["dev_id"].asString();
onDeviceRequestDelete(sn);
}else if(jsonObject["method"] =="unbind_gateway"){ //解绑网关-删除所有子设备
string device_name = jsonObject["device_name"].asString();
string client_id = jsonObject["client_id"].asString();
string method = jsonObject["method"].asString();
on_unbind_gateway(device_name,client_id,method);
}else if(jsonObject["method"] =="unbind_gateway.keep"){ //解绑网关-不删除所有子设备
string device_name = jsonObject["device_name"].asString();
string client_id = jsonObject["client_id"].asString();
string method = jsonObject["method"].asString();
on_unbind_gateway(device_name,client_id,method);
}else if(jsonObject["method"] =="control_device"){ //控制设备状态
string sn = jsonObject["dev_id"].asString();
string channel = IntToString(jsonObject["dev_ep_id"].asInt());
string svcid = IntToString(jsonObject["cmd_type"].asInt());
EH::Json::Value cmd = jsonObject["cmd"];
kk_handleDeviceInfo::GetInstance()->onSetDevService(hwSubDevs,sn,channel,svcid,cmd); //下发控制命令
}else{
LOGW("recv unknown payload:%s.\n",jsonObject["method"].asString().c_str());
}else if(chanMac[4] == "set"){ //6. 改变子设备状态
string sn = jsonObject["mac"].asString();
EH::Json::Value cmd = jsonObject["datapoints"];
kk_handleDeviceInfo::GetInstance()->onSetDevService(hwSubDevs,sn,cmd); //下发控制命令
}
}else if(Sub_Topic == "cg_custom_msg"){ //16. XCG发送自定义数据到PTP
}else if(Sub_Topic == "firmware"){ //18. 查询最新的升级任务结果 "/firmware/check/result";
}else{
LOGW("recv unknown Topic:%s.\n",Sub_Topic.c_str());
}
// else if(Sub_Topic == "commands"){ //下发控制指令
// //判别控制指令
// if(jsonObject["method"] =="prepare_join_gw"){ //添加设备的界面下准备命令
// on_Query_GateWay_Infomation();
// }else if(jsonObject["method"] =="disable_join_gw"){ //关闭网关允许加网模式
// onDeviceRequestJoin(false);
// }else if(jsonObject["method"] =="unbind_gateway"){ //解绑网关-删除所有子设备
// string device_name = jsonObject["device_name"].asString();
// string client_id = jsonObject["client_id"].asString();
// string method = jsonObject["method"].asString();
// on_unbind_gateway(device_name,client_id,method);
// }else if(jsonObject["method"] =="unbind_gateway.keep"){ //解绑网关-不删除所有子设备
// string device_name = jsonObject["device_name"].asString();
// string client_id = jsonObject["client_id"].asString();
// string method = jsonObject["method"].asString();
// on_unbind_gateway(device_name,client_id,method);
// }
// }
} catch (exception &e) {
LOGW("%s.\n", e.what());
}
......@@ -398,10 +409,10 @@ void MQ_CCU_Adapter::handleGetDevsHardwareInfo(EH::Json::Value arg){
}
}
//子设备的在线离线
void MQ_CCU_Adapter::handleDevOnlinePush(EH::Json::Value arg){
try {
string respTopic_1 = pushTopic +"/deviceonline";//子设备在线
string respTopic_2 = pushTopic +"/deviceoffline";//子设备离线
string respTopic_1 = pushTopic +"/device/sync";//子设备在线
string sn = Replace(arg["mac"].asString(),":","");
map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(sn);
if(subDevIt != hwSubDevs.end()){
......@@ -409,16 +420,16 @@ void MQ_CCU_Adapter::handleDevOnlinePush(EH::Json::Value arg){
int product_id = arg["product_id"].asInt();
if(onlinestatus == 2){
EH::Json::Value payload;
payload["dev_id"] = sn; //请求发起者
payload["dev_type"] = subDevIt->second->dev_type;
payload["gw_id"] = gw_id;
sendtoRoma(payload,respTopic_2); //推送新设备到Roma //在线状态的推送 ,待更改 2020.10.15.9
payload["mac"] = sn;
payload["product_id"] = subDevIt->second->dev_model_id;
payload["state"] = 3; //设备无响应即离线
sendtoRoma(payload,respTopic_1); //推送新设备到Roma //在线状态的推送 ,待更改 2020.10.15.9
subDevIt->second->online = false;
}else{
EH::Json::Value payload;
payload["dev_id"] = sn; //请求发起者
payload["dev_type"] = subDevIt->second->dev_type;
payload["gw_id"] = gw_id;
payload["mac"] = sn;
payload["product_id"] = subDevIt->second->dev_model_id;
payload["state"] = 1; //设备在线
sendtoRoma(payload,respTopic_1); //推送新设备到Roma
subDevIt->second->online = true;
}
......@@ -478,9 +489,9 @@ void MQ_CCU_Adapter::handleSyncInfo(EH::Json::Value arg,string opcode) {
}
}
//添加到房间的中央空调网关信息同步
EH::Json::Value CentralAcGwsDevices = arg["central_ac_gws"];
SYNC_handle_AcGw_Devices(CentralAcGwsDevices);
// //添加到房间的中央空调网关信息同步
// EH::Json::Value CentralAcGwsDevices = arg["central_ac_gws"];
// SYNC_handle_AcGw_Devices(CentralAcGwsDevices);
//添加到房间的中央空调内机信息同步
EH::Json::Value CentralAcindoorunits = arg["central_ac_indoorunits"];
......@@ -489,7 +500,7 @@ void MQ_CCU_Adapter::handleSyncInfo(EH::Json::Value arg,string opcode) {
int infocome = 1 ;
string devroomid = "1" ;//临时默认
string devname = "内机";
handleACindoor(devroomid,devname,indoorunitsCount,CentralAcindoorunits,infocome);
// handleACindoor(devroomid,devname,indoorunitsCount,CentralAcindoorunits,infocome);
}
}catch (exception &e) {
LOGW("%s.\n", e.what());
......@@ -564,7 +575,7 @@ void MQ_CCU_Adapter::handleNewDevice(EH::Json::Value devices, EH::Json::Value ce
EH::Json::Value devProductinfo = kk_handleDeviceInfo::GetInstance()->XiaoK_Zu_Wang(Joinproductid);
kk_handleDeviceInfo::HwSubDev* dev = new kk_handleDeviceInfo::HwSubDev();
dev->dev_model_id = devProductinfo["dev_model_id"].asString();
dev->dev_type = devProductinfo["dev_type"].asString();
// dev->dev_type = devProductinfo["dev_type"].asString();
dev->mac = mac;
dev->operatetype = operateType;
dev->sn = devsn;
......@@ -588,223 +599,16 @@ void MQ_CCU_Adapter::handleNewDevice(EH::Json::Value devices, EH::Json::Value ce
//deleteCCuDevices(ccuDeleteMacs); //调试
ccuDeleteMacs.clear();
if(indoorunitsdevCount > 0){//有内机的话,单独进行处理
int infocome = 0 ;//infocome = 0为新设备列表里的
handleACindoor(devroomid,devname,indoorunitsdevCount,centralACdevices,infocome);
}
//2021.3.34 当前未接入空调,先注释
// if(indoorunitsdevCount > 0){//有内机的话,单独进行处理
// int infocome = 0 ;//infocome = 0为新设备列表里的
// handleACindoor(devroomid,devname,indoorunitsdevCount,centralACdevices,infocome);
// }
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
}
//开始处理空调内机
void MQ_CCU_Adapter::handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome){
try {
LOGD("开始处理空调内机\n");
int error;
map<string,kk_handleDeviceInfo::HwSubDev*> toAddSubDevs;//需要添加到华为的设备信息。
controllerModel.clear();
for (int i = 0; i < indoorunitsdevCount; i++) { ////中央空调内机数目
string channel= "1";
EH::Json::Value centralAC = centralACdevices[i];
string indoorunitId = centralAC["id"].asString();
string indoorunitAddr = centralAC["indooruint_addr"].asString();
string centralACGwId;//中央空调网关设备 ID,注:非 zigbee 设备 ID 体系
EH::Json::Value indoorunitStatus;//内机当前的状态
if(infocome == 0){ //infocome = 0为新设备列表里的,=1表示为添加到房间里了。
centralACGwId = centralAC["bind_central_ac_gw_node_id"].asString();//
indoorunitStatus = centralAC["status"];
}else if(infocome == 1){
centralACGwId = centralAC["central_ac_gw_devid"].asString();//中央空调网关设备 ID,注:非 zigbee 设备 ID 体系
devroomid = centralAC["room_id"].asString(); //房间号
indoorunitStatus = centralAC["indoorunit_state"]; //内机当前的状态
devname =centralAC["indoorunit_name"].asString(); //内机的名称,不同步到华为。
}
string centralAcGwNodeId; //这个内机对应的空调网关ID
string centralACGwMac; //这个内机对应的空调网关Mac
string centralACGwoperateType; //这个内机对应的空调网关类型
string centralACindoorMac; //这个内机mac为空调网关Mac+id
string centralACindoorsn; //这个内机sn为空调网关Mac+id 去除“:”
//1。 这个内机对应的空调网关ID
LOGD("centralACGwId:%s,indoorunitAddr:%s,indoorunitnodeId:%s \n",centralACGwId.c_str(),indoorunitAddr.c_str(),indoorunitId.c_str());
map<string,string>::iterator gwIt = gwId2NodeId.find(centralACGwId);
if(gwIt == gwId2NodeId.end()){
LOGW("centralACGwId:%s not in gwId2NodeId.\n",centralACGwId.c_str());
continue;
} else {
centralAcGwNodeId = gwIt->second;
}
//2。 这个内机对应的空调网关类型
map<string,string>::iterator operateIt = operate2NodeId.find(centralAcGwNodeId);
if(operateIt == operate2NodeId.end()){
LOGW("centralAcGwNodeId:%s not in operate2NodeId.\n",centralAcGwNodeId.c_str());
continue;
} else {
centralACGwoperateType = "-13"; //情景模式里的内机operateId
}
//3。 这个内机对应的空调网关Mac
map<string,string>::iterator centralAcNodeIt = nodeId2channelMap.find(centralAcGwNodeId);
if(centralAcNodeIt == nodeId2channelMap.end()){
LOGW("nodeid:%s not in nodeId2channelMap.\n",centralAcGwNodeId.c_str());
continue;
} else {
vector<string> chanMac;
Split(centralAcNodeIt->second,"_",chanMac);
centralACGwMac = chanMac[0];
}
indoorunitsId2AddrMap[indoorunitId] = centralACGwMac+"_"+indoorunitAddr; //网关与内机通道id之间的对应关系
LOGD("indoorunitId:%s .\n",indoorunitId.c_str());
if(StringToInt(indoorunitAddr,error)<10){
centralACindoorMac = centralACGwMac+"0"+indoorunitAddr; //*****************内机的mac构成关系
}else{
centralACindoorMac = centralACGwMac+indoorunitAddr;
}
centralACindoorsn = Replace(centralACindoorMac,":",""); //*****************内机的sn
if(!ccuDeleteMacs.empty() && ccuDeleteMacs.find(centralACGwMac)!=ccuDeleteMacs.end()){
continue;
}
map<string,vector<string> >::iterator numIt = controllerModel.find(centralACGwMac);//说明待定
if (numIt == controllerModel.end()) {
vector<string> addrId;
addrId.push_back(centralACindoorsn);
controllerModel[centralACGwMac] = addrId;
} else {
numIt->second.push_back(centralACindoorsn);
}
map<string,kk_handleDeviceInfo::KKDevmapinfo>::iterator it = hwBindDevMap.find(centralACindoorMac);
if(it!=hwBindDevMap.end()){
kk_handleDeviceInfo::KKDevmapinfo tripInfo = it->second;
LOGD("centralACGwMac:%s in hwBindDevMap \n",centralACindoorMac.c_str());
handleCcuDeviceToHwSubDev(centralACGwoperateType,indoorunitId,centralACindoorMac,channel,tripInfo.sn,indoorunitStatus,devroomid,devname);
}else{
if(toAddSubDevs.find(centralACindoorMac) == toAddSubDevs.end()){
kk_handleDeviceInfo::HwSubDev* dev = new kk_handleDeviceInfo::HwSubDev();
dev->dev_model_id = DAIKINCentralACindoor_PID; //内机 对应的华为prodID
dev->dev_type = DAIKINCentralACindoor_PID;
dev->mac = centralACindoorMac;
dev->operatetype = centralACGwoperateType;
dev->sn = Replace(centralACindoorMac,":","");
kk_handleDeviceInfo::KKChannelDev* chanDev = kk_handleDeviceInfo::GetInstance()->buildChannelDevice(centralACGwoperateType,indoorunitId,channel,indoorunitStatus,devroomid,devname);
map<string,kk_handleDeviceInfo::KKChannelDev*> channelDevs;
if(chanDev!=NULL){
channelDevs[channel] = chanDev;
}
dev->channelDevs = channelDevs;
toAddSubDevs[centralACindoorMac] = dev;
LOGD("\n found JoinDevProductkey for dev:%s,operateid:%s,channel:%s,close zigbee net now. \n",
centralACindoorMac.c_str(),centralACGwoperateType.c_str(), channel.c_str());
}
}
}
// 空调网关数量及内机地址推送
// map<string, string> hwCentralAcList;
// for(map<string, string>::iterator addrIt = indoorunitsId2AddrMap.begin(); addrIt!=indoorunitsId2AddrMap.end(); addrIt++){
// vector<string> macChan;
// Split(addrIt->second,"_", macChan);
// EH::Json::Value batchArg;
// hwCentralAcList[macChan[0]] = macChan[1];
// }
if( toAddSubDevs.size() > 0){
discoveryEnable = true;
KKdiscover = true;
LOGD(" discoveryEnable:%d,KKdiscover:%d\n",discoveryEnable,KKdiscover);
}
addCCuDevicesToCloud(toAddSubDevs);
LOGD("after handle NEW_DEVICES,total hw dev count is:%d \n",hwSubDevs.size());
//推送内机数目
for(map<string, string>::iterator gwIt = gwId2NodeId.begin(); gwIt!=gwId2NodeId.end(); gwIt++){
map<string,string>::iterator nodeIt = nodeId2channelMap.find(gwIt->second);
if(nodeIt == nodeId2channelMap.end()){
LOGW("gwId:%s not in gwId2NodeId, affect Internal machine push.\n",gwIt->second.c_str());
continue;
}
vector<string> chanMac;
Split(nodeIt->second,"_",chanMac);
string sn = Replace(chanMac[0],":","");
string channel = chanMac[1];
map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(sn);
if(subDevIt == hwSubDevs.end()){
LOGW("dev:%s not in hwSubDevs.\n",sn.c_str());
continue;
} else {
string dev_model_id = subDevIt->second->dev_model_id;
string devType = subDevIt->second->devTypeId;
EH::Json::Value payload;
EH::Json::Value msgstate0;
payload["gw_id"] = EH_ToolKit::GetMacNoSpace(); //请求发起者
payload["dev_id"] = sn;
payload["dev_type"] = subDevIt->second->dev_model_id;
payload["dev_ep_id"] = StringToInt(channel,error); //通道
EH::Json::Value msgstate2;
string respTopic = pushTopic+string("/devicestatechange");//设备状态变化上报
EH::Json::Value array;
map<string,vector<string> >::iterator numIt = controllerModel.find(subDevIt->second->mac);
if (numIt != controllerModel.end()) {
for (vector<string>::iterator it = numIt->second.begin(); it != numIt->second.end(); it++) {
EH::Json::Value idJson;
idJson["sn"] = *it;
array.append(idJson);
}
msgstate2["deviceList"] = array;
} else {
msgstate2["deviceList"].resize(0);//空数组
}
payload["state"] = msgstate2;
payload["dev_ep_id"] = 3; //功能映射为通道3
MQ_CCU_Adapter::GetInstance()->sendtoRoma(payload,respTopic);
}
}
//deleteCCuDevices(ccuDeleteMacs); //调试
ccuDeleteMacs.clear();
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
}
void MQ_CCU_Adapter::SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevices){ //SYNC_INFO 处理中央空调网关
try {
for (int i = 0; i < CentralAcGwsDevices.size(); i++) {
EH::Json::Value Gwdevice = CentralAcGwsDevices[i];
string operateType = "12001";//中央空调网关
string gwId = Gwdevice["id"].asString();
string nodeid = Gwdevice["bind_central_ac_gw_node_id"].asString();
gwId2NodeId[gwId] = nodeid;
NodeId2gwId[nodeid] = gwId;
LOGD("add ccu system gwId:%s, gwNodeId:%s \n",gwId.c_str() ,nodeid.c_str());
operate2NodeId[nodeid] = operateType;
string mac = "未接入以后再改";
// string mac = KK_CcuConfig::GetInstance()->getCentralAcGwMac(nodeid);
string channel = "1";
string devroomid = Gwdevice["room_id"].asString(); //房间号
string devname = Gwdevice["dev_name"].asString(); //设备的名称
nodeId2channelMap[nodeid] = mac+"_"+channel;
EH::Json::Value status = "0"; //设备的状态 Gwdevice["status"].asString()"status": {"gwStatusCode": -1,"online": false}
map<string,kk_handleDeviceInfo::KKDevmapinfo>::iterator devIt = hwBindDevMap.find(mac);
if(devIt!=hwBindDevMap.end()){
kk_handleDeviceInfo::KKDevmapinfo tripInfo = devIt->second;
handleCcuDeviceToHwSubDev(operateType,nodeid,mac,channel,tripInfo.sn,status,devroomid,devname);
}else{
LOGW("ccu dev:%s not found bind info in lcoal cache.delete fron ccu later. \n",mac.c_str());
}
}
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
}
void MQ_CCU_Adapter::handleCcuDeviceToHwSubDev(string operateType,string nodeid,string mac,
string channel,string sn,EH::Json::Value status,string devroomid,string devname){
......@@ -817,7 +621,7 @@ void MQ_CCU_Adapter::handleCcuDeviceToHwSubDev(string operateType,string nodeid,
map<string,kk_handleDeviceInfo::KKDevmapinfo>::iterator devIt = hwBindDevMap.find(mac);
kk_handleDeviceInfo::KKDevmapinfo tripInfo = devIt->second;
dev->dev_model_id = tripInfo.dev_model_id;
dev->dev_type = tripInfo.dev_type;
// dev->dev_type = tripInfo.dev_type;
dev->sn = sn;
dev->mac = mac;
dev->operatetype = operateType;
......@@ -861,64 +665,61 @@ void MQ_CCU_Adapter::handleCcuDeviceToHwSubDev(string operateType,string nodeid,
void MQ_CCU_Adapter::addCCuDevicesToCloud(map<string,kk_handleDeviceInfo::HwSubDev*> toAddSubDevs){
try {
LOGI("prepare to add %d new devices to Roma cloud.\n",toAddSubDevs.size());
string respTopic ;
respTopic= pushTopic + string("/devicejoined");//子设备入网
if(toAddSubDevs.empty()){
return ;
}
set<string> addFailedDevs;
for(map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = toAddSubDevs.begin();it!=toAddSubDevs.end();it++){
int ret;
EH::Json::Value payload;
string devMac = it->first;
string dev_model_id = it->second->dev_model_id;
LOGI("prepare to add dev: %s ,dev_model_id: %s. \n",devMac.c_str(),dev_model_id.c_str());
it->second->online = true;
mtx.acquire();
string devSn = it->second->sn;
//是否已经加入映射列表里
map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(devSn);
if(subDevIt != hwSubDevs.end() && (dev_model_id == DAIKINCentralACindoor_PID||dev_model_id == DAIKINCentralACgw_PID)){//空调内机,重复推送入网会有问题,改为推上线。
respTopic= pushTopic + string("/deviceonline");
payload["gw_id"] = gw_id; //请求发起者
payload["dev_id"] = devSn;
payload["dev_type"] = it->second->dev_model_id;
}else{
payload["gw_id"] = gw_id; //请求发起者
payload["dev_id"] = devSn;
payload["dev_type"] = it->second->dev_model_id;
payload["dev_model_id"] = it->second->dev_type;
LOGI("prepare to add %d new devices to Roma cloud.\n",toAddSubDevs.size());
string respTopic ;
respTopic= pushTopic + string("/device/new");//子设备入网
if(toAddSubDevs.empty()){
return ;
}
ret = sendtoRoma(payload,respTopic); //推送新设备到Roma
if(ret == 0){
LOGI("add device:%s,dev_model_id:%s,to Roma cloud success!!!\n",devMac.c_str(),dev_model_id.c_str());
//添加到映射列表里
hwSubDevs[devSn] = it->second; //add to cache;
LOGD("add to cache devSn :%s.\n", devSn.c_str());
// if(discoveryEnable == true && KKdiscover == true){//小K 华为组网时,自动添加设备到特定房间里。
LOGD(" 添加到房间 discoveryEnable:%d , prodId:%d\n",discoveryEnable,KKdiscover);
kk_handleDeviceInfo::GetInstance()->addCCuDevicesToRoom(it,NodeId2gwId);
// }
mtx.release();
if(dev_model_id == DAIKINCentralACindoor_PID){
continue; //使得内机都可以添加进入
set<string> addFailedDevs;
for(map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = toAddSubDevs.begin();it!=toAddSubDevs.end();it++){
int ret;
EH::Json::Value payload;
string devMac = it->first;
string dev_model_id = it->second->dev_model_id;
LOGI("prepare to add dev: %s ,dev_model_id: %s. \n",devMac.c_str(),dev_model_id.c_str());
it->second->online = true;
mtx.acquire();
string devSn = it->second->sn;
//是否已经加入映射列表里
map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(devSn);
if(subDevIt != hwSubDevs.end() && (dev_model_id == DAIKINCentralACindoor_PID||dev_model_id == DAIKINCentralACgw_PID)){//空调内机,重复推送入网会有问题,改为推上线。
// respTopic= pushTopic + string("/deviceonline");
// payload["mac"] = devSn;
// payload["product_id"] = it->second->dev_model_id;
}else{
payload["mac"] = devSn;
payload["product_id"] = it->second->dev_model_id;
}
ret = sendtoRoma(payload,respTopic); //推送新设备到Roma
if(ret == 0){
LOGI("add device:%s,dev_model_id:%s,to Roma cloud success!!!\n",devMac.c_str(),dev_model_id.c_str());
//添加到映射列表里
hwSubDevs[devSn] = it->second; //add to cache;
LOGD("add to cache devSn :%s.\n", devSn.c_str());
// if(discoveryEnable == true && KKdiscover == true){//小K 华为组网时,自动添加设备到特定房间里。
LOGD(" 添加到房间 discoveryEnable:%d , prodId:%d\n",discoveryEnable,KKdiscover);
kk_handleDeviceInfo::GetInstance()->addCCuDevicesToRoom(it,NodeId2gwId);
// }
mtx.release();
if(dev_model_id == DAIKINCentralACindoor_PID){
continue; //使得内机都可以添加进入
}
client->sendRequest(gw_NodeId, "CLOSE_NET_CHANNEL", "*", "HJ_Server");
this->discoveryEnable = false;
}else{
hwSubDevs.erase(devSn);
mtx.release();
addFailedDevs.insert(devMac);
LOGE("add dev:%s to hw cloud failed.\n",devMac.c_str());
client->sendRequest(gw_NodeId, "CLOSE_NET_CHANNEL", "*", "HJ_Server");
this->discoveryEnable = false;
}else{
hwSubDevs.erase(devSn);
mtx.release();
addFailedDevs.insert(devMac);
LOGE("add dev:%s to hw cloud failed.\n",devMac.c_str());
}
}
}
this->discoveryEnable = false;
LOGD("discoveryEnable:%d,\n",discoveryEnable);
this->discoveryEnable = false;
LOGD("discoveryEnable:%d,\n",discoveryEnable);
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
......@@ -957,14 +758,16 @@ void MQ_CCU_Adapter::oninitmqttsdk() {
username = "123456";
//订阅的 Topic
pushTopic = string("$xlink/ptp/{")+gw_id+"}"; //ptpo运维
subTopic = string("$xlink/ptpo/{")+gw_id+"}"; //ptp交互
pushTopic = string("$xlink/ptp/")+gw_id; //ptpo运维
subTopic = string("$xlink/ptp/")+gw_id; //ptp交互
willTopic = string("xChfVJ501599/out/")+username+"/heartbeat"; //遗嘱信息
heartbeat_Topic = string("$xlink/ptp/{")+gw_id+"}/ptp_keepalive"; //心跳
heartbeat_Topic = string("$xlink/ptp/")+gw_id+"/ptp_keepalive"; //心跳
LOGD("pushTopic is: %s subTopic is: %s willTopic is: %s\n",pushTopic.c_str(),subTopic.c_str(),willTopic.c_str());
// int ret = mqttSdk->init("172.25.240.31:1983", //临时调试环境
// clientId,"SmartMonitor","k9CCGgJZlkKhVottLvN0EVBOMc1sCC",willTopic,heartbeat_Topic,gw_id); //与云智易的XCG建立连接除了IP以外都不需要。
int ret = mqttSdk->init("127.0.0.1:23883", //正式环境
clientId,"2CEDC38940644E439649389C08329B09","}A12a#RM^r)iXy/?|sHW:z3!",willTopic,heartbeat_Topic,gw_id);
clientId,"2CEDC38940644E439649389C08329B09","}A12a#RM^r)iXy/?|sHW:z3!",willTopic,heartbeat_Topic,gw_id); //与云智易的XCG建立连接除了IP以外都不需要。
mqttOnline = false;
if(ret == 0){
mqttSdk->con(1,3);
......@@ -977,7 +780,7 @@ void MQ_CCU_Adapter::oninitmqttsdk() {
// sleep(1);
// goto loop;
}
// }
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
......@@ -1041,8 +844,8 @@ int MQ_CCU_Adapter::on_unbind_gateway(string device_name,string client_id,string
LOGD(" 解绑网关 unbind_gateway SendOk :%d.\n",SendOk);
//删除主机内所有设备
if(method == "unbind_gateway"){
int Del_Devs = deleteCCuDevices();
LOGD("删除主机内所有设备 :%d.\n",Del_Devs);
// int Del_Devs = deleteCCuDevices();
// LOGD("删除主机内所有设备 :%d.\n",Del_Devs);
}
if(SendOk == 0){
//程序重启
......@@ -1063,109 +866,109 @@ int MQ_CCU_Adapter::on_unbind_gateway(string device_name,string client_id,string
return -1;
}
//删除所有设备
int MQ_CCU_Adapter::deleteCCuDevices(){
LOGD("删除所有设备\n");
try{
map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = hwSubDevs.begin();
for (; it != hwSubDevs.end(); it++) {
string DevDodeId;
string devMac = it->second->mac;
string devProdId = it->second->dev_model_id;
// LOGD("临时注释删除所有所有设备mac %s\n",arg.c_str());
unsigned long nowReqTime = getSystemTimeInMs();
if( (nowReqTime - lastCcuReqTime) > 100){
if (devProdId == DAIKINCentralACgw_PID) {
// string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// client->sendRequest(gwNodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
if(gwIt == NodeId2gwId.end()){
LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
} else {
DevDodeId = gwIt->second;
}
client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
} else {
string arg = "{\"mac\":\""+devMac+"\"}";
client->sendRequest("*", "DEL_DEVICE", arg, "HJ_Server");
}
lastCcuReqTime = nowReqTime;
}else{
LOGI("delete Devicessend req to ccu too fast,wait 100ms.\n");
usleep(200*1000);
if (devProdId == DAIKINCentralACgw_PID) {
// string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// client->sendRequest(gwNodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
if(gwIt == NodeId2gwId.end()){
LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
} else {
DevDodeId = gwIt->second;
}
client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
// //删除所有设备
// int MQ_CCU_Adapter::deleteCCuDevices(){
// LOGD("删除所有设备\n");
// try{
// map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = hwSubDevs.begin();
// for (; it != hwSubDevs.end(); it++) {
// string DevDodeId;
// string devMac = it->second->mac;
// string devProdId = it->second->dev_model_id;
// // LOGD("临时注释删除所有所有设备mac %s\n",arg.c_str());
// unsigned long nowReqTime = getSystemTimeInMs();
// if( (nowReqTime - lastCcuReqTime) > 100){
// if (devProdId == DAIKINCentralACgw_PID) {
// // string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// // client->sendRequest(gwNodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
// map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
// if(gwIt == NodeId2gwId.end()){
// LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
// } else {
// DevDodeId = gwIt->second;
// }
// client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
// } else {
// string arg = "{\"mac\":\""+devMac+"\"}";
// client->sendRequest("*", "DEL_DEVICE", arg, "HJ_Server");
// }
// lastCcuReqTime = nowReqTime;
// }else{
// LOGI("delete Devicessend req to ccu too fast,wait 100ms.\n");
// usleep(200*1000);
// if (devProdId == DAIKINCentralACgw_PID) {
// // string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// // client->sendRequest(gwNodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
// map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
// if(gwIt == NodeId2gwId.end()){
// LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
// } else {
// DevDodeId = gwIt->second;
// }
// client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
} else {
string arg = "{\"mac\":\""+devMac+"\"}";
client->sendRequest("*", "DEL_DEVICE", arg, "HJ_Server");
}
lastCcuReqTime = getSystemTimeInMs();
}
}
} catch (exception &e) {
LOGW("%s.\n", e.what());
}
return 0;
}
// } else {
// string arg = "{\"mac\":\""+devMac+"\"}";
// client->sendRequest("*", "DEL_DEVICE", arg, "HJ_Server");
// }
// lastCcuReqTime = getSystemTimeInMs();
// }
// }
// } catch (exception &e) {
// LOGW("%s.\n", e.what());
// }
// return 0;
// }
//删除请求
int MQ_CCU_Adapter::onDeviceRequestDelete(string devSn){
try {
LOGD("删除设备:%s .\n",devSn.c_str());
Deletesn = devSn;
mtx.acquire();
map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = hwSubDevs.find(devSn);
if(it != hwSubDevs.end()){
string channel= "1";
string DevDodeId;
string devMac = it->second->mac;
string devProdId = it->second->dev_model_id;
hwSubDevs.erase(devSn);
if(devProdId == DAIKINCentralACindoor_PID){ //内机特殊处理
LOGD("删除内机设备:%s .\n",devSn.c_str());
mtx.release();
return 0;
}
hwBindDevMap.erase(devMac);
//KK_HwDevManager::GetInstance()->saveAllSubDev(hwBindDevMap);
if (devProdId == DAIKINCentralACgw_PID) {
// string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// sendReqToCcu("DEL_CENTRAL_AC_GW",gwNodeId,"*","HJ_CentralAC");
map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
if(gwIt == NodeId2gwId.end()){
LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
} else {
DevDodeId = gwIt->second;
LOGD("删除设备:%s .\n",devSn.c_str());
Deletesn = devSn;
mtx.acquire();
map<string,kk_handleDeviceInfo::HwSubDev*>::iterator it = hwSubDevs.find(devSn);
if(it != hwSubDevs.end()){
string channel= "1";
string DevDodeId;
string devMac = it->second->mac;
string devProdId = it->second->dev_model_id;
hwSubDevs.erase(devSn);
if(devProdId == DAIKINCentralACindoor_PID){ //内机特殊处理
LOGD("删除内机设备:%s .\n",devSn.c_str());
mtx.release();
return 0;
}
client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
onACindoorDelete(devMac);
} else {
string arg = "{\"mac\":\""+devMac+"\"}";
sendReqToCcu("DEL_DEVICE","*",arg,"HJ_Server");
hwBindDevMap.erase(devMac);
//KK_HwDevManager::GetInstance()->saveAllSubDev(hwBindDevMap);
if (devProdId == DAIKINCentralACgw_PID) {
// string gwNodeId = KK_CcuConfig::GetInstance()->getCentralAcGwNodeId(it->second->mac);
// sendReqToCcu("DEL_CENTRAL_AC_GW",gwNodeId,"*","HJ_CentralAC");
map<string,string>::iterator gwIt = NodeId2gwId.find(it->second->channelDevs["1"]->nodeId); //NodeId2gwId gwId2NodeId
if(gwIt == NodeId2gwId.end()){
LOGW("centralACnodeId:%s not in gwId2NodeId.\n",it->second->channelDevs["1"]->nodeId.c_str());
} else {
DevDodeId = gwIt->second;
}
client->sendRequest(DevDodeId, "DEL_CENTRAL_AC_GW", "*", "HJ_CentralAC");
onACindoorDelete(devMac);
} else {
string arg = "{\"mac\":\""+devMac+"\"}";
sendReqToCcu("DEL_DEVICE","*",arg,"HJ_Server");
//子设备解绑或移除
string respTopic;
respTopic = pushTopic+"/deviceremove";
EH::Json::Value payload;
payload["gw_id"] = gw_id;
payload["dev_id"] = devSn;
int SendOk;
SendOk = sendtoRoma(payload,respTopic);
//子设备解绑或移除
string respTopic;
respTopic = pushTopic+"/device/left";
EH::Json::Value payload;
payload["product_id"] = devProdId;
payload["mac"] = devSn;
int SendOk;
SendOk = sendtoRoma(payload,respTopic);
}
}else{
LOGW("not found hw dev:%s in local triple cache.\n",devSn.c_str());
}
}else{
LOGW("not found hw dev:%s in local triple cache.\n",devSn.c_str());
}
mtx.release();
mtx.release();
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
......@@ -1187,16 +990,17 @@ int MQ_CCU_Adapter::loadDelete(EH::Json::Value arg){
// KK_HwSdk::GetInstance()->changeDevOnline(devSn, false);
it->second->online = false;
if(it->second->dev_model_id == DAIKINCentralACgw_PID){
handleACindoorDevNolinePush(arg.asString());
// handleACindoorDevNolinePush(arg.asString());
}
LOGI("本地删除设备sn:%s 推送设备离线:%s.\n",devSn.c_str(),devSn.c_str());
hwSubDevs.erase(devSn);
hwBindDevMap.erase(devMac);
//子设备解绑或移除
string respTopic;
respTopic = pushTopic+"/deviceoffline";//子设备离线
respTopic = pushTopic+"/device/left";//子设备删除
EH::Json::Value payload;
payload["dev_id"] = devSn; //请求发起者
payload["mac"] = devSn; //请求发起者
payload["product_id"] = it->second->dev_model_id;
sendtoRoma(payload,respTopic);
}else{
LOGW("这个设备已删除,不推送.\n");
......@@ -1234,31 +1038,32 @@ int MQ_CCU_Adapter::onACindoorDelete(string devMac){
return 0;
}
void MQ_CCU_Adapter::handleACindoorDevNolinePush(string mac){
try {
string sn;
map<string,vector<string> >::iterator numIt = controllerModel.find(mac);
if (numIt != controllerModel.end()) {
for (vector<string>::iterator it = numIt->second.begin(); it != numIt->second.end(); it++) {
EH::Json::Value idJson;
sn = (*it).c_str();
map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(sn);
//子设备解绑或移除
string respTopic;
respTopic = pushTopic + "/deviceoffline";//子设备离线
EH::Json::Value payload;
payload["dev_id"] = sn; //请求发起者
sendtoRoma(payload,respTopic); //网关离线,内机向华为推送离线。 //删除空调网关也会删除内机,
subDevIt->second->online = false;
LOGI("删除内机sn:%s .\n",sn.c_str());
hwSubDevs.erase(sn);
}
}
}catch (exception &e) {
LOGW("%s.\n", e.what());
}
}
// //空调内机在线离线推送
// void MQ_CCU_Adapter::handleACindoorDevNolinePush(string mac){
// try {
// string sn;
// map<string,vector<string> >::iterator numIt = controllerModel.find(mac);
// if (numIt != controllerModel.end()) {
// for (vector<string>::iterator it = numIt->second.begin(); it != numIt->second.end(); it++) {
// EH::Json::Value idJson;
// sn = (*it).c_str();
// map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(sn);
// //子设备解绑或移除
// string respTopic;
// respTopic = pushTopic + "/deviceoffline";//子设备离线
// EH::Json::Value payload;
// payload["dev_id"] = sn; //请求发起者
// sendtoRoma(payload,respTopic); //网关离线,内机向华为推送离线。 //删除空调网关也会删除内机,
// subDevIt->second->online = false;
// LOGI("删除内机sn:%s .\n",sn.c_str());
// hwSubDevs.erase(sn);
// }
// }
// }catch (exception &e) {
// LOGW("%s.\n", e.what());
// }
// }
int MQ_CCU_Adapter::sendtoRoma(EH::Json::Value payload,string respTopic){
LOGD("send to Roma msg payload:%s respTopic:%s.\n",payload.toStyledString().c_str(),respTopic.c_str());
......@@ -1291,7 +1096,7 @@ int MQ_CCU_Adapter::loadAllSubDev(EH::Json::Value arg){
EH::Json::Value XiaoKprodId = kk_handleDeviceInfo::GetInstance()->XiaoK_Zu_Wang(productid); //调用设备是否接入华为
if ( XiaoKprodId["dev_model_id"] != "-1"){
bindDev.dev_model_id = XiaoKprodId["dev_model_id"].asString();
bindDev.dev_type = XiaoKprodId["dev_type"].asString();
// bindDev.dev_type = XiaoKprodId["dev_type"].asString();
}else {
continue;
}
......@@ -1321,3 +1126,214 @@ int MQ_CCU_Adapter::handle_Dev_Join_Notify(EH::Json::Value arg){
}
return 0;
}
// //开始处理空调内机
// void MQ_CCU_Adapter::handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome){
// try {
// LOGD("开始处理空调内机\n");
// int error;
// map<string,kk_handleDeviceInfo::HwSubDev*> toAddSubDevs;//需要添加到华为的设备信息。
// controllerModel.clear();
// for (int i = 0; i < indoorunitsdevCount; i++) { ////中央空调内机数目
// string channel= "1";
// EH::Json::Value centralAC = centralACdevices[i];
// string indoorunitId = centralAC["id"].asString();
// string indoorunitAddr = centralAC["indooruint_addr"].asString();
// string centralACGwId;//中央空调网关设备 ID,注:非 zigbee 设备 ID 体系
// EH::Json::Value indoorunitStatus;//内机当前的状态
// if(infocome == 0){ //infocome = 0为新设备列表里的,=1表示为添加到房间里了。
// centralACGwId = centralAC["bind_central_ac_gw_node_id"].asString();//
// indoorunitStatus = centralAC["status"];
// }else if(infocome == 1){
// centralACGwId = centralAC["central_ac_gw_devid"].asString();//中央空调网关设备 ID,注:非 zigbee 设备 ID 体系
// devroomid = centralAC["room_id"].asString(); //房间号
// indoorunitStatus = centralAC["indoorunit_state"]; //内机当前的状态
// devname =centralAC["indoorunit_name"].asString(); //内机的名称,不同步到华为。
// }
// string centralAcGwNodeId; //这个内机对应的空调网关ID
// string centralACGwMac; //这个内机对应的空调网关Mac
// string centralACGwoperateType; //这个内机对应的空调网关类型
// string centralACindoorMac; //这个内机mac为空调网关Mac+id
// string centralACindoorsn; //这个内机sn为空调网关Mac+id 去除“:”
// //1。 这个内机对应的空调网关ID
// LOGD("centralACGwId:%s,indoorunitAddr:%s,indoorunitnodeId:%s \n",centralACGwId.c_str(),indoorunitAddr.c_str(),indoorunitId.c_str());
// map<string,string>::iterator gwIt = gwId2NodeId.find(centralACGwId);
// if(gwIt == gwId2NodeId.end()){
// LOGW("centralACGwId:%s not in gwId2NodeId.\n",centralACGwId.c_str());
// continue;
// } else {
// centralAcGwNodeId = gwIt->second;
// }
// //2。 这个内机对应的空调网关类型
// map<string,string>::iterator operateIt = operate2NodeId.find(centralAcGwNodeId);
// if(operateIt == operate2NodeId.end()){
// LOGW("centralAcGwNodeId:%s not in operate2NodeId.\n",centralAcGwNodeId.c_str());
// continue;
// } else {
// centralACGwoperateType = "-13"; //情景模式里的内机operateId
// }
// //3。 这个内机对应的空调网关Mac
// map<string,string>::iterator centralAcNodeIt = nodeId2channelMap.find(centralAcGwNodeId);
// if(centralAcNodeIt == nodeId2channelMap.end()){
// LOGW("nodeid:%s not in nodeId2channelMap.\n",centralAcGwNodeId.c_str());
// continue;
// } else {
// vector<string> chanMac;
// Split(centralAcNodeIt->second,"_",chanMac);
// centralACGwMac = chanMac[0];
// }
// indoorunitsId2AddrMap[indoorunitId] = centralACGwMac+"_"+indoorunitAddr; //网关与内机通道id之间的对应关系
// LOGD("indoorunitId:%s .\n",indoorunitId.c_str());
// if(StringToInt(indoorunitAddr,error)<10){
// centralACindoorMac = centralACGwMac+"0"+indoorunitAddr; //*****************内机的mac构成关系
// }else{
// centralACindoorMac = centralACGwMac+indoorunitAddr;
// }
// centralACindoorsn = Replace(centralACindoorMac,":",""); //*****************内机的sn
// if(!ccuDeleteMacs.empty() && ccuDeleteMacs.find(centralACGwMac)!=ccuDeleteMacs.end()){
// continue;
// }
// map<string,vector<string> >::iterator numIt = controllerModel.find(centralACGwMac);//说明待定
// if (numIt == controllerModel.end()) {
// vector<string> addrId;
// addrId.push_back(centralACindoorsn);
// controllerModel[centralACGwMac] = addrId;
// } else {
// numIt->second.push_back(centralACindoorsn);
// }
// map<string,kk_handleDeviceInfo::KKDevmapinfo>::iterator it = hwBindDevMap.find(centralACindoorMac);
// if(it!=hwBindDevMap.end()){
// kk_handleDeviceInfo::KKDevmapinfo tripInfo = it->second;
// LOGD("centralACGwMac:%s in hwBindDevMap \n",centralACindoorMac.c_str());
// handleCcuDeviceToHwSubDev(centralACGwoperateType,indoorunitId,centralACindoorMac,channel,tripInfo.sn,indoorunitStatus,devroomid,devname);
// }else{
// if(toAddSubDevs.find(centralACindoorMac) == toAddSubDevs.end()){
// kk_handleDeviceInfo::HwSubDev* dev = new kk_handleDeviceInfo::HwSubDev();
// dev->dev_model_id = DAIKINCentralACindoor_PID; //内机 对应的华为prodID
// dev->dev_type = DAIKINCentralACindoor_PID;
// dev->mac = centralACindoorMac;
// dev->operatetype = centralACGwoperateType;
// dev->sn = Replace(centralACindoorMac,":","");
// kk_handleDeviceInfo::KKChannelDev* chanDev = kk_handleDeviceInfo::GetInstance()->buildChannelDevice(centralACGwoperateType,indoorunitId,channel,indoorunitStatus,devroomid,devname);
// map<string,kk_handleDeviceInfo::KKChannelDev*> channelDevs;
// if(chanDev!=NULL){
// channelDevs[channel] = chanDev;
// }
// dev->channelDevs = channelDevs;
// toAddSubDevs[centralACindoorMac] = dev;
// LOGD("\n found JoinDevProductkey for dev:%s,operateid:%s,channel:%s,close zigbee net now. \n",
// centralACindoorMac.c_str(),centralACGwoperateType.c_str(), channel.c_str());
// }
// }
// }
// // 空调网关数量及内机地址推送
// // map<string, string> hwCentralAcList;
// // for(map<string, string>::iterator addrIt = indoorunitsId2AddrMap.begin(); addrIt!=indoorunitsId2AddrMap.end(); addrIt++){
// // vector<string> macChan;
// // Split(addrIt->second,"_", macChan);
// // EH::Json::Value batchArg;
// // hwCentralAcList[macChan[0]] = macChan[1];
// // }
// if( toAddSubDevs.size() > 0){
// discoveryEnable = true;
// KKdiscover = true;
// LOGD(" discoveryEnable:%d,KKdiscover:%d\n",discoveryEnable,KKdiscover);
// }
// addCCuDevicesToCloud(toAddSubDevs);
// LOGD("after handle NEW_DEVICES,total hw dev count is:%d \n",hwSubDevs.size());
// //推送内机数目
// for(map<string, string>::iterator gwIt = gwId2NodeId.begin(); gwIt!=gwId2NodeId.end(); gwIt++){
// map<string,string>::iterator nodeIt = nodeId2channelMap.find(gwIt->second);
// if(nodeIt == nodeId2channelMap.end()){
// LOGW("gwId:%s not in gwId2NodeId, affect Internal machine push.\n",gwIt->second.c_str());
// continue;
// }
// vector<string> chanMac;
// Split(nodeIt->second,"_",chanMac);
// string sn = Replace(chanMac[0],":","");
// string channel = chanMac[1];
// map<string, kk_handleDeviceInfo::HwSubDev*>::iterator subDevIt = hwSubDevs.find(sn);
// if(subDevIt == hwSubDevs.end()){
// LOGW("dev:%s not in hwSubDevs.\n",sn.c_str());
// continue;
// } else {
// string dev_model_id = subDevIt->second->dev_model_id;
// string devType = subDevIt->second->devTypeId;
// EH::Json::Value payload;
// EH::Json::Value msgstate0;
// payload["gw_id"] = EH_ToolKit::GetMacNoSpace(); //请求发起者
// payload["dev_id"] = sn;
// payload["dev_type"] = subDevIt->second->dev_model_id;
// payload["dev_ep_id"] = StringToInt(channel,error); //通道
// EH::Json::Value msgstate2;
// string respTopic = pushTopic+string("/devicestatechange");//设备状态变化上报
// EH::Json::Value array;
// map<string,vector<string> >::iterator numIt = controllerModel.find(subDevIt->second->mac);
// if (numIt != controllerModel.end()) {
// for (vector<string>::iterator it = numIt->second.begin(); it != numIt->second.end(); it++) {
// EH::Json::Value idJson;
// idJson["sn"] = *it;
// array.append(idJson);
// }
// msgstate2["deviceList"] = array;
// } else {
// msgstate2["deviceList"].resize(0);//空数组
// }
// payload["state"] = msgstate2;
// payload["dev_ep_id"] = 3; //功能映射为通道3
// // MQ_CCU_Adapter::GetInstance()->sendtoRoma(payload,respTopic);
// }
// }
// //deleteCCuDevices(ccuDeleteMacs); //调试
// ccuDeleteMacs.clear();
// }catch (exception &e) {
// LOGW("%s.\n", e.what());
// }
// }
//SYNC_INFO 处理中央空调网关
// void MQ_CCU_Adapter::SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevices){
// try {
// for (int i = 0; i < CentralAcGwsDevices.size(); i++) {
// EH::Json::Value Gwdevice = CentralAcGwsDevices[i];
// string operateType = "12001";//中央空调网关
// string gwId = Gwdevice["id"].asString();
// string nodeid = Gwdevice["bind_central_ac_gw_node_id"].asString();
// gwId2NodeId[gwId] = nodeid;
// NodeId2gwId[nodeid] = gwId;
// LOGD("add ccu system gwId:%s, gwNodeId:%s \n",gwId.c_str() ,nodeid.c_str());
// operate2NodeId[nodeid] = operateType;
// string mac = "未接入以后再改";
// // string mac = KK_CcuConfig::GetInstance()->getCentralAcGwMac(nodeid);
// string channel = "1";
// string devroomid = Gwdevice["room_id"].asString(); //房间号
// string devname = Gwdevice["dev_name"].asString(); //设备的名称
// nodeId2channelMap[nodeid] = mac+"_"+channel;
// EH::Json::Value status = "0"; //设备的状态 Gwdevice["status"].asString()"status": {"gwStatusCode": -1,"online": false}
// map<string,kk_handleDeviceInfo::KKDevmapinfo>::iterator devIt = hwBindDevMap.find(mac);
// if(devIt!=hwBindDevMap.end()){
// kk_handleDeviceInfo::KKDevmapinfo tripInfo = devIt->second;
// handleCcuDeviceToHwSubDev(operateType,nodeid,mac,channel,tripInfo.sn,status,devroomid,devname);
// }else{
// LOGW("ccu dev:%s not found bind info in lcoal cache.delete fron ccu later. \n",mac.c_str());
// }
// }
// }catch (exception &e) {
// LOGW("%s.\n", e.what());
// }
// }
\ No newline at end of file
......@@ -25,7 +25,7 @@
#include "KK_HwDevManager.h"
#define CENTRALAC_INDOORUNIT_NUM_MAX 64
#define MQ_VERSION "1.0.1"
#define MQ_VERSION "1.0.5.2"
using namespace std;
......@@ -77,13 +77,13 @@ private:
void handleNewDevice(EH::Json::Value devices, EH::Json::Value centralACdevices, EH::Json::Value newCentralAcGwsDevices);
void handleCcuDeviceToHwSubDev(string operateType,string nodeid,string mac,
string channel,string sn,EH::Json::Value status,string devroomid,string devname);
void handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome);
// void handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome);
//当网关离线,其下内机也推送离线
void handleACindoorDevNolinePush(string mac);
// void handleACindoorDevNolinePush(string mac);
//处理已加入房间的设备报文
void handleSyncInfo(EH::Json::Value arg,string opcode);\
void SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevices);
// void SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevices);
//添加设备到云
void addCCuDevicesToCloud(map<string,kk_handleDeviceInfo::HwSubDev*> toAddSubDevs);
......@@ -96,7 +96,7 @@ private:
//删除子设备
int onDeviceRequestDelete(string devSn);
//删除所有设备
int deleteCCuDevices();
// int deleteCCuDevices();
//删除网关时,也删除其下的内机。
int onACindoorDelete(string devMac);
......
......@@ -77,6 +77,7 @@ int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { //主函数
}
if(argc == 2 && string(argv[1]) == "-d") {
LOGI("000000000000000000000000000000000000000000\n");
oepnDebug();
}
......@@ -130,6 +131,7 @@ static void* run0(void* opt){
static void oepnDebug(){
LOGI("333333333333333333333333\n");
struct sockaddr_in addr; /*描述Internet套接字地址的结构。*/
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
......@@ -160,5 +162,6 @@ static void oepnDebug(){
正在传递ARG。创造属性来自属性。新的
句柄存储在*NEWTHREAD中。*/
pthread_t tid;
LOGI("444444444444444444444444444\n");
pthread_create(&tid, NULL, run0, NULL);
}
/*
* EH_DB.h
* Copyright by AirDream Co., Ltd. 2010-2011 All right reserved
*
* Description:
* Created on:2011-12-22
* Modified on:2011-12-22
*
*/
//#define NOT_X86
#ifndef NOT_X86
#ifndef EH_DB_H_
#define EH_DB_H_
#include <iostream>
#include <string>
#include <pqxx/pqxx>
#include "ace/Recursive_Thread_Mutex.h"
using namespace std;
using namespace pqxx;
typedef vector<map<string, string>*> DBRESVEC;
typedef vector<map<string, string>*>* DBRES;
typedef map<string, string> DBROWMAP;
typedef map<string, string>* DBROW;
class EH_DB {
public:
EH_DB(string username,string passwd,string addr,string port,string dbname);
~EH_DB();
void OpenConnection();
void CloseConnection();
void ResetConnection();
void StartTransaction();
void CommitTransaction();
void AbortTransaction();
void ExceptionInTransaction();
DBRES ExecuteQuery(string sql);
int ExecuteUpdate(string sql);
static void ReleaseResultSet(DBRES& res);
private:
connection* m_pConn;
transaction<>* m_pAction;
string m_strConnectionInfo;
ACE_Recursive_Thread_Mutex m_Lock;
};
#endif /* EH_DB_H_ */
#else
#include "EH_DB_Sqlite.h"
#endif
/*
* EH_DB.h
*
* Created on: 2013-9-10
* Author: sj
usage:
try {
StartTransaction();
ExecuteQuery
ExecuteUpdate
.....
CommitTransaction();
} catch (...) {
ExceptionInTransaction();//NOTE!!
....//custom process code
}
modify by zgm at 2016-5-6
*/
#ifdef NOT_X86
#ifndef EH_DB_H_
#define EH_DB_H_
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Process_Mutex.h"
#include "EH_Sqlite3.h"
using namespace std;
typedef vector<map<string, string>*> DBRESVEC;
typedef vector<map<string, string>*>* DBRES;
typedef map<string, string> DBROWMAP;
typedef map<string, string>* DBROW;
class EH_DB {
public:
EH_DB(string username,string passwd,string addr,string port,string dbname);
~EH_DB();
/*
* If already have a connection,don't get a new connection.
* if success return 0, else are other values.
*/
int OpenConnection();
/*
* close the db connection
*/
void CloseConnection();
/*
* if success return 0, else are other values.
*/
int ResetConnection();
/*
* if success return 0, else are other values.
*/
int StartTransaction();
/*
* if success return 0, else are other values.
*/
int CommitTransaction();
/*
* if success return 0, else are other values.
*/
int AbortTransaction();
/* must be called when exception occur when in executing a transaction.
* if success return 0, else are other values.
*/
int ExceptionInTransaction();
/*
* release the DBRES is the application's responsibility
*/
DBRES ExecuteQuery(string sql);
void ExecuteUpdate(string sql);
static void ReleaseResultSet(DBRES& res);
private:
private:
void* m_pConn;
string m_strConnectionInfo;
int m_iTransactionFlag;
int m_iErrCode;
};
#endif /* EH_DB_H_ */
#endif
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -61,7 +61,7 @@ private:
string password;
string willTopic;
string heartbeat_Topic;
string topic1 ,topic2; //订阅的主题
string topic1 ,topic2,topic3,topic4,topic5,topic6,topic7; //订阅的主题
long reconTimerId;
EH_SDKCallbackHandler* callbackHandler;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -16,10 +16,10 @@
using namespace std;
//第1期接入设备 //临时定义 2020.10.14
#define ZeroFire1ChanChopinSwitch_PID "400100" //肖邦-一路灯控
#define ZeroFire2ChanChopinSwitch_PID "400200" //肖邦-二路灯控
#define ZeroFire3ChanChopinSwitch_PID "400300" //肖邦-三路灯控
//第1期接入设备 //产品ID 2021.3.35
#define ZeroFire1ChanChopinSwitch_PID "1603bcc0b8a103e91603bcc0b8a16001" //肖邦-一路灯控
#define ZeroFire2ChanChopinSwitch_PID "1603bec0b8a203e91603bec0b8a28001" //肖邦-二路灯控
#define ZeroFire3ChanChopinSwitch_PID "1603bec0b8a303e91603bec0b8a32a01" //肖邦-三路灯控
//第2期接入设备 //临时定义 2020.11.18
#define Chopin1CurtainSwitch_PID "410100" //肖邦-一路窗帘
#define Chopin2CurtainSwitch_PID "410200" //肖邦-二路窗帘
......@@ -44,7 +44,7 @@ public: ////////先建立
class KKDevmapinfo {
public:
string dev_model_id;
string dev_type;
// string dev_type;
string sn;
string mac;
};
......@@ -61,7 +61,7 @@ public:
class HwSubDev {
public:
string dev_model_id; //产品ID ProdID
string dev_type; //产品类型
// string dev_type; //产品类型
string operatetype; //设备的operate_type类型
string devTypeId;
string model;
......@@ -160,15 +160,15 @@ public:
//更改设备状态信息
void handleDeviceStatusChanged(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,map<string,string>&nodeId2channelMap,
string nodeId,string opcode,EH::Json::Value arg,string pushTopic,string gw_id);
//空调内机更改设备状态信息
void handleCentralACStatusPush(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,map<string,string>&indoorunitsId2AddrMap,string pushTopic,
string nodeId,EH::Json::Value arg,string opcode);
// //空调内机更改设备状态信息
// void handleCentralACStatusPush(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,map<string,string>&indoorunitsId2AddrMap,string pushTopic,
// string nodeId,EH::Json::Value arg,string opcode);
//允许加入的设备类型
bool isValidJoinDeviceType(string kkDevOperateId,string kkDevProductId,string mac);
//获取全部设备信息
int onGetDevService(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,map<string,vector<string> >& controllerModel,string pushTopic,string gw_id);
int onGetDevService(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,map<string,vector<string> >& controllerModel,string pushTopic);
//控制设备
int onSetDevService(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,string sn,string channel,string svcId,EH::Json::Value cmd);
int onSetDevService(map<string,kk_handleDeviceInfo::HwSubDev*>&hwSubDevs,string sn,EH::Json::Value cmd);
//建立通道设备模型
kk_handleDeviceInfo::KKChannelDev* buildChannelDevice(string operateId,string nodeId,string channel,EH::Json::Value status,string devroomid,string devname);
......
/*
* EH_DB.cpp
* Copyright by AirDream Co., Ltd. 2010-2011 All right reserved
*
* Description:
* Created on:2011-12-22
* Modified on:2011-12-22
*
*/
#ifndef NOT_X86
#include "EH_DB.h"
#include "EH_DEBUG.h"
EH_DB::EH_DB(string username, string passwd,
string addr, string port,string dbname) {
string conn = "";
conn.append("dbname=");
conn.append(dbname);
conn.append(" ");
conn.append("host=");
conn.append(addr);
conn.append(" ");
conn.append("port=");
conn.append(port);
conn.append(" ");
conn.append("user=");
conn.append(username);
conn.append(" ");
conn.append("password=");
conn.append(passwd);
m_strConnectionInfo = conn;
m_pConn = NULL;
m_pAction = NULL;
}
/*
*If already have a connection,don't get a
*new connection.
*/
void EH_DB::OpenConnection() {
m_Lock.acquire();
if (m_pConn == NULL) {
m_pConn = new connection(m_strConnectionInfo);
}
m_Lock.release();
}
void EH_DB::ResetConnection() {
m_Lock.acquire();
CloseConnection();
OpenConnection();
m_Lock.release();
}
void EH_DB::CloseConnection() {
m_Lock.acquire();
if (m_pConn) {
m_pConn->disconnect();
delete m_pConn;
m_pConn = NULL;
}
m_Lock.release();
}
void EH_DB::StartTransaction() {
m_Lock.acquire();
if (m_pAction) {
delete m_pAction;
m_pAction = NULL;
}
m_pAction = new transaction<>(*m_pConn, "EH_DB");
}
void EH_DB::CommitTransaction() {
if (m_pAction) {
m_pAction->commit();
delete m_pAction;
m_pAction = NULL;
m_Lock.release();
}
}
void EH_DB::AbortTransaction() {
if (m_pAction) {
m_pAction->abort();
delete m_pAction;
m_pAction = NULL;
m_Lock.release();
}
}
/*must be called when exception occur when in executing a transaction.
usage:
try {
StartTransaction();
ExecuteQuery
ExecuteUpdate
.....
CommitTransaction();
} catch (...) {
ExceptionInTransaction();//NOTE!!
....//custom process code
}
*/
void EH_DB::ExceptionInTransaction() {
if (m_pAction) {
delete m_pAction;
m_pAction = NULL;
}
m_Lock.release();
}
DBRES EH_DB::ExecuteQuery(string sql) {
if (m_pConn == NULL) {
throw runtime_error("EXCEPTION:connection instance is NULL");
}
if (m_pAction == NULL) {
throw runtime_error("EXCEPTION:transaction instance is NULL");
}
result R = m_pAction->exec(sql);
DBRES result = new DBRESVEC();
DBROW row = NULL;
for (uint i = 0; i < R.size(); i++) {
row = new DBROWMAP();
for (uint j = 0; j < R.columns(); j++) {
string colName = R.column_name(j);
string vlaue(R[i][colName].c_str());
row->insert(DBROWMAP::value_type(colName, vlaue));
}
result->push_back(DBRESVEC::value_type(row));
row = NULL;
}
return result;
}
int EH_DB::ExecuteUpdate(string sql) {
if (m_pConn == NULL) {
throw runtime_error("EXCEPTION:connection instance is NULL");
}
if (m_pAction == NULL) {
throw runtime_error("EXCEPTION:transaction instance is NULL");
}
result ret = m_pAction->exec(sql);
return ret.affected_rows();
}
void EH_DB::ReleaseResultSet(DBRES& res) {
if (!res) {
return;
}
DBRESVEC::const_iterator it = res->begin();
while (it != res->end()) {
DBROW row = *it;
if (row) {
delete row;
row = NULL;
}
++it;
}
delete res;
res = NULL;
}
EH_DB::~EH_DB() {
CloseConnection();
}
#endif
/*
* LS_DB_Sqlite.cpp
*
*/
#ifdef NOT_X86
#include "EH_Exception.h"
#include "EH_DB_Sqlite.h"
#include "EH_DEBUG.h"
#include <unistd.h>
#include "ace/Process_Mutex.h"
#include "ace/Thread_Mutex.h"
#define DB_PATHNAME "/etc/dropbear/accessory/hj_db/kk_db.db"
#define DB_BEGIN_TRANSACTION "BEGIN TRANSACTION;"
#define DB_COMMIT_TRANSACTION "COMMIT;"
#define DB_ROLLBACK_TRANSACTION "ROLLBACK;"
//ACE_Process_Mutex gProcessSqliteLock("sds_sqlite_lk");
ACE_Thread_Mutex gThreadSqliteLock;
static int execSql_callback(void *p_otherData, int argc, char **argv, char **azColName);
EH_DB::EH_DB(string username, string passwd, string addr, string port, string dbname) {
m_pConn = NULL;
m_iTransactionFlag = 0;
}
/*
* If already have a connection,don't get a new connection.
* if success return 0, else are other values.
*/
int EH_DB::OpenConnection() {
int i_errCode = SQLITE_OK;
#if 0
sqlite3 *p_db = NULL;
gProcessSqliteLock.acquire();
if (m_pConn == NULL) {
i_errCode = sqlite3_open(DB_PATHNAME, &p_db);
if ( i_errCode == SQLITE_OK ) {
m_pConn = p_db;
} else {
EH_WARNING("open db failed.\n");
sqlite3_close(p_db);
}
}
gProcessSqliteLock.release();
#endif
return i_errCode;
}
int EH_DB::ResetConnection() {
int i_errCode = SQLITE_OK;
#if 0
CloseConnection();
i_errCode = OpenConnection();
#endif
return i_errCode;
}
void EH_DB::CloseConnection() {
#if 0
gProcessSqliteLock.acquire();
if (m_pConn) {
sqlite3_close((sqlite3 *)m_pConn);
m_pConn = NULL;
}
m_iTransactionFlag = 0;
gProcessSqliteLock.release();
#endif
}
int EH_DB::StartTransaction() {
int i_errCode = SQLITE_ERROR;
int i_retryCount = 0, i_retFlag = 0;
char *p_errMsg = NULL;
string str_errMsg;
//EH_DEBUG("StartTransaction try get lock...\n");
//gProcessSqliteLock.acquire();
gThreadSqliteLock.acquire();
//EH_DEBUG("StartTransaction try get lock ok.\n");
if ( !m_pConn ) {
sqlite3 *p_db = NULL;
i_errCode = sqlite3_open(DB_PATHNAME, &p_db);
if ( i_errCode == SQLITE_OK ) {
m_pConn = p_db;
sqlite3_exec((sqlite3*) m_pConn,"ATTACH DATABASE '/tmp/zigbee_dev_status.db' as 'zds_in_ramdisk'",
0,0,&p_errMsg);
} else {
EH_WARNING("open db failed.\n");
sqlite3_close(p_db);
}
}
if ( !m_iTransactionFlag && m_pConn) {
while (1) {
i_errCode = sqlite3_exec((sqlite3*) m_pConn, DB_BEGIN_TRANSACTION,
NULL, NULL, &p_errMsg);
switch (i_errCode) {
case SQLITE_OK: {
m_iTransactionFlag = 1;
m_iErrCode = 0;
i_retFlag = 1;
break;
}
case SQLITE_BUSY:
case SQLITE_LOCKED:
if (i_retryCount < 10) {
i_retryCount++;
i_retFlag = 0;
if (p_errMsg) {
sqlite3_free(p_errMsg);
p_errMsg = NULL;
}
usleep(100 * 1000);
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED,try %d.\n",
DB_BEGIN_TRANSACTION, i_retryCount);
break;
} else {/*do default case;*/
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED.\n",
DB_BEGIN_TRANSACTION);
}
default: {
i_retFlag = 1;
if (p_errMsg) {
str_errMsg = p_errMsg;
str_errMsg.append(" database EXCEPTION!");
sqlite3_free(p_errMsg);
p_errMsg = NULL;
} else {
str_errMsg = "EXCEPTION: SQL error or missing database";
}
break;
}
}
if (i_retFlag)
break;
}
//EH_DEBUG("StartTransaction with error code:%d.conn:%d.total try:%d.\n",i_errCode,m_pConn,
// i_retryCount);
if (m_iErrCode != SQLITE_OK) {
EH_WARNING("StartTransaction with error code:%d.conn:%d.total try:%d.\n",i_errCode,m_pConn,
i_retryCount);
}
} else {
EH_WARNING("StartTransaction transaction in use or no conn.trans flag:%d conn:%d.\n",
m_iTransactionFlag,m_pConn);
}
return i_errCode;
}
int EH_DB::CommitTransaction() {
int i_errCode = SQLITE_ERROR;
int i_retryCount = 0, i_retFlag = 0;
char *p_errMsg = NULL;
string str_errMsg;
if ( m_iTransactionFlag && m_pConn ) {
m_iTransactionFlag = 0;
while (1) {
i_errCode = sqlite3_exec((sqlite3*)m_pConn, m_iErrCode ? DB_ROLLBACK_TRANSACTION : DB_COMMIT_TRANSACTION,
NULL, NULL, &p_errMsg);
switch (i_errCode) {
case SQLITE_OK: {
i_retFlag = 1;
break;
}
case SQLITE_BUSY:
case SQLITE_LOCKED:
if (i_retryCount < 10) {
i_retryCount++;
i_retFlag = 0;
if (p_errMsg) {
sqlite3_free(p_errMsg);
p_errMsg = NULL;
}
usleep(100 * 1000);
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED,try %d.\n",
m_iErrCode ? DB_ROLLBACK_TRANSACTION : DB_COMMIT_TRANSACTION, i_retryCount);
break;
} else {/*do default case;*/
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED.\n",
m_iErrCode ? DB_ROLLBACK_TRANSACTION : DB_COMMIT_TRANSACTION);
}
default: {
i_retFlag = 1;
if (p_errMsg) {
str_errMsg = p_errMsg;
str_errMsg.append(" database EXCEPTION!");
sqlite3_free(p_errMsg);
p_errMsg = NULL;
} else {
str_errMsg = "EXCEPTION: SQL error or missing database";
}
break;
}
}
if (i_retFlag)
break;
}
if (m_iErrCode != SQLITE_OK) {
EH_WARNING("CommitTransaction with error code:%d.conn:%d.trans code:%d.\n",i_errCode, m_pConn, m_iErrCode);
}
} else {
EH_WARNING("CommitTransaction no transaction or no conn.trans flag:%d conn:%d.\n",
m_iTransactionFlag,m_pConn);
}
if ( m_pConn) {
// close the db. it will open when invoke StartTransaction
sqlite3_close((sqlite3 *)m_pConn);
m_pConn = NULL;
}
m_iTransactionFlag = 0;
m_iErrCode = 0;
//EH_DEBUG("CommitTransaction try release lock...\n");
gThreadSqliteLock.release();
//gProcessSqliteLock.release();
//EH_DEBUG("CommitTransaction release lock ok.\n");
return i_errCode;
}
int EH_DB::AbortTransaction() {
EH_WARNING("Abort transaction try do commit.\n");
m_iErrCode = 1;
return CommitTransaction();
}
/*
* must be called when exception occur when in executing a transaction.
*/
int EH_DB::ExceptionInTransaction() {
EH_WARNING("Exception in transaction try do commit.\n");
m_iErrCode = 1;
return CommitTransaction();
}
DBRES EH_DB::ExecuteQuery(string sql) {
int i_errCode = SQLITE_ERROR;
int i_retryCount = 0, i_retFlag = 0;;
char *p_errMsg = NULL;
string str_errMsg;
if (m_pConn == NULL) {
throw EH_EXCEPTION("ExecuteQuery EXCEPTION:connection instance is NULL");
}
if ( !m_iTransactionFlag ) {
throw EH_EXCEPTION("ExecuteQuery EXCEPTION:transaction is not start");
}
//EH_WARNING("ExecuteQuery,in sql:%s.\n",sql.c_str());
DBRES result = new DBRESVEC();
while( 1 ) {
i_errCode = sqlite3_exec((sqlite3*)m_pConn, sql.c_str(), execSql_callback, (void*)result, &p_errMsg);
switch (i_errCode) {
case SQLITE_OK:
i_retFlag = 1;
break;
case SQLITE_BUSY:
case SQLITE_LOCKED:
if ( i_retryCount < 10 ) {
i_retryCount++;
i_retFlag = 0;
if ( p_errMsg ) {
sqlite3_free(p_errMsg);
p_errMsg = NULL;
}
usleep(100*1000);
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED,try:%d.\n",sql.c_str(),i_retryCount);
break;
} else {/*do default case;*/
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED.\n",sql.c_str());
}
default:
i_retFlag = 1;
m_iErrCode = 1;
if ( p_errMsg ) {
str_errMsg = p_errMsg;
str_errMsg.append(" database EXCEPTION!");
sqlite3_free(p_errMsg);
p_errMsg = NULL;
} else {
str_errMsg = "EXCEPTION: SQL error or missing database";
}
break;
}
if ( i_retFlag )
break;
}
if ( m_iErrCode ) {
EH_WARNING("ExecuteQuery:%s failed.error info:%s.\n",sql.c_str(),str_errMsg.c_str());
string exceptionString = "ExecuteQuery:"+sql+"failed.error info:"+str_errMsg;
throw EH_EXCEPTION(exceptionString.c_str());
}
//EH_WARNING("ExecuteQuery,out sql:%s.\n",sql.c_str());
return result;
}
void EH_DB::ExecuteUpdate(string sql) {
int i_errCode = SQLITE_ERROR;
int i_retryCount = 0, i_retFlag = 0;;
char *p_errMsg = NULL;
string str_errMsg;
if (m_pConn == NULL) {
throw EH_EXCEPTION("ExecuteUpdate EXCEPTION:connection instance is NULL");
}
if ( !m_iTransactionFlag ) {
throw EH_EXCEPTION("ExecuteUpdate EXCEPTION:transaction is not start");
}
//EH_WARNING("ExecuteUpdate,in sql:%s.\n",sql.c_str());
while( 1 ) {
i_errCode = sqlite3_exec((sqlite3*)m_pConn, sql.c_str(), NULL, (void*)NULL, &p_errMsg);
switch (i_errCode) {
case SQLITE_OK:
i_retFlag = 1;
break;
case SQLITE_BUSY:
case SQLITE_LOCKED:
if ( i_retryCount < 10 ) {
i_retryCount++;
i_retFlag = 0;
if ( p_errMsg ) {
sqlite3_free(p_errMsg);
p_errMsg = NULL;
}
usleep(100*1000);
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED,try:%d.\n",sql.c_str(),i_retryCount);
break;
} else {//do default case;
EH_WARNING("do:%s SQLITE_BUSY or SQLITE_LOCKED.\n",sql.c_str());
}
default:
i_retFlag = 1;
m_iErrCode = 1;
if ( p_errMsg ) {
str_errMsg = p_errMsg;
str_errMsg.append(" database EXCEPTION!");
sqlite3_free(p_errMsg);
p_errMsg = NULL;
} else {
str_errMsg = "EXCEPTION: SQL error or missing database";
}
break;
}
if ( i_retFlag )
break;
}
if ( m_iErrCode ) {
EH_WARNING("ExecuteUpdate:%s failed.error info:%s.\n",sql.c_str(),str_errMsg.c_str());
string exceptionString = "ExecuteUpdate:"+sql+"failed.error info:"+str_errMsg;
throw EH_EXCEPTION(exceptionString.c_str());
}
//EH_WARNING("ExecuteUpdate,out sql:%s.\n",sql.c_str());
}
void EH_DB::ReleaseResultSet(DBRES& res) {
if (!res) {
return;
}
DBRESVEC::const_iterator it = res->begin();
while (it != res->end()) {
DBROW row = *it;
if (row) {
delete row;
row = NULL;
}
++it;
}
delete res;
res = NULL;
}
static int execSql_callback(void *p_otherData, int argc, char **argv, char **azColName) {
if ( !p_otherData )
return 0;
DBRES result = (DBRES)p_otherData;
DBROW row = new DBROWMAP();
for (int i = 0; i < argc; i++) {
string colName = azColName[i] ? azColName[i] : "";
string vlaue = argv[i] ? argv[i] : "";
row->insert(DBROWMAP::value_type(colName, vlaue));
}
result->push_back(DBRESVEC::value_type(row));
return 0;
}
EH_DB::~EH_DB() {
CloseConnection();
}
#endif
......@@ -187,7 +187,7 @@ int KK_MQTTSDK::init(string brokerAddr,string clientId,string username,string pa
MQTTAsync_setMessageArrivedCallback(S_client,NULL,onMessageArrived);
//此函数用于为特定客户端的传递完成事件设置回调函数
MQTTAsync_setDeliveryCompleteCallback(S_client,NULL,onDeliveryComplete);
//为客户端设置MQTTAsync_connected()回调函数
// 为客户端设置MQTTAsync_connected()回调函数
MQTTAsync_setConnected(S_client,NULL,onConnectBuild);
//为客户端设置MQTTAsync_disconnected()回调函数
MQTTAsync_setDisconnected(S_client,NULL,onDisConnected);
......@@ -213,28 +213,43 @@ int KK_MQTTSDK::subTopic(string topic,int qos,int waitTimeout){
// Init list of topics to subscribe
char** topicsPath = (char**)calloc(2, sizeof(char*));
int* topicsQoS = (int*)calloc(2, sizeof(int));
topic1 = topic+"/publishstate";
topicsPath[0] = const_cast<char*>(topic1.c_str());
char** topicsPath = (char**)calloc(7, sizeof(char*));
int* topicsQoS = (int*)calloc(7, sizeof(int));
topic1 = topic+"/device/delete"; //删除子设备
topicsPath[0] = const_cast<char*>(topic1.c_str());
topicsQoS[0] = 0;
topic2 = topic+"/commands";
topic2 = topic+"/device/set"; //6. 改变子设备状态
topicsPath[1] = const_cast<char*>(topic2.c_str());
topicsQoS[1] = 0;
topic3 = topic+"/devices_public"; //13. 获取设备列表信息
topicsPath[2] = const_cast<char*>(topic3.c_str());
topicsQoS[2] = 0;
topic4 = topic+"/cg_custom_msg"; //16. XCG发送自定义数据到PTP
topicsPath[3] = const_cast<char*>(topic4.c_str());
topicsQoS[3] = 0;
topic5 = topic+"/firmware/check/result"; //18. 查询最新的升级任务结果
topicsPath[4] = const_cast<char*>(topic5.c_str());
topicsQoS[4] = 0;
topic6 = topic+"/device/search"; //2. 搜索子设备
topicsPath[5] = const_cast<char*>(topic6.c_str());
topicsQoS[5] = 0;
topic7 = topic+"/device/stopsearch"; //20. 停止搜索子设备
topicsPath[6] = const_cast<char*>(topic7.c_str());
topicsQoS[6] = 0;
// Create subcribe parameters
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc ;
opts.onSuccess = onOptSuccess;
opts.onFailure = onOptFail;
opts.context = (void*)OPT_SUB;
for(int i = 0; i < 2; i++)
for(int i = 0; i < 7 ; i++)
{
LOGI("test:%s %d\n",topicsPath[i], topicsQoS[i]);
LOGI("topicsPath:%s topicsQoS: %d\n",topicsPath[i], topicsQoS[i]);
}
// Subscribe and disconnect if error
if ((rc = MQTTAsync_subscribeMany(S_client,2,topicsPath,topicsQoS, &opts)) != MQTTASYNC_SUCCESS){//订阅多个主题
if ((rc = MQTTAsync_subscribeMany(S_client,7,topicsPath,topicsQoS, &opts)) != MQTTASYNC_SUCCESS){//订阅多个主题
// if ((rc = MQTTAsync_subscribe(S_client,topic.c_str(), qos, &opts)) != MQTTASYNC_SUCCESS){//订阅单个主题
LOGW("Failed to start subscribe Many, return code:%d.\n", rc);
return -1;
......@@ -392,6 +407,8 @@ void* KK_MQTTSDK::run0(void* opt) {
KK_MQTTSDK* mqttheartbeat_pthread = (KK_MQTTSDK*) opt; //这个的含义是什么?
return mqttheartbeat_pthread->run(opt);
}
//MQTT心跳
void* KK_MQTTSDK::run(void* opt){
while (true) {
EH::Json::FastWriter writer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment