Files
zTC1/TC1/user_rtc.c
Zip c50db6a0bb add:功率功能
add:尝试增加ota进度功能
2019-03-29 16:26:35 +08:00

273 lines
9.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#define os_log(format, ...) custom_log("RTC", format, ##__VA_ARGS__)
#include "main.h"
#include "user_gpio.h"
#include "sntp.h"
#include "user_sntp.h"
#include "cJSON/cJSON.h"
#include "user_mqtt_client.h"
#include "user_function.h"
void rtc_thread( mico_thread_arg_t arg );
OSStatus user_sntp_get_time( )
{
OSStatus err = kNoErr;
ntp_timestamp_t current_time;
struct hostent * hostent_content = NULL;
char ** pptr = NULL;
struct in_addr ipp;
// mico_rtc_time_t rtc_time;
hostent_content = gethostbyname( "pool.ntp.org" );
pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time );
if ( err != kNoErr )
{
os_log("sntp_get_time1 err = %d.", err);
hostent_content = gethostbyname( "cn.pool.ntp.org" );
pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time );
}
if ( err != kNoErr )
{
os_log("sntp_get_time2 err = %d.", err);
hostent_content = gethostbyname( "s1a.time.edu.cn" );
pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time );
}
if ( err != kNoErr )
{
os_log("sntp_get_time3 err = %d.", err);
hostent_content = gethostbyname( "ntp.sjtu.edu.cn" );
pptr = hostent_content->h_addr_list;
ipp.s_addr = *(uint32_t *) (*pptr);
err = sntp_get_time( &ipp, &current_time );
}
if ( err == kNoErr )
{
mico_utc_time_ms_t utc_time_ms = (uint64_t) current_time.seconds * (uint64_t) 1000
+ (current_time.microseconds / 1000);
mico_time_set_utc_time_ms( &utc_time_ms );
// mico_utc_time_t utc_time = utc_time_ms / 1000 + 28800; //+8:00
// struct tm * currentTime = localtime( (const time_t *) &utc_time );
// rtc_time.sec = currentTime->tm_sec;
// rtc_time.min = currentTime->tm_min;
// rtc_time.hr = currentTime->tm_hour;
// rtc_time.date = currentTime->tm_mday;
// rtc_time.weekday = currentTime->tm_wday;
// rtc_time.month = currentTime->tm_mon + 1;
// rtc_time.year = (currentTime->tm_year + 1900) % 100;
// MicoRtcSetTime( &rtc_time );
}
else
{
os_log("sntp_get_time4 err = %d.", err);
return err;
}
return kNoErr;
}
OSStatus user_rtc_init( void )
{
OSStatus err = kNoErr;
// mico_rtc_time_t rtc_time;
// rtc_time.sec = 0;
// rtc_time.min = 0;
// rtc_time.hr = 0;
//
// rtc_time.date = 1;
// rtc_time.weekday = 1;
// rtc_time.month = 1;
// rtc_time.year = 1;
//
// MicoRtcSetTime( &rtc_time );
// /* create mqtt msg send queue */
// err = mico_rtos_init_queue( &mqtt_msg_send_queue, "mqtt_msg_send_queue", sizeof(p_mqtt_send_msg_t),
// MAX_MQTT_SEND_QUEUE_SIZE );
// require_noerr_action( err, exit, app_log("ERROR: create mqtt msg send queue err=%d.", err) );
/* start rtc client */
err = mico_rtos_create_thread( NULL, MICO_APPLICATION_PRIORITY, "rtc",
(mico_thread_function_t) rtc_thread,
0x1000, 0 );
require_noerr_string( err, exit, "ERROR: Unable to start the rtc thread." );
if ( kNoErr != err ) os_log("ERROR, app thread exit err: %d", err);
exit:
return err;
}
void rtc_thread( mico_thread_arg_t arg )
{
int i, j;
char task_flag[PLUG_NUM] = { -1, -1, -1, -1, -1, -1 }; //<2F><>¼ÿ<C2BC><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸ<EFBFBD><C4B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
OSStatus err = kUnknownErr;
LinkStatusTypeDef LinkStatus;
mico_rtc_time_t rtc_time;
uint32_t power_last = 0xffffffff;
mico_utc_time_t utc_time;
while ( 1 )
{ //<2F>ϵ<EFBFBD><CFB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>wifi<66>ſ<EFBFBD>ʼ<EFBFBD><CABC>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>
micoWlanGetLinkStatus( &LinkStatus );
if ( LinkStatus.is_connected == 1 )
{
err = user_sntp_get_time( );
if ( err == kNoErr )
{
rtc_init = 1;
break;
}
}
mico_rtos_thread_sleep( 3 );
}
while ( 1 )
{
mico_time_get_utc_time( &utc_time );
utc_time += 28800;
struct tm * currentTime = localtime( (const time_t *) &utc_time );
rtc_time.sec = currentTime->tm_sec;
rtc_time.min = currentTime->tm_min;
rtc_time.hr = currentTime->tm_hour;
rtc_time.date = currentTime->tm_mday;
rtc_time.weekday = currentTime->tm_wday;
rtc_time.month = currentTime->tm_mon + 1;
rtc_time.year = (currentTime->tm_year + 1900) % 100;
// MicoRtcSetTime( &rtc_time ); //MicoRtc<74><63><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>ʱ!
if ( rtc_time.sec == 0 )
os_log("time:20%02d/%02d/%02d %d %02d:%02d:%02d",rtc_time.year,rtc_time.month,rtc_time.date,rtc_time.weekday,rtc_time.hr,rtc_time.min,rtc_time.sec);
char update_user_config_flag = 0;
for ( i = 0; i < PLUG_NUM; i++ )
{
for ( j = 0; j < PLUG_TIME_TASK_NUM; j++ )
{
if ( user_config->plug[i].task[j].on != 0 )
{
uint8_t repeat = user_config->plug[i].task[j].repeat;
if ( //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1>̵<EFBFBD><CCB5><EFBFBD>״̬: <20><>Ϊ0 ʱ<>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>趨ֵ, <20>ظ<EFBFBD><D8B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>趨ֵ
rtc_time.sec == 0 && rtc_time.min == user_config->plug[i].task[j].minute
&& rtc_time.hr == user_config->plug[i].task[j].hour
&& ((repeat == 0x00) || repeat & (1 << (rtc_time.weekday - 1)))
)
{
if ( user_config->plug[i].on != user_config->plug[i].task[j].action )
{
user_relay_set( i, user_config->plug[i].task[j].action );
update_user_config_flag = 1;
user_mqtt_send_plug_state( i );
}
if ( repeat == 0x00 )
{
task_flag[i] = j;
user_config->plug[i].task[j].on = 0;
update_user_config_flag = 1;
}
}
}
}
}
//<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 )
{
os_log("update_user_config_flag");
mico_system_context_update( sys_config );
update_user_config_flag = 0;
cJSON *json_send = cJSON_CreateObject( );
cJSON_AddStringToObject( json_send, "mac", strMac );
for ( i = 0; i < PLUG_NUM; i++ )
{
char strTemp1[] = "plug_X";
strTemp1[5] = i + '0';
cJSON *json_send_plug = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug, "on", user_config->plug[i].on );
if ( task_flag[i] >= 0 )
{
cJSON *json_send_plug_setting = cJSON_CreateObject( );
j = task_flag[i];
char strTemp2[] = "task_X";
strTemp2[5] = j + '0';
cJSON *json_send_plug_task = cJSON_CreateObject( );
cJSON_AddNumberToObject( json_send_plug_task, "hour", user_config->plug[i].task[j].hour );
cJSON_AddNumberToObject( json_send_plug_task, "minute", user_config->plug[i].task[j].minute );
cJSON_AddNumberToObject( json_send_plug_task, "repeat", user_config->plug[i].task[j].repeat );
cJSON_AddNumberToObject( json_send_plug_task, "action", user_config->plug[i].task[j].action );
cJSON_AddNumberToObject( json_send_plug_task, "on", user_config->plug[i].task[j].on );
cJSON_AddItemToObject( json_send_plug_setting, strTemp2, json_send_plug_task );
cJSON_AddItemToObject( json_send_plug, "setting", json_send_plug_setting );
task_flag[i] = -1;
}
cJSON_AddItemToObject( json_send, strTemp1, json_send_plug );
}
char *json_str = cJSON_Print( json_send );
user_send( false, json_str ); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
free( json_str );
cJSON_Delete( json_send );
// os_log("cJSON_Delete");
}
//SNTP<54><50><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB>
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) )
{
micoWlanGetLinkStatus( &LinkStatus );
if ( LinkStatus.is_connected == 1 )
{
err = user_sntp_get_time( );
if ( err == kNoErr )
rtc_init = 1;
else
rtc_init = 2;
}
}
//<2F><><EFBFBD>͹<EFBFBD><CDB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if ( power_last != power )
{
power_last = power;
uint8_t *power_buf = NULL;
power_buf = malloc( 128 ); //
if ( power_buf != NULL )
{
sprintf( power_buf, "{\"mac\":\"%s\",\"power\":\"%d.%d\"}", strMac, power/10,power%10 );
user_send( 0, power_buf );
free( power_buf );
}
user_mqtt_hass_power();
}
mico_rtos_thread_msleep( 900 );
}
// exit:
os_log("EXIT: rtc exit with err = %d.", err);
mico_rtos_delete_thread( NULL );
}