123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Session;
- use think\Db;
- use think\Request;
- class Index extends Controller
- {
- private $host="https://device.api.ct10649.com:8743/";
- private $appId="wvdOfp7JVe0ULwnFKwBF0edfbcAa";
- private $secret="Bihoe_rY9vebXMw6sFfpQy0jiRca";
- /* //鉴权接口
- public function Auth()
- {
-
- $data="appId=wvdOfp7JVe0ULwnFKwBF0edfbcAa&secret=Bihoe_rY9vebXMw6sFfpQy0jiRca";
- $header=[
- "Content-Type:application/x-www-form-urlencoded",
- ];
- // https://server:port/iocm/app/sec/v1.1.0/log
- $res=curl("https://device.api.ct10649.com:8743/iocm/app/sec/v1.1.0/login",$data,$header);
- $res=json_decode($res,true);
- $res["token_time_out"]=$res['expiresIn']+time();
- $res["refresh_token_time_out"]=time()+86400*7;
- session('token',$res);
-
-
- return json_encode($res);
- }
- //刷新token并返回新的token
- public function refreshtoken(){
- $token=session("token");
- //查看刷新token是否过期,过期了调鉴权接口,没过期调刷新
- if($token["refresh_token_time_out"]>=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);
-
- }
- }
|