mirror of
https://github.com/oopuuu/zTC1.git
synced 2025-12-12 21:18:13 +08:00
add:json数据解析处理(未测试)
fix:优化代码注释
This commit is contained in:
@@ -37,13 +37,13 @@ typedef struct
|
|||||||
uint8_t repeat; //bit7:һ<><D2BB> bit6-0:<3A><><EFBFBD><EFBFBD>-<2D><>һ
|
uint8_t repeat; //bit7:һ<><D2BB> bit6-0:<3A><><EFBFBD><EFBFBD>-<2D><>һ
|
||||||
char action; //<2F><><EFBFBD><EFBFBD>
|
char action; //<2F><><EFBFBD><EFBFBD>
|
||||||
char on; //<2F><><EFBFBD><EFBFBD>
|
char on; //<2F><><EFBFBD><EFBFBD>
|
||||||
|
|
||||||
} user_plug_task_config_t;
|
} user_plug_task_config_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char name[PLUG_NAME_LENGTH];
|
char name[PLUG_NAME_LENGTH];
|
||||||
char idx;
|
char idx;
|
||||||
|
char on; //<2F><>¼<EFBFBD><C2BC>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD>
|
||||||
user_plug_task_config_t task[PLUG_TIME_TASK_NUM];
|
user_plug_task_config_t task[PLUG_TIME_TASK_NUM];
|
||||||
|
|
||||||
} user_plug_config_t;
|
} user_plug_config_t;
|
||||||
|
|||||||
@@ -8,21 +8,26 @@ typedef struct _user_json_context_t
|
|||||||
{
|
{
|
||||||
int8_t idx;
|
int8_t idx;
|
||||||
char name[maxNameLen];
|
char name[maxNameLen];
|
||||||
|
|
||||||
int8_t val;
|
int8_t val;
|
||||||
} user_json_context_t;
|
} user_json_context_t;
|
||||||
|
|
||||||
void user_send( int udp_flag, uint8_t *s )
|
bool json_plug_analysis( int udp_flag, char x, cJSON * pJsonRoot, cJSON * pJsonSend );
|
||||||
|
bool json_plug_task_analysis( char x, char y, cJSON * pJsonRoot, cJSON * pJsonSend );
|
||||||
|
|
||||||
|
void user_send( int udp_flag, char *s )
|
||||||
{
|
{
|
||||||
if ( udp_flag )
|
if ( udp_flag )
|
||||||
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
user_udp_send( s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
else
|
else
|
||||||
user_mqtt_send( s );
|
user_mqtt_send( s );
|
||||||
}
|
}
|
||||||
|
|
||||||
void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
||||||
{
|
{
|
||||||
OSStatus err = kNoErr;
|
OSStatus err = kNoErr;
|
||||||
|
char i;
|
||||||
|
bool update_user_config_flag = false; //<2F><>־λ,<2C><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
bool return_flag = true; //Ϊtrueʱ<65><CAB1><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><F2B2BBB7><EFBFBD>
|
||||||
|
|
||||||
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
cJSON * pJsonRoot = cJSON_Parse( pusrdata );
|
||||||
if ( !pJsonRoot )
|
if ( !pJsonRoot )
|
||||||
@@ -31,7 +36,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>device report
|
//<2F><><EFBFBD><EFBFBD>UDP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>device report(MQTTͬ<54><CDAC><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD>)
|
||||||
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
cJSON *p_cmd = cJSON_GetObjectItem( pJsonRoot, "cmd" );
|
||||||
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
if ( p_cmd && cJSON_IsString( p_cmd ) && strcmp( p_cmd->valuestring, "device report" ) == 0 )
|
||||||
{
|
{
|
||||||
@@ -50,44 +55,68 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
// cJSON_Delete(p_cmd);
|
// cJSON_Delete(p_cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" );
|
||||||
cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" );
|
cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" );
|
||||||
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" );
|
||||||
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" );
|
cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" );
|
||||||
|
|
||||||
//
|
//<EFBFBD>ж<EFBFBD>p_idx,ʹ<><CAB9>"idx"<22><>Ϊ<EFBFBD><CEAA>Ҫ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ʱ
|
||||||
|
for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
|
{
|
||||||
|
//<2F><><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD><EFBFBD><EFBFBD>idxֵ<78>ж<EFBFBD> <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>״̬
|
||||||
|
if ( p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint >= 0 && p_idx->valueint == user_config->plug[i].idx )
|
||||||
|
{
|
||||||
|
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
||||||
|
if ( p_nvalue && cJSON_IsNumber( p_nvalue ) )
|
||||||
|
{
|
||||||
|
if ( p_nvalue->valueint != user_config->plug[i].on )
|
||||||
|
{
|
||||||
|
user_relay_set( i, p_nvalue->valueint );
|
||||||
|
mico_system_context_update( sys_config ); //<2F><><EFBFBD>浱ǰ<E6B5B1><C7B0><EFBFBD><EFBFBD>״̬
|
||||||
|
cJSON *json_send = cJSON_CreateObject( );
|
||||||
|
cJSON_AddNumberToObject( json_send, "idx", user_config->plug[i].idx );
|
||||||
|
cJSON_AddNumberToObject( json_send, "nvalue", user_config->plug[i].on );
|
||||||
|
char *json_str = cJSON_Print( json_send );
|
||||||
|
user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
free( json_str );
|
||||||
|
cJSON_Delete( json_send );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cJSON_Delete( pJsonRoot );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><>ʼ<EFBFBD><CABC>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
if ( (p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == user_config->idx) //idx
|
if ( (p_idx && cJSON_IsNumber( p_idx ) && p_idx->valueint == user_config->idx) //idx
|
||||||
|| (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, sys_config->micoSystemConfig.name ) == 0) //p_description name
|
|| (p_description && cJSON_IsString( p_description ) && strcmp( p_description->valuestring, sys_config->micoSystemConfig.name ) == 0) //p_description name
|
||||||
|| (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name
|
|| (p_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name
|
||||||
|| (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac
|
|| (p_mac && cJSON_IsString( p_mac ) && strcmp( p_mac->valuestring, strMac ) == 0) //mac
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
// os_log("device enter\r\n");
|
|
||||||
cJSON *json_send = cJSON_CreateObject( );
|
cJSON *json_send = cJSON_CreateObject( );
|
||||||
cJSON_AddStringToObject( json_send, "mac", strMac );
|
cJSON_AddStringToObject( json_send, "mac", strMac );
|
||||||
|
|
||||||
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" );
|
||||||
// if ( p_nvalue && cJSON_IsNumber( p_nvalue ) )
|
if ( p_nvalue && cJSON_IsNumber( p_nvalue ) )
|
||||||
// {
|
{
|
||||||
//// uint32 now_time = system_get_time( );
|
// uint32 now_time = system_get_time( );
|
||||||
//// os_log( "system_get_time:%d,%d = %09d\r\n", last_time, now_time, now_time - last_time );
|
// os_log( "system_get_time:%d,%d = %09d\r\n", last_time, now_time, now_time - last_time );
|
||||||
//// if ( now_time - last_time < 1500000 && p_idx && p_nvalue->valueint == user_rudder_get_direction( ) )
|
// if ( now_time - last_time < 1500000 && p_idx && p_nvalue->valueint == user_rudder_get_direction( ) )
|
||||||
//// {
|
// {
|
||||||
//// return_flag = false;
|
// return_flag = false;
|
||||||
//// } else
|
// } else
|
||||||
//// {
|
// {
|
||||||
//// user_rudder_press( p_nvalue->valueint );
|
if ( p_nvalue->valueint != user_config->plug[i].on )
|
||||||
//// }
|
{
|
||||||
//// user_json_set_last_time( );
|
user_relay_set_all( p_nvalue->valueint );
|
||||||
// }
|
}
|
||||||
//
|
// }
|
||||||
// if ( p_nvalue )
|
// user_json_set_last_time( );
|
||||||
// {
|
}
|
||||||
// cJSON_AddNumberToObject( json_send, "nvalue", user_rudder_get_direction( ) );
|
|
||||||
// } else
|
|
||||||
// last_time = 0;
|
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting-----------------------------------------------------------------
|
||||||
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" );
|
cJSON *p_setting = cJSON_GetObjectItem( pJsonRoot, "setting" );
|
||||||
if ( p_setting )
|
if ( p_setting )
|
||||||
{
|
{
|
||||||
@@ -96,6 +125,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" );
|
cJSON *p_setting_name = cJSON_GetObjectItem( p_setting, "name" );
|
||||||
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
|
if ( p_setting_name && cJSON_IsString( p_setting_name ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
sprintf( sys_config->micoSystemConfig.name, p_setting_name->valuestring );
|
sprintf( sys_config->micoSystemConfig.name, p_setting_name->valuestring );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -103,6 +133,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" );
|
cJSON *p_mqtt_ip = cJSON_GetObjectItem( p_setting, "mqtt_uri" );
|
||||||
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) )
|
if ( p_mqtt_ip && cJSON_IsString( p_mqtt_ip ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
sprintf( user_config->mqtt_ip, p_mqtt_ip->valuestring );
|
sprintf( user_config->mqtt_ip, p_mqtt_ip->valuestring );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,6 +141,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" );
|
cJSON *p_mqtt_port = cJSON_GetObjectItem( p_setting, "mqtt_port" );
|
||||||
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) )
|
if ( p_mqtt_port && cJSON_IsNumber( p_mqtt_port ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
user_config->mqtt_port = p_mqtt_port->valueint;
|
user_config->mqtt_port = p_mqtt_port->valueint;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,6 +149,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" );
|
cJSON *p_mqtt_user = cJSON_GetObjectItem( p_setting, "mqtt_user" );
|
||||||
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
|
if ( p_mqtt_user && cJSON_IsString( p_mqtt_user ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
sprintf( user_config->mqtt_user, p_mqtt_user->valuestring );
|
sprintf( user_config->mqtt_user, p_mqtt_user->valuestring );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,6 +157,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" );
|
cJSON *p_mqtt_password = cJSON_GetObjectItem( p_setting, "mqtt_password" );
|
||||||
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) )
|
if ( p_mqtt_password && cJSON_IsString( p_mqtt_password ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
sprintf( user_config->mqtt_password, p_mqtt_password->valuestring );
|
sprintf( user_config->mqtt_password, p_mqtt_password->valuestring );
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,58 +165,37 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON *p_setting_idx = cJSON_GetObjectItem( p_setting, "idx" );
|
cJSON *p_setting_idx = cJSON_GetObjectItem( p_setting, "idx" );
|
||||||
if ( p_setting_idx && cJSON_IsNumber( p_setting_idx ) )
|
if ( p_setting_idx && cJSON_IsNumber( p_setting_idx ) )
|
||||||
{
|
{
|
||||||
|
update_user_config_flag = true;
|
||||||
user_config->idx = p_setting_idx->valueint;
|
user_config->idx = p_setting_idx->valueint;
|
||||||
os_log( "idx:%d",user_config->idx );
|
os_log( "idx:%d",user_config->idx );
|
||||||
mico_system_context_update( sys_config );
|
mico_system_context_update( sys_config );
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( p_setting_name || p_mqtt_ip || p_mqtt_port || p_mqtt_user || p_mqtt_password || p_setting_idx )
|
|
||||||
{
|
|
||||||
os_log( "mico_system_context_update" );
|
|
||||||
err = mico_system_context_update( sys_config );
|
|
||||||
require_noerr( err, exit );
|
|
||||||
}
|
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>豸<EFBFBD><E8B1B8><EFBFBD><EFBFBD>/deviceid
|
||||||
if ( p_setting_name )
|
if ( p_setting_name ) cJSON_AddStringToObject( json_setting_send, "name", sys_config->micoSystemConfig.name );
|
||||||
{
|
//<2F><><EFBFBD><EFBFBD>mqtt ip
|
||||||
cJSON_AddStringToObject( json_setting_send, "name", sys_config->micoSystemConfig.name );
|
if ( p_mqtt_ip ) cJSON_AddStringToObject( json_setting_send, "mqtt_uri", user_config->mqtt_ip );
|
||||||
}
|
//<2F><><EFBFBD><EFBFBD>mqtt port
|
||||||
|
if ( p_mqtt_port ) cJSON_AddNumberToObject( json_setting_send, "mqtt_port", user_config->mqtt_port );
|
||||||
//<2F><><EFBFBD><EFBFBD>mqtt ip
|
//<2F><><EFBFBD><EFBFBD>mqtt user
|
||||||
if ( p_mqtt_ip )
|
if ( p_mqtt_user ) cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user );
|
||||||
{
|
//<2F><><EFBFBD><EFBFBD>mqtt password
|
||||||
cJSON_AddStringToObject( json_setting_send, "mqtt_uri", user_config->mqtt_ip );
|
if ( p_mqtt_password ) cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password );
|
||||||
}
|
//<2F><><EFBFBD><EFBFBD>domoticz idx
|
||||||
|
if ( p_setting_idx ) cJSON_AddNumberToObject( json_setting_send, "idx", user_config->idx );
|
||||||
//<2F><><EFBFBD><EFBFBD>mqtt port
|
|
||||||
if ( p_mqtt_port )
|
|
||||||
{
|
|
||||||
cJSON_AddNumberToObject( json_setting_send, "mqtt_port", user_config->mqtt_port );
|
|
||||||
}
|
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>mqtt user
|
|
||||||
if ( p_mqtt_user )
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user );
|
|
||||||
}
|
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>mqtt password
|
|
||||||
if ( p_mqtt_password )
|
|
||||||
{
|
|
||||||
cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password );
|
|
||||||
}
|
|
||||||
|
|
||||||
//<2F><><EFBFBD><EFBFBD>domoticz idx
|
|
||||||
if ( p_setting_idx )
|
|
||||||
{
|
|
||||||
cJSON_AddNumberToObject( json_setting_send, "idx", user_config->idx );
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON_AddItemToObject( json_send, "setting", json_setting_send );
|
cJSON_AddItemToObject( json_send, "setting", json_setting_send );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug-----------------------------------------------------------------
|
||||||
|
char plug_str[] = "plug_X";
|
||||||
|
for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
|
{
|
||||||
|
if ( json_plug_analysis( udp_flag, i, pJsonRoot, json_send ) )
|
||||||
|
update_user_config_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name );
|
cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name );
|
||||||
|
|
||||||
//if (p_idx)
|
//if (p_idx)
|
||||||
@@ -199,15 +212,157 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
|
|||||||
cJSON_Delete( json_send );
|
cJSON_Delete( json_send );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if ( update_user_config_flag )
|
||||||
|
// {
|
||||||
|
// mico_system_context_update( sys_config );
|
||||||
|
// update_user_config_flag = false;
|
||||||
|
// }
|
||||||
|
|
||||||
cJSON_Delete( pJsonRoot );
|
cJSON_Delete( pJsonRoot );
|
||||||
return;
|
return;
|
||||||
exit:
|
exit:
|
||||||
os_log("user_function_cmd_received ERROR:0x%x",err);
|
os_log( "user_function_cmd_received ERROR:0x%x", err );
|
||||||
|
|
||||||
cJSON_Delete( pJsonRoot );
|
cJSON_Delete( pJsonRoot );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>json
|
||||||
|
*udp_flag:<3A><><EFBFBD><EFBFBD>udp/mqtt<74><74>־λ,<2C>˴<EFBFBD><CBB4>IJ<DEB8><C4B2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬ʱ,<2C><>Ҫʵʱ<CAB5><CAB1><EFBFBD>¸<EFBFBD>domoticz
|
||||||
|
*x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
*/
|
||||||
|
bool json_plug_analysis( int udp_flag, char x, cJSON * pJsonRoot, cJSON * pJsonSend )
|
||||||
|
{
|
||||||
|
if ( !pJsonRoot ) return false;
|
||||||
|
if ( !pJsonSend ) return false;
|
||||||
|
char i;
|
||||||
|
bool return_flag = false;
|
||||||
|
char plug_str[] = "plug_X";
|
||||||
|
plug_str[5] = x + '0';
|
||||||
|
|
||||||
|
cJSON *p_plug = cJSON_GetObjectItem( pJsonRoot, plug_str );
|
||||||
|
if ( !p_plug ) return false;
|
||||||
|
|
||||||
|
cJSON *json_plug_send = cJSON_CreateObject( );
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug on------------------------------------------------------
|
||||||
|
cJSON *p_plug_on = cJSON_GetObjectItem( p_plug, "on" );
|
||||||
|
if ( p_plug_on )
|
||||||
|
{
|
||||||
|
if ( cJSON_IsNumber( p_plug_on ) )
|
||||||
|
{
|
||||||
|
user_relay_set( x, p_plug_on->valueint );
|
||||||
|
|
||||||
|
if ( user_config->plug[x].idx > 0 )
|
||||||
|
{
|
||||||
|
cJSON *json_return_now = cJSON_CreateObject( );
|
||||||
|
cJSON_AddNumberToObject( json_return_now, "idx", user_config->plug[x].idx );
|
||||||
|
cJSON_AddNumberToObject( json_return_now, "nvalue", user_config->plug[x].on );
|
||||||
|
char *json_str = cJSON_Print( json_return_now );
|
||||||
|
user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
free( json_str );
|
||||||
|
cJSON_Delete( json_return_now );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cJSON_AddNumberToObject( json_plug_send, "on", user_config->plug[x].on );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>Ŀ----------------------------------------------
|
||||||
|
cJSON *p_plug_setting = cJSON_GetObjectItem( p_plug, "setting" );
|
||||||
|
if ( p_plug_setting )
|
||||||
|
{
|
||||||
|
cJSON *json_plug_setting_send = cJSON_CreateObject( );
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>name----------------------------------------
|
||||||
|
cJSON *p_plug_setting_name = cJSON_GetObjectItem( p_plug_setting, "name" );
|
||||||
|
if ( p_plug_setting_name )
|
||||||
|
{
|
||||||
|
if ( cJSON_IsString( p_plug_setting_name ) )
|
||||||
|
{
|
||||||
|
return_flag = true;
|
||||||
|
sprintf( user_config->plug[x].name, p_plug_setting_name->valuestring );
|
||||||
|
}
|
||||||
|
cJSON_AddStringToObject( json_plug_setting_send, "name", user_config->plug[x].name );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>idx----------------------------------------
|
||||||
|
cJSON *p_plug_setting_idx = cJSON_GetObjectItem( p_plug_setting, "idx" );
|
||||||
|
if ( p_plug_setting_idx )
|
||||||
|
{
|
||||||
|
if ( cJSON_IsNumber( p_plug_setting_idx ) )
|
||||||
|
{
|
||||||
|
return_flag = true;
|
||||||
|
user_config->plug[x].idx = p_plug_setting_idx->valueint;
|
||||||
|
}
|
||||||
|
cJSON_AddNumberToObject( json_plug_setting_send, "idx", user_config->plug[x].idx );
|
||||||
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD><EFBFBD>plug<75><67>setting<6E><67>task----------------------------------------
|
||||||
|
for ( i = 0; i < PLUG_TIME_TASK_NUM; i++ )
|
||||||
|
{
|
||||||
|
if ( json_plug_task_analysis( x, i, p_plug_setting, json_plug_setting_send ) )
|
||||||
|
return_flag = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cJSON_AddItemToObject( pJsonSend, plug_str, json_plug_send );
|
||||||
|
return return_flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>json
|
||||||
|
*x:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> y:<3A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
*/
|
||||||
|
bool json_plug_task_analysis( char x, char y, cJSON * pJsonRoot, cJSON * pJsonSend )
|
||||||
|
{
|
||||||
|
if ( !pJsonRoot ) return false;
|
||||||
|
bool return_flag = false;
|
||||||
|
char i;
|
||||||
|
char plug_task_str[] = "task_X";
|
||||||
|
plug_task_str[5] = y + '0';
|
||||||
|
|
||||||
|
cJSON *p_plug_task = cJSON_GetObjectItem( pJsonRoot, plug_task_str );
|
||||||
|
if ( !p_plug_task ) return false;
|
||||||
|
|
||||||
|
cJSON *json_plug_task_send = cJSON_CreateObject( );
|
||||||
|
|
||||||
|
cJSON *p_plug_task_hour = cJSON_GetObjectItem( p_plug_task, "hour" );
|
||||||
|
cJSON *p_plug_task_minute = cJSON_GetObjectItem( p_plug_task, "minute" );
|
||||||
|
cJSON *p_plug_task_repeat = cJSON_GetObjectItem( p_plug_task, "repeat" );
|
||||||
|
cJSON *p_plug_task_action = cJSON_GetObjectItem( p_plug_task, "action" );
|
||||||
|
cJSON *p_plug_task_on = cJSON_GetObjectItem( p_plug_task, "on" );
|
||||||
|
|
||||||
|
if ( p_plug_task_hour && p_plug_task_minute && p_plug_task_repeat &&
|
||||||
|
p_plug_task_action
|
||||||
|
&& p_plug_task_on )
|
||||||
|
{
|
||||||
|
|
||||||
|
if ( cJSON_IsNumber( p_plug_task_hour )
|
||||||
|
&& cJSON_IsNumber( p_plug_task_minute )
|
||||||
|
&& cJSON_IsNumber( p_plug_task_repeat )
|
||||||
|
&& cJSON_IsNumber( p_plug_task_action )
|
||||||
|
&& cJSON_IsNumber( p_plug_task_on )
|
||||||
|
)
|
||||||
|
{
|
||||||
|
return_flag = true;
|
||||||
|
user_config->plug[x].task[y].hour = p_plug_task_hour->valueint;
|
||||||
|
user_config->plug[x].task[y].minute = p_plug_task_minute->valueint;
|
||||||
|
user_config->plug[x].task[y].repeat = p_plug_task_repeat->valueint;
|
||||||
|
user_config->plug[x].task[y].action = p_plug_task_action->valueint;
|
||||||
|
user_config->plug[x].task[y].on = p_plug_task_on->valueint;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
cJSON_AddNumberToObject( json_plug_task_send, "hour", user_config->plug[x].task[y].hour );
|
||||||
|
cJSON_AddNumberToObject( json_plug_task_send, "minute", user_config->plug[x].task[y].minute );
|
||||||
|
cJSON_AddNumberToObject( json_plug_task_send, "repeat", user_config->plug[x].task[y].repeat );
|
||||||
|
cJSON_AddNumberToObject( json_plug_task_send, "action", user_config->plug[x].task[y].action );
|
||||||
|
cJSON_AddNumberToObject( json_plug_task_send, "on", user_config->plug[x].task[y].on );
|
||||||
|
|
||||||
|
cJSON_AddItemToObject( pJsonSend, plug_task_str, json_plug_task_send );
|
||||||
|
return return_flag;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned char strtohex( char a, char b )
|
unsigned char strtohex( char a, char b )
|
||||||
{
|
{
|
||||||
if ( a >= 0x30 && a <= 0x39 )
|
if ( a >= 0x30 && a <= 0x39 )
|
||||||
|
|||||||
@@ -16,44 +16,60 @@ void user_led_set( char x )
|
|||||||
MicoGpioOutputLow( Led );
|
MicoGpioOutputLow( Led );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define set_relay(a,b) if(((b) == 1) ? Relay_ON : Relay_OFF) MicoGpioOutputHigh( relay[(a)] );else MicoGpioOutputLow( relay[(a)] )
|
||||||
|
/*user_relay_set
|
||||||
|
* <20><><EFBFBD>ü̵<C3BC><CCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
* x:<3A><><EFBFBD><EFBFBD> 0-5
|
||||||
|
* y:<3A><><EFBFBD><EFBFBD> 0:<3A><> 1:<3A><>
|
||||||
|
*/
|
||||||
void user_relay_set( char x, char y )
|
void user_relay_set( char x, char y )
|
||||||
|
{
|
||||||
|
if ( x < 0 || x >= PLUG_NUM ) return;
|
||||||
|
set_relay( x, y );
|
||||||
|
user_config->plug[x].on = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>м̵<D0BC><CCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
* y:0:ȫ<><C8AB><EFBFBD><EFBFBD> 1:<3A><><EFBFBD>ݼ<EFBFBD>¼״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void user_relay_set_all( char y )
|
||||||
{
|
{
|
||||||
char onoff = (y == 1 ? Relay_ON : Relay_OFF);
|
char onoff = (y == 1 ? Relay_ON : Relay_OFF);
|
||||||
|
char i, temp;
|
||||||
|
|
||||||
OSStatus (* Gpiosetfunction)( mico_gpio_t );
|
if ( y != 0 )
|
||||||
if ( y == -1 )
|
|
||||||
Gpiosetfunction = MicoGpioOutputTrigger;
|
|
||||||
else if ( onoff )
|
|
||||||
Gpiosetfunction = MicoGpioOutputHigh;
|
|
||||||
else
|
|
||||||
Gpiosetfunction = MicoGpioOutputLow;
|
|
||||||
|
|
||||||
|
|
||||||
if ( x >= 0 && x < Relay_NUM )
|
|
||||||
{
|
{
|
||||||
(*Gpiosetfunction)( relay[x] );
|
for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
os_log("set relay %d:%d",x,y);
|
temp |= user_config->plug[i].on;
|
||||||
} else if ( x == Relay_NUM )
|
|
||||||
{
|
if ( temp == 0 )
|
||||||
for ( int i = 0; i < Relay_NUM; i++ )
|
|
||||||
{
|
{
|
||||||
(*Gpiosetfunction)( relay[i] );
|
for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
os_log("set relay %d:%d",i,y);
|
user_config->plug[i].on = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for ( i = 0; i < PLUG_NUM; i++ )
|
||||||
|
set_relay( i, user_config->plug[i].on );
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
set_relay( i, 0 );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void key_long_press( void )
|
static void key_long_press( void )
|
||||||
{
|
{
|
||||||
os_log("key_long_press");
|
os_log("key_long_press");
|
||||||
user_led_set(1);
|
user_led_set( 1 );
|
||||||
user_mqtt_send( "mqtt test" );
|
user_mqtt_send( "mqtt test" );
|
||||||
|
|
||||||
}
|
}
|
||||||
static void key_short_press( void )
|
static void key_short_press( void )
|
||||||
{
|
{
|
||||||
//os_log("test");
|
//os_log("test");
|
||||||
user_led_set(-1);
|
user_led_set( -1 );
|
||||||
//user_relay_set(6,-1);
|
//user_relay_set(6,-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -9,6 +9,6 @@
|
|||||||
extern void user_led_set(char x);
|
extern void user_led_set(char x);
|
||||||
extern void key_init(void);
|
extern void key_init(void);
|
||||||
extern void user_relay_set( char x, char y );
|
extern void user_relay_set( char x, char y );
|
||||||
|
extern void user_relay_set_all( char y );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -174,13 +174,16 @@ void rtc_thread( mico_thread_arg_t arg )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//<2F><><EFBFBD>´<EFBFBD><C2B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||||
if ( update_user_config_flag == 1 )
|
if ( update_user_config_flag == 1 )
|
||||||
{
|
{
|
||||||
mico_system_context_update( sys_config ); //<2F><><EFBFBD>¶<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
mico_system_context_update( sys_config );
|
||||||
update_user_config_flag = 0;
|
update_user_config_flag = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) ) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУһ<D7BC><D2BB>
|
//SNTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУһ<EFBFBD><EFBFBD>
|
||||||
|
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) )
|
||||||
{
|
{
|
||||||
micoWlanGetLinkStatus( &LinkStatus );
|
micoWlanGetLinkStatus( &LinkStatus );
|
||||||
if ( LinkStatus.is_connected == 1 )
|
if ( LinkStatus.is_connected == 1 )
|
||||||
|
|||||||
Reference in New Issue
Block a user