fix:domoticz反馈数据导致的重复动作的问题

fix:优化全开全关是逻辑
add:增加mqtt连接状态判断函数
This commit is contained in:
Zip
2019-03-14 16:55:36 +08:00
parent 6bbbd7aba3
commit ea4ff98a48
7 changed files with 93 additions and 48 deletions

View File

@@ -48,6 +48,7 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size )
for ( i = 0; i < PLUG_NUM; i++ ) for ( i = 0; i < PLUG_NUM; i++ )
{ {
userConfigDefault->plug[i].idx = -1; userConfigDefault->plug[i].idx = -1;
userConfigDefault->plug[i].on = 1;
sprintf( userConfigDefault->plug[i].name, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d", i ); sprintf( userConfigDefault->plug[i].name, "<EFBFBD><EFBFBD><EFBFBD><EFBFBD>%d", i );
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) 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 application_start( void )
{ {
int i, j; int i;
os_log( "Start" ); os_log( "Start" );
OSStatus err = kNoErr; OSStatus err = kNoErr;

View File

@@ -9,7 +9,7 @@
#define ZTC1_NAME "zTC1_%02X%02X" #define ZTC1_NAME "zTC1_%02X%02X"
#define USER_CONFIG_VERSION 1 #define USER_CONFIG_VERSION 2
#define SETTING_MQTT_STRING_LENGTH_MAX 32 //<2F><><EFBFBD><EFBFBD> 4 <20>ֽڶ<D6BD><DAB6> #define SETTING_MQTT_STRING_LENGTH_MAX 32 //<2F><><EFBFBD><EFBFBD> 4 <20>ֽڶ<D6BD><DAB6>
#define PLUG_NAME_LENGTH 32 #define PLUG_NAME_LENGTH 32

View File

@@ -4,6 +4,13 @@
#include "user_gpio.h" #include "user_gpio.h"
#include "cJSON/cJSON.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 typedef struct _user_json_context_t
{ {
int8_t idx; 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 ) void user_send( int udp_flag, char *s )
{ {
if ( udp_flag ) if ( udp_flag || !user_mqtt_isconnect( ) )
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 );
@@ -47,8 +54,7 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
cJSON_AddNumberToObject( pRoot, "type", TYPE ); cJSON_AddNumberToObject( pRoot, "type", TYPE );
cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME ); cJSON_AddStringToObject( pRoot, "type_name", TYPE_NAME );
char *s = cJSON_Print( pRoot ); char *s = cJSON_Print( pRoot );
os_log( "pRoot: %s\r\n", s ); // os_log( "pRoot: %s\r\n", s );
user_send( udp_flag, s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> user_send( udp_flag, s ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( (void *) s ); free( (void *) s );
cJSON_Delete( pRoot ); 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_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 )
{
if ( cJSON_IsNumber( p_nvalue ) )
{
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
{ {
// 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 ( p_nvalue->valueint != user_config->plug[i].on )
{ {
user_relay_set_all( p_nvalue->valueint ); user_relay_set_all( p_nvalue->valueint );
update_user_config_flag = true;
} }
// } }
// user_json_set_last_time( ); user_function_set_last_time( );
}
cJSON_AddNumberToObject( json_send, "nvalue", relay_out( ) );
} }
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting----------------------------------------------------------------- //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>setting-----------------------------------------------------------------
@@ -202,10 +213,10 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata )
if ( user_config->idx >= 0 ) if ( user_config->idx >= 0 )
cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); cJSON_AddNumberToObject( json_send, "idx", user_config->idx );
//if ( return_flag == true ) if ( return_flag == true )
{ {
char *json_str = cJSON_Print( json_send ); 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 ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> user_send( udp_flag, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( (void *) 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 ) ) if ( cJSON_IsNumber( p_plug_on ) )
{ {
user_relay_set( x, p_plug_on->valueint ); user_relay_set( x, p_plug_on->valueint );
return_flag = true;
if ( user_config->plug[x].idx > 0 ) if ( user_config->plug[x].idx > 0 )
{ {
cJSON *json_return_now = cJSON_CreateObject( ); 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; if ( !pJsonRoot ) return false;
bool return_flag = false; bool return_flag = false;
char i;
char plug_task_str[] = "task_X"; char plug_task_str[] = "task_X";
plug_task_str[5] = y + '0'; plug_task_str[5] = y + '0';

View File

@@ -3,6 +3,7 @@
#include "main.h" #include "main.h"
#include "user_gpio.h" #include "user_gpio.h"
#include "user_mqtt_client.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 }; 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 ); 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)] ) #define set_relay(a,b) if(((b) == 1) ? Relay_ON : Relay_OFF) MicoGpioOutputHigh( relay[(a)] );else MicoGpioOutputLow( relay[(a)] )
/*user_relay_set /*user_relay_set
* <20><><EFBFBD>ü̵<C3BC><CCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> * <20><><EFBFBD>ü̵<C3BC><CCB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@@ -25,8 +38,14 @@ void user_led_set( char x )
void user_relay_set( char x, char y ) void user_relay_set( char x, char y )
{ {
if ( x < 0 || x >= PLUG_NUM ) return; if ( x < 0 || x >= PLUG_NUM ) return;
set_relay( x, y ); set_relay( x, y );
user_config->plug[x].on = 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 ) void user_relay_set_all( char y )
{ {
char onoff = (y == 1 ? Relay_ON : Relay_OFF); char i;
char i, temp;
if ( y != 0 )
{
for ( i = 0; i < PLUG_NUM; i++ ) for ( i = 0; i < PLUG_NUM; i++ )
temp |= user_config->plug[i].on; user_relay_set( i, y );
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 );
}
} }
static void key_long_press( void ) static void key_long_press( void )
@@ -68,9 +69,30 @@ static void key_long_press( void )
} }
static void key_short_press( void ) static void key_short_press( void )
{ {
//os_log("test"); char i;
user_led_set( -1 );
//user_relay_set(6,-1); 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() )//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
user_udp_send( json_str );
else
user_mqtt_send( json_str );
free( (void *) json_str );
} }
void key_init( void ) void key_init( void )

View File

@@ -108,7 +108,7 @@ OSStatus user_recv_handler( void *arg );
/****************************************************** /******************************************************
* Variables Definitions * Variables Definitions
******************************************************/ ******************************************************/
bool isconnect=false;
mico_queue_t mqtt_msg_send_queue = NULL; mico_queue_t mqtt_msg_send_queue = NULL;
Client c; // mqtt client object 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!!!") ); require_action( msg_send_event_fd >= 0, exit, mqtt_log("ERROR: create msg send queue event fd failed!!!") );
MQTT_start: MQTT_start:
isconnect=false;
/* 1. create network connection */ /* 1. create network connection */
#ifdef MQTT_CLIENT_SSL_ENABLE #ifdef MQTT_CLIENT_SSL_ENABLE
ssl_settings.ssl_enable = true; ssl_settings.ssl_enable = true;
@@ -246,6 +248,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
LinkStatusTypeDef LinkStatus; LinkStatusTypeDef LinkStatus;
while ( 1 ) while ( 1 )
{ {
isconnect=false;
mico_rtos_thread_sleep( 3 ); mico_rtos_thread_sleep( 3 );
if(MQTT_SERVER[0]<0x20 ||MQTT_SERVER[0]>0x7f ||MQTT_SERVER_PORT<1) continue; //δ<><CEB4><EFBFBD><EFBFBD>mqtt<74><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD> if(MQTT_SERVER[0]<0x20 ||MQTT_SERVER[0]>0x7f ||MQTT_SERVER_PORT<1) continue; //δ<><CEB4><EFBFBD><EFBFBD>mqtt<74><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@@ -294,6 +297,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
/* 5. client loop for recv msg && keepalive */ /* 5. client loop for recv msg && keepalive */
while ( 1 ) while ( 1 )
{ {
isconnect=true;
no_mqtt_msg_exchange = true; no_mqtt_msg_exchange = true;
FD_ZERO( &readfds ); FD_ZERO( &readfds );
FD_SET( c.ipstack->my_socket, &readfds ); FD_SET( c.ipstack->my_socket, &readfds );
@@ -342,7 +346,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
MQTT_reconnect: MQTT_reconnect:
mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err ); mqtt_log("Disconnect MQTT client, and reconnect after 5s, reason: mqtt_rc = %d, err = %d", rc, err );
mqtt_client_release( &c, &n ); mqtt_client_release( &c, &n );
isconnect=false;
user_led_set( -1 ); user_led_set( -1 );
mico_rtos_thread_msleep(100); mico_rtos_thread_msleep(100);
user_led_set( -1 ); user_led_set( -1 );
@@ -350,6 +354,7 @@ void mqtt_client_thread( mico_thread_arg_t arg )
goto MQTT_start; goto MQTT_start;
exit: exit:
isconnect=false;
mqtt_log("EXIT: MQTT client exit with err = %d.", err); mqtt_log("EXIT: MQTT client exit with err = %d.", err);
mqtt_client_release( &c, &n ); mqtt_client_release( &c, &n );
mico_rtos_delete_thread( NULL ); mico_rtos_delete_thread( NULL );
@@ -438,3 +443,8 @@ OSStatus user_mqtt_send( char *arg )
return err; return err;
} }
bool user_mqtt_isconnect()
{
return isconnect;
}

View File

@@ -8,6 +8,6 @@
extern OSStatus user_mqtt_init(void); extern OSStatus user_mqtt_init(void);
extern OSStatus user_mqtt_send( char *arg ); extern OSStatus user_mqtt_send( char *arg );
extern bool user_mqtt_isconnect(void);
#endif #endif

View File

@@ -87,6 +87,7 @@ static void wifi_led_timer_callback( void* arg )
{ {
static unsigned int num = 0; static unsigned int num = 0;
num++; num++;
switch ( wifi_status ) switch ( wifi_status )
{ {
case WIFI_STATE_FAIL: case WIFI_STATE_FAIL: