Files
zTC1/TC1/user_rtc.c
Zip 1608a20279 add:增加user_config版本,不同时初始化user_config
add:定时开关功能(未测试)
2019-01-29 17:08:20 +08:00

202 lines
6.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"
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;
}
}
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 mqtt 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;
OSStatus err = kUnknownErr;
LinkStatusTypeDef LinkStatus;
mico_rtc_time_t rtc_time;
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>ʱ!
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 == 0x80) || repeat & (1 << (rtc_time.weekday - 1)))
)
{
user_relay_set( i, user_config->plug[i].task[j].action );
if ( repeat == 0x80 )
{
user_config->plug[i].task[j].on = 0;
update_user_config_flag = 1;
}
}
}
}
}
if ( update_user_config_flag == 1 )
{
mico_system_context_update( sys_config ); //<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
update_user_config_flag = 0;
}
if ( rtc_init != 1 || (rtc_time.sec == 0 && rtc_time.min == 0) ) //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿСʱУ׼һ<D7BC><D2BB>
{
micoWlanGetLinkStatus( &LinkStatus );
if ( LinkStatus.is_connected == 1 )
{
err = user_sntp_get_time( );
if ( err == kNoErr )
rtc_init = 1;
else
rtc_init = 2;
}
}
mico_rtos_thread_msleep( 900 );
}
exit:
os_log("EXIT: rtc exit with err = %d.", err);
mico_rtos_delete_thread( NULL );
}