123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Session;
- use think\Db;
- use think\Request;
- use think\Log;
- class Device extends Controller
- {
- private $host="https://device.api.ct10649.com:8743/";
- private $appId="wvdOfp7JVe0ULwnFKwBF0edfbcAa";
- private $secret="Bihoe_rY9vebXMw6sFfpQy0jiRca";
- 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('device_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);
- // dump($token); exit();
- $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"]
- ];
- $rescurl=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials?appId=".$this->appId,json_encode($sendata),$header);
- // $res1=json_decode($res1,true);
- // dump($rescurl);
- if($rescurl[0]==200){//成功
- $res1=json_decode($rescurl[1],true);
- }else{//失败
- // $res2=json_decode($res1[1],true);
- return $this->error($rescurl[1]);
- }
-
- //添加
- $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="dev_".$list['device_id'];
- $create_sql="
- CREATE TABLE `".$device_table."` (
- `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
- `device_id` varchar(200) DEFAULT NULL,
- `iot_id` varchar(255) DEFAULT NULL,
- `service` text,
- `addtime` varchar(50) DEFAULT NULL,
- `notifyType` varchar(50) DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB 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("改设备编号已经添加");
- }
- //所有业主信息
- $owners= Db::name('owner')->select();
- $this->assign('owners', $owners);
- 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}
- $rescurl=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id']."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT");
-
- if($rescurl[0]==204){//成功
- $res1=json_decode($rescurl[1],true);
- }else{//失败
- // $res2=json_decode($res1[1],true);
- return $this->error($rescurl[1]);
- }
-
- //添加
- $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);
- $res1 = Db::name('device')->where('id',$data['id'])->find();
- $device_id=$res1['device_id'];
- $deviceType=$list['deviceType'];
- $res2=addtable($device_id,$deviceType);
- $deviceStatus=deviceStatus($res1['device_id']);
- if (!$deviceStatus){
- $data2=array();
- $data2['deviceid'] = $res1['device_id'];
- $data2['deviceno'] = 0;
- $data2['statustime'] = date('Y-m-d H:i:s');
- $sql2 = "INSERT INTO sp_devices_status VALUES ('','{$data2['deviceid']}',{$data2['deviceno']},'','{$data2['statustime']}','40','00','','0','')";
- $res2 = addDataInfo($sql2);
- }
- $owner1=owner($res1['device_id']);
- if ($data['company']){
- if (!$owner1){
- //echo 123456;
- $data3 = array();
- $data3['owner_name'] = $data['unitinfo'];
- $data3['unitinfo'] = $data['unitinfo'];
- $data3['louyu'] = $data['louyu'];
- $data3['sim'] = '';
- $data3['maintainTime'] = '';
- $data3['dest_address'] = 0;
- $data3['addr1'] = '';
- $data3['addr2'] = '';
- $data3['addr3'] = '';
- $data3['address'] = '';
- $data3['posistion'] = '';
- $data3['install_time'] = date('Y-m-d H:i:s');
- $data3['install_man'] = '';
- if ($deviceType=='Smoke'){
- $data3['owner_code'] = $res1['device_id'];
- $data3['dwtype'] =3;
- }else{
- $data3['dwtype'] =2;
- $data3['owner_code'] = $res1['device_id'];
- }
- $data3['owner_xh'] = '';
- $data3['s_interval'] =8;
- $data3['community'] = '';
- $data3['company'] = $data['company'];
- $data3['rtmp'] = '';
- $data3['hls'] = '';
- $data3['html5'] = '';
- $sql = "INSERT INTO sp_owner VALUES ('','{$data3['owner_code']}','{$data3['owner_name']}','{$data3['unitinfo']}','{$data3['louyu']}','{$data3['sim']}','{$data3['maintainTime']}',{$data3['dest_address']},
- '{$data3['addr1']}', '{$data3['addr2']}', '{$data3['addr3']}','{$data3['address']}','{$data3['posistion']}','{$data3['install_time']}','{$data3['install_man']}',{$data3['dwtype']},'{$data3['owner_xh']}'
- ,{$data3['s_interval']},'{$data3['community']}','{$data3['company']}','{$data3['rtmp']}','{$data3['hls']}','{$data3['html5']}','')";
- $res3 = addDataInfo($sql);
- //print_r($sql);
- }
- }
- if($res) {
- return $this->success('编辑成功','index');
- }else{
- return $this->error("编辑失败,请稍后再试");
- }
-
- }
- $res=Db::name('device')->where('id',input('id'))->find();
- $owner=owner($res['device_id']);
- $address=address();
- //print_r($address);
- $this->assign('address', $address);
- $this->assign('owner', $owner);
- $this->assign('data', $res);
- return $this->fetch();
- }
- //删除设备
- public function delete()
- {
- $data=input('');
-
- if(isset($data['auth'])&&$data['auth']=="xw12345"){
- $token=json_decode(gettoken(),true);
-
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
- $rescurl=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id'],"",$header,"DELETE");
- if($rescurl[0]==204){//成功
- $res1=json_decode($rescurl[1],true);
- }else{//失败
- // $res2=json_decode($res1[1],true);
- return $this->error($rescurl[1]);
- }
-
- $res= Db::name("device")->where("iot_id",$data['iot_id'])->delete();
- $droptable= Db::execute("drop table dev_".$data['device_id']);
- if($res) {
- return $this->success('删除成功','index');
- }else{
- return $this->error("删除失败,请稍后再试");
- }
- }
-
- // https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}&cascade={cascade}
- }
- //查看状态
- public function status()
- {
-
- if ($this->request->isPost()) {
- $data=$_POST;
- //调用电信接口
- $token=json_decode(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}
- $rescurl=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/".$data['iot_id']."?appId=".$this->appId,"",$header,"GET");
- // if($rescurl[0]==200){//成功
- $res1=$rescurl[1];
- // }else{//失败
- // $res2=json_decode($res1[1],true);
- // return $this->error($rescurl[1]);
- // }
- return $res1;
- }
- }
- //查看设备历史数据
- public function history()
- {
- //设备详细表
- $data=input('');
- $data_list =Db::name('dev_'.$data['id'])->paginate();
- $pages = $data_list->render();
- $this->assign('data_list', $data_list);
- $this->assign('pages', $pages);
- return $this->fetch();
-
- // if ($this->request->isPost()) {
- /* $data=input('');
- echo $data['id'];
- //调用电信接口
- $token=json_decode(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}
- $rescurl=curl($this->host."iocm/app/data/v1.2.0/deviceDataHistory?deviceId=a1d88da9-5156-4507-995a-bb20af09001a&gatewayId=a1d88da9-5156-4507-995a-bb20af09001a","",$header,"GET");
- dump($rescurl);*/
- // if($rescurl[0]==200){//成功
- // $res1=$rescurl[1];
- // }else{//失败
- // $res2=json_decode($res1[1],true);
- // return $this->error($rescurl[1]);
- // }
- // return $res1;
- // }
- }
- public function testedit()
- {
- $token=json_decode(gettoken(),true);
-
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
- $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/7da7261d-82a7-47f9-8fd3-73546e397e33","",$header,"DELETE");
- dump($res1);
-
- }
- public function test()
- {
- // https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
- //调用电信接口
- /* $token=json_decode(gettoken(),true);
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
-
- $res1=curltest($this->host."iocm/app/reg/v1.1.0/deviceCredentials/"."7da7261d-82a7-47f9-8fd3-73546e397e33"."?appId=".$this->appId,"",$header,"GET");
- dump($res1);
- if($res1[0]==200){
- $res2=json_decode($res1[1],true);
- }else{
- echo $res1[1];
- $res2=json_decode($res1[1],true);
- }
- dump($res2);*/
- //删除测试
- /* $token=json_decode(gettoken(),true);
-
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
- $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/"."d8509aa2-b478-4701-9edd-56cc243c38c5","",$header,"DELETE");
- dump($res1);*/
-
- // $res= Db::name("device")->where("iot_id",$data['iot_id'])->delete();
- dump(Auth());
- test();
-
-
- }
-
- }
|