diff --git a/TC1/main.c b/TC1/main.c index 4d0b6f1..3c97437 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -48,6 +48,7 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) for ( i = 0; i < PLUG_NUM; i++ ) { userConfigDefault->plug[i].idx = -1; + userConfigDefault->plug[i].on = 1; sprintf( userConfigDefault->plug[i].name, "插座%d", i ); for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) { @@ -65,7 +66,7 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) int application_start( void ) { - int i, j; + int i; os_log( "Start" ); OSStatus err = kNoErr; diff --git a/TC1/main.h b/TC1/main.h index 5ac63b8..4bd85e2 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -9,7 +9,7 @@ #define ZTC1_NAME "zTC1_%02X%02X" -#define USER_CONFIG_VERSION 1 +#define USER_CONFIG_VERSION 2 #define SETTING_MQTT_STRING_LENGTH_MAX 32 //必须 4 字节对齐。 #define PLUG_NAME_LENGTH 32 diff --git a/TC1/user_function.c b/TC1/user_function.c index f80de92..f611070 100644 --- a/TC1/user_function.c +++ b/TC1/user_function.c @@ -4,6 +4,13 @@ #include "user_gpio.h" #include "cJSON/cJSON.h" +uint32_t last_time = 0; + +void user_function_set_last_time( ) +{ + last_time = UpTicks( ); +} + typedef struct _user_json_context_t { int8_t idx; @@ -16,7 +23,7 @@ bool json_plug_task_analysis( char x, char y, cJSON * pJsonRoot, cJSON * pJsonSe void user_send( int udp_flag, char *s ) { - if ( udp_flag ) + if ( udp_flag || !user_mqtt_isconnect( ) ) user_udp_send( s ); //发送数据 else user_mqtt_send( s ); @@ -47,8 +54,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) cJSON_AddNumberToObject( pRoot, "type", TYPE ); cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME ); char *s = cJSON_Print( pRoot ); - os_log( "pRoot: %s\r\n", s ); - +// os_log( "pRoot: %s\r\n", s ); user_send( udp_flag, s ); //发送数据 free( (void *) s ); cJSON_Delete( pRoot ); @@ -99,21 +105,26 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) cJSON_AddStringToObject( json_send, "mac", strMac ); cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "nvalue" ); - if ( p_nvalue && cJSON_IsNumber( p_nvalue ) ) + if ( p_nvalue ) { -// uint32 now_time = system_get_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( ) ) -// { -// return_flag = false; -// } else -// { - if ( p_nvalue->valueint != user_config->plug[i].on ) + if ( cJSON_IsNumber( p_nvalue ) ) { - user_relay_set_all( p_nvalue->valueint ); + uint32_t now_time = UpTicks( ); + os_log( "system_get_time:%d,%d = %09d\r\n", last_time, now_time, (now_time - last_time) ); + if ( now_time - last_time < 1000 && p_idx ) + { + return_flag = false; + } else + { + if ( p_nvalue->valueint != user_config->plug[i].on ) + { + user_relay_set_all( p_nvalue->valueint ); + update_user_config_flag = true; + } + } + user_function_set_last_time( ); } -// } -// user_json_set_last_time( ); + cJSON_AddNumberToObject( json_send, "nvalue", relay_out( ) ); } //解析主机setting----------------------------------------------------------------- @@ -202,10 +213,10 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) if ( user_config->idx >= 0 ) cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); - //if ( return_flag == true ) + if ( return_flag == true ) { char *json_str = cJSON_Print( json_send ); - os_log( "pRoot: %s\r\n", json_str ); +// os_log( "pRoot: %s\r\n", json_str ); user_send( udp_flag, json_str ); //发送数据 free( (void *) json_str ); } @@ -253,7 +264,7 @@ bool json_plug_analysis( int udp_flag, char x, cJSON * pJsonRoot, cJSON * pJsonS if ( cJSON_IsNumber( p_plug_on ) ) { user_relay_set( x, p_plug_on->valueint ); - + return_flag = true; if ( user_config->plug[x].idx > 0 ) { cJSON *json_return_now = cJSON_CreateObject( ); @@ -319,7 +330,7 @@ bool json_plug_task_analysis( char x, char y, cJSON * pJsonRoot, cJSON * pJsonSe { if ( !pJsonRoot ) return false; bool return_flag = false; - char i; + char plug_task_str[] = "task_X"; plug_task_str[5] = y + '0'; diff --git a/TC1/user_gpio.c b/TC1/user_gpio.c index 84f6683..c6b493f 100644 --- a/TC1/user_gpio.c +++ b/TC1/user_gpio.c @@ -3,6 +3,7 @@ #include "main.h" #include "user_gpio.h" #include "user_mqtt_client.h" +#include "cJSON/cJSON.h" mico_gpio_t relay[Relay_NUM] = { Relay_0, Relay_1, Relay_2, Relay_3, Relay_4, Relay_5 }; @@ -16,6 +17,18 @@ void user_led_set( char x ) MicoGpioOutputLow( Led ); } +bool relay_out( void ) +{ + char i; + for ( i = 0; i < PLUG_NUM; i++ ) + { + if ( user_config->plug[i].on != 0 ) + { + return true; + } + } + return false; +} #define set_relay(a,b) if(((b) == 1) ? Relay_ON : Relay_OFF) MicoGpioOutputHigh( relay[(a)] );else MicoGpioOutputLow( relay[(a)] ) /*user_relay_set * 设置继电器开关 @@ -25,8 +38,14 @@ void user_led_set( char x ) 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; + + if ( relay_out( ) ) + user_led_set( 1 ); + else + user_led_set( 0 ); } /* @@ -36,27 +55,9 @@ void user_relay_set( char x, char y ) */ void user_relay_set_all( char y ) { - char onoff = (y == 1 ? Relay_ON : Relay_OFF); - char i, temp; - - if ( y != 0 ) - { - for ( i = 0; i < PLUG_NUM; i++ ) - temp |= user_config->plug[i].on; - - if ( temp == 0 ) - { - for ( i = 0; i < PLUG_NUM; i++ ) - 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 ); - } + char i; + for ( i = 0; i < PLUG_NUM; i++ ) + user_relay_set( i, y ); } static void key_long_press( void ) @@ -68,9 +69,30 @@ static void key_long_press( void ) } static void key_short_press( void ) { -//os_log("test"); - user_led_set( -1 ); - //user_relay_set(6,-1); + char i; + + cJSON *json_send = cJSON_CreateObject( ); + + cJSON_AddStringToObject( json_send, "mac", strMac ); + if ( user_config->idx >= 0 ) cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); + + if ( relay_out( ) ) + { + user_relay_set_all( 0 ); + cJSON_AddNumberToObject( json_send, "nvalue", 1 ); + } + else + { + user_relay_set_all( 1 ); + cJSON_AddNumberToObject( json_send, "nvalue", 0 ); + } + + char *json_str = cJSON_Print( json_send ); + if ( !user_mqtt_isconnect() )//发送数据 + user_udp_send( json_str ); + else + user_mqtt_send( json_str ); + free( (void *) json_str ); } void key_init( void ) diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index a16a56f..2d97f86 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -108,7 +108,7 @@ OSStatus user_recv_handler( void *arg ); /****************************************************** * Variables Definitions ******************************************************/ - +bool isconnect=false; mico_queue_t mqtt_msg_send_queue = NULL; Client c; // mqtt client object @@ -233,6 +233,8 @@ void mqtt_client_thread( mico_thread_arg_t arg ) require_action( msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!") ); MQTT_start: + + isconnect=false; /* 1. create network connection */ #ifdef MQTT_CLIENT_SSL_ENABLE ssl_settings.ssl_enable = true; @@ -246,6 +248,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) LinkStatusTypeDef LinkStatus; while ( 1 ) { + isconnect=false; mico_rtos_thread_sleep( 3 ); if(MQTT_SERVER[0]<0x20 ||MQTT_SERVER[0]>0x7f ||MQTT_SERVER_PORT<1) continue; //未配置mqtt服务器时不连接 @@ -294,6 +297,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) /* 5. client loop for recv msg && keepalive */ while ( 1 ) { + isconnect=true; no_mqtt_msg_exchange = true; FD_ZERO( &readfds ); FD_SET( c.ipstack->my_socket, &readfds ); @@ -342,7 +346,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) MQTT_reconnect: mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err ); mqtt_client_release( &c, &n ); - + isconnect=false; user_led_set( -1 ); mico_rtos_thread_msleep(100); user_led_set( -1 ); @@ -350,6 +354,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) goto MQTT_start; exit: + isconnect=false; mqtt_log("EXIT: MQTT client exit with err = %d.", err); mqtt_client_release( &c, &n ); mico_rtos_delete_thread( NULL ); @@ -438,3 +443,8 @@ OSStatus user_mqtt_send( char *arg ) return err; } + +bool user_mqtt_isconnect() +{ + return isconnect; +} diff --git a/TC1/user_mqtt_client.h b/TC1/user_mqtt_client.h index 2d0fb84..060e7cb 100644 --- a/TC1/user_mqtt_client.h +++ b/TC1/user_mqtt_client.h @@ -8,6 +8,6 @@ extern OSStatus user_mqtt_init(void); extern OSStatus user_mqtt_send( char *arg ); - +extern bool user_mqtt_isconnect(void); #endif diff --git a/TC1/user_wifi.c b/TC1/user_wifi.c index 00712e7..a6774fe 100644 --- a/TC1/user_wifi.c +++ b/TC1/user_wifi.c @@ -87,6 +87,7 @@ static void wifi_led_timer_callback( void* arg ) { static unsigned int num = 0; num++; + switch ( wifi_status ) { case WIFI_STATE_FAIL: