int SensorData_Upload(char *dataPoint, void* value, unsigned char datatype) { int ret = 0; EdpPacket* update_pkg; switch (datatype) { default: case DATATYPE_INT: update_pkg = PacketSavedataInt(kTypeFullJson, SRC_DEVID, dataPoint, *(int*)value, 0, EDP_TOKEN); break; case DATATYPE_DOUBLE: update_pkg = PacketSavedataDouble(kTypeFullJson, SRC_DEVID, dataPoint, *(double*)value, 0, EDP_TOKEN); break; case DATATYPE_STRING: update_pkg = PacketSavedataString(kTypeFullJson, SRC_DEVID, dataPoint, (const char*)value, 0, EDP_TOKEN); break; } OSTimeDly(10); /* 发送转存数据到OneNet并转发到Dev2设备 */ ret = Socket_TCPClientSendData((char*)update_pkg->_data, update_pkg->_write_pos); if (ret <= 0) { log_info("Save and transmit full json data failed(ret:%d).\n", ret); } else { log_notice("Save and transmit full json data success.\n"); } DeleteBuffer(&update_pkg); return 0; }
int Agent_cloud_process(void) { EdpPacket* connect_pkg = NULL; int ret; unsigned char failedcnt = 0; cloud_conn_status.conn_status = SOCK_DONE; #ifndef Devid_RestFul AGAIN: /* 向Onenet服务器发送EDP连接报文 */ connect_pkg = PacketConnect1((char*)SRC_DEVID, (char*)CLOUD_API_KEY); /* dump package data */ log_info("Packet connect data(len:%d):\n", connect_pkg->_write_pos); dump_hex(connect_pkg->_data, connect_pkg->_write_pos); log_info("SRC_DEVID:%s CLOUD_API_KEY:%s\r\n",SRC_DEVID,CLOUD_API_KEY); ret = Socket_TCPClientSendData((char *)connect_pkg->_data, connect_pkg->_write_pos); DeleteBuffer(&connect_pkg); if (ret < 0) { cloud_conn_status.conn_status = CLOUD_CONN_ERROR; log_info("Send connect data failed(ret:%d).\n", ret); failedcnt++; /* 发送连接次数超过3次则退出连接 */ if (failedcnt > 3) { Socket_TCPClientClose(); return -1; } OSTimeDly(50); goto AGAIN; } else { log_notice("Send connect data success.\n"); } #endif /* 主进程用于接收云端服务器的数据 */ while (1) { ret = Socket_TCPClientRecvData(recv_buf, CLOUD_RECV_BUFFER_SIZE); if (ret > 0) { recv_buf[ret] = '\0'; #if (4 & Devid_Mode) //restful方式 log_notice("%s\r\n",recv_buf); RestFul_RecvProcess(recv_buf,ret); #else recv_data_process(recv_buf, ret); #endif } if (ret < 0 || TCPClient_reset_flag == 0xa5 ) { //关闭TCP链接 log_err("Recv tcp client data error(%d)\n", ret); Socket_TCPClientClose(); return -1; } } }
/* * 向OneNet云端发送心跳包 * */ void cloud_heartbeat_timer_handle(void *ptmr, void *parg) { int ret = 0; EdpPacket* send_pkg = NULL; if (cloud_conn_status.conn_status == CLOUD_CONN_DONE) { /* 封装心跳包 */ send_pkg = PacketPing(); /* 发送心跳包 */ ret = Socket_TCPClientSendData((char*)send_pkg->_data, send_pkg->_write_pos); if (ret < 0) { log_info("Send Heartbeat data failed(ret:%d).\n", ret); } else { log_notice("Send Heartbeat data success.\n"); } DeleteBuffer(&send_pkg); } }
int SensorData_PassThrough(char *dest, void* data, unsigned char datalen) { int ret = 0; EdpPacket* update_pkg; //透传数据,设备1传给设备2 update_pkg = PacketPushdata(dest, data, datalen); //dump_hex(update_pkg->_data, update_pkg->_write_pos); /* 发送转存数据到OneNet并转发到Dev2设备 */ ret = Socket_TCPClientSendData((char*)update_pkg->_data, update_pkg->_write_pos); if (ret <= 0) { log_info("Pass through and transmit full json data failed(ret:%d).\n", ret); } else { log_notice("Pass through and transmit full json data success.\n"); } DeleteBuffer(&update_pkg); return 0; }
void recv_data_process(char * buffer, int len) { int rtn; uint8 mtype; EdpPacket* pkg; //LOUIS add uint8 jsonorbin; char* src_devid; cJSON* save_json; char* save_json_str; cJSON* desc_json; char* desc_json_str; char* save_bin; uint32 save_binlen; char* json_ack; char* push_data; uint32 push_datalen; char* cmdid; uint16 cmdid_len; char* cmd_req; uint32 cmd_req_len; EdpPacket* send_pkg; // char* ds_id; // double dValue = 0; char* simple_str = NULL; char cmd_resp[] = "ok"; unsigned cmd_resp_len = 0; RecvBuffer recvbuf; recvbuf._data = (uint8*)buffer; recvbuf._write_pos = len; recvbuf._read_pos = 0; recvbuf._capacity = BUFFER_SIZE; log_info("Recv data(len:%d).\n", len); while (1) { /* 获取一个完成的EDP包 */ if ((pkg = GetEdpPacket(&recvbuf)) == 0) { log_info("need more bytes...\n"); break; } /* 获取这个EDP包的消息类型,根据这个EDP包的消息类型, 分别做EDP包解析 */ mtype = EdpPacketType(pkg); switch(mtype) { case CONNRESP: /* 解析EDP包 - 连接响应 */ rtn = UnpackConnectResp(pkg); log_info("Receive connect response, rtn: %d\n", rtn); /* 启动Sensor Task任务 */ SensorTaskFlag = 1; cloud_conn_status.conn_status = CLOUD_CONN_DONE; break; case PINGRESP: /* 解析EDP包 - 心跳响应 */ UnpackPingResp(pkg); log_info("recv ping resp\n"); break; case PUSHDATA: /* 解析EDP包 - 数据转发 */ UnpackPushdata(pkg, &src_devid, &push_data, &push_datalen); log_info("recv push data, src_devid: %s, push_data: %s, len: %d\n", src_devid, push_data, push_datalen); free(src_devid); free(push_data); break; case SAVEDATA: /* 解析EDP包 - 数据存储 */ if (UnpackSavedata(pkg, &src_devid, &jsonorbin) == 0) { if (jsonorbin == kTypeFullJson || jsonorbin == kTypeSimpleJsonWithoutTime || jsonorbin == kTypeSimpleJsonWithTime) { printf("json type is %d\n", jsonorbin); /* 解析EDP包 - json数据存储 */ UnpackSavedataJson(pkg, &save_json); save_json_str = cJSON_Print(save_json); log_info("recv save data json, src_devid: %s, json: %s\n", src_devid, save_json_str); free(save_json_str); cJSON_Delete(save_json); /* UnpackSavedataInt(jsonorbin, pkg, &ds_id, &iValue); */ /* log_info("ds_id = %s\nvalue= %d\n", ds_id, iValue); */ //UnpackSavedataDouble((SaveDataType)jsonorbin, pkg, &ds_id, &dValue); //log_info("ds_id = %s\nvalue = %f\n", ds_id, dValue); /* UnpackSavedataString(jsonorbin, pkg, &ds_id, &cValue); */ /* log_info("ds_id = %s\nvalue = %s\n", ds_id, cValue); */ /* free(cValue); */ //free(ds_id); } else if (jsonorbin == kTypeBin) {/* 解析EDP包 - bin数据存储 */ UnpackSavedataBin(pkg, &desc_json, (uint8**)&save_bin, &save_binlen); desc_json_str=cJSON_Print(desc_json); log_info("recv save data bin, src_devid: %s, desc json: %s, bin: %s, binlen: %d\n", src_devid, desc_json_str, save_bin, save_binlen); free(desc_json_str); cJSON_Delete(desc_json); free(save_bin); } else if (jsonorbin == kTypeString){ UnpackSavedataSimpleString(pkg, &simple_str); log_info("%s\n", simple_str); free(simple_str); } free(src_devid); }else{ printf("error\n"); } break; case SAVEACK: json_ack = NULL; UnpackSavedataAck(pkg, &json_ack); log_info("save json ack = %s\n", json_ack); free(json_ack); break; case CMDREQ: log_info("recevice server cmd:\r\n"); dump_hex(pkg->_data, pkg->_write_pos); if (UnpackCmdReq(pkg, &cmdid, &cmdid_len, &cmd_req, &cmd_req_len) == 0){ /* * 用户按照自己的需求处理并返回,响应消息体可以为空,此处假设返回2个字符"ok"。 * 处理完后需要释放 */ cmd_resp_len = strlen(cmd_resp); send_pkg = PacketCmdResp(cmdid, cmdid_len, cmd_resp, cmd_resp_len); #ifdef _ENCRYPT if (g_is_encrypt){ SymmEncrypt(send_pkg); } #endif Socket_TCPClientSendData((char*)send_pkg->_data, send_pkg->_write_pos); DeleteBuffer(&send_pkg); free(cmdid); free(cmd_req); } break; default: /* 未知消息类型 */ log_info("recv failed...\n"); break; } DeleteBuffer(&pkg); } }
VOID Yeelink_Run(VOID *arg) { int ret; char *buffer = NULL; while(1){ if(TCPClient_reset_flag == 1) { //得到各个传感器数值 GetSensorData(); //粉尘浓度 376377 buffer = yeelink_post(AirTest_ID,Dust_ID,sensordata.Dust); ret = Socket_TCPClientSendData(buffer,strlen(buffer)); if (ret <= 0) { Yeelink_Printf(GAGENT_WARNING,"Dust=>Send failed(ret:%d).\n", ret); } else { TCPClient_reset_flag = 2; Yeelink_Printf(GAGENT_WARNING,"Dust=>Send success.\n"); } while(TCPClient_reset_flag != 1) OSTimeDly(100); //室内温度 376378 buffer = yeelink_post(AirTest_ID,Temp_ID,sensordata.Temperature); ret = Socket_TCPClientSendData(buffer,strlen(buffer)); if (ret <= 0) { Yeelink_Printf(GAGENT_WARNING,"Temperature=>Send data failed(ret:%d).\n", ret); } else { TCPClient_reset_flag = 2; Yeelink_Printf(GAGENT_WARNING,"Temperature=>Send data success.\n"); } while(TCPClient_reset_flag != 1) OSTimeDly(10); OSTimeDly(100); //甲醛浓度 376382 buffer = yeelink_post(AirTest_ID,Methanal_ID,sensordata.Methanal); ret = Socket_TCPClientSendData(buffer,strlen(buffer)); if (ret <= 0) { Yeelink_Printf(GAGENT_WARNING,"Methanal=>Send data failed(ret:%d).\n", ret); } else { TCPClient_reset_flag = 2; Yeelink_Printf(GAGENT_WARNING,"Methanal=>Send data success.\n"); } while(TCPClient_reset_flag != 1) OSTimeDly(100); //室内湿度 376383 buffer = yeelink_post(AirTest_ID,Humidity_ID,(int)sensordata.Humidity); ret = Socket_TCPClientSendData(buffer,strlen(buffer)); if (ret <= 0) { Yeelink_Printf(GAGENT_WARNING,"Humidity=>Send data failed(ret:%d).\n", ret); } else { TCPClient_reset_flag = 2; Yeelink_Printf(GAGENT_WARNING,"Humidity=>Send data success.\n"); } while(TCPClient_reset_flag != 1) OSTimeDly(100); //空气质量指数 376376 buffer = yeelink_post(AirTest_ID,AQI_ID,sensordata.AQI); ret = Socket_TCPClientSendData(buffer,strlen(buffer)); if (ret <= 0) { Yeelink_Printf(GAGENT_WARNING,"AQI=>Send data failed(ret:%d).\n", ret); } else { TCPClient_reset_flag = 2; Yeelink_Printf(GAGENT_WARNING,"AQI=>Send data success.\n"); } while(TCPClient_reset_flag != 1) OSTimeDly(100); } OSTimeDly(100); } }