Device6.php.bak 15 KB

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