Skip to content

Commit

Permalink
定时任务增加智能跳过节假日
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkaimr committed Mar 15, 2022
1 parent 6c9387f commit 456fd5b
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ SmartConfig是安信可提供的一种快捷配网方式,将esp8266设置为

- “关联延时”:该定时任务执行完成后会自动启动关联的延时任务。

- “重复”:若周一到周日都没选择则该任务只执行一次。
- “重复”:若周一到周日都没选择则该任务只执行一次。(新加功能:智能跳过节假日,从互联网获取节假日信息只在法定节假日生效。)

![](media/6a8322aed50869bf983582514a592960.png)

Expand Down Expand Up @@ -423,7 +423,7 @@ SmartConfig是安信可提供的一种快捷配网方式,将esp8266设置为


**响应码:**200:成功,其他:失败
**响应码:** 200:成功,其他:失败

**响应:**

Expand Down
1 change: 1 addition & 0 deletions html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,7 @@ <h4 id="timerModalHead">定时</h4>
<div class="col-sm-7">
<label class="checkbox-inline"><input id="week6" type="checkbox">周六</label>
<label class="checkbox-inline"><input id="week7" type="checkbox">周日</label>
<label class="checkbox-inline"><input id="week8" type="checkbox">跳过节假日</label>
</div>
</div>
</div>
Expand Down
7 changes: 6 additions & 1 deletion html/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,9 @@ function weekConversionString( week ) {
if ( week == 96 ){
return "周末";
}
if ( week == 128 ){
return "跳过节假日";
}

var str="";
for ( var i = 0; i < 7; i++ ){
Expand All @@ -522,6 +525,7 @@ function stringConversionWeek() {
if( $("#week5").is(':checked') ){week=week|(1<<4)}
if( $("#week6").is(':checked') ){week=week|(1<<5)}
if( $("#week7").is(':checked') ){week=week|(1<<6)}
if( $("#week8").is(':checked') ){week=week|(1<<7)}
return week;
}

Expand Down Expand Up @@ -1014,7 +1018,7 @@ function tabTimerSubmit(){
$("#timerCascodeNum").val(data[0].CascodeNum);
$("#timerCascodeEnable").attr('checked',data[0].Cascode);
var week= parseInt(data[0].Week);
for ( var i = 0; i < 7; i++ ){
for ( var i = 0; i < 8; i++ ){
switch(i){
case 0: $("#week1").prop('checked',week&(1<<i));break;
case 1: $("#week2").prop('checked',week&(1<<i));break;
Expand All @@ -1023,6 +1027,7 @@ function tabTimerSubmit(){
case 4: $("#week5").prop('checked',week&(1<<i));break;
case 5: $("#week6").prop('checked',week&(1<<i));break;
case 6: $("#week7").prop('checked',week&(1<<i));break;
case 7: $("#week8").prop('checked',week&(1<<i));break;
}
}
}else{
Expand Down
Binary file modified html/release/index.html.gz
Binary file not shown.
Binary file modified html/release/index.js.gz
Binary file not shown.
11 changes: 11 additions & 0 deletions plug2.0/app/include/user_plug.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,20 @@ typedef enum
REPET_SAT = 0x20, /* 每周六 */
REPET_SUN = 0x40, /* 每周日 */
REPET_ALL = 0x7F, /* 每天 */
REPET_AUTO = 0x80, /* 智能跳过节假日 */

REPET_BUFF
}PLUG_REPETITION_E;

typedef enum
{
DAY_Weekday = 0, /* 工作日 */
DAY_Weekends, /* 周末 */
DAY_Festivals, /* 节日 */
DAY_Compensatory, /* 调休 */

DAY_BUFF
}PLUG_DAY_E;

typedef struct tagPLUG_TIMER /* 定时模块 */
{
Expand Down Expand Up @@ -318,6 +328,7 @@ UINT PLUG_ParseDate( CHAR* pDateStr);
UINT PLUG_ParseTimerData( CHAR* pData );
UINT PLUG_ParseRelayStatus( CHAR* pDataStr);
UINT PLUG_ParseWebSetData( CHAR* pData );
UINT8 PLUG_Isholiday();

VOID PLUG_StartJudgeTimeHanderTimer( VOID );

Expand Down
2 changes: 1 addition & 1 deletion plug2.0/app/include/user_web.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define WEB_SENDBUF_SIZE (1024) //发送缓冲区大小


#define WEB_MAX_FD 2 //最大可接受连接数并发(根据空闲栈大小来调整,不建议将该值调大,除非你有更好的优化方案来节省内存)
#define WEB_MAX_FD 3 //最大可接受连接数并发(根据空闲栈大小来调整,不建议将该值调大,除非你有更好的优化方案来节省内存)
#define WEB_CONTINUE_TMOUT 5 //连接超时时间,超过该时间未进行数据交互将主动断开连接

VOID WEB_StartWebServerTheard( VOID );
Expand Down
165 changes: 163 additions & 2 deletions plug2.0/app/user/user_plug.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ static VOID PLUG_TimerCascade( PLUG_TIMER_S *pstTimer );
static VOID PLUG_JudgeDelay( VOID );
VOID PLUG_GetNextDelayTime( PLUG_DELAY_S *pstDelay );
VOID PLUG_StartDelayTime( PLUG_DELAY_S *pstDelay );
UINT PLUG_ParseHolidayToday( VOID* pData );

PLUG_TIMER_S g_astPLUG_Timer[PLUG_TIMER_MAX];
PLUG_DELAY_S g_astPLUG_Delay[PLUG_DELAY_MAX];
Expand Down Expand Up @@ -2310,6 +2311,7 @@ static VOID PLUG_JudgeTimer( VOID )
UINT8 ucWeek = 0;
PLUG_DATE_S stDate = {0, 0, 0, 0, 0, 0, 0};
PLUG_TIMER_S *pstTimer = NULL;
UINT8 ucHoliday = 0;

static BOOL bIsJudged = FALSE;
static PLUG_DATE_S stDateLast = {0, 0, 0, 0, 0, 0, 0};
Expand Down Expand Up @@ -2339,7 +2341,23 @@ static VOID PLUG_JudgeTimer( VOID )
FALSE == bIsJudged)
{
/* 判断重复 */
if ( pstTimer->eWeek & (1 << (ucWeek-1)) )

// 智能跳过节假日
if ( pstTimer->eWeek == REPET_AUTO )
{
ucHoliday = PLUG_Isholiday();
if ( ucHoliday == DAY_Weekends ||
ucHoliday == DAY_Festivals ||
ucHoliday == DAY_BUFF )
{
// 周末或假日跳过
continue;
}

PLUG_SetRelayOn( TRUE );
bIsJudged = TRUE;
}
else if ( pstTimer->eWeek & (1 << (ucWeek-1)) )
{
PLUG_SetRelayOn( TRUE );
bIsJudged = TRUE;
Expand All @@ -2362,7 +2380,22 @@ static VOID PLUG_JudgeTimer( VOID )
pstTimer->stOffTime.iMinute == stDate.iMinute &&
FALSE == bIsJudged)
{
if ( pstTimer->eWeek & (1 << (ucWeek-1)) )
if ( pstTimer->eWeek == REPET_AUTO )
{
// 智能跳过节假日
ucHoliday = PLUG_Isholiday();
if ( ucHoliday == DAY_Weekends ||
ucHoliday == DAY_Festivals ||
ucHoliday == DAY_BUFF )
{
// 周末或假日跳过
continue;
}

PLUG_SetRelayOff( TRUE );
bIsJudged = TRUE;
}
else if ( pstTimer->eWeek & (1 << (ucWeek-1)) )
{
PLUG_SetRelayOff( TRUE );
bIsJudged = TRUE;
Expand Down Expand Up @@ -2650,4 +2683,132 @@ VOID PLUG_StartJudgeTimeHanderTimer( VOID )
}
}

typedef struct tagParseHolidayPara{
HTTP_CLIENT_S* pstCli;
UINT8 uiHoliday;
}MQTT_ParseHolidayPara_S;


UINT8 PLUG_Isholiday()
{
HTTP_CLIENT_S* pstCli = NULL;
CHAR szBuf[120] = {0};
UINT uiRet = 0;
UINT8 uiRetry = 0;
PLUG_DATE_S stDate;
MQTT_ParseHolidayPara_S stHolidayPara;

if ( WIFI_GetWifiConnStatus() != STATION_GOT_IP )
{
LOG_OUT(LOGOUT_ERROR, "not connect wifi");
return DAY_BUFF;
}

retry:
if ( uiRetry >= 3 )
{
LOG_OUT(LOGOUT_ERROR, "HTTP_ClientDoRequest timeout, retry exceeds maximum");
goto end;
}

PLUG_GetDate( &stDate );
sprintf(szBuf, "/api/holiday/info/%d-%02d-%02d", stDate.iYear, stDate.iMonth, stDate.iDay);
pstCli = HTTP_NewClient("GET", "timor.tech", szBuf, "", 0);
if ( pstCli == NULL )
{
LOG_OUT(LOGOUT_ERROR, "HTTP_NewClient failed");
goto end;
}

uiRet = HTTP_ClientDoRequest(pstCli);
if ( uiRet != 0 )
{
LOG_OUT(LOGOUT_ERROR, "HTTP_ClientDoRequest failed");
goto end;
}

stHolidayPara.pstCli = pstCli;
stHolidayPara.uiHoliday = DAY_BUFF;
uiRet = HTTP_ClientDoResponse(pstCli, PLUG_ParseHolidayToday, (VOID*)&stHolidayPara);
if ( uiRet == -1 )
{
LOG_OUT(LOGOUT_ERROR, "HTTP_ClientDoRequest failed");
goto end;
}
// 接收超时
else if ( uiRet == -2 )
{
LOG_OUT(LOGOUT_ERROR, "HTTP_ClientDoRequest timeout, retry...");
HTTP_DestoryClient(pstCli);
goto retry;
}

end:
HTTP_DestoryClient(pstCli);
return stHolidayPara.uiHoliday;
}


UINT PLUG_ParseHolidayToday( VOID* pData )
{
cJSON *pJsonRoot = NULL;
cJSON *pJsonIteam = NULL;
MQTT_ParseHolidayPara_S *stHolidayPara = pData;
HTTP_CLIENT_S *pstCli = NULL;
UINT uiRet = OK;

if ( stHolidayPara == NULL )
{
LOG_OUT(LOGOUT_ERROR, "stHolidayPara is NULL");
return FAIL;
}

pstCli = stHolidayPara->pstCli;
if ( pstCli == NULL )
{
LOG_OUT(LOGOUT_ERROR, "pstCli:%p", pstCli);
return FAIL;
}

if (pstCli->stReson.eHttpCode != HTTP_CODE_Ok )
{
LOG_OUT(LOGOUT_ERROR, "get response code: %s", szHttpCodeMap[pstCli->stReson.eHttpCode]);
return FAIL;
}

pJsonRoot = cJSON_Parse( pstCli->stReson.pcBody );
if ( pJsonRoot == NULL )
{
LOG_OUT(LOGOUT_ERROR, "cJSON_Parse failed, pcBody:%s", pstCli->stReson.pcBody);
uiRet = FAIL;
goto exit;
}

pJsonIteam = cJSON_GetObjectItem(pJsonRoot, "code");
if (pJsonIteam && pJsonIteam->type == cJSON_Number && pJsonIteam->valueint != 0 )
{
LOG_OUT(LOGOUT_ERROR, "expect 0 but got %d", pJsonIteam->valueint);
uiRet = FAIL;
goto exit;
}

pJsonIteam = cJSON_GetObjectItem(pJsonRoot, "type");
if (pJsonIteam && pJsonIteam->type == cJSON_Object )
{
pJsonIteam = cJSON_GetObjectItem(pJsonIteam, "type");
if (pJsonIteam && pJsonIteam->type == cJSON_Number )
{
stHolidayPara->uiHoliday = pJsonIteam->valueint;
uiRet = OK;
}
}

LOG_OUT(LOGOUT_INFO, "holiday type %d", stHolidayPara->uiHoliday);

exit:
cJSON_Delete(pJsonRoot);
return uiRet;
}



25 changes: 2 additions & 23 deletions plug2.0/app/user/user_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -904,29 +904,8 @@ UINT WIFI_DeviceInfoMarshalJson( CHAR* pcBuf, UINT uiBufLen)
return strlen(pcBuf);
}

UINT WIFI_TemperatureMarshalJson( CHAR* pcBuf, UINT uiBufLen)
UINT WIFI_GetWifiConnStatus()
{
cJSON *pJson = NULL;
CHAR *pJsonStr = NULL;
CHAR szBuf[20];

pJson = cJSON_CreateObject();

cJSON_AddNumberToObject( pJson, "Temperature", PLUG_GetRunTime());

pJsonStr = cJSON_PrintUnformatted(pJson);
if ( pJsonStr != NULL )
{
strncpy(pcBuf, pJsonStr, uiBufLen);
}
else
{
snprintf(pcBuf, uiBufLen, "{}");
}

cJSON_Delete(pJson);
FREE_MEM(pJsonStr);

return strlen(pcBuf);
return uiCurStatus;
}

0 comments on commit 456fd5b

Please sign in to comment.