282 lines
8.7 KiB
C
282 lines
8.7 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:夏天模式 - 较高亮度,冷色调
|
||
{(0), (0)}, // 模式7 离家模式- 关闭
|
||
};
|
||
|
||
#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(APP_CHANGE_LIGHT_BRIGHTNESS_CCT, 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;
|
||
}
|
||
|
||
// // 在单色温模式下,不允许修改色温
|
||
// if (g_device_control.colourMode == COLOUR_MODE_SINGLE) {
|
||
// e_printf("Cannot change CCT in single colour mode\r\n");
|
||
// 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(APP_CHANGE_LIGHT_BRIGHTNESS_CCT, -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) {
|
||
// 如果切换到离家模式,设置bitmask并直接关灯
|
||
if (mode == LIGHT_MODE_LEAVE) {
|
||
g_device_control.elightMode = LIGHT_MODE_SET_LEAVE(LIGHT_MODE_GET_BASE(g_device_control.elightMode));
|
||
ret = set_light(APP_CLOSE_LIGHT, -1, -1);
|
||
} else {
|
||
// 普通模式切换,清除离家模式位
|
||
g_device_control.elightMode = mode;
|
||
ret = set_light(APP_CHANGE_LIGHT_MODE, 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);
|
||
if (on) {
|
||
// 如果当前是离家模式,取消离家模式并恢复到基础模式
|
||
if (LIGHT_MODE_IS_LEAVE(g_device_control.elightMode)) {
|
||
lightMode_e base_mode = LIGHT_MODE_GET_BASE(g_device_control.elightMode);
|
||
g_device_control.elightMode = base_mode;
|
||
e_printf("从离家模式恢复,基础模式:%d\r\n", base_mode);
|
||
}
|
||
ret = set_light(APP_OPEN_LIGHT, -1, -1);
|
||
} else {
|
||
ret = set_light(APP_CLOSE_LIGHT, -1, -1);
|
||
}
|
||
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;
|
||
}
|
||
// 使用转换函数确保上报正确的模式
|
||
lightMode_e report_mode = convert_mode_for_report(g_device_control.elightMode);
|
||
*out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_MODE, report_mode);
|
||
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;
|
||
}
|
||
|
||
// 处理色温模式控制
|
||
int myhandle_put_colourMode(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_COLOUR_MODE);
|
||
if (item == NULL || !cJSON_IsNumber(item)) {
|
||
cJSON_Delete(json);
|
||
return -1;
|
||
}
|
||
|
||
uint8_t mode = item->valueint;
|
||
g_device_control.colourMode = mode;
|
||
if (mode == COLOUR_MODE_SINGLE) {
|
||
ret = set_light(APP_CHANGE_LIGHT_BRIGHTNESS_CCT, -1, SINGLE_COLOUR_CCT_LOCAL);
|
||
} else {
|
||
ret = set_light(APP_CHANGE_LIGHT_BRIGHTNESS_CCT, g_device_control.brightness_local, g_device_control.cct_local);
|
||
}
|
||
|
||
cJSON_Delete(json);
|
||
return ret;
|
||
}
|
||
|
||
// 获取色温模式状态
|
||
int myhandle_get_colourMode(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_COLOUR_MODE, g_device_control.colourMode);
|
||
return 0;
|
||
}
|