LPT26x-HSF-4MB-Hilink_14.2..../application/ws63/user_main/spotlight/device_module.c

71 lines
1.7 KiB
C
Raw Normal View History

2025-05-13 22:00:58 +08:00
#include "spotlight.h"
#include "cJSON.h"
#define CHECK_JSON_ITEM(condition) if(condition) { \
goto lab_end; \
}
#define MAX_BRIGHTNESS 100
#define MIN_BRIGHTNESS 0
#define MAX_CCT 6500
#define MIN_CCT 2700
typedef struct device_control_s {
bool on;
int32_t colorTemperature;
int32_t brightness;
} device_control_t;
device_control_t g_device_control = {
.on = false,
.colorTemperature = MAX_CCT,
.brightness = MAX_BRIGHTNESS / 2,
};
int device_module_control(const char *svcId, const char *payload, unsigned int len)
{
if ((svcId == NULL) || (payload == NULL)) {
return -1;
}
cJSON *item = NULL;
cJSON *json = cJSON_Parse(payload);
CHECK_JSON_ITEM(json == NULL);
e_printf("put char sid:%s,payload:%s\r\n",svcId,payload);
if(strcmp(svcId, "brightness") == 0)
{
item = cJSON_GetObjectItem(json,"brightness");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
else if(strcmp(svcId, "cct") == 0)
{
item = cJSON_GetObjectItem(json,"colorTemperature");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
} else if(strcmp(svcId, "lightMode") == 0) // scense mode
{
item = cJSON_GetObjectItem(json,"mode");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
else if(strcmp(svcId, "switch") == 0) // device open/close
{
item = cJSON_GetObjectItem(json,"on");
CHECK_JSON_ITEM(item == NULL);
CHECK_JSON_ITEM(!cJSON_IsNumber(item));
}
lab_end:
if(json != NULL)
{
cJSON_Delete(json);
json = NULL;
}
return 0;
}