220 lines
6.3 KiB
C
220 lines
6.3 KiB
C
#include "spotlight.h"
|
||
#include "cJSON.h"
|
||
#include "hilink_device.h"
|
||
|
||
#define CHECK_JSON_ITEM(condition) if(condition) { \
|
||
goto lab_end; \
|
||
}
|
||
|
||
extern device_control_t g_device_control;
|
||
|
||
// 场景模式预设值定义
|
||
typedef struct {
|
||
int brightness;
|
||
int cct;
|
||
} scene_preset_t;
|
||
|
||
static const scene_preset_t scene_presets[] = {
|
||
{(INIT_STA__BRIGHTNESS), (INIT_STA__CCT)}, // 模式0:非情景模式 配网成功后的默认
|
||
{(50), (4000)}, // 模式1:休闲模式 - 较低亮度,暖色调
|
||
{(10), (3000)}, // 模式2:观影模式 - 低亮度,暖色调
|
||
{(100), (4000)}, // 模式3:用餐模式 - 中等亮度,自然光
|
||
{(80), (3500)}, // 模式4:回家模式 - 较高亮度,自然光
|
||
{(100), (CCT_MIN)}, // 模式5:冬天模式 - 中等亮度,暖色调
|
||
{(100), (CCT_MAX)}, // 模式6:夏天模式 - 较高亮度,冷色调
|
||
};
|
||
|
||
#define SCENE_MODE_COUNT (sizeof(scene_presets) / sizeof(scene_presets[0]))
|
||
|
||
// 处理亮度控制
|
||
int myhandle_put_brightness(const char* svc_id, const char* payload, unsigned int len)
|
||
{
|
||
int ret = -1;
|
||
cJSON *json = cJSON_Parse(payload);
|
||
if (json == NULL) {
|
||
return -1;
|
||
}
|
||
|
||
cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_BRIGHTNESS);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
int brightness = item->valueint;
|
||
if (brightness < BRIGHTNESS_MIN) brightness = BRIGHTNESS_MIN;
|
||
if (brightness > BRIGHTNESS_MAX) brightness = BRIGHTNESS_MAX;
|
||
|
||
// g_device_control.brightness_local = brightness;
|
||
g_device_control.elightMode = LIGHT_MODE_CUSTOMER;
|
||
ret = set_light(BRIGHTNESS_REMOTE2LOCAL(brightness), -1); // 只更新亮度,色温保持不变
|
||
|
||
cJSON_Delete(json);
|
||
return ret; //异步上报
|
||
}
|
||
|
||
// 处理色温控制
|
||
int myhandle_put_cct(const char* svc_id, const char* payload, unsigned int len)
|
||
{
|
||
int ret = -1;
|
||
cJSON *json = cJSON_Parse(payload);
|
||
if (json == NULL) {
|
||
return -1;
|
||
}
|
||
|
||
cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_CCT);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
int cct = item->valueint;
|
||
if (cct < CCT_MIN) cct = CCT_MIN;
|
||
if (cct > CCT_MAX) cct = CCT_MAX;
|
||
|
||
// g_device_control.cct_local = cct;
|
||
g_device_control.elightMode = LIGHT_MODE_CUSTOMER;
|
||
ret = set_light(-1, CCT_REMOTE2LOCAL(cct)); // 只更新色温,亮度保持不变
|
||
|
||
cJSON_Delete(json);
|
||
return ret ;//异步上报
|
||
}
|
||
|
||
// 处理场景模式控制
|
||
int myhandle_put_lightMode(const char* svc_id, const char* payload, unsigned int len)
|
||
{
|
||
int ret = -1;
|
||
cJSON *json = cJSON_Parse(payload);
|
||
if (json == NULL) {
|
||
return -1;
|
||
}
|
||
|
||
cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_MODE);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
uint16_t mode = item->valueint;
|
||
if (mode >= 0 && mode < SCENE_MODE_COUNT) {
|
||
g_device_control.elightMode = mode;
|
||
|
||
// 同时更新亮度和色温
|
||
ret = set_light(BRIGHTNESS_REMOTE2LOCAL(scene_presets[mode].brightness), CCT_REMOTE2LOCAL(scene_presets[mode].cct));
|
||
}
|
||
|
||
cJSON_Delete(json);
|
||
return ret;
|
||
}
|
||
|
||
// 处理开关控制
|
||
int myhandle_put_switch(const char* svc_id, const char* payload, unsigned int len)
|
||
{
|
||
int ret = -1;
|
||
cJSON *json = cJSON_Parse(payload);
|
||
if (json == NULL) {
|
||
return -1;
|
||
}
|
||
|
||
cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_ON);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
bool on = (item->valueint != 0);
|
||
g_device_control.on = on;
|
||
ret = set_switch(on);
|
||
|
||
cJSON_Delete(json);
|
||
return ret;
|
||
}
|
||
|
||
// 获取亮度状态
|
||
int myhandle_get_brightness(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
|
||
{
|
||
*out_len = 32;
|
||
*out = (char*)malloc(*out_len);
|
||
if (NULL == *out) {
|
||
return -1;
|
||
}
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_BRIGHTNESS, BRIGHTNESS_LOCAL2REMOTE(g_device_control.brightness_local));
|
||
return 0;
|
||
}
|
||
|
||
// 获取色温状态
|
||
int myhandle_get_cct(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
|
||
{
|
||
*out_len = 32;
|
||
*out = (char*)malloc(*out_len);
|
||
if (NULL == *out) {
|
||
return -1;
|
||
}
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_CCT, CCT_LOCAL2REMOTE(g_device_control.cct_local));
|
||
return 0;
|
||
}
|
||
|
||
// 获取场景模式状态
|
||
int myhandle_get_lightMode(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
|
||
{
|
||
*out_len = 32;
|
||
*out = (char*)malloc(*out_len);
|
||
if (NULL == *out) {
|
||
return -1;
|
||
}
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_MODE, g_device_control.elightMode);
|
||
return 0;
|
||
}
|
||
|
||
// 获取开关状态
|
||
int myhandle_get_switch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
|
||
{
|
||
*out_len = 32;
|
||
*out = (char*)malloc(*out_len);
|
||
if (NULL == *out) {
|
||
return -1;
|
||
}
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_ON, g_device_control.on);
|
||
return 0;
|
||
}
|
||
|
||
// 处理渐变时长控制
|
||
int myhandle_put_progressSwitch(const char* svc_id, const char* payload, unsigned int len)
|
||
{
|
||
int ret = -1;
|
||
|
||
cJSON *json = cJSON_Parse(payload);
|
||
if (json == NULL) {
|
||
e_printf("JSON parsing failed\n");
|
||
return -1;
|
||
}
|
||
|
||
cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_FADE_TIME);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
e_printf("Invalid progressSwitch parameter\n");
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
int smooth_time = item->valueint;
|
||
ret = set_smooth_time(smooth_time);
|
||
|
||
cJSON_Delete(json);
|
||
// e_printf("Smooth time setting completed\n");
|
||
return ret;
|
||
}
|
||
|
||
// 获取渐变时长状态
|
||
int myhandle_get_progressSwitch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
|
||
{
|
||
*out_len = 32;
|
||
*out = (char*)malloc(*out_len);
|
||
if (NULL == *out) {
|
||
e_printf("Memory allocation failed\n");
|
||
return -1;
|
||
}
|
||
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_FADE_TIME, g_device_control.fade_time);
|
||
return 0;
|
||
}
|