=time()){//刷新 $data=[ "appId"=>$this->appId, "secret"=>$this->secret, "refreshToken"=>$token["refreshToken"], ]; $header=[ "Content-Type:application/json", ]; $res=curl($this->host."iocm/app/sec/v1.1.0/refreshToken",json_encode($data),$header); $res=json_decode($res,true); $res["token_time_out"]=time()+$res['expiresIn']; $res["refresh_token_time_out"]=time()+86400*7; session('token',$res);// return json_encode($res); }else{//鉴权 return $this->Auth(); } } //获取token public function gettoken(){ //判断是否设置session以及session里的token是否过期 if(session('?token')){ $token=session('token'); if($token['token_time_out']>=time()){//还没有过期直接返回token; return json_encode($token); }else{ return $this->refreshtoken(); } }else{ return $token=$this->Auth(); } }*/ public function index() { $data_list =Db::table('device')->paginate(); $pages = $data_list->render(); $this->assign('data_list', $data_list); $this->assign('pages', $pages); return $this->fetch(); } public function add() { $res=Db::name('device')->where('id',input('device_id'))->find(); if(!$res){ if ($this->request->isPost()) { $data=$_POST; //验证数据 if(empty($data['device_id'])||empty($data['name'])){ return $this->error("请填写好必要内容"); } //调用电信接口 $token=json_decode(gettoken(),true); $sendata=[ "verifyCode"=>$data['device_id'],//"868744030067098" "nodeId"=>$data['device_id'], "timeout"=>0 ]; $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials?appId=".$this->appId,json_encode($sendata),$header); $res1=json_decode($res1,true); //添加 $list=array(); $list['device_id']= trim($data['device_id']); $list['name']= $data['name']; $list['iot_id']= $res1['deviceId']; $list['psk']= $res1['psk']; $list['addtime']= date('Y-m-d H:i:s'); $list['updatetime']= date('Y-m-d H:i:s'); //创建新表 /* $device_table="hp_device_".$list['device_id']; $create_sql=" CREATE TABLE `".$device_table."` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `pressure` varchar(255) DEFAULT NULL, `status` tinyint(10) DEFAULT NULL, `addtime` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; "; $table= Db::execute($create_sql); */ $res = Db::name('device')->insertGetId($list); if($res) { return $this->success('添加成功','index'); }else{ return $this->error("添加失败,请稍后再试"); } } }else{ return $this->error("改设备编号已经添加"); } return $this->fetch(); } public function edit() { if ($this->request->isPost()) { $data=$_POST; //验证数据 if(empty($data['deviceType'])||empty($data['manufacturerName'])||empty($data['model'])||empty($data['location'])||empty($data['protocolType'])||empty($data['manufacturerName'])){ return $this->error("请填写好必要内容"); } //调用电信接口 $token=json_decode(gettoken(),true); $sendata=[ "name"=>$data["device_id"], "deviceType"=>$data['deviceType'], "manufacturerName"=>$data['manufacturerName'], "manufacturerId"=>$data['manufacturerId'], "model"=>$data['model'], "protocolType"=>$data['protocolType'], "location"=>trim($data['location']), ]; $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; //https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId} $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id']."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT"); $res1=json_decode($res1,true); //添加 $list=array(); $list['deviceType']= $data['deviceType'];//设备类型 $list['name']= $data['name'];//设备类型 $list['manufacturerName']= $data['manufacturerName'];//厂商名称 $list['model']= $data['model'];//设备型号 $list['isSecure']= $data['isSecure']; $list['manufacturerId']= $data['manufacturerId'];//设备型号 $list['protocolType']= $data['protocolType']; $list['location']= $data['location']; $list['updatetime']= date('Y-m-d H:i:s'); $res = Db::name('device')->where('id',$data['id'])->update($list); if($res) { return $this->success('编辑成功','index'); }else{ return $this->error("编辑失败,请稍后再试"); } } $res=Db::name('device')->where('id',input('id'))->find(); $this->assign('data', $res); return $this->fetch(); } //删除设备 public function delete() { } //查看状态 public function status() { return 111; /*if ($this->request->isPost()) { $data=$_POST; //调用电信接口 $token=json_decode($this->gettoken(),true); $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; //https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId} $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/".$data['iot_id']."?appId=".$this->appId,"",$header,"GET"); return $res1; } */ } public function testedit() { //https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId} //调用电信接口 $token=json_decode(gettoken(),true); dump($token); /* $sendata=[ "name"=>"test1", "deviceType"=>"HXKCNBDTU100", "manufacturerName"=>"SingPie", "model"=>"HXKC100NB", "location"=>"上海青浦", ]; $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/"."8d696965-8a63-4200-bc1f-202791e57883"."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT"); dump($res1);*/ } public function teststatus() { // https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId} //调用电信接口 $token=json_decode($this->gettoken(),true); $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/"."8d696965-8a63-4200-bc1f-202791e57883"."?appId=".$this->appId,"",$header,"GET"); dump($res1); } }