支持节假日,支持经纬度,支持调休时控器

1.时控器为什么要支持经纬度?

   我国地大物博,每个地区天亮天黑时间不一样,东部沿海与中部地区相差近1个小时.新疆地区更多,北京时间晚上23点还没黑.因此PHP 函数date_sunrise()可以直接根据经纬度输出天黑时间.

http://www.wmdfw.com/jiejiari/jiejiari.php
返回JSON数据:
zhouein({"code":"200","msg":"success","newslist":[{"city":"HangZhou","cnweekday":"星期四","correctsunsettime":"17:31","date":"2020-11-12","daycode":0,"end":"","enname":"","holiday":"","info":"工作日","isnotwork":0,"latitude":"30.26667","longitude":"120.20000","lunarday":"廿七","lunarmonth":"九月","lunaryear":"庚子","name":"","remarks":"不开灯","rest":"","start":"","sunrisetime":"06:25","sunsettime":"17:01","tip":"","vacation":"","weekday":4}]})
View Code

2.目前我们法定节假日是根据国务院办公厅下发通知,春节,元旦,端午等休息,又考虑到五一放个小长假,将前后某几天周末调休.每年都不一样,程序上很难实现统一的标准.因此写了一个法定节假日的列表.

//法定节假日 daycode=1 
    $lst_holiday = [
        '2020-1-1' => '元旦',
        '2020-1-24' => '春节',
        '2020-1-25' => '春节假',
        '2020-1-26' => '春节假',
        '2020-1-27' => '春节假',
        '2020-1-28' => '春节假',
        '2020-1-29' => '春节假',
        '2020-1-30' => '春节假',
        '2020-4-4' => '清明节',
        '2020-4-5' => '清明假',
        '2020-4-6' => '清明假',
        '2020-5-1' => '劳动节',
        '2020-5-2' => '劳动假',
        '2020-5-3' => '劳动假',
        '2020-5-4' => '劳动假',
        '2020-5-5' => '劳动假',
        '2020-6-25' => '端午节',
        '2020-6-26' => '端午假',
        '2020-6-27' => '端午假',
        '2020-10-1' => '国庆',
        '2020-10-2' => '国庆假',
        '2020-10-3' => '国庆假',
        '2020-10-4' => '国庆假',
        '2020-10-5' => '国庆假',
        '2020-10-6' => '国庆假',
        '2020-10-7' => '国庆假',
        '2020-10-8' => '国庆假',
    ];
 
 
    // 调休日 (传统节日前后补班)daycode=3
    $lst_working_day = [
        '2020-1-19' => '春节调休',
        '2020-2-1' => '春节调休',
        '2020-4-26' => '劳动节调休',
        '2020-5-9' => '劳动节调休',
        '2020-6-28' => '端午调休',
        '2020-9-27' => '国庆调休',
        '2020-10-10' => '国庆调休',
    ];
