fix:优化按键按下发送逻辑

已知issue:按键导致复位
This commit is contained in:
Zip
2019-03-14 17:27:06 +08:00
parent ea4ff98a48
commit 2fd16b4239

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 "user_udp.h"
#include "cJSON/cJSON.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 };
@@ -70,29 +71,35 @@ static void key_long_press( void )
static void key_short_press( void ) static void key_short_press( void )
{ {
char i; char i;
OSStatus err;
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( ) ) if ( relay_out( ) )
{ {
user_relay_set_all( 0 ); user_relay_set_all( 0 );
cJSON_AddNumberToObject( json_send, "nvalue", 1 );
} }
else else
{ {
user_relay_set_all( 1 ); user_relay_set_all( 1 );
cJSON_AddNumberToObject( json_send, "nvalue", 0 );
} }
char *json_str = cJSON_Print( json_send ); uint8_t *buf = NULL;
if ( !user_mqtt_isconnect() )//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> if ( user_config->idx >= 0 )
user_udp_send( json_str ); {
else
user_mqtt_send( json_str ); buf = malloc( 1024 );
free( (void *) json_str ); require_action( buf, exit, err = kNoMemoryErr );
sprintf( buf, "{\"idx\" : %d,\"mac\" : \"%s\",\"nvalue\" : %d}", user_config->idx,strMac, relay_out() );
if ( !user_mqtt_isconnect( ) ) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
user_udp_send( buf );
else
user_mqtt_send( buf );
}
exit:
if ( err != kNoErr )
os_log("key_short_press Send data with err: %d", err);
if ( buf != NULL ) free( buf );
} }
void key_init( void ) void key_init( void )