Commit 9523bf65 authored by 杨熙's avatar 杨熙

1.0.1:去除数据库,去除连接部分东西。连接XCG 发送心跳

parent 7fb42583
#include <stdio.h>
#include <string>
#include "json/writer.h"
#include "json/reader.h"
#include "json/value.h"
#include "KK_CcuConfig.h"
#include "LSC_Logger.h"
#include "KK_Util.h"
#include "MQ_CCU_Adapter.h"
KK_CcuConfig* KK_CcuConfig::ms_pInstance = NULL;
KK_CcuConfig* KK_CcuConfig::GetInstance() {
if (ms_pInstance == NULL) {
ms_pInstance = new KK_CcuConfig();
}
return ms_pInstance;
}
KK_CcuConfig::KK_CcuConfig(): db("hj", "hj", "127.0.0.1", "5432", "hjhome"){
}
int KK_CcuConfig::Open(){
try {
db.OpenConnection();
} catch (exception& e) {
LOGW("%s.\n",e.what());
return -1;
}
return 0;
}
//这个好像未用到
string KK_CcuConfig::getCentralAcGwMac(string NodeId){
string CentralAcGwMac = "";
DBRES result = NULL;
string sql = "SELECT mac from all_node_config where id = '"+NodeId+"';";
try {
db.StartTransaction();
result = db.ExecuteQuery(sql);
db.CommitTransaction();
if (result->size() == 1) {
CentralAcGwMac = result->at(0)->find("mac")->second;
} else {
LOGW("query table all_gw_config get error result size:%d.\n",result->size());
}
EH_DB::ReleaseResultSet(result);
} catch (exception& e) {
LOGW("%s.\n",e.what());
EH_DB::ReleaseResultSet(result);
}
return CentralAcGwMac;
}
This diff is collapsed.
/*
* KK_Update.h
*
* Created on: Dec 27, 2018
* Author: sds
*/
#ifndef KK_CCUCONFIG_H_
#define KK_CCUCONFIG_H_
#include <string>
#include "EH_DB.h"
using namespace std;
class KK_CcuConfig{
public: ////////先建立
class KKDevmapinfo {
public:
string dev_model_id;
string sn;
string mac;
};
public:
static KK_CcuConfig* GetInstance();
int Open();
string getCentralAcGwMac(string NodeId); //中控网关mac
private:
KK_CcuConfig();
private:
static KK_CcuConfig* ms_pInstance;
EH_DB db;
};
#endif /* KK_CCUCONFIG_H_ */
This diff is collapsed.
......@@ -21,11 +21,11 @@
#include "EH_Client.h"
#include "MQ_CCU_Config.h"
#include "kk_connect.h"
// #include "kk_connect.h"
#include "LSC_Logger.h"
#include "EH_ToolKit.h"
#include "EH_Exception.h"
#include "KK_CcuConfig.h"
// #include "KK_CcuConfig.h"
#define CCU_REQ_TIMEOUT 3
......@@ -147,7 +147,7 @@ int MQ_CCU_Adapter::open() {
client->init("", "", this, false);
//开启TCP 与UDP
kk_connect::GetInstance()->init( KK_HwDevManager::GetInstance()->getCcuId(), "",false);
// kk_connect::GetInstance()->init( KK_HwDevManager::GetInstance()->getCcuId(), "",false);
//连接Roma平台,绑定就进行启动
oninitmqttsdk();
......@@ -597,6 +597,7 @@ void MQ_CCU_Adapter::handleNewDevice(EH::Json::Value devices, EH::Json::Value ce
}
}
//开始处理空调内机
void MQ_CCU_Adapter::handleACindoor(string devroomid,string devname,int indoorunitsdevCount,EH::Json::Value centralACdevices,int infocome){
try {
LOGD("开始处理空调内机\n");
......@@ -783,7 +784,8 @@ void MQ_CCU_Adapter::SYNC_handle_AcGw_Devices(EH::Json::Value CentralAcGwsDevice
operate2NodeId[nodeid] = operateType;
string mac = KK_CcuConfig::GetInstance()->getCentralAcGwMac(nodeid);
string mac = "未接入以后再改";
// string mac = KK_CcuConfig::GetInstance()->getCentralAcGwMac(nodeid);
string channel = "1";
string devroomid = Gwdevice["room_id"].asString(); //房间号
string devname = Gwdevice["dev_name"].asString(); //设备的名称
......@@ -945,18 +947,20 @@ void MQ_CCU_Adapter::SendSETUPToCcu(string name,string DeviceIcon,string DevDode
void MQ_CCU_Adapter::oninitmqttsdk() {
try {
//goto跳转,谨慎使用。
loop:
EH::Json::Value connectStateInfo = KK_HwDevManager::GetInstance()->loadConnectState();//获取绑定信息
if(connectStateInfo["result"] == "1"){
// loop:
// EH::Json::Value connectStateInfo = KK_HwDevManager::GetInstance()->loadConnectState();//获取绑定信息
// if(connectStateInfo["result"] == "1"){
string heartbeat_Topic;
clientId = connectStateInfo["client_id"].asString();
username = connectStateInfo["device_name"].asString();
// clientId = connectStateInfo["client_id"].asString();
// username = connectStateInfo["device_name"].asString();
clientId = "123456";
username = "123456";
//订阅的 Topic
pushTopic = string("xChfVJ501599/out/")+username;
subTopic = string("xChfVJ501599/in/")+username; //****************临时信息
pushTopic = string("$xlink/ptp/{")+gw_id+"}"; //ptpo运维
subTopic = string("$xlink/ptpo/{")+gw_id+"}"; //ptp交互
willTopic = string("xChfVJ501599/out/")+username+"/heartbeat"; //遗嘱信息
heartbeat_Topic = string("xChfVJ501599/out/")+username+"/heartbeat";
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("127.0.0.1:23883", //正式环境
......@@ -968,10 +972,10 @@ void MQ_CCU_Adapter::oninitmqttsdk() {
LOGE("init mqtt sdk failed.\n");
return;
}
}else{
LOGI("与APP未绑定 Not bound to app\n");
sleep(1);
goto loop;
// }else{
// LOGI("与APP未绑定 Not bound to app\n");
// sleep(1);
// goto loop;
}
}catch (exception &e) {
......
This diff is collapsed.
......@@ -16,16 +16,16 @@
#include "json/value.h"
#include "json/reader.h"
#include "KK_CcuConfig.h"
// #include "KK_CcuConfig.h"
#include "EH_Client.h"
#include "KK_MqttSdk.h"
#include "kk_connect.h"
// #include "kk_connect.h"
#include "lib/EH_SDKDef.h"
#include "kk_handleDeviceInfo.h"
#include "KK_HwDevManager.h"
#define CENTRALAC_INDOORUNIT_NUM_MAX 64
#define MQ_VERSION "1.0.2.10"
#define MQ_VERSION "1.0.1"
using namespace std;
......
This diff is collapsed.
......@@ -20,7 +20,7 @@ int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { //主函数
if(argc == 2) {
string flag = argv[1];
if (flag == "-v") {
cout<<"Konkexinghai:"<< MQ_VERSION << endl;
cout<<"konkeyunzhiyi:"<< MQ_VERSION << endl;
exit(0);
}
}
......@@ -35,28 +35,28 @@ int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { //主函数
EH_ToolKit::SetCoredumpMsgDump();
//开启日志并开始记录
if (LSC_LOGGER::instance()->open("Konkexinghai", DEF_LOG_LEAVE) != 0) {
LOGE("Init lsc logger failed.\n");
if (LSC_LOGGER::instance()->open("konkeyunzhiyi", DEF_LOG_LEAVE) != 0) {
LOGE("Init lsc logger failed.\n");
ACE_OS::exit(1);
}
//创建ACE_进程 Konkexinghai
ACE_Process_Mutex processMutex("Konkexinghai"); //进程名检测
//创建ACE_进程 konkeyunzhiyi
ACE_Process_Mutex processMutex("konkeyunzhiyi"); //进程名检测
if (processMutex.tryacquire() < 0) { //
//相同的进程正在运行,当前进程将中止 。异常的判段处理
LOGE("The same process is running.current process will abort.\n");
return -1;
} else {
LOGI("Konke xinghai Starting...Version:%s \n",MQ_VERSION);
LOGI("Konke xinghai Starting...Version 编译时间:%s %s \n",__DATE__,__TIME__);
LOGI("Konkexinghai acquired.\n");
LOGI("konke yunzhiyi Starting...Version:%s \n",MQ_VERSION);
LOGI("konke yunzhiyi Starting...Version 编译时间:%s %s \n",__DATE__,__TIME__);
LOGI("konkeyunzhiyi acquired.\n");
}
if(KK_CcuConfig::GetInstance()->Open()!=0){
LOGE("Ccu Config failed.\n");
ACE_OS::exit(-1);
return -1;
}
// if(KK_CcuConfig::GetInstance()->Open()!=0){
// LOGE("Ccu Config failed.\n");
// ACE_OS::exit(-1);
// return -1;
// }
//暂不开启延时。
//sleep(10);
......@@ -86,8 +86,9 @@ int ACE_TMAIN(int argc, ACE_TCHAR* argv[]) { //主函数
static int serverSocket;
static void processReq(string req) { //处理请求
LOGI("1111111111111111111111111111111processReq req:%s \n",req.c_str());
if(req == "new_mqtt_tcp"){
KK_MQTTSDK::GetInstance()->reinit("tcp://121.37.12.6:1883");
KK_MQTTSDK::GetInstance()->reinit("tcp://127.0.0.1:23883");
}else if(req == "new_mqtt_ssl"){
KK_MQTTSDK::GetInstance()->reinit("ssl://120.55.149.201:8883");
}else if(req == "con_mqtt_0") {
......@@ -109,6 +110,7 @@ static void* run0(void* opt){
int n;
int len = sizeof(clientAddr);
while (true){
LOGI("2222222222222222222222222222222\n");
//接收信息
//recvfrom函数从套接字将一个数据包读入缓冲区。size参数指定要读取的最大字节数。
n = ACE_OS::recvfrom(serverSocket, buff, sizeof(buff), 0, (struct sockaddr*)&clientAddr, &len);
......
This diff is collapsed.
/*
* kk_connect.cpp
*
* Created on: 2020年10月1日
* Author: hj
*/
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include "json/reader.h"
#include "json/writer.h"
#include "json/value.h"
#include "EH_ToolKit.h"
#include "EH_FindCCU.h"
#include "kk_connect.h"
#include "LSC_Logger.h"
#include "MQ_CCU_Adapter.h"
kk_connect* kk_connect::ms_instance = NULL;
kk_connect* kk_connect::GetInstance() {
if (ms_instance == NULL) {
ms_instance = new kk_connect();
}
return ms_instance;
}
kk_connect::kk_connect() {
}
void* kk_connect::run0(void* opt) {
kk_connect* client = (kk_connect*) opt;
return client->run(opt);
}
//
int kk_connect::init(string ccuId, string accessKey, bool autoDestroyHandler) {
this->ccuId = ccuId;
this->accessKey = accessKey;
this->ccuServerPort = SERVER_LISTEN_PORT;
this->autoDestroyHandler = autoDestroyHandler;
#ifdef NOT_X86
this->ccuLocalIP = string(EH_ToolKit::GetLocalIP("eth1"));
#else
this->ccuLocalIP = string(EH_ToolKit::GetLocalIP());
#endif
this->ccuSn = EH_ToolKit::GetMacNoSpace();
LOGI(" ccuLocalIP:%s.\n", ccuLocalIP.c_str());
ccuServerIp = ccuLocalIP;
ccuServerPort = 6768;
findCCU = new EH_FindCCU();
// findCCU->init(ccuId, this,ccuLocalIP);
findCCU->init(ccuId,ccuLocalIP);
pthread_t tid;
pthread_create(&tid, NULL, run0, this);
return 0;
}
string kk_connect::sendLoginReq(string msg) {
string loginReq = "";
string logingwsn = ccuSn;
EH::Json::Reader reader;
EH::Json::Value buf;
if (!reader.parse(msg, buf) || !buf.isObject()) {
LOGW("Message format is wrong. msg is: %s\n",msg.c_str());
return "-1";
}
try{
EH::Json::Value arg;
arg["method"] = "bind";
arg["gw_id"] = buf["gw_id"];
arg["client_id"] = buf["client_id"];
arg["device_name"] = buf["device_name"];
//错误类型
//网关ID不符result=3 ;
if(logingwsn != buf["gw_id"].asString()){
// arg["gw_id"] = logingwsn;
arg["result"] = "3";
loginReq = arg.toStyledString();
return loginReq;
}
//网关未联网result=2
//进行网络校验
if (EH_ToolKit::Ping(string("114.114.114.114"))!= true) {
arg["result"] = "2";
loginReq = arg.toStyledString();
return loginReq;
}
//网关已被绑定result=1(如需绑定,请先解除绑定)
EH::Json::Value connectStateInfo = KK_HwDevManager::GetInstance()->loadConnectState();//获取绑定信息
if(connectStateInfo["result"] == "0"){ //0为未绑定
EH::Json::Value newconnectInfo;
newconnectInfo["result"] = "1";
newconnectInfo["client_id"] = buf["client_id"];
newconnectInfo["device_name"] = buf["device_name"];
int saveok = KK_HwDevManager::GetInstance()->saveConnectState(newconnectInfo);//获取绑定信息
LOGI("saves :%d.\n",saveok);
//保存成功就会进行连接Roma程序启动。void MQ_CCU_Adapter::oninitmqttsdk()
arg["result"] = "0"; //绑定成功result=0,
loginReq = arg.toStyledString();
return loginReq;
}else{
arg["result"] = "1"; //网关已被绑定result=1
loginReq = arg.toStyledString();
return loginReq;
}
} catch (exception &e) {
LOGW("%s.\n", e.what());
}
return "-1";
}
//
void* kk_connect::run(void* opt) {
//定义用于连接,和接收的socket
// bind
try{
int socket_fd,accept_fd;
sockaddr_in myserver, remote_addr;
if(( socket_fd = socket(PF_INET,SOCK_STREAM,IPPROTO_TCP)) < 0 ){
LOGW("socket() failed!.\n");
}
memset(&myserver,0,sizeof(myserver));
myserver.sin_family = AF_INET;
myserver.sin_addr.s_addr = htonl(INADDR_ANY);
myserver.sin_port = htons(6768);
//使用端口复用,在bind之前先设置好socket的选线,让其可以绑定在已经正在被别的socket使用的端口上。
setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(const void*)&opt,sizeof(1));
if( bind(socket_fd,(sockaddr*) &myserver,sizeof(myserver)) < 0 ) {
LOGW("bind() failed!.\n");
}
// listen
if( listen(socket_fd,50) < 0 ) {
LOGW("listen() failed!.\n");
}
LOGI("Step3: 侦听.\n");
// while(!destroyed) {
while(true) {
socklen_t sin_size = sizeof(struct sockaddr_in);
if(( accept_fd = accept(socket_fd,(struct sockaddr*) &remote_addr,&sin_size)) == -1 ){
LOGI("收发数据%d.\n",accept_fd);
LOGW("Accept error!.\n");
continue;
}
LOGI("收发数据%d.\n",accept_fd);
LOGI("Received a connection from %s\n",(char*) inet_ntoa(remote_addr.sin_addr));
if(!fork() ) {
char buffer[RECV_MSG_SIZE];
memset(buffer,0,RECV_MSG_SIZE);
if((read(accept_fd,buffer,RECV_MSG_SIZE)) < 0) {
LOGW("Accept error!.\n");
throw("Read() error!");
} else {
LOGI("收到的消息: %s\n",buffer);
string sendbuffer = buffer;
string loginReq = sendLoginReq(sendbuffer);
int loginReq_Len = loginReq.length();
//对下面进行封装,达到发送信息的要求
if(loginReq != "-1"){
LOGI("发送的消息: %s\n",loginReq.c_str());
send(accept_fd, (void *)loginReq.c_str(), loginReq_Len, 0);
}
}
}
close(accept_fd);
// close(socket_fd);
}
delete this;
} catch (exception &e) {
LOGW("%s.\n", e.what());
}
return NULL;
}
void kk_connect::destroy() {
// destroyed = true;
// close(connectToCcuFd);
LOGI("destroy:%s.\n", ccuId.c_str());
}
kk_connect::~kk_connect() {
// close(connectToCcuFd);
if (findCCU) {
findCCU->destroy();
findCCU = NULL;
}
LOGI("~EH_Client:%p.\n", this);
}
This diff is collapsed.
/*
* kk_connect.h
*
* Created on: 2020年10月1日
* Author: hj
*/
#ifndef KK_CONNECT_H_
#define KK_CONNECT_H_
#include <sys/types.h>
#include <vector>
#include <string>
#include "json/value.h"
// #include "../KK_HwDevManager.h"
using namespace std;
class EH_FindCCU;
class kk_connect{
public:
static kk_connect* GetInstance();
kk_connect();
virtual ~kk_connect();
int init(string ccuId, string accessKey,bool autoDestroyHandler);
static void* run0(void* opt);
void destroy();
private:
static kk_connect* ms_instance;
void* run(void* opt);
string sendLoginReq(string msg);
private:
string ccuId;
string accessKey;
string ccuServerIp;
string ccuLocalIP;
u_short ccuServerPort;
string ccuSn;
bool autoDestroyHandler;
EH_FindCCU* findCCU;
// string cacheInputMsg;
};
#endif /* KK_CONNECT_H_ */
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
EH_DB_Sqlite.o: ../src/lib/EH_DB_Sqlite.cpp
EH_Exception.o: ../src/lib/EH_Exception.cpp ../src/include/EH_Exception.h
This diff is collapsed.
EH_GetCallStack.o: ../src/lib/EH_GetCallStack.cpp ../src/lib/EH_DEBUG.h
This diff is collapsed.
This diff is collapsed.
......@@ -396,13 +396,13 @@ void* KK_MQTTSDK::run(void* opt){
while (true) {
EH::Json::FastWriter writer;
EH::Json::Value Payload;
Payload["gw_id"] = gw_id;
Payload["interval"] = 30;
string sendPayload = writer.write(Payload);
//与Roma平台30秒心跳
sleep(30);
// LOGD("Topic:%s Payload:%s\n",heartbeat_Topic.c_str(),sendPayload.c_str());
// sendMsg(heartbeat_Topic,sendPayload,0);
LOGD("Topic:%s Payload:%s\n",heartbeat_Topic.c_str(),sendPayload.c_str());
sendMsg(heartbeat_Topic,sendPayload,0);
}
//delete this;
return NULL;
......
This diff is collapsed.
KK_Util.o: ../src/lib/KK_Util.cpp ../src/include/KK_Util.h
This diff is collapsed.
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