Device.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Session;
  5. use think\Db;
  6. use think\Request;
  7. use think\Log;
  8. class Device extends Controller
  9. {
  10. private $host = "https://device.api.ct10649.com:8743/";
  11. private $appId = "wvdOfp7JVe0ULwnFKwBF0edfbcAa";
  12. private $secret = "Bihoe_rY9vebXMw6sFfpQy0jiRca";
  13. public function index()
  14. {
  15. $data_list = Db::table('device')->paginate();
  16. $pages = $data_list->render();
  17. $this->assign('data_list', $data_list);
  18. $this->assign('pages', $pages);
  19. return $this->fetch();
  20. }
  21. public function add()
  22. {
  23. $res = Db::name('device')->where('device_id', input('device_id'))->find();
  24. if (!$res) {
  25. if ($this->request->isPost()) {
  26. $data = $_POST;
  27. //验证数据
  28. if (empty($data['device_id']) || empty($data['name'])) {
  29. return $this->error("请填写好必要内容");
  30. }
  31. if ($data['applicationType'] == "bIS92ndpAf_fhmPdLaNuTidbFfka") {
  32. $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
  33. $type = 3;
  34. } else {
  35. $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
  36. $type = 2;
  37. }
  38. $appId = $data['applicationType'];
  39. //调用电信接口
  40. $token = json_decode(gettoken1($appId, $secret, $type), true);
  41. // dump($token); exit();
  42. $sendata = [
  43. "verifyCode" => $data['device_id'],//"868744030067098"
  44. "nodeId" => $data['device_id'],
  45. "timeout" => 0
  46. ];
  47. $header = [
  48. "Content-Type:application/json",
  49. "app_key:" . $appId,
  50. "Authorization:Bearer " . $token["accessToken"]
  51. ];
  52. $rescurl = curl($this->host . "iocm/app/reg/v1.1.0/deviceCredentials?appId=" . $appId, json_encode($sendata), $header);
  53. if ($rescurl[0] == 200) {//成功
  54. $res1 = json_decode($rescurl[1], true);
  55. } else {//失败
  56. // $res2=json_decode($res1[1],true);
  57. return $this->error($rescurl[1]);
  58. }
  59. //添加
  60. $list = array();
  61. $list['device_id'] = trim($data['device_id']);
  62. $list['name'] = $data['name'];
  63. $list['iot_id'] = $res1['deviceId'];
  64. $list['psk'] = $res1['psk'];
  65. $list['addtime'] = date('Y-m-d H:i:s');
  66. $list['updatetime'] = date('Y-m-d H:i:s');
  67. //创建新表
  68. $device_table = "dev_" . $list['device_id'];
  69. $create_sql = "
  70. CREATE TABLE `" . $device_table . "` (
  71. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  72. `device_id` varchar(200) DEFAULT NULL,
  73. `iot_id` varchar(255) DEFAULT NULL,
  74. `service` text,
  75. `addtime` varchar(50) DEFAULT NULL,
  76. `notifyType` varchar(50) DEFAULT NULL,
  77. PRIMARY KEY (`id`)
  78. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  79. ";
  80. $table = Db::execute($create_sql);
  81. $res = Db::name('device')->insertGetId($list);
  82. if ($res) {
  83. return $this->success('添加成功', 'index');
  84. } else {
  85. return $this->error("添加失败,请稍后再试");
  86. }
  87. }
  88. } else {
  89. return $this->error("改设备编号已经添加");
  90. }
  91. //所有业主信息
  92. $owners = Db::name('owner')->select();
  93. $this->assign('owners', $owners);
  94. return $this->fetch();
  95. }
  96. public function edit()
  97. {
  98. if ($this->request->isPost()) {
  99. $data = $_POST;
  100. //验证数据
  101. if (empty($data['deviceType']) || empty($data['manufacturerName']) || empty($data['model']) || empty($data['location']) || empty($data['protocolType']) || empty($data['manufacturerName'])) {
  102. return $this->error("请填写好必要内容");
  103. }
  104. if ($data['deviceType'] == 'TBSNode' && $data['manufacturerName'] == 'Turboes' && $data['manufacturerName'] == 'Turboes') {
  105. $appId = 'bIS92ndpAf_fhmPdLaNuTidbFfka';
  106. $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
  107. $type = 3;
  108. } else {
  109. $appId = 'wvdOfp7JVe0ULwnFKwBF0edfbcAa';
  110. $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
  111. $type = 2;
  112. }
  113. $owner1 = owner($data['device_id']);
  114. if ($owner1) {
  115. $data3 = array();
  116. $data3['name'] = $data['name'];
  117. $data3['unitinfo'] = $data['unitinfo'];
  118. $data3['louyu'] = $data['louyu'];
  119. $data3['company'] = $data['company'];
  120. $sql = "UPDATE sp_owner SET unitinfo = '{$data3['unitinfo']}',owner_name = '{$data3['name']}',louyu = '{$data3['louyu']}',company = '{$data3['company']}' WHERE owner_code = '{$data['device_id']}'";
  121. $res4 = addDataInfo($sql);
  122. } else {
  123. //调用电信接口
  124. $token = json_decode(gettoken1($appId, $secret, $type), true);
  125. $sendata = [
  126. "name" => $data["device_id"],
  127. "deviceType" => $data['deviceType'],
  128. "manufacturerName" => $data['manufacturerName'],
  129. "manufacturerId" => $data['manufacturerId'],
  130. "model" => $data['model'],
  131. "protocolType" => $data['protocolType'],
  132. "location" => trim($data['location']),
  133. ];
  134. $header = [
  135. "Content-Type:application/json",
  136. "app_key:" . $appId,
  137. "Authorization:Bearer " . $token["accessToken"]
  138. ];
  139. $rescurl = curl($this->host . "iocm/app/dm/v1.4.0/devices/" . $data['iot_id'] . "?appId=" . $appId, json_encode($sendata, 320), $header, "PUT");
  140. if ($rescurl[0] == 204) {//成功
  141. $res1 = json_decode($rescurl[1], true);
  142. } else {//失败
  143. // $res2=json_decode($res1[1],true);
  144. return $this->error($rescurl[1]);
  145. }
  146. //添加
  147. $list = array();
  148. $list['deviceType'] = $data['deviceType'];//设备类型
  149. $list['name'] = $data['name'];//设备名称
  150. $list['manufacturerName'] = $data['manufacturerName'];//厂商名称
  151. $list['model'] = $data['model'];//设备型号
  152. $list['isSecure'] = $data['isSecure'];
  153. $list['manufacturerId'] = $data['manufacturerId'];//设备型号
  154. $list['protocolType'] = $data['protocolType'];
  155. $list['location'] = $data['location'];
  156. $list['updatetime'] = date('Y-m-d H:i:s');
  157. $res = Db::name('device')->where('id', $data['id'])->update($list);
  158. $res1 = Db::name('device')->where('id', $data['id'])->find();
  159. $device_id = $res1['device_id'];
  160. $deviceType = $list['deviceType'];
  161. $res2 = addtable($device_id, $deviceType);
  162. $deviceStatus = deviceStatus($res1['device_id']);
  163. if (!$deviceStatus) {
  164. $data2 = array();
  165. $data2['deviceid'] = $res1['device_id'];
  166. $data2['deviceno'] = 0;
  167. $data2['statustime'] = date('Y-m-d H:i:s');
  168. $sql2 = "INSERT INTO sp_devices_status VALUES ('','{$data2['deviceid']}',{$data2['deviceno']},'','{$data2['statustime']}','40','00','','0','')";
  169. $res2 = addDataInfo($sql2);
  170. }
  171. if ($data['company']) {
  172. //echo 123456;
  173. $data3 = array();
  174. $data3['owner_name'] = $data['name'];
  175. $data3['unitinfo'] = $data['unitinfo'];
  176. $data3['louyu'] = $data['louyu'];
  177. $data3['sim'] = '';
  178. $data3['maintainTime'] = '';
  179. $data3['dest_address'] = 0;
  180. $data3['addr1'] = '';
  181. $data3['addr2'] = '';
  182. $data3['addr3'] = '';
  183. $data3['address'] = '';
  184. $data3['posistion'] = '';
  185. $data3['install_time'] = date('Y-m-d H:i:s');
  186. $data3['install_man'] = '';
  187. if ($deviceType == 'Smoke' || $deviceType == 'TBSNode') {
  188. $data3['owner_code'] = $res1['device_id'];
  189. $data3['dwtype'] = 3;
  190. } else {
  191. $data3['dwtype'] = 2;
  192. $data3['owner_code'] = $res1['device_id'];
  193. }
  194. $data3['owner_xh'] = '';
  195. $data3['s_interval'] = 8;
  196. $data3['community'] = '';
  197. $data3['company'] = $data['company'];
  198. $data3['rtmp'] = '';
  199. $data3['hls'] = '';
  200. $data3['html5'] = '';
  201. $sql = "INSERT INTO sp_owner VALUES ('','{$data3['owner_code']}','{$data3['owner_name']}','{$data3['unitinfo']}','{$data3['louyu']}','{$data3['sim']}','{$data3['maintainTime']}',{$data3['dest_address']},
  202. '{$data3['addr1']}', '{$data3['addr2']}', '{$data3['addr3']}','{$data3['address']}','{$data3['posistion']}',
  203. '{$data3['install_time']}','{$data3['install_man']}',{$data3['dwtype']},'{$data3['owner_xh']}'
  204. ,{$data3['s_interval']},'{$data3['community']}','{$data3['company']}','{$data3['rtmp']}',
  205. '{$data3['hls']}','{$data3['html5']}','','','','','','','','')";
  206. $res3 = addDataInfo($sql);
  207. //print_r($sql);
  208. }
  209. if ($res) {
  210. return $this->success('编辑成功', 'index');
  211. } else {
  212. return $this->error("编辑失败,请稍后再试");
  213. }
  214. }
  215. }
  216. $res = Db::name('device')->where('id', input('id'))->find();
  217. $owner = owner($res['device_id']);
  218. $address = address();
  219. //print_r($address);
  220. $this->assign('address', $address);
  221. $this->assign('owner', $owner);
  222. $this->assign('data', $res);
  223. return $this->fetch();
  224. }
  225. //删除设备
  226. public function delete()
  227. {
  228. $data = input('');
  229. if (isset($data['auth']) && $data['auth'] == "xw12345") {
  230. $R = Db::name("device")->where("iot_id", $data['iot_id'])->find();
  231. if ($R['deviceType']=='TBSNode'&&$R['manufacturerName']=='Turboes'&&$R['manufacturerName']=='Turboes'){
  232. $appId = 'bIS92ndpAf_fhmPdLaNuTidbFfka';
  233. $secret = 'NZMe1G42OdSmzlEuDpR_3ywKPhIa';
  234. $type = 3;
  235. }else{
  236. $appId = 'wvdOfp7JVe0ULwnFKwBF0edfbcAa';
  237. $secret = 'Bihoe_rY9vebXMw6sFfpQy0jiRca';
  238. $type = 2;
  239. }
  240. $token = json_decode(gettoken1($appId,$secret,$type), true);
  241. $header = [
  242. "Content-Type:application/json",
  243. "app_key:" . $appId,
  244. "Authorization:Bearer " . $token["accessToken"]
  245. ];
  246. $rescurl = curl($this->host . "iocm/app/dm/v1.4.0/devices/" . $data['iot_id'], "", $header, "DELETE");
  247. if ($rescurl[0] == 204) {//成功
  248. $res1 = json_decode($rescurl[1], true);
  249. } else {//失败
  250. // $res2=json_decode($res1[1],true);
  251. return $this->error($rescurl[1]);
  252. }
  253. $res = Db::name("device")->where("iot_id", $data['iot_id'])->delete();
  254. $droptable = Db::execute("drop table dev_" . $data['device_id']);
  255. if ($res) {
  256. return $this->success('删除成功', 'index');
  257. } else {
  258. return $this->error("删除失败,请稍后再试");
  259. }
  260. }
  261. // https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}&cascade={cascade}
  262. }
  263. //查看状态
  264. public function status()
  265. {
  266. if ($this->request->isPost()) {
  267. $data = $_POST;
  268. //调用电信接口
  269. $token = json_decode(gettoken(), true);
  270. $header = [
  271. "Content-Type:application/json",
  272. "app_key:" . $this->appId,
  273. "Authorization:Bearer " . $token["accessToken"]
  274. ];
  275. //https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  276. $rescurl = curl($this->host . "iocm/app/reg/v1.1.0/deviceCredentials/" . $data['iot_id'] . "?appId=" . $this->appId, "", $header, "GET");
  277. // if($rescurl[0]==200){//成功
  278. $res1 = $rescurl[1];
  279. // }else{//失败
  280. // $res2=json_decode($res1[1],true);
  281. // return $this->error($rescurl[1]);
  282. // }
  283. return $res1;
  284. }
  285. }
  286. //查看设备历史数据
  287. public function history()
  288. {
  289. //设备详细表
  290. $data = input('');
  291. $data_list = Db::name('dev_' . $data['id'])->paginate();
  292. $pages = $data_list->render();
  293. $this->assign('data_list', $data_list);
  294. $this->assign('pages', $pages);
  295. return $this->fetch();
  296. }
  297. public function testedit()
  298. {
  299. $token = json_decode(gettoken(), true);
  300. $header = [
  301. "Content-Type:application/json",
  302. "app_key:" . $this->appId,
  303. "Authorization:Bearer " . $token["accessToken"]
  304. ];
  305. $res1 = curl($this->host . "iocm/app/dm/v1.4.0/devices/7da7261d-82a7-47f9-8fd3-73546e397e33", "", $header, "DELETE");
  306. dump($res1);
  307. }
  308. public function test()
  309. {
  310. dump(Auth());
  311. test();
  312. }
  313. }