Commit 6817d452 authored by chen.weican's avatar chen.weican

【修改内容】增加midware层对于mqtt连接状态的check

【提交人】陈伟灿
parent 1ed830cc
......@@ -24,6 +24,7 @@
#define KK_FILTER_STATUS_OFFLINE_REPLY "/thing/status/offline_reply"
#define KK_CLOUDSTATE_MSG "/thing/ccu/cloudstate"
#define KK_CLOUDSTATE_MSG_REPLY "/thing/ccu/cloudstate_reply"
const char DM_MSG_TO_MIDDWARE[] = "{\"msgtype\":\"%s\",\"productCode\":\"%s\",\"deviceCode\":\"%s\"}";
#define KK_TOPIC_SERVICE_DELIMITER '/'
......@@ -54,6 +55,14 @@ int _kk_sendto_cloud(cJSON *root)
//If topo delete,need UnSubscribe the topic
if(strstr(msgTypeStr->valuestring,KK_FILTER_DELETE_TOPIC) != NULL){
KK_Subdev_UnSubscribe(pData);
free(topic);
return 0;
}
else if(strstr(msgTypeStr->valuestring,KK_CLOUDSTATE_MSG) != NULL){
KK_Send_CloudState(kk_get_cloud_status());
free(topic);
return 0;
}
char* out=cJSON_Print(pData);
......@@ -215,7 +224,7 @@ static char * _kk_data_create(const char *topic,const char *data)
return out;
//free(out); /* Print to text, Delete the cJSON, print it, release the string. */
}
const char DM_MSG_CLOUDSTATE[] = "{\"msgId\":\"1\",\"version\":\"1.0\",\"params\":{\"IOTCloudState\":\"%d\"},\"method\":\"/thing/ccu/cloudstate\"}";
const char DM_MSG_CLOUDSTATE[] = "{\"msgId\":\"1\",\"version\":\"1.0\",\"params\":{\"IOTCloudState\":\"%d\"},\"method\":\"thing.ccu.cloudstate_reply\"}";
int KK_Send_CloudState(int state)
{
......@@ -226,14 +235,14 @@ int KK_Send_CloudState(int state)
cJSON *root;
char *out;
infoStr_len = strlen(DM_MSG_TO_MIDDWARE)+strlen(KK_CLOUDSTATE_MSG)+10;
infoStr_len = strlen(DM_MSG_TO_MIDDWARE)+strlen(KK_CLOUDSTATE_MSG_REPLY)+10;
infoStr = malloc(infoStr_len);
if(infoStr == NULL){
ERROR_PRINT("[%s][%d]\n",__FUNCTION__,__LINE__);
return -1;
}
memset(infoStr,0x0,infoStr_len);
snprintf(infoStr,infoStr_len,DM_MSG_TO_MIDDWARE,KK_CLOUDSTATE_MSG,"","");
snprintf(infoStr,infoStr_len,DM_MSG_TO_MIDDWARE,KK_CLOUDSTATE_MSG_REPLY,"","");
payloadStr_len = strlen(DM_MSG_CLOUDSTATE) + 10;
payloadStr = malloc(payloadStr_len);
......
......@@ -10,7 +10,11 @@ static const char* OPT_SUB = "MQTTAsync_subscribe";
static const char* OPT_UNSUB = "MQTTAsync_unsubscribe";
static MQTTAsync s_Client;
static int s_mqttStop = 0;
static int s_cloudStatus = 0;
int kk_get_cloud_status(void){
return s_cloudStatus;
}
/*Connect lost callback*/
static void connlost(void *context, char *cause)
{
......@@ -26,7 +30,8 @@ static void connlost(void *context, char *cause)
INFO_PRINT("\nConnection lost\n");
INFO_PRINT("cause: %s\n", cause);
KK_Send_CloudState(0);
s_cloudStatus = 0;
KK_Send_CloudState(s_cloudStatus);
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;
if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS)
......@@ -98,7 +103,8 @@ void onConnectFailure(void* context, MQTTAsync_failureData* response)
void onConnect(void* context, MQTTAsync_successData* response)
{
INFO_PRINT("Successful connection\n");
KK_Send_CloudState(1);
s_cloudStatus = 1;
KK_Send_CloudState(s_cloudStatus);
}
......
......@@ -104,6 +104,15 @@ int iotx_dm_subdev_topo_del(_IN_ int devid)
_dm_api_unlock();
return res;
}
int iotx_dm_ccu_cloud_check(void)
{
int res = 0;
_dm_api_lock();
res = dm_mgr_ccu_status_cloud(0);
_dm_api_unlock();
return res;
}
int iotx_dm_dev_online(_IN_ int devid)
{
......
......@@ -943,6 +943,59 @@ int dm_mgr_upstream_thing_list_found(_IN_ int devid)
return res;
}
const char DM_URI_STATUS_CLOUD[] = "/thing/ccu/cloudstate";
int dm_mgr_ccu_status_cloud(_IN_ int devid)
{
int res = 0;
dm_mgr_dev_node_t *node = NULL;
dm_mgr_dev_node_t *gw_node = NULL;
dm_msg_request_t request;
if (devid < 0) {
return INVALID_PARAMETER;
}
memset(&request, 0, sizeof(dm_msg_request_t));
res = dm_mgr_search_dev_by_devid(devid, &node);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
memcpy(request.productCode,node->productCode,strlen(node->productCode));
memcpy(request.deviceCode,node->deviceCode,strlen(node->deviceCode));
request.msgTypeStr = DM_URI_STATUS_CLOUD;
/* Get Params And Method */
res = dm_msg_status_cloud( node->productCode, node->deviceCode, &request);
if (res != SUCCESS_RETURN) {
return FAIL_RETURN;
}
printf("[%s][%d]\n",__FUNCTION__,__LINE__);
/* Get Msg ID */
request.msgid = iotx_report_id();
/* Get Dev ID */
request.devid = devid;
/* Callback */
//request.callback = dm_client_combine_login_reply;
/* Send Message To Cloud */
res = dm_msg_request(&request);
if (res == SUCCESS_RETURN) {
res = request.msgid;
}
free(request.params);
return res;
}
const char DM_URI_STATUS_ONLINE[] = "/thing/status/online";
int dm_mgr_upstream_status_online(_IN_ int devid)
{
......
......@@ -259,6 +259,40 @@ int dm_msg_thing_topo_delete(_IN_ char deviceCode[DEVICE_CODE_MAXLEN],_OU_ dm_ms
return SUCCESS_RETURN;
}
const char DM_MSG_STATUS_CLOUD_METHOD[] DM_READ_ONLY = "thing.ccu.cloudstate";
const char DM_MSG_STATUS_CLOUD[] DM_READ_ONLY =
"{\"deviceCode\":\"%s\"}";
int dm_msg_status_cloud(_IN_ char productCode[PRODUCT_CODE_MAXLEN],
_IN_ char deviceCode[DEVICE_CODE_MAXLEN], _OU_ dm_msg_request_t *request)
{
char *params = NULL;
int params_len = 0;
if (request == NULL ||
deviceCode == NULL || productCode == NULL ||
(strlen(deviceCode) >= DEVICE_CODE_MAXLEN) ||
(strlen(productCode) >= PRODUCT_CODE_MAXLEN) ||
(strlen(request->deviceCode) >= DEVICE_CODE_MAXLEN)) {
return INVALID_PARAMETER;
}
/* Params */
request->method = (char *)DM_MSG_STATUS_CLOUD_METHOD;
params_len = strlen(DM_MSG_STATUS_CLOUD) + strlen(deviceCode) + 1;
params = malloc(params_len);
if (params == NULL) {
return DM_MEMORY_NOT_ENOUGH;
}
memset(params, 0, params_len);
HAL_Snprintf(params, params_len, DM_MSG_STATUS_CLOUD,deviceCode);
request->params = params;
request->params_len = strlen(request->params);
return SUCCESS_RETURN;
}
const char DM_MSG_COMBINE_STATUS_ONLINE_METHOD[] DM_READ_ONLY = "thing.status.online";
const char DM_MSG_COMBINE_STATUS_ONLINE[] DM_READ_ONLY =
"{\"deviceCode\":\"%s\"}";
......@@ -536,7 +570,6 @@ int dm_msg_request (_IN_ dm_msg_request_t *request)
free(payload);
return FAIL_RETURN;
}
//if (type & DM_MSG_DEST_CLOUD) {
// dm_client_publish(uri, (unsigned char *)payload, strlen(payload), request->callback);
kk_sendData2app(req_info, payload);
......
......@@ -44,10 +44,10 @@ const char DM_MSG_INFO[] DM_READ_ONLY;
#define KK_REGISTER_TOPIC_REPLY "/thing/sub/register_reply"
#define KK_ADD_TOPIC_REPLY "/thing/topo/add_reply"
#define KK_LOGIN_TOPIC_REPLY "/thing/combine/login_reply"
#define KK_ONLINE_TOPIC_REPLY "/thing/status/online_reply"
#define KK_ONLINE_TOPIC_REPLY "/thing/status/online_reply"
#define KK_THING_SERVICE_PROPERTY_SET "/thing/service/property/set"
#define KK_THING_OTA_DEVICE_UPGRADE "/ota/device/upgrade"
#define KK_THING_CLOUDSTATE_MSG "/thing/ccu/cloudstate"
#define KK_THING_CLOUDSTATE_MSG "/thing/ccu/cloudstate_reply"
//const char DM_URI_SYS_PREFIX[] DM_READ_ONLY = "/sys/%s/%s/";
......
......@@ -225,9 +225,15 @@ static void _iotx_linkkit_upstream_callback_remove(int msgid, int code)
extern void dm_server_free_context(_IN_ void *ctx);
#endif
static int s_CloudStatus = 0;
static int s_CloudStatusRecv = 0;
int kk_get_cloudstatus(void){
return s_CloudStatus;
}
int kk_get_cloud_recv_status(void){
return s_CloudStatusRecv;
}
static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
{
INFO_PRINT("_iotx_linkkit_event_callback ================== [%s]\n",data);
......@@ -295,6 +301,7 @@ static void _iotx_linkkit_event_callback(iotx_dm_event_types_t type, char *data)
//kk_tsl_service_property_set(topic->valuestring, payload->valuestring, strlen(payload->valuestring), NULL);
}else if (strstr(typeJson->valuestring,KK_THING_CLOUDSTATE_MSG)){
INFO_PRINT("cloud state notify \n");
s_CloudStatusRecv = 1;
cJSON *paramStr = cJSON_GetObjectItem(payload, MSG_PARAMS_STR);
cJSON *state = cJSON_GetObjectItem(paramStr, MSG_IOTClOUDSTATE_STR);
s_CloudStatus = atoi(state->valuestring);
......
......@@ -566,6 +566,11 @@ void *ccu_property_monitor(void *args)
int time_second = 60;
while (mid_ctx->g_ccuProChg_dispatch_thread_running) {
if(kk_get_cloud_recv_status() == 0){
iotx_dm_ccu_cloud_check();
sleep(10);
continue;
}
//dm_ota_yield(MID_YIELD_TIMEOUT_MS);
HAL_Get_IP(s_IP,NULL);
res = kk_tsl_get_value(kk_tsl_get_property_value,0,KK_TSL_CCU_WANIP_IDENTIFIER,NULL,&s_IP_TSL);
......
......@@ -30,7 +30,7 @@ normal = "%d %-6V (%f:%L) %m%n"
[rules]
*.DEBUG >stdout; normal
*.INFO >stdout; normal
*.* "/home/kk/share/%c.log", 1MB*2; normal
......
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