3.剩下就是诸如周末休假具有规律的查询,PHP 语言也好,其他的语言都很轻松.
    $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    $cnweekday = $weekarray[date("w",strtotime($date))];

    if((date('w',strtotime($date))==6) || (date('w',strtotime($date)) == 0)){
        if (key_exists($date, $lst_working_day)) {
            $daycode=3;
            $info = "调休日";
            //echo "{'daycode':".$daycode."}";
            //echo "调休日";
         }

 

4.上全代码:
<?php
//header("Content-Type: text/html; charset=utf-8");
header('Content-type: application/json');  
if(isset($_REQUEST["callback"])){
   $callback = $_REQUEST["callback"];
   }else{
   $callback = "zhouein";
   }
if(isset($_REQUEST["date"])){
   $date = $_REQUEST["date"];
   }else{
   $date = date("Y-m-d");
   }
if(isset($_REQUEST["correcttime"])){
   $correcttime = $_REQUEST["correcttime"];
   }else{
   $correcttime = 30;  //修正时间,正为延迟,负为提前,单位分钟
   }
if(isset($_REQUEST["latitude"])){
   $latitude = $_REQUEST["latitude"];
   }else{
   $latitude = '30.26667';
   }
if(isset($_REQUEST["longitude"])){
   $longitude = $_REQUEST["longitude"];
   }else{
   $longitude = '120.20000';
   }
if(isset($_REQUEST["tianding"])){
   $tianding = $_REQUEST["tianding"];
   }else{
   $tianding = 90;
   }
if(isset($_REQUEST["shiqu"])){
   $shiqu = $_REQUEST["shiqu"];
   }else{
   $shiqu = 8;
   }
    //计算日落日出时间
    $sunrise = date_sunrise(time(),SUNFUNCS_RET_STRING,$latitude,$longitude,$tianding,$shiqu);
    $sunset=date_sunset(time(),SUNFUNCS_RET_STRING,$latitude,$longitude,$tianding,$shiqu);
    $sunsetcorrect = date("H:i", strtotime("$sunset +$correcttime min"));
function is_weekend($date,$callback){
//法定节假日 daycode=1 
    $lst_holiday = [
        '2020-1-1' => '元旦',
        '2020-1-24' => '春节',
        '2020-1-25' => '春节假',
        '2020-1-26' => '春节假',
        '2020-1-27' => '春节假',
        '2020-1-28' => '春节假',
        '2020-1-29' => '春节假',
        '2020-1-30' => '春节假',
        '2020-4-4' => '清明节',
        '2020-4-5' => '清明假',
        '2020-4-6' => '清明假',
        '2020-5-1' => '劳动节',
        '2020-5-2' => '劳动假',
        '2020-5-3' => '劳动假',
        '2020-5-4' => '劳动假',
        '2020-5-5' => '劳动假',
        '2020-6-25' => '端午节',
        '2020-6-26' => '端午假',
        '2020-6-27' => '端午假',
        '2020-10-1' => '国庆',
        '2020-10-2' => '国庆假',
        '2020-10-3' => '国庆假',
        '2020-10-4' => '国庆假',
        '2020-10-5' => '国庆假',
        '2020-10-6' => '国庆假',
        '2020-10-7' => '国庆假',
        '2020-10-8' => '国庆假',
    ];
 
 
    // 调休日 (传统节日前后补班)daycode=3
    $lst_working_day = [
        '2020-1-19' => '春节调休',
        '2020-2-1' => '春节调休',
        '2020-4-26' => '劳动节调休',
        '2020-5-9' => '劳动节调休',
        '2020-6-28' => '端午调休',
        '2020-9-27' => '国庆调休',
        '2020-10-10' => '国庆调休',
    ];
    $weekarray=array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
    $cnweekday = $weekarray[date("w",strtotime($date))];

    if((date('w',strtotime($date))==6) || (date('w',strtotime($date)) == 0)){
        if (key_exists($date, $lst_working_day)) {
            $daycode=3;
            $info = "调休日";
            //echo "{'daycode':".$daycode."}";
            //echo "调休日";
         }
        elseif (key_exists($date, $lst_holiday)) {
            $daycode=1;
            $info = "法定节假日";
            //echo "{'daycode':".$daycode."}";
            //echo "法定节假日";
         }else{
            $daycode=2;
            $info = "双休日";
            //echo '双休日'.date('w',strtotime($date)).'\$daycode=2'; 
         }

      }else{
          //echo '工作日'.date('w',strtotime($date));
          $daycode=0;
          $info = "工作日";
          //echo "{'daycode':".$daycode."}";
      }
      //print_r($daycode);
      $arry= array(
        'code' => '200',
        'msg' => 'success',
        'newslist' => array(
            '0'=>array(
                'city' => 'HangZhou',
                'cnweekday' => $cnweekday,
                'correctsunsettime' => '17:37',
                'date' => '2020-10-11',
                'daycode'=>$daycode,
                'end' => '',
                'enname' => '',
                'holiday' => '',
                'info' => $info,
                'isnotwork' => 1,
                'latitude' => '30.26667',
                'longitude' => '120.20000',
                'lunarday' => '廿五',
                'lunarmonth' => '八月',
                'lunaryear' => '庚子',
                'name' => '',
                'remarks' => '开灯',
                'start' => '',
                'sunrisetime' => '06:18',
                'sunsettime' => '17:07',
                'tip' => '',
                'vacation' => '',
                'weekday' => 0
           )
        ),
              
      );
      $json_date=json_encode($arry,JSON_UNESCAPED_UNICODE);//转换为json数据
      //print_r($arry);
      echo $callback."(".$json_date.")";
      
   }
is_weekend($date,$callback);
?>
View Code

  5.查询别的平台,再显示的数据的代码:

<?php
//header("Content-Type: text/html; charset=utf-8");
header('Content-type: application/json');          
if(isset($_REQUEST["callback"])){
   $callback = $_REQUEST["callback"];
   }else{
   $callback = 'zhouein';
   }
if(isset($_REQUEST["date"])){
   $date = $_REQUEST["date"];
   }else{
   $date = date("Y-m-d");
   }
if(isset($_REQUEST["correcttime"])){
   $correcttime = $_REQUEST["correcttime"];
   }else{
   $correcttime = 30;  //修正时间,正为延迟,负为提前,单位分钟
   }
if(isset($_REQUEST["latitude"])){
   $latitude = $_REQUEST["latitude"];
   }else{
   $latitude = '30.26667';
   }
if(isset($_REQUEST["longitude"])){
   $longitude = $_REQUEST["longitude"];
   }else{
   $longitude = '120.20000';
   }
if(isset($_REQUEST["tianding"])){
   $tianding = $_REQUEST["tianding"];
   }else{
   $tianding = 90;
   }
if(isset($_REQUEST["shiqu"])){
   $shiqu = $_REQUEST["shiqu"];
   }else{
   $shiqu = 8;
   }
    //计算日落日出时间
    $sunrise = date_sunrise(time(),SUNFUNCS_RET_STRING,$latitude,$longitude,$tianding,$shiqu);
    $sunset=date_sunset(time(),SUNFUNCS_RET_STRING,$latitude,$longitude,$tianding,$shiqu);
    $sunsetcorrect = date("H:i", strtotime("$sunset +$correcttime min"));
    //查询国家法定节假日
    $date = file_get_contents ('http://api.tianapi.com/txapi/jiejiari/index?key=KEY自己申请&date='.$date );//API接口
    $json = json_decode($date,true);//将json解析成数组
    $tip=$json['newslist']['0']['tip'];

function zhouein($latitude,$longitude,$sunrise,$sunset,$sunsetcorrect,$tip,$json,$remarks,$callback){
      $number = array(
            'city'              => 'HangZhou',
            'latitude'          => $latitude,
            'longitude'         => $longitude,
            'sunrisetime'       => $sunrise,
            'sunsettime'        => $sunset,
            'correctsunsettime' => $sunsetcorrect,
            'remarks'               => $remarks
            
            );
    $hebing = array_merge_recursive($json['newslist']['0'], $number);
    ksort($hebing);
    $arryA= array(
        'code' => '200',
        'msg' => 'success',
        'newslist' => Array
              (
              '0' =>$hebing
              )
    );

    $json_date=json_encode($arryA,JSON_UNESCAPED_UNICODE);//转换为json数据
    //print_r($arryA);
    echo $callback ."(". $json_date.")";
}

  if($json['code'] == 200){ //判断状态码
      if($json['newslist']['0']['daycode'] == 0){
        zhouein($latitude,$longitude,$sunrise,$sunset,$sunsetcorrect,$tip,$json,$remarks='不开灯',$callback);
      }
      if($json['newslist']['0']['daycode'] == 1){
        zhouein($latitude,$longitude,$sunrise,$sunset,$sunsetcorrect,$tip,$json,$remarks='开灯',$callback);
      }
      if($json['newslist']['0']['daycode'] == 2){
        zhouein($latitude,$longitude,$sunrise,$sunset,$sunsetcorrect,$tip,$json,$remarks='开灯',$callback);
      }
      if($json['newslist']['0']['daycode'] == 3){
        zhouein($latitude,$longitude,$sunrise,$sunset,$sunsetcorrect,$tip,$json,$remarks='不开灯',$callback);
      }

    }else{    
        echo "返回错误,状态消息:".$json['msg'];
      }


?>

 

  6.因此,我们想在休息日(周末,以及法定节假日,排除调休)在天黑前打开路灯,我们是不是不难做到了?  再举例来说,工作日(周一到周五,以及调班,排除法定节假日).
7.接下来,我们需要去找一个智能开关,目前市场上的云平台只支持简单的周一到周日,几点到几点,很显然是不支持法定佳节日和经纬度天黑.我们将解析到的JSON数据,定时同步到云平台即可.
8.根据有人物联网云平台提供的API,采用 PHP 中的函数CURL 去模拟登录,提交数据.见代码:
<?php
//公共方法
/**
 * 模拟post进行url请求
 * @param $postUrl
 * @param $curlPost
 * @return string
 */
function http_json_data($postUrl, $curlPost, $token) {
    if (empty($postUrl) || empty($curlPost)) {
        return false;
    }
    $ch = curl_init();//初始化curl
    $header = array();
    $header[] = 'Accept:application/json';
    $header[] = 'Content-Type:application/json';
    //兼容新版api接口,token在header中请求
    $header[] = 'token:'.$token;
    curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
    curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//验证对方的SSL证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//检查声称服务器的证书的身份
    $data = curl_exec($ch);//运行curl
    curl_close($ch);
    return $data;
    
}
function json_to_array($data) {    //得到数组,JSON转数组
      $data = json_decode($data,JSON_UNESCAPED_UNICODE);
      return $data;
   }
function array_to_json($data) {    //得到JSON,数组转JSON
      $data = json_encode($data,JSON_UNESCAPED_UNICODE);
      return $data;
   }

   //登陆,想得到token
   $curlPost='{"account":"ci78dny8","password":"密码为MD5后的小写","platformId":"W3rfV"}';
   /* $curlPost=array{
       "account"=>"ci78dny8",
       "password"=>"密码为MD5后的小写",
       "platformId"=>"W3rfV"
       }; */
   $getToken = http_json_data($postUrl='https://openapi.mp.usr.cn/usrCloud/user/login', $curlPost,$token='');
   $usrInfo = json_decode($getToken,JSON_UNESCAPED_UNICODE);
   //$usrInfo = json_to_array($data);
   //echo $getToken;
   $token = $usrInfo['data']['token'];
   echo "//得到Token";
   echo "<BR>";
   echo $token;
   echo "<BR>";
   //得到定时器列表
   //$curlPost = '{"uid":'.$usrInfo['data']['uid'].'}';
   /* $curlPost = array{
         "uid"=>$usrInfo['data']['uid']
   } */
   //$getTask=http_json_data($postUrl='https://api.mp.usr.cn/usrCloud/timing/getTimingTasksByUid', $curlPost, $token);
   //echo $getTask;

   //修改定时器
   //$curlPost = '{"id":1330,"name":"\u672a\u547d\u540d_\u5b9a\u65f6\u4efb\u52a1_78","projectId":33222,"cronType":1,"recordType":1,"deviceNo":"01001219121100008587","taskTargets":[{"slaveIndex":"1","dataId":587555,"sendData":"0","cronWeek":"7,1","cronHour":"18","cronMinute":"44"}]}';

   //$updateTask=http_json_data($postUrl='https://api.mp.usr.cn/usrCloud/timing/updateTimingTask', $curlPost, $token);
   //echo $updateTask;
   //增加定时器
   $curlPost = '{"name":"\u672a\u547d\u540d_\u5b9a\u65f6\u4efb\u52a1_12","projectId":33222,"cronType":1,"recordType":1,"deviceNo":"01001219121100008587","taskTargets":[{"slaveIndex":"1","dataId":587555,"sendData":"0","cronWeek":"2,1,7","cronHour":22,"cronMinute":4,"startDt":1605141168000,"endDt":4102415940000,"taskType":1}]}';

   $updateTask=http_json_data($postUrl='https://api.mp.usr.cn/usrCloud/timing/addTimingTask', $curlPost, $token);
   echo "<BR>";
   echo "//反馈增加定时器结果";
   echo $updateTask;
View Code

 

 

posted @ 2020-11-12 13:27  钢锅  阅读(157)  评论(0编辑  收藏  举报