123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?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("请填写好必要内容");
- }
- if ($data['applicationType'] == "bIS92ndpAf_fhmPdLaNuTidbFfka") {
- $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
- $type = 3;
- } else {
- $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
- $type = 2;
- }
- $appId = $data['applicationType'];
- //调用电信接口
- $token = json_decode(gettoken1($appId, $secret, $type), true);
- // dump($token); exit();
- $sendata = [
- "verifyCode" => $data['device_id'],//"868744030067098"
- "nodeId" => $data['device_id'],
- "timeout" => 0
- ];
- $header = [
- "Content-Type:application/json",
- "app_key:" . $appId,
- "Authorization:Bearer " . $token["accessToken"]
- ];
- $rescurl = curl($this->host . "iocm/app/reg/v1.1.0/deviceCredentials?appId=" . $appId, json_encode($sendata), $header);
- 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("请填写好必要内容");
- }
- if ($data['deviceType'] == 'TBSNode' && $data['manufacturerName'] == 'Turboes' && $data['manufacturerName'] == 'Turboes') {
- $appId = 'bIS92ndpAf_fhmPdLaNuTidbFfka';
- $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
- $type = 3;
- } else {
- $appId = 'wvdOfp7JVe0ULwnFKwBF0edfbcAa';
- $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
- $type = 2;
- }
- $owner1 = owner($data['device_id']);
- if ($owner1) {
- $data3 = array();
- $data3['name'] = $data['name'];
- $data3['unitinfo'] = $data['unitinfo'];
- $data3['louyu'] = $data['louyu'];
- $data3['company'] = $data['company'];
- $sql = "UPDATE sp_owner SET unitinfo = '{$data3['unitinfo']}',owner_name = '{$data3['name']}',louyu = '{$data3['louyu']}',company = '{$data3['company']}' WHERE owner_code = '{$data['device_id']}'";
- $res4 = addDataInfo($sql);
- } else {
- //调用电信接口
- $token = json_decode(gettoken1($appId, $secret, $type), 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:" . $appId,
- "Authorization:Bearer " . $token["accessToken"]
- ];
- $rescurl = curl($this->host . "iocm/app/dm/v1.4.0/devices/" . $data['iot_id'] . "?appId=" . $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);
- }
- if ($data['company']) {
- //echo 123456;
- $data3 = array();
- $data3['owner_name'] = $data['name'];
- $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' || $deviceType == 'TBSNode') {
- $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") {
- $R = Db::name("device")->where("iot_id", $data['iot_id'])->find();
- if ($R['deviceType']=='TBSNode'&&$R['manufacturerName']=='Turboes'&&$R['manufacturerName']=='Turboes'){
- $appId = 'bIS92ndpAf_fhmPdLaNuTidbFfka';
- $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
- $type = 3;
- }else{
- $appId = 'wvdOfp7JVe0ULwnFKwBF0edfbcAa';
- $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
- $type = 2;
- }
- $token = json_decode(gettoken1($appId,$secret,$type), true);
- $header = [
- "Content-Type:application/json",
- "app_key:" . $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();
- }
- 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()
- {
- dump(Auth());
- test();
- }
- }
|