• 有人物联网云平台 API,增加修改定时器


    案例:

    市场上的定时器,只支持简单的按日期,星期几点到几点执行, 几乎没有做到按国家法定节假日来执行指令

    例如国家法定节假日前后周末调休,既是周末,也不是周一到周五.做一个支持法定节假日的数据列表.得到JSON数据.

    云平台定时去查询这个接口返回JSON数据去修改简单的定时器,就做到了支持法定节假日功能的定时器

    在功能上,增加了按经纬度获取日出日落时间,如下.

    <?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);
    ?>
    

      

    有人物联网云平台https://mp.usr.cn 提供了API接口.

    接口见http://cloud.usr.cn/document/117.html

    首先封装一个PHP 函数,登录带Cookies

    <?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;
       }
    

      Curl模拟登录得到token

       //登陆,想得到token
       $curlPost='{"account":"ci78XXXX","password":"密码的MD5加密","platformId":"W3rfV"}';
       /* $curlPost=array{
    	   "account"=>"ci78XXXX",
    	   "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;
    

      接下来根据API  增加一个定时器,获取列表,修改一个定时器.均能实现

       //得到定时器列表
       //$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":"u672au547du540d_u5b9au65f6u4efbu52a1_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":"u672au547du540d_u5b9au65f6u4efbu52a1_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;
    

      以下是完整PHP  源代码

    <?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":"ci78XXXX","password":"密码的MD5加密","platformId":"W3rfV"}';
       /* $curlPost=array{
    	   "account"=>"ci78XXXX",
    	   "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":"u672au547du540d_u5b9au65f6u4efbu52a1_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":"u672au547du540d_u5b9au65f6u4efbu52a1_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;
    

      

  • 相关阅读:
    jmap使用
    Spring boot后台搭建一使用MyBatis集成Mapper和PageHelper
    aspose.slides for java去除水印
    win10 解决Failed installing 'Tomcat8' service
    NoSQL数据库
    windows下Java调用可执行文件
    Neo4j CQL基本使用
    初识Neo4j
    (原)下载pubFig的python代码
    (原+译)win7远程连接ubuntu16.04
  • 原文地址:https://www.cnblogs.com/zhouein/p/14022653.html
Copyright © 2020-2023  润新知