Device.php.bak 12 KB

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