diff --git a/TC1/main.c b/TC1/main.c index f2cdc4f..d476c52 100644 --- a/TC1/main.c +++ b/TC1/main.c @@ -36,11 +36,9 @@ void appRestoreDefault_callback( void * const user_config_data, uint32_t size ) userConfigDefault->mqtt_user[0] = 0; userConfigDefault->mqtt_password[0] = 0; - userConfigDefault->idx = -1; userConfigDefault->version = USER_CONFIG_VERSION; for ( i = 0; i < PLUG_NUM; i++ ) { - userConfigDefault->plug[i].idx = -1; userConfigDefault->plug[i].on = 1; //插座名称 插口1-6 @@ -134,12 +132,10 @@ int application_start( void ) os_log( "mqtt_password:%s",user_config->mqtt_password ); os_log( "version:%d",user_config->version ); - os_log( "idx:%d",user_config->idx ); // for ( i = 0; i < PLUG_NUM; i++ ) // { // os_log("plug_%d:",i); // os_log("\tname:%s:",user_config->plug[i].name); -// os_log("\tidx:%d:",user_config->plug[i].idx); // for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ ) // { // os_log("\t\ton:%d\t %02d:%02d repeat:0x%X",user_config->plug[i].task[j].on, @@ -154,13 +150,16 @@ int application_start( void ) require_noerr( err, exit ); err = user_rtc_init( ); require_noerr( err, exit ); - user_udp_init( ); +// user_power_init(); while ( 1 ) { // mico_thread_msleep(500); // MicoGpioOutputTrigger(MICO_GPIO_5); // mico_gpio_output_toggle( MICO_SYS_LED ); +// mico_rtos_delay_milliseconds(1000); +// uint32_t a=mico_nanosecond_clock_value(); +// os_log("nano=%lu",a/1000); // mico_rtos_delay_milliseconds(1000); } exit: diff --git a/TC1/main.h b/TC1/main.h index 8acdbd2..98c4906 100644 --- a/TC1/main.h +++ b/TC1/main.h @@ -4,14 +4,14 @@ #include "mico.h" #include "MiCOKit_EXT.h" -#define VERSION "v0.5" +#define VERSION "v0.6" #define TYPE 1 #define TYPE_NAME "zTC1" #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 @@ -45,7 +45,6 @@ typedef struct typedef struct { char name[PLUG_NAME_LENGTH]; - char idx; char on; //记录当前开关 user_plug_task_config_t task[PLUG_TIME_TASK_NUM]; @@ -61,7 +60,6 @@ typedef struct // char mqtt_device_id[SETTING_MQTT_STRING_LENGTH_MAX]; //mqtt service user device name char version; - char idx; user_plug_config_t plug[PLUG_NUM]; } user_config_t; diff --git a/TC1/user_function.c b/TC1/user_function.c index 29ff46b..1ffd75d 100644 --- a/TC1/user_function.c +++ b/TC1/user_function.c @@ -16,12 +16,7 @@ void user_function_set_last_time( ) last_time = UpTicks( ); } -typedef struct _user_json_context_t -{ - int8_t idx; - char name[maxNameLen]; - int8_t val; -} user_json_context_t; + bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON * pJsonSend ); bool json_plug_task_analysis(unsigned char x, unsigned char y, cJSON * pJsonRoot, cJSON * pJsonSend ); @@ -66,79 +61,17 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) } //以下为解析命令部分 - cJSON *p_idx = cJSON_GetObjectItem( pJsonRoot, "idx" ); - cJSON *p_description = cJSON_GetObjectItem( pJsonRoot, "description" ); cJSON *p_name = cJSON_GetObjectItem( pJsonRoot, "name" ); cJSON *p_mac = cJSON_GetObjectItem( pJsonRoot, "mac" ); - //判断p_idx,使用"idx"作为主要字符串时 - for ( i = 0; i < PLUG_NUM; i++ ) - { - //仅从插座的idx值判断 成立时不处理其他功能,仅改变插座状态 - 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 ); //储存当前开关状态 - 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 ); - cJSON_AddStringToObject( json_send, "mac", strMac ); - - char strTemp1[] = "plug_X"; - strTemp1[5] = i + '0'; - cJSON *json_send_plug_on = cJSON_CreateObject( ); - cJSON_AddNumberToObject( json_send_plug_on, "on", p_nvalue->valueint ); - - cJSON_AddItemToObject( json_send, strTemp1, json_send_plug_on ); - - char *json_str = cJSON_Print( json_send ); - user_send( udp_flag, json_str ); //发送数据 - free( json_str ); - cJSON_Delete( json_send ); - } - } - cJSON_Delete( pJsonRoot ); - return; - } - } - //开始正式处理所有命令 - 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_name && cJSON_IsString( p_name ) && strcmp( p_name->valuestring, sys_config->micoSystemConfig.name ) == 0) //name + if ( (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 ) { cJSON *json_send = cJSON_CreateObject( ); cJSON_AddStringToObject( json_send, "mac", strMac ); - cJSON *p_nvalue = cJSON_GetObjectItem( pJsonRoot, "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 - { - 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( ); - } - cJSON_AddNumberToObject( json_send, "nvalue", p_nvalue->valueint ); - } //解析版本 cJSON *p_version = cJSON_GetObjectItem( pJsonRoot, "version" ); if ( p_version ) @@ -199,15 +132,6 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) sprintf( user_config->mqtt_password, p_mqtt_password->valuestring ); } - //设置domoticz idx - cJSON *p_setting_idx = cJSON_GetObjectItem( p_setting, "idx" ); - if ( p_setting_idx && cJSON_IsNumber( p_setting_idx ) ) - { - update_user_config_flag = true; - user_config->idx = p_setting_idx->valueint; - os_log( "idx:%d",user_config->idx ); - mico_system_context_update( sys_config ); - } //开发返回数据 //返回设备ota @@ -223,8 +147,6 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) if ( p_mqtt_user ) cJSON_AddStringToObject( json_setting_send, "mqtt_user", user_config->mqtt_user ); //返回mqtt password if ( p_mqtt_password ) cJSON_AddStringToObject( json_setting_send, "mqtt_password", user_config->mqtt_password ); - //返回domoticz idx - if ( p_setting_idx ) cJSON_AddNumberToObject( json_setting_send, "idx", user_config->idx ); cJSON_AddItemToObject( json_send, "setting", json_setting_send ); } @@ -238,9 +160,6 @@ void user_function_cmd_received( int udp_flag, uint8_t *pusrdata ) cJSON_AddStringToObject( json_send, "name", sys_config->micoSystemConfig.name ); - //if (p_idx) - if ( user_config->idx >= 0 ) - cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); if ( return_flag == true ) { @@ -291,18 +210,8 @@ bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON { user_relay_set( x, p_plug_on->valueint ); return_flag = true; - 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 ); //发送数据 - free( json_str ); - cJSON_Delete( json_return_now ); - } } - + user_mqtt_send_plug_state(x); } //解析plug中setting项目---------------------------------------------- @@ -322,18 +231,6 @@ bool json_plug_analysis( int udp_flag, unsigned char x, cJSON * pJsonRoot, cJSON cJSON_AddStringToObject( json_plug_setting_send, "name", user_config->plug[x].name ); } - //解析plug中setting中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 ); - } - //解析plug中setting中task---------------------------------------- for ( i = 0; i < PLUG_TIME_TASK_NUM; i++ ) { diff --git a/TC1/user_gpio.c b/TC1/user_gpio.c index a41811d..fbb6280 100644 --- a/TC1/user_gpio.c +++ b/TC1/user_gpio.c @@ -99,28 +99,12 @@ static void key_short_press( void ) user_relay_set_all( 1 ); } - char relay_out_temp; - cJSON *json_send = cJSON_CreateObject( ); - cJSON_AddStringToObject( json_send, "mac", strMac ); - if ( user_config->idx >= 0 ) - cJSON_AddNumberToObject( json_send, "idx", user_config->idx ); - - relay_out_temp = relay_out( ) ? 1 : 0; - char plug_str[] = "plug_X"; for ( i = 0; i < PLUG_NUM; i++ ) { - plug_str[5] = i + '0'; - cJSON *json_plug_send = cJSON_CreateObject( ); - cJSON_AddNumberToObject( json_plug_send, "on", relay_out_temp ); - cJSON_AddItemToObject( json_send, plug_str, json_plug_send ); + user_mqtt_send_plug_state(i); } - char *json_str = cJSON_Print( json_send ); - user_send(false,json_str); - - free( (void *) json_str ); - cJSON_Delete( json_send ); } mico_timer_t user_key_timer; diff --git a/TC1/user_mqtt_client.c b/TC1/user_mqtt_client.c index c0c967d..a01e162 100644 --- a/TC1/user_mqtt_client.c +++ b/TC1/user_mqtt_client.c @@ -41,8 +41,8 @@ #define MQTT_CLIENT_USERNAME user_config->mqtt_user #define MQTT_CLIENT_PASSWORD user_config->mqtt_password #define MQTT_CLIENT_KEEPALIVE 30 -#define MQTT_CLIENT_SUB_TOPIC1 "domoticz/out" // loop msg -#define MQTT_CLIENT_PUB_TOPIC "domoticz/in" +#define MQTT_CLIENT_SUB_TOPIC1 "device/ztc1/set" // loop msg +#define MQTT_CLIENT_PUB_TOPIC "device/ztc1/%s/state" #define MQTT_CMD_TIMEOUT 5000 // 5s #define MQTT_YIELD_TMIE 5000 // 5s //#define MQTT_CLIENT_SSL_ENABLE // ssl @@ -109,6 +109,9 @@ static OSStatus mqtt_msg_publish( Client *c, const char* topic, char qos, char r OSStatus user_send_handler( void *arg ); OSStatus user_recv_handler( void *arg ); +OSStatus user_mqtt_send_plug_state( char plug_id ); +void user_mqtt_hass_auto( char plug_id ); + /****************************************************** * Variables Definitions ******************************************************/ @@ -121,8 +124,9 @@ Network n; // socket network for mqtt client static mico_worker_thread_t mqtt_client_worker_thread; /* Worker thread to manage send/recv events */ static mico_timed_event_t mqtt_client_send_event; -char hass_topic_in[MAX_MQTT_TOPIC_SIZE]; -char hass_topic_out[MAX_MQTT_TOPIC_SIZE]; +char topic_state[MAX_MQTT_TOPIC_SIZE]; +char topic_set[MAX_MQTT_TOPIC_SIZE]; +char hass_mqtt_flag; /****************************************************** * Function Definitions ******************************************************/ @@ -132,16 +136,14 @@ OSStatus user_mqtt_init( void ) { OSStatus err = kNoErr; - - sprintf(hass_topic_out,"ztc1/%s/out",strMac); - sprintf(hass_topic_in,"ztc1/%s/in",strMac); - - + sprintf( topic_set, MQTT_CLIENT_SUB_TOPIC1, strMac ); + sprintf( topic_state, MQTT_CLIENT_PUB_TOPIC, strMac ); #ifdef MQTT_CLIENT_SSL_ENABLE int mqtt_thread_stack_size = 0x3000; #else - int mqtt_thread_stack_size = 0x800; + //TODO size:0x800 + int mqtt_thread_stack_size = 0x2000; #endif uint32_t mqtt_lib_version = MQTTClientLibVersion( ); app_log( "MQTT client version: [%ld.%ld.%ld]", @@ -224,7 +226,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) { OSStatus err = kUnknownErr; - int i,rc = -1; + int i, rc = -1; fd_set readfds; struct timeval t = { 0, MQTT_YIELD_TMIE * 1000 }; @@ -277,7 +279,7 @@ void mqtt_client_thread( mico_thread_arg_t arg ) mqtt_log("ERROR: MQTT network connection err=%d, reconnect after 3s...", rc); } - + hass_mqtt_flag = PLUG_NUM; mqtt_log("MQTT network connection success!"); /* 2. init mqtt client */ @@ -302,13 +304,9 @@ void mqtt_client_thread( mico_thread_arg_t arg ) mqtt_log("MQTT client connect success!"); /* 4. mqtt client subscribe */ - rc = MQTTSubscribe( &c, MQTT_CLIENT_SUB_TOPIC1, QOS0, messageArrived ); + rc = MQTTSubscribe( &c, topic_set, QOS0, messageArrived ); require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); - mqtt_log("MQTT client subscribe success! recv_topic=[%s].", MQTT_CLIENT_SUB_TOPIC1); - - rc = MQTTSubscribe( &c, hass_topic_out, QOS0, messageArrived ); - require_noerr_string( rc, MQTT_reconnect, "ERROR: MQTT client subscribe err." ); - mqtt_log("MQTT client subscribe success! recv_topic=[%s].", hass_topic_out); + mqtt_log("MQTT client subscribe success! recv_topic=[%s].", topic_set); /*4.1 连接成功后先更新发送一次数据*/ isconnect = true; uint8_t *buf1 = NULL; @@ -320,15 +318,6 @@ void mqtt_client_thread( mico_thread_arg_t arg ) "{\"mac\":\"%s\",\"version\":null,\"plug_0\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_1\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_2\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_3\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_4\":{\"on\":null,\"setting\":{\"name\":null}},\"plug_5\":{\"on\":null,\"setting\":{\"name\":null}}}", strMac ); user_function_cmd_received( 0, buf1 ); - - for ( i = 0; i < PLUG_NUM; i++ ) - { - if ( user_config->plug[i].idx >= 0 ) - { - sprintf( buf1, "{\"idx\":%d,\"nvalue\":%d}", user_config->plug[i].idx, user_config->plug[i].on ); - user_mqtt_send( buf1 ); - } - } free( buf1 ); } @@ -350,6 +339,12 @@ void mqtt_client_thread( mico_thread_arg_t arg ) no_mqtt_msg_exchange = false; } + if ( hass_mqtt_flag>0 ) + { + hass_mqtt_flag --; + user_mqtt_hass_auto(hass_mqtt_flag); + user_mqtt_send_plug_state(hass_mqtt_flag); + } /* recv msg from user worker thread to be sent to server */ if ( FD_ISSET( msg_send_event_fd, &readfds ) ) { @@ -445,7 +440,7 @@ OSStatus user_recv_handler( void *arg ) return err; } -OSStatus user_mqtt_send_topic( char *topic, char *arg ) +OSStatus user_mqtt_send_topic( char *topic, char *arg, char retained ) { OSStatus err = kUnknownErr; p_mqtt_send_msg_t p_send_msg = NULL; @@ -465,7 +460,7 @@ OSStatus user_mqtt_send_topic( char *topic, char *arg ) require_action( p_send_msg, exit, err = kNoMemoryErr ); p_send_msg->qos = 0; - p_send_msg->retained = 0; + p_send_msg->retained = retained; p_send_msg->datalen = strlen( arg ); memcpy( p_send_msg->data, arg, p_send_msg->datalen ); strncpy( p_send_msg->topic, topic, MAX_MQTT_TOPIC_SIZE ); @@ -483,8 +478,54 @@ OSStatus user_mqtt_send_topic( char *topic, char *arg ) /* Application collect data and seng them to MQTT send queue */ OSStatus user_mqtt_send( char *arg ) { - user_mqtt_send_topic(hass_topic_in,arg); - return user_mqtt_send_topic(MQTT_CLIENT_PUB_TOPIC,arg); + return user_mqtt_send_topic( topic_state, arg, 0 ); +} + +//更新ha开关状态 +OSStatus user_mqtt_send_plug_state( char plug_id ) +{ + + uint8_t *send_buf = NULL; + uint8_t *topic_buf = NULL; + send_buf = malloc( 64 ); // + topic_buf = malloc( 64 ); // + if ( send_buf != NULL && topic_buf != NULL ) + { + sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/state", strMac, plug_id ); + sprintf( send_buf, "{\"mac\":\"%s\",\"plug_%d\":{\"on\":%d}}", strMac, plug_id, user_config->plug[plug_id].on ); + user_mqtt_send_topic( topic_buf, send_buf, 1 ); + } + if ( send_buf ) free( send_buf ); + if ( topic_buf ) free( topic_buf ); +} + +//hass mqtt自动发现数据发送 +void user_mqtt_hass_auto( char plug_id ) +{ + uint8_t i; + uint8_t *send_buf = NULL; + uint8_t *topic_buf = NULL; + send_buf = malloc( 512 ); // + topic_buf = malloc( 128 ); // + if ( send_buf != NULL && topic_buf != NULL ) + { + sprintf( topic_buf, "homeassistant/switch/%s/plug_%d/config", strMac, plug_id ); + sprintf( send_buf, + "{" + "\"name\":\"%s\"," + "\"state_topic\":\"homeassistant/switch/%s/plug_%d/state\"," + "\"command_topic\":\"device/ztc1/set\"," + "\"payload_on\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":1}}\"," + "\"payload_off\":\"{\\\"mac\\\":\\\"%s\\\",\\\"plug_%d\\\":{\\\"on\\\":0}}\"" + "}", + user_config->plug[plug_id].name, + strMac, plug_id, + strMac, plug_id, + strMac, plug_id ); + user_mqtt_send_topic( topic_buf, send_buf, 1 ); + } + if ( send_buf ) free( send_buf ); + if ( topic_buf ) free( topic_buf ); } diff --git a/TC1/user_mqtt_client.h b/TC1/user_mqtt_client.h index 060e7cb..f02e945 100644 --- a/TC1/user_mqtt_client.h +++ b/TC1/user_mqtt_client.h @@ -9,5 +9,6 @@ extern OSStatus user_mqtt_init(void); extern OSStatus user_mqtt_send( char *arg ); extern bool user_mqtt_isconnect(void); - +extern OSStatus user_mqtt_send_plug_state( char plug_id ); +extern void user_mqtt_hass_auto( char plug_id ); #endif diff --git a/TC1/user_power.c b/TC1/user_power.c index 4a40336..253bba6 100644 --- a/TC1/user_power.c +++ b/TC1/user_power.c @@ -31,8 +31,9 @@ static void power_timer_handler( void* arg ) static void power_irq_handler( void* arg ) { // timer_count_last=timer_count; - timer_count = UpTicks( ); + timer_count = mico_nanosecond_clock_value(); timer=timer_count-timer_count_last; + if(timer_countplug[i].task[j].action ); update_user_config_flag = 1; - - //更新domoticz状态 - if ( user_config->plug[i].idx >= 0 ) - { - uint8_t *buf = NULL; - buf = malloc( 32 ); //idx为1位时长度为24 - if ( buf != NULL ) - { - sprintf( buf, "{\"idx\":%d,\"nvalue\":%d}", user_config->plug[i].idx, user_config->plug[i].on ); - user_send(false,buf); - free( buf ); - } - } + user_mqtt_send_plug_state(i); } if ( repeat == 0x00 ) { diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin index 7ea7954..74b9a6d 100644 Binary files a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.all.bin differ diff --git a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin index 04be1ca..1b74721 100644 Binary files a/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin and b/build/TC1@MK3031@moc/binary/TC1@MK3031@moc.ota.bin differ