Improve readability of code
more readability
This commit is contained in:
@@ -15,7 +15,6 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
// #include <unistd.h>
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -46,55 +45,54 @@ size_t sg_data_report_buffersize = sizeof(sg_data_report_buffer) / sizeof(sg_dat
|
|||||||
#include "events_config.c"
|
#include "events_config.c"
|
||||||
static void update_events_timestamp(sEvent *pEvents, int count)
|
static void update_events_timestamp(sEvent *pEvents, int count)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i = 0; i < count; i++){
|
for(i = 0; i < count; i++){
|
||||||
if (NULL == (&pEvents[i])) {
|
if (NULL == (&pEvents[i])) {
|
||||||
Log_e("null event pointer");
|
Log_e("null event pointer");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#ifdef EVENT_TIMESTAMP_USED
|
#ifdef EVENT_TIMESTAMP_USED
|
||||||
pEvents[i].timestamp = time(NULL); //should be UTC and accurate
|
pEvents[i].timestamp = time(NULL); //should be UTC and accurate
|
||||||
#else
|
#else
|
||||||
pEvents[i].timestamp = 0;
|
pEvents[i].timestamp = 0;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void event_post_cb(void *pClient, MQTTMessage *msg)
|
static void event_post_cb(void *pClient, MQTTMessage *msg)
|
||||||
{
|
{
|
||||||
Log_d("Reply:%.*s", msg->payload_len, msg->payload);
|
Log_d("Reply:%.*s", msg->payload_len, msg->payload);
|
||||||
IOT_Event_clearFlag(pClient, FLAG_EVENT0|FLAG_EVENT1|FLAG_EVENT2);
|
IOT_Event_clearFlag(pClient, FLAG_EVENT0|FLAG_EVENT1|FLAG_EVENT2);
|
||||||
}
|
}
|
||||||
|
|
||||||
//event check and post
|
//event check and post
|
||||||
static void eventPostCheck(void *client)
|
static void eventPostCheck(void *client)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int rc;
|
int rc;
|
||||||
uint32_t eflag;
|
uint32_t eflag;
|
||||||
uint8_t event_count;
|
uint8_t event_count;
|
||||||
sEvent *pEventList[EVENT_COUNTS];
|
sEvent *pEventList[EVENT_COUNTS];
|
||||||
|
|
||||||
IOT_Event_setFlag(client, FLAG_EVENT0|FLAG_EVENT1|FLAG_EVENT2);
|
IOT_Event_setFlag(client, FLAG_EVENT0|FLAG_EVENT1|FLAG_EVENT2);
|
||||||
eflag = IOT_Event_getFlag(client);
|
eflag = IOT_Event_getFlag(client);
|
||||||
if((EVENT_COUNTS > 0 )&& (eflag > 0)){
|
if((EVENT_COUNTS > 0 )&& (eflag > 0)){
|
||||||
event_count = 0;
|
event_count = 0;
|
||||||
for(i = 0; i < EVENT_COUNTS; i++){
|
for(i = 0; i < EVENT_COUNTS; i++){
|
||||||
if((eflag&(1<<i))&ALL_EVENTS_MASK){
|
if((eflag&(1<<i))&ALL_EVENTS_MASK){
|
||||||
pEventList[event_count++] = &(g_events[i]);
|
pEventList[event_count++] = &(g_events[i]);
|
||||||
update_events_timestamp(&g_events[i], 1);
|
update_events_timestamp(&g_events[i], 1);
|
||||||
IOT_Event_clearFlag(client, 1<<i);
|
IOT_Event_clearFlag(client, 1<<i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = IOT_Post_Event(client, sg_data_report_buffer, sg_data_report_buffersize, \
|
|
||||||
event_count, pEventList, event_post_cb);
|
|
||||||
if(rc < 0){
|
|
||||||
Log_e("events post failed: %d", rc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
rc = IOT_Post_Event(client, sg_data_report_buffer, sg_data_report_buffersize, \
|
||||||
|
event_count, pEventList, event_post_cb);
|
||||||
|
if(rc < 0){
|
||||||
|
Log_e("events post failed: %d", rc);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -105,145 +103,143 @@ static void eventPostCheck(void *client)
|
|||||||
// action : regist action and set the action handle callback, add your aciton logic here
|
// action : regist action and set the action handle callback, add your aciton logic here
|
||||||
static void OnActionCallback(void *pClient, const char *pClientToken, DeviceAction *pAction)
|
static void OnActionCallback(void *pClient, const char *pClientToken, DeviceAction *pAction)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
sReplyPara replyPara;
|
sReplyPara replyPara;
|
||||||
|
|
||||||
//do something base on input, just print as an sample
|
//do something base on input, just print as an sample
|
||||||
DeviceProperty *pActionInput = pAction->pInput;
|
DeviceProperty *pActionInput = pAction->pInput;
|
||||||
for (i = 0; i < pAction->input_num; i++) {
|
for (i = 0; i < pAction->input_num; i++) {
|
||||||
if (JSTRING == pActionInput[i].type) {
|
if (JSTRING == pActionInput[i].type) {
|
||||||
Log_d("Input:[%s], data:[%s]", pActionInput[i].key, pActionInput[i].data);
|
Log_d("Input:[%s], data:[%s]", pActionInput[i].key, pActionInput[i].data);
|
||||||
HAL_Free(pActionInput[i].data);
|
HAL_Free(pActionInput[i].data);
|
||||||
} else {
|
} else {
|
||||||
if(JINT32 == pActionInput[i].type) {
|
if(JINT32 == pActionInput[i].type) {
|
||||||
Log_d("Input:[%s], data:[%d]", pActionInput[i].key, *((int*)pActionInput[i].data));
|
Log_d("Input:[%s], data:[%d]", pActionInput[i].key, *((int*)pActionInput[i].data));
|
||||||
} else if( JFLOAT == pActionInput[i].type) {
|
} else if( JFLOAT == pActionInput[i].type) {
|
||||||
Log_d("Input:[%s], data:[%f]", pActionInput[i].key, *((float*)pActionInput[i].data));
|
Log_d("Input:[%s], data:[%f]", pActionInput[i].key, *((float*)pActionInput[i].data));
|
||||||
} else if( JUINT32 == pActionInput[i].type) {
|
} else if( JUINT32 == pActionInput[i].type) {
|
||||||
Log_d("Input:[%s], data:[%u]", pActionInput[i].key, *((uint32_t*)pActionInput[i].data));
|
Log_d("Input:[%s], data:[%u]", pActionInput[i].key, *((uint32_t*)pActionInput[i].data));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// construct output
|
// construct output
|
||||||
memset((char *)&replyPara, 0, sizeof(sReplyPara));
|
memset((char *)&replyPara, 0, sizeof(sReplyPara));
|
||||||
replyPara.code = eDEAL_SUCCESS;
|
replyPara.code = eDEAL_SUCCESS;
|
||||||
replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||||
strcpy(replyPara.status_msg, "action execute success!"); //add the message about the action resault
|
strcpy(replyPara.status_msg, "action execute success!"); //add the message about the action resault
|
||||||
|
|
||||||
|
DeviceProperty *pActionOutnput = pAction->pOutput;
|
||||||
|
(void)pActionOutnput; //elimate warning
|
||||||
|
//TO DO: add your aciont logic here and set output properties which will be reported by action_reply
|
||||||
|
|
||||||
DeviceProperty *pActionOutnput = pAction->pOutput;
|
IOT_ACTION_REPLY(pClient, pClientToken, sg_data_report_buffer, sg_data_report_buffersize, pAction, &replyPara);
|
||||||
(void)pActionOutnput; //elimate warning
|
|
||||||
//TO DO: add your aciont logic here and set output properties which will be reported by action_reply
|
|
||||||
|
|
||||||
|
|
||||||
IOT_ACTION_REPLY(pClient, pClientToken, sg_data_report_buffer, sg_data_report_buffersize, pAction, &replyPara);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int _register_data_template_action(void *pTemplate_client)
|
static int _register_data_template_action(void *pTemplate_client)
|
||||||
{
|
{
|
||||||
int i,rc;
|
int i,rc;
|
||||||
|
|
||||||
for (i = 0; i < TOTAL_ACTION_COUNTS; i++) {
|
for (i = 0; i < TOTAL_ACTION_COUNTS; i++) {
|
||||||
rc = IOT_Template_Register_Action(pTemplate_client, &g_actions[i], OnActionCallback);
|
rc = IOT_Template_Register_Action(pTemplate_client, &g_actions[i], OnActionCallback);
|
||||||
if (rc != QCLOUD_RET_SUCCESS) {
|
if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
rc = IOT_Template_Destroy(pTemplate_client);
|
rc = IOT_Template_Destroy(pTemplate_client);
|
||||||
Log_e("register device data template action failed, err: %d", rc);
|
Log_e("register device data template action failed, err: %d", rc);
|
||||||
return rc;
|
return rc;
|
||||||
} else {
|
} else {
|
||||||
Log_i("data template action=%s registered.", g_actions[i].pActionId);
|
Log_i("data template action=%s registered.", g_actions[i].pActionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return QCLOUD_RET_SUCCESS;
|
return QCLOUD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
static void event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg)
|
static void event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg)
|
||||||
{
|
{
|
||||||
uintptr_t packet_id = (uintptr_t)msg->msg;
|
uintptr_t packet_id = (uintptr_t)msg->msg;
|
||||||
|
|
||||||
switch(msg->event_type) {
|
switch(msg->event_type) {
|
||||||
case MQTT_EVENT_UNDEF:
|
case MQTT_EVENT_UNDEF:
|
||||||
Log_i("undefined event occur.");
|
Log_i("undefined event occur.");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_DISCONNECT:
|
case MQTT_EVENT_DISCONNECT:
|
||||||
Log_i("MQTT disconnect.");
|
Log_i("MQTT disconnect.");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_RECONNECT:
|
case MQTT_EVENT_RECONNECT:
|
||||||
Log_i("MQTT reconnect.");
|
Log_i("MQTT reconnect.");
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_SUBCRIBE_SUCCESS:
|
case MQTT_EVENT_SUBCRIBE_SUCCESS:
|
||||||
sg_subscribe_event_result = msg->event_type;
|
sg_subscribe_event_result = msg->event_type;
|
||||||
Log_i("subscribe success, packet-id=%u", packet_id);
|
Log_i("subscribe success, packet-id=%u", packet_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_SUBCRIBE_TIMEOUT:
|
case MQTT_EVENT_SUBCRIBE_TIMEOUT:
|
||||||
sg_subscribe_event_result = msg->event_type;
|
sg_subscribe_event_result = msg->event_type;
|
||||||
Log_i("subscribe wait ack timeout, packet-id=%u", packet_id);
|
Log_i("subscribe wait ack timeout, packet-id=%u", packet_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_SUBCRIBE_NACK:
|
case MQTT_EVENT_SUBCRIBE_NACK:
|
||||||
sg_subscribe_event_result = msg->event_type;
|
sg_subscribe_event_result = msg->event_type;
|
||||||
Log_i("subscribe nack, packet-id=%u", packet_id);
|
Log_i("subscribe nack, packet-id=%u", packet_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_PUBLISH_SUCCESS:
|
case MQTT_EVENT_PUBLISH_SUCCESS:
|
||||||
Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
|
Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_PUBLISH_TIMEOUT:
|
case MQTT_EVENT_PUBLISH_TIMEOUT:
|
||||||
Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
|
Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MQTT_EVENT_PUBLISH_NACK:
|
case MQTT_EVENT_PUBLISH_NACK:
|
||||||
Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
|
Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
Log_i("Should NOT arrive here.");
|
Log_i("Should NOT arrive here.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Setup MQTT construct parameters
|
// Setup MQTT construct parameters
|
||||||
static int _setup_connect_init_params(TemplateInitParams* initParams)
|
static int _setup_connect_init_params(TemplateInitParams* initParams)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = HAL_GetDevInfo((void *)&sg_devInfo);
|
ret = HAL_GetDevInfo((void *)&sg_devInfo);
|
||||||
if(QCLOUD_RET_SUCCESS != ret){
|
if(QCLOUD_RET_SUCCESS != ret){
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
initParams->device_name = sg_devInfo.device_name;
|
initParams->device_name = sg_devInfo.device_name;
|
||||||
initParams->product_id = sg_devInfo.product_id;
|
initParams->product_id = sg_devInfo.product_id;
|
||||||
|
|
||||||
#ifdef AUTH_MODE_CERT
|
#ifdef AUTH_MODE_CERT
|
||||||
/* TLS with certs*/
|
/* TLS with certs*/
|
||||||
char certs_dir[PATH_MAX + 1] = "certs";
|
char certs_dir[PATH_MAX + 1] = "certs";
|
||||||
char current_path[PATH_MAX + 1];
|
char current_path[PATH_MAX + 1];
|
||||||
char *cwd = getcwd(current_path, sizeof(current_path));
|
char *cwd = getcwd(current_path, sizeof(current_path));
|
||||||
if (cwd == NULL)
|
if (cwd == NULL)
|
||||||
{
|
{
|
||||||
Log_e("getcwd return NULL");
|
Log_e("getcwd return NULL");
|
||||||
return QCLOUD_ERR_FAILURE;
|
return QCLOUD_ERR_FAILURE;
|
||||||
}
|
}
|
||||||
sprintf(sg_cert_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_cert_file_name);
|
sprintf(sg_cert_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_cert_file_name);
|
||||||
sprintf(sg_key_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_key_file_name);
|
sprintf(sg_key_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_key_file_name);
|
||||||
|
|
||||||
initParams->cert_file = sg_cert_file;
|
initParams->cert_file = sg_cert_file;
|
||||||
initParams->key_file = sg_key_file;
|
initParams->key_file = sg_key_file;
|
||||||
#else
|
#else
|
||||||
initParams->device_secret = sg_devInfo.device_secret;
|
initParams->device_secret = sg_devInfo.device_secret;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
initParams->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
initParams->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||||
initParams->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
initParams->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
||||||
initParams->auto_connect_enable = 1;
|
initParams->auto_connect_enable = 1;
|
||||||
initParams->event_handle.h_fp = event_handler;
|
initParams->event_handle.h_fp = event_handler;
|
||||||
|
|
||||||
return QCLOUD_RET_SUCCESS;
|
return QCLOUD_RET_SUCCESS;
|
||||||
@@ -254,7 +250,7 @@ static void OnControlMsgCallback(void *pClient, const char *pJsonValueBuffer, ui
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
||||||
/* handle self defined string/json here. Other properties are dealed in _handle_delta()*/
|
/* handle self defined string/json here. Other properties are dealed in _handle_delta()*/
|
||||||
if (strcmp(sg_DataTemplate[i].data_property.key, pProperty->key) == 0) {
|
if (strcmp(sg_DataTemplate[i].data_property.key, pProperty->key) == 0) {
|
||||||
sg_DataTemplate[i].state = eCHANGED;
|
sg_DataTemplate[i].state = eCHANGED;
|
||||||
Log_i("Property=%s changed", pProperty->key);
|
Log_i("Property=%s changed", pProperty->key);
|
||||||
@@ -267,26 +263,26 @@ static void OnControlMsgCallback(void *pClient, const char *pJsonValueBuffer, ui
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void OnReportReplyCallback(void *pClient, Method method, ReplyAck replyAck, const char *pJsonDocument, void *pUserdata) {
|
static void OnReportReplyCallback(void *pClient, Method method, ReplyAck replyAck, const char *pJsonDocument, void *pUserdata) {
|
||||||
Log_i("recv report reply response, reply ack: %d", replyAck);
|
Log_i("recv report reply response, reply ack: %d", replyAck);
|
||||||
}
|
}
|
||||||
|
|
||||||
// register data template properties
|
// register data template properties
|
||||||
static int _register_data_template_property(void *pTemplate_client)
|
static int _register_data_template_property(void *pTemplate_client)
|
||||||
{
|
{
|
||||||
int i,rc;
|
int i,rc;
|
||||||
|
|
||||||
for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
for (i = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
||||||
rc = IOT_Template_Register_Property(pTemplate_client, &sg_DataTemplate[i].data_property, OnControlMsgCallback);
|
rc = IOT_Template_Register_Property(pTemplate_client, &sg_DataTemplate[i].data_property, OnControlMsgCallback);
|
||||||
if (rc != QCLOUD_RET_SUCCESS) {
|
if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
rc = IOT_Template_Destroy(pTemplate_client);
|
rc = IOT_Template_Destroy(pTemplate_client);
|
||||||
Log_e("register device data template property failed, err: %d", rc);
|
Log_e("register device data template property failed, err: %d", rc);
|
||||||
return rc;
|
return rc;
|
||||||
} else {
|
} else {
|
||||||
Log_i("data template property=%s registered.", sg_DataTemplate[i].data_property.key);
|
Log_i("data template property=%s registered.", sg_DataTemplate[i].data_property.key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return QCLOUD_RET_SUCCESS;
|
return QCLOUD_RET_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -303,17 +299,17 @@ __weak void OLED_ShowString(int x, int y, uint8_t *str, int bold)
|
|||||||
// handle the light(simulated)
|
// handle the light(simulated)
|
||||||
static void light_change_color(const char *color)
|
static void light_change_color(const char *color)
|
||||||
{
|
{
|
||||||
// <EFBFBD><EFBFBD>Ϊdemo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>oled<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>
|
// 作为demo,这里用oled屏字符显示来模拟灯颜色的切换
|
||||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ʵ<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵƵ<EFBFBD><EFBFBD><EFBFBD>ɫ
|
// 这里应该由用户实现硬件操作代码,来改变智能灯的颜色
|
||||||
// <EFBFBD>˴<EFBFBD>demo<EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
// 此处demo,在开发板显示屏上显示具体的颜色
|
||||||
OLED_ShowString(0, 0, (uint8_t *)color, 8);
|
OLED_ShowString(0, 0, (uint8_t *)color, 8);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void light_change_brightness(TYPE_DEF_TEMPLATE_INT brightness)
|
static void light_change_brightness(TYPE_DEF_TEMPLATE_INT brightness)
|
||||||
{
|
{
|
||||||
// <EFBFBD><EFBFBD>Ϊdemo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>oled<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>
|
// 作为demo,这里用oled屏字符显示来模拟灯颜色的切换
|
||||||
// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD>ʵ<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>룬<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܵƵ<EFBFBD><EFBFBD><EFBFBD>ɫ
|
// 这里应该由用户实现硬件操作代码,来改变智能灯的颜色
|
||||||
// <EFBFBD>˴<EFBFBD>demo<EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
// 此处demo,在开发板显示屏上显示具体的颜色
|
||||||
char brightness_str[12];
|
char brightness_str[12];
|
||||||
|
|
||||||
snprintf(brightness_str, sizeof(brightness_str), "%d", brightness);
|
snprintf(brightness_str, sizeof(brightness_str), "%d", brightness);
|
||||||
@@ -323,13 +319,13 @@ static void light_change_brightness(TYPE_DEF_TEMPLATE_INT brightness)
|
|||||||
|
|
||||||
static void light_power_on(void)
|
static void light_power_on(void)
|
||||||
{
|
{
|
||||||
// <EFBFBD><EFBFBD>Ϊdemo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>oled<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>
|
// 作为demo,这里用oled屏字符显示来模拟灯颜色的切换
|
||||||
OLED_Clear();
|
OLED_Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void light_power_off(void)
|
static void light_power_off(void)
|
||||||
{
|
{
|
||||||
// <EFBFBD><EFBFBD>Ϊdemo<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>oled<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ<EFBFBD><EFBFBD><EFBFBD>л<EFBFBD>
|
// 作为demo,这里用oled屏字符显示来模拟灯颜色的切换
|
||||||
char *info = "light off";
|
char *info = "light off";
|
||||||
OLED_Clear();
|
OLED_Clear();
|
||||||
OLED_ShowString(0, 0, (uint8_t *)info, 16);
|
OLED_ShowString(0, 0, (uint8_t *)info, 16);
|
||||||
@@ -340,12 +336,12 @@ static void light_power_off(void)
|
|||||||
// you should add your logic how to use pData
|
// you should add your logic how to use pData
|
||||||
void deal_down_stream_user_logic(void *client, ProductDataDefine * pData)
|
void deal_down_stream_user_logic(void *client, ProductDataDefine * pData)
|
||||||
{
|
{
|
||||||
Log_d("someting about your own product logic wait to be done");
|
Log_d("someting about your own product logic wait to be done");
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
char *color_name;
|
char *color_name;
|
||||||
|
|
||||||
/* <EFBFBD>ƹ<EFBFBD><EFBFBD><EFBFBD>ɫ */
|
/* 灯光颜色 */
|
||||||
switch (sg_ProductData.m_color) {
|
switch (sg_ProductData.m_color) {
|
||||||
case 0:
|
case 0:
|
||||||
color_name = " RED ";
|
color_name = " RED ";
|
||||||
@@ -361,24 +357,25 @@ void deal_down_stream_user_logic(void *client, ProductDataDefine * pData)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sg_ProductData.m_power_switch == 1) {
|
if (sg_ProductData.m_power_switch == 1) {
|
||||||
/* <EFBFBD>ƹ<EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>տ<EFBFBD><EFBFBD>Ʋ<EFBFBD><EFBFBD><EFBFBD>չʾ */
|
/* 灯光开启式,按照控制参数展示 */
|
||||||
light_power_on();
|
light_power_on();
|
||||||
light_change_color(color_name);
|
light_change_color(color_name);
|
||||||
light_change_brightness(sg_ProductData.m_brightness);
|
light_change_brightness(sg_ProductData.m_brightness);
|
||||||
} else {
|
} else {
|
||||||
/* <EFBFBD>ƹ<EFBFBD><EFBFBD>ر<EFBFBD>չʾ */
|
/* 灯光关闭展示 */
|
||||||
light_power_off();
|
light_power_off();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void first_time_report_construct(DeviceProperty *pReportDataList[], int *pCount)
|
void first_time_report_construct(DeviceProperty *pReportDataList[], int *pCount)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
for (i = 0, j = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
||||||
pReportDataList[j++] = &(sg_DataTemplate[i].data_property);
|
pReportDataList[j++] = &(sg_DataTemplate[i].data_property);
|
||||||
}
|
}
|
||||||
*pCount = j;
|
|
||||||
|
*pCount = j;
|
||||||
}
|
}
|
||||||
/////////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@@ -388,23 +385,23 @@ void first_time_report_construct(DeviceProperty *pReportDataList[], int *pCount)
|
|||||||
// you should add your own logic for how to get the changed properties
|
// you should add your own logic for how to get the changed properties
|
||||||
int deal_up_stream_user_logic(DeviceProperty *pReportDataList[], int *pCount)
|
int deal_up_stream_user_logic(DeviceProperty *pReportDataList[], int *pCount)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
|
|
||||||
for (i = 0, j = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
for (i = 0, j = 0; i < TOTAL_PROPERTY_COUNT; i++) {
|
||||||
if(eCHANGED == sg_DataTemplate[i].state) {
|
if(eCHANGED == sg_DataTemplate[i].state) {
|
||||||
pReportDataList[j++] = &(sg_DataTemplate[i].data_property);
|
pReportDataList[j++] = &(sg_DataTemplate[i].data_property);
|
||||||
sg_DataTemplate[i].state = eNOCHANGE;
|
sg_DataTemplate[i].state = eNOCHANGE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
*pCount = j;
|
*pCount = j;
|
||||||
|
|
||||||
return (*pCount > 0)?QCLOUD_RET_SUCCESS:QCLOUD_ERR_FAILURE;
|
return (*pCount > 0)?QCLOUD_RET_SUCCESS:QCLOUD_ERR_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*You should get the real info for your device, here just for example*/
|
/*You should get the real info for your device, here just for example*/
|
||||||
static int _get_sys_info(void *handle, char *pJsonDoc, size_t sizeOfBuffer)
|
static int _get_sys_info(void *handle, char *pJsonDoc, size_t sizeOfBuffer)
|
||||||
{
|
{
|
||||||
/*platform info has at least one of module_hardinfo/module_softinfo/fw_ver property*/
|
/*platform info has at least one of module_hardinfo/module_softinfo/fw_ver property*/
|
||||||
DeviceProperty plat_info[] = {
|
DeviceProperty plat_info[] = {
|
||||||
{.key = "module_hardinfo", .type = TYPE_TEMPLATE_STRING, .data = "ESP8266"},
|
{.key = "module_hardinfo", .type = TYPE_TEMPLATE_STRING, .data = "ESP8266"},
|
||||||
{.key = "module_softinfo", .type = TYPE_TEMPLATE_STRING, .data = "V1.0"},
|
{.key = "module_softinfo", .type = TYPE_TEMPLATE_STRING, .data = "V1.0"},
|
||||||
@@ -413,22 +410,22 @@ static int _get_sys_info(void *handle, char *pJsonDoc, size_t sizeOfBuffer)
|
|||||||
{.key = "lat", .type = TYPE_TEMPLATE_STRING, .data = "22.546015"},
|
{.key = "lat", .type = TYPE_TEMPLATE_STRING, .data = "22.546015"},
|
||||||
{.key = "lon", .type = TYPE_TEMPLATE_STRING, .data = "113.941125"},
|
{.key = "lon", .type = TYPE_TEMPLATE_STRING, .data = "113.941125"},
|
||||||
{NULL, NULL, 0} //end
|
{NULL, NULL, 0} //end
|
||||||
};
|
};
|
||||||
|
|
||||||
/*self define info*/
|
/*self define info*/
|
||||||
DeviceProperty self_info[] = {
|
DeviceProperty self_info[] = {
|
||||||
{.key = "append_info", .type = TYPE_TEMPLATE_STRING, .data = "your self define info"},
|
{.key = "append_info", .type = TYPE_TEMPLATE_STRING, .data = "your self define info"},
|
||||||
{NULL, NULL, 0} //end
|
{NULL, NULL, 0} //end
|
||||||
};
|
};
|
||||||
|
|
||||||
return IOT_Template_JSON_ConstructSysInfo(handle, pJsonDoc, sizeOfBuffer, plat_info, self_info);
|
return IOT_Template_JSON_ConstructSysInfo(handle, pJsonDoc, sizeOfBuffer, plat_info, self_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
int data_template_light_thread(void) {
|
int data_template_light_thread(void) {
|
||||||
int rc;
|
int rc;
|
||||||
sReplyPara replyPara;
|
sReplyPara replyPara;
|
||||||
DeviceProperty *pReportDataList[TOTAL_PROPERTY_COUNT];
|
DeviceProperty *pReportDataList[TOTAL_PROPERTY_COUNT];
|
||||||
int ReportCont;
|
int ReportCont;
|
||||||
|
|
||||||
//init log level
|
//init log level
|
||||||
IOT_Log_Set_Level(eLOG_DEBUG);
|
IOT_Log_Set_Level(eLOG_DEBUG);
|
||||||
@@ -439,7 +436,7 @@ int data_template_light_thread(void) {
|
|||||||
if (rc != QCLOUD_RET_SUCCESS) {
|
if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
Log_e("init params err,rc=%d", rc);
|
Log_e("init params err,rc=%d", rc);
|
||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
void *client = IOT_Template_Construct(&init_params);
|
void *client = IOT_Template_Construct(&init_params);
|
||||||
if (client != NULL) {
|
if (client != NULL) {
|
||||||
@@ -459,7 +456,7 @@ int data_template_light_thread(void) {
|
|||||||
//init data template
|
//init data template
|
||||||
_init_data_template();
|
_init_data_template();
|
||||||
|
|
||||||
//register data template propertys here
|
//register data template propertys here
|
||||||
rc = _register_data_template_property(client);
|
rc = _register_data_template_property(client);
|
||||||
if (rc == QCLOUD_RET_SUCCESS) {
|
if (rc == QCLOUD_RET_SUCCESS) {
|
||||||
Log_i("Register data template propertys Success");
|
Log_i("Register data template propertys Success");
|
||||||
@@ -468,7 +465,7 @@ int data_template_light_thread(void) {
|
|||||||
return rc;
|
return rc;
|
||||||
}
|
}
|
||||||
|
|
||||||
//register data template actions here
|
//register data template actions here
|
||||||
#ifdef ACTION_ENABLED
|
#ifdef ACTION_ENABLED
|
||||||
rc = _register_data_template_action(client);
|
rc = _register_data_template_action(client);
|
||||||
if (rc == QCLOUD_RET_SUCCESS) {
|
if (rc == QCLOUD_RET_SUCCESS) {
|
||||||
@@ -479,26 +476,26 @@ int data_template_light_thread(void) {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//report device info, then you can manager your product by these info, like position
|
//report device info, then you can manager your product by these info, like position
|
||||||
rc = _get_sys_info(client, sg_data_report_buffer, sg_data_report_buffersize);
|
rc = _get_sys_info(client, sg_data_report_buffer, sg_data_report_buffersize);
|
||||||
if(QCLOUD_RET_SUCCESS == rc){
|
if(QCLOUD_RET_SUCCESS == rc){
|
||||||
rc = IOT_Template_Report_SysInfo_Sync(client, sg_data_report_buffer, sg_data_report_buffersize, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
rc = IOT_Template_Report_SysInfo_Sync(client, sg_data_report_buffer, sg_data_report_buffersize, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
||||||
if (rc != QCLOUD_RET_SUCCESS) {
|
if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
Log_e("Report system info fail, err: %d", rc);
|
Log_e("Report system info fail, err: %d", rc);
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
|
||||||
}else{
|
|
||||||
Log_e("Get system info fail, err: %d", rc);
|
|
||||||
}
|
}
|
||||||
|
}else {
|
||||||
|
Log_e("Get system info fail, err: %d", rc);
|
||||||
|
}
|
||||||
|
|
||||||
//get the property changed during offline
|
//get the property changed during offline
|
||||||
rc = IOT_Template_GetStatus_sync(client, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
rc = IOT_Template_GetStatus_sync(client, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
||||||
if (rc != QCLOUD_RET_SUCCESS) {
|
if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
Log_e("Get data status fail, err: %d", rc);
|
Log_e("Get data status fail, err: %d", rc);
|
||||||
//goto exit;
|
//goto exit;
|
||||||
}else{
|
}else {
|
||||||
Log_d("Get data status success");
|
Log_d("Get data status success");
|
||||||
}
|
}
|
||||||
|
|
||||||
while (IOT_Template_IsConnected(client) || rc == QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT
|
while (IOT_Template_IsConnected(client) || rc == QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT
|
||||||
|| rc == QCLOUD_RET_MQTT_RECONNECTED || QCLOUD_RET_SUCCESS == rc) {
|
|| rc == QCLOUD_RET_MQTT_RECONNECTED || QCLOUD_RET_SUCCESS == rc) {
|
||||||
@@ -509,31 +506,30 @@ int data_template_light_thread(void) {
|
|||||||
HAL_SleepMs(1000);
|
HAL_SleepMs(1000);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
else if (rc != QCLOUD_RET_SUCCESS) {
|
else if (rc != QCLOUD_RET_SUCCESS) {
|
||||||
Log_e("Exit loop caused of errCode: %d", rc);
|
Log_e("Exit loop caused of errCode: %d", rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* handle control msg from server */
|
/* handle control msg from server */
|
||||||
if (sg_control_msg_arrived) {
|
if (sg_control_msg_arrived) {
|
||||||
|
deal_down_stream_user_logic(client, &sg_ProductData);
|
||||||
|
|
||||||
deal_down_stream_user_logic(client, &sg_ProductData);
|
/* control msg should reply, otherwise server treat device didn't receive and retain the msg which would be get by get status*/
|
||||||
|
memset((char *)&replyPara, 0, sizeof(sReplyPara));
|
||||||
|
replyPara.code = eDEAL_SUCCESS;
|
||||||
|
replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||||
|
replyPara.status_msg[0] = '\0'; //add extra info to replyPara.status_msg when error occured
|
||||||
|
|
||||||
/* control msg should reply, otherwise server treat device didn't receive and retain the msg which would be get by get status*/
|
rc = IOT_Template_ControlReply(client, sg_data_report_buffer, sg_data_report_buffersize, &replyPara);
|
||||||
memset((char *)&replyPara, 0, sizeof(sReplyPara));
|
|
||||||
replyPara.code = eDEAL_SUCCESS;
|
|
||||||
replyPara.timeout_ms = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
|
||||||
replyPara.status_msg[0] = '\0'; //add extra info to replyPara.status_msg when error occured
|
|
||||||
|
|
||||||
rc = IOT_Template_ControlReply(client, sg_data_report_buffer, sg_data_report_buffersize, &replyPara);
|
|
||||||
if (rc == QCLOUD_RET_SUCCESS) {
|
if (rc == QCLOUD_RET_SUCCESS) {
|
||||||
Log_d("Contol msg reply success");
|
Log_d("Contol msg reply success");
|
||||||
sg_control_msg_arrived = false;
|
sg_control_msg_arrived = false;
|
||||||
} else {
|
} else {
|
||||||
Log_e("Contol msg reply failed, err: %d", rc);
|
Log_e("Contol msg reply failed, err: %d", rc);
|
||||||
}
|
}
|
||||||
} else{
|
}else {
|
||||||
Log_d("No control msg received...");
|
Log_d("No control msg received...");
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
// some mistake(kind of bug) in this official sample, must do the first time report
|
// some mistake(kind of bug) in this official sample, must do the first time report
|
||||||
@@ -559,13 +555,13 @@ int data_template_light_thread(void) {
|
|||||||
} else {
|
} else {
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
|
|
||||||
/*report msg to server*/
|
/*report msg to server*/
|
||||||
/*report the lastest properties's status*/
|
/*report the lastest properties's status*/
|
||||||
if(QCLOUD_RET_SUCCESS == deal_up_stream_user_logic(pReportDataList, &ReportCont)){
|
if(QCLOUD_RET_SUCCESS == deal_up_stream_user_logic(pReportDataList, &ReportCont)){
|
||||||
rc = IOT_Template_JSON_ConstructReportArray(client, sg_data_report_buffer, sg_data_report_buffersize, ReportCont, pReportDataList);
|
rc = IOT_Template_JSON_ConstructReportArray(client, sg_data_report_buffer, sg_data_report_buffersize, ReportCont, pReportDataList);
|
||||||
if (rc == QCLOUD_RET_SUCCESS) {
|
if (rc == QCLOUD_RET_SUCCESS) {
|
||||||
rc = IOT_Template_Report(client, sg_data_report_buffer, sg_data_report_buffersize,
|
rc = IOT_Template_Report(client, sg_data_report_buffer, sg_data_report_buffersize,
|
||||||
OnReportReplyCallback, NULL, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
OnReportReplyCallback, NULL, QCLOUD_IOT_MQTT_COMMAND_TIMEOUT);
|
||||||
if (rc == QCLOUD_RET_SUCCESS) {
|
if (rc == QCLOUD_RET_SUCCESS) {
|
||||||
Log_i("data template reporte success");
|
Log_i("data template reporte success");
|
||||||
} else {
|
} else {
|
||||||
@@ -575,19 +571,19 @@ int data_template_light_thread(void) {
|
|||||||
Log_e("construct reporte data failed, err: %d", rc);
|
Log_e("construct reporte data failed, err: %d", rc);
|
||||||
}
|
}
|
||||||
|
|
||||||
}else{
|
}else {
|
||||||
//Log_d("no data need to be reported or someting goes wrong");
|
//Log_d("no data need to be reported or someting goes wrong");
|
||||||
}
|
}
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
}
|
}
|
||||||
///////////////////////////////////
|
///////////////////////////////////
|
||||||
|
|
||||||
#ifdef EVENT_POST_ENABLED
|
#ifdef EVENT_POST_ENABLED
|
||||||
eventPostCheck(client);
|
eventPostCheck(client);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
HAL_SleepMs(3000);
|
HAL_SleepMs(3000);
|
||||||
}
|
}/*end of while*/
|
||||||
exit:
|
exit:
|
||||||
rc = IOT_Template_Destroy(client);
|
rc = IOT_Template_Destroy(client);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user