Device7.php.bak 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. $owner1=owner($data['device_id']);
  99. if ($owner1){
  100. $data3 = array();
  101. $data3['unitinfo'] = $data['unitinfo'];
  102. $data3['louyu'] = $data['louyu'];
  103. $data3['company'] = $data['company'];
  104. $sql="UPDATE sp_owner SET unitinfo = '{$data3['unitinfo']}',owner_name = '{$data3['unitinfo']}',louyu = '{$data3['louyu']}',company = '{$data3['company']}' WHERE owner_code = '{$data['device_id']}'";
  105. $res4 = addDataInfo($sql);
  106. }else{
  107. //调用电信接口
  108. $token=json_decode(gettoken(),true);
  109. $sendata=[
  110. "name"=>$data["device_id"],
  111. "deviceType"=>$data['deviceType'],
  112. "manufacturerName"=>$data['manufacturerName'],
  113. "manufacturerId"=>$data['manufacturerId'],
  114. "model"=>$data['model'],
  115. "protocolType"=>$data['protocolType'],
  116. "location"=>trim($data['location']),
  117. ];
  118. $header=[
  119. "Content-Type:application/json",
  120. "app_key:".$this->appId,
  121. "Authorization:Bearer ".$token["accessToken"]
  122. ];
  123. //https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}
  124. $rescurl=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id']."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT");
  125. if($rescurl[0]==204){//成功
  126. $res1=json_decode($rescurl[1],true);
  127. }else{//失败
  128. // $res2=json_decode($res1[1],true);
  129. return $this->error($rescurl[1]);
  130. }
  131. //添加
  132. $list=array();
  133. $list['deviceType']= $data['deviceType'];//设备类型
  134. $list['name']= $data['name'];//设备类型
  135. $list['manufacturerName']= $data['manufacturerName'];//厂商名称
  136. $list['model']= $data['model'];//设备型号
  137. $list['isSecure']= $data['isSecure'];
  138. $list['manufacturerId']= $data['manufacturerId'];//设备型号
  139. $list['protocolType']= $data['protocolType'];
  140. $list['location']= $data['location'];
  141. $list['updatetime']= date('Y-m-d H:i:s');
  142. $res = Db::name('device')->where('id',$data['id'])->update($list);
  143. $res1 = Db::name('device')->where('id',$data['id'])->find();
  144. $device_id=$res1['device_id'];
  145. $deviceType=$list['deviceType'];
  146. $res2=addtable($device_id,$deviceType);
  147. $deviceStatus=deviceStatus($res1['device_id']);
  148. if (!$deviceStatus){
  149. $data2=array();
  150. $data2['deviceid'] = $res1['device_id'];
  151. $data2['deviceno'] = 0;
  152. $data2['statustime'] = date('Y-m-d H:i:s');
  153. $sql2 = "INSERT INTO sp_devices_status VALUES ('','{$data2['deviceid']}',{$data2['deviceno']},'','{$data2['statustime']}','40','00','','0','')";
  154. $res2 = addDataInfo($sql2);
  155. }
  156. if ($data['company']){
  157. //echo 123456;
  158. $data3 = array();
  159. $data3['owner_name'] = $data['name'];
  160. $data3['unitinfo'] = $data['unitinfo'];
  161. $data3['louyu'] = $data['louyu'];
  162. $data3['sim'] = '';
  163. $data3['maintainTime'] = '';
  164. $data3['dest_address'] = 0;
  165. $data3['addr1'] = '';
  166. $data3['addr2'] = '';
  167. $data3['addr3'] = '';
  168. $data3['address'] = '';
  169. $data3['posistion'] = '';
  170. $data3['install_time'] = date('Y-m-d H:i:s');
  171. $data3['install_man'] = '';
  172. if ($deviceType=='Smoke'){
  173. $data3['owner_code'] = $res1['device_id'];
  174. $data3['dwtype'] =3;
  175. }else{
  176. $data3['dwtype'] =2;
  177. $data3['owner_code'] = $res1['device_id'];
  178. }
  179. $data3['owner_xh'] = '';
  180. $data3['s_interval'] =8;
  181. $data3['community'] = '';
  182. $data3['company'] = $data['company'];
  183. $data3['rtmp'] = '';
  184. $data3['hls'] = '';
  185. $data3['html5'] = '';
  186. $sql = "INSERT INTO sp_owner VALUES ('','{$data3['owner_code']}','{$data3['owner_name']}','{$data3['unitinfo']}','{$data3['louyu']}','{$data3['sim']}','{$data3['maintainTime']}',{$data3['dest_address']},
  187. '{$data3['addr1']}', '{$data3['addr2']}', '{$data3['addr3']}','{$data3['address']}','{$data3['posistion']}','{$data3['install_time']}','{$data3['install_man']}',{$data3['dwtype']},'{$data3['owner_xh']}'
  188. ,{$data3['s_interval']},'{$data3['community']}','{$data3['company']}','{$data3['rtmp']}','{$data3['hls']}','{$data3['html5']}','')";
  189. $res3 = addDataInfo($sql);
  190. //print_r($sql);
  191. }
  192. if($res) {
  193. return $this->success('编辑成功','index');
  194. }else{
  195. return $this->error("编辑失败,请稍后再试");
  196. }
  197. }
  198. }
  199. $res=Db::name('device')->where('id',input('id'))->find();
  200. $owner=owner($res['device_id']);
  201. $address=address();
  202. //print_r($address);
  203. $this->assign('address', $address);
  204. $this->assign('owner', $owner);
  205. $this->assign('data', $res);
  206. return $this->fetch();
  207. }
  208. //删除设备
  209. public function delete()
  210. {
  211. $data=input('');
  212. if(isset($data['auth'])&&$data['auth']=="xw12345"){
  213. $token=json_decode(gettoken(),true);
  214. $header=[
  215. "Content-Type:application/json",
  216. "app_key:".$this->appId,
  217. "Authorization:Bearer ".$token["accessToken"]
  218. ];
  219. $rescurl=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id'],"",$header,"DELETE");
  220. if($rescurl[0]==204){//成功
  221. $res1=json_decode($rescurl[1],true);
  222. }else{//失败
  223. // $res2=json_decode($res1[1],true);
  224. return $this->error($rescurl[1]);
  225. }
  226. $res= Db::name("device")->where("iot_id",$data['iot_id'])->delete();
  227. $droptable= Db::execute("drop table dev_".$data['device_id']);
  228. if($res) {
  229. return $this->success('删除成功','index');
  230. }else{
  231. return $this->error("删除失败,请稍后再试");
  232. }
  233. }
  234. // https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}&cascade={cascade}
  235. }
  236. //查看状态
  237. public function status()
  238. {
  239. if ($this->request->isPost()) {
  240. $data=$_POST;
  241. //调用电信接口
  242. $token=json_decode(gettoken(),true);
  243. $header=[
  244. "Content-Type:application/json",
  245. "app_key:".$this->appId,
  246. "Authorization:Bearer ".$token["accessToken"]
  247. ];
  248. //https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  249. $rescurl=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/".$data['iot_id']."?appId=".$this->appId,"",$header,"GET");
  250. // if($rescurl[0]==200){//成功
  251. $res1=$rescurl[1];
  252. // }else{//失败
  253. // $res2=json_decode($res1[1],true);
  254. // return $this->error($rescurl[1]);
  255. // }
  256. return $res1;
  257. }
  258. }
  259. //查看设备历史数据
  260. public function history()
  261. {
  262. //设备详细表
  263. $data=input('');
  264. $data_list =Db::name('dev_'.$data['id'])->paginate();
  265. $pages = $data_list->render();
  266. $this->assign('data_list', $data_list);
  267. $this->assign('pages', $pages);
  268. return $this->fetch();
  269. // if ($this->request->isPost()) {
  270. /* $data=input('');
  271. echo $data['id'];
  272. //调用电信接口
  273. $token=json_decode(gettoken(),true);
  274. $header=[
  275. "Content-Type:application/json",
  276. "app_key:".$this->appId,
  277. "Authorization:Bearer ".$token["accessToken"]
  278. ];
  279. //https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  280. $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");
  281. dump($rescurl);*/
  282. // if($rescurl[0]==200){//成功
  283. // $res1=$rescurl[1];
  284. // }else{//失败
  285. // $res2=json_decode($res1[1],true);
  286. // return $this->error($rescurl[1]);
  287. // }
  288. // return $res1;
  289. // }
  290. }
  291. public function testedit()
  292. {
  293. $token=json_decode(gettoken(),true);
  294. $header=[
  295. "Content-Type:application/json",
  296. "app_key:".$this->appId,
  297. "Authorization:Bearer ".$token["accessToken"]
  298. ];
  299. $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/7da7261d-82a7-47f9-8fd3-73546e397e33","",$header,"DELETE");
  300. dump($res1);
  301. }
  302. public function test()
  303. {
  304. // https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  305. //调用电信接口
  306. /* $token=json_decode(gettoken(),true);
  307. $header=[
  308. "Content-Type:application/json",
  309. "app_key:".$this->appId,
  310. "Authorization:Bearer ".$token["accessToken"]
  311. ];
  312. $res1=curltest($this->host."iocm/app/reg/v1.1.0/deviceCredentials/"."7da7261d-82a7-47f9-8fd3-73546e397e33"."?appId=".$this->appId,"",$header,"GET");
  313. dump($res1);
  314. if($res1[0]==200){
  315. $res2=json_decode($res1[1],true);
  316. }else{
  317. echo $res1[1];
  318. $res2=json_decode($res1[1],true);
  319. }
  320. dump($res2);*/
  321. //删除测试
  322. /* $token=json_decode(gettoken(),true);
  323. $header=[
  324. "Content-Type:application/json",
  325. "app_key:".$this->appId,
  326. "Authorization:Bearer ".$token["accessToken"]
  327. ];
  328. $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/"."d8509aa2-b478-4701-9edd-56cc243c38c5","",$header,"DELETE");
  329. dump($res1);*/
  330. // $res= Db::name("device")->where("iot_id",$data['iot_id'])->delete();
  331. dump(Auth());
  332. test();
  333. }
  334. }