Index.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Session;
  5. use think\Db;
  6. use think\Request;
  7. class Index extends Controller
  8. {
  9. private $host="https://device.api.ct10649.com:8743/";
  10. private $appId="wvdOfp7JVe0ULwnFKwBF0edfbcAa";
  11. private $secret="Bihoe_rY9vebXMw6sFfpQy0jiRca";
  12. /* //鉴权接口
  13. public function Auth()
  14. {
  15. $data="appId=wvdOfp7JVe0ULwnFKwBF0edfbcAa&secret=Bihoe_rY9vebXMw6sFfpQy0jiRca";
  16. $header=[
  17. "Content-Type:application/x-www-form-urlencoded",
  18. ];
  19. // https://server:port/iocm/app/sec/v1.1.0/log
  20. $res=curl("https://device.api.ct10649.com:8743/iocm/app/sec/v1.1.0/login",$data,$header);
  21. $res=json_decode($res,true);
  22. $res["token_time_out"]=$res['expiresIn']+time();
  23. $res["refresh_token_time_out"]=time()+86400*7;
  24. session('token',$res);
  25. return json_encode($res);
  26. }
  27. //刷新token并返回新的token
  28. public function refreshtoken(){
  29. $token=session("token");
  30. //查看刷新token是否过期,过期了调鉴权接口,没过期调刷新
  31. if($token["refresh_token_time_out"]>=time()){//刷新
  32. $data=[
  33. "appId"=>$this->appId,
  34. "secret"=>$this->secret,
  35. "refreshToken"=>$token["refreshToken"],
  36. ];
  37. $header=[
  38. "Content-Type:application/json",
  39. ];
  40. $res=curl($this->host."iocm/app/sec/v1.1.0/refreshToken",json_encode($data),$header);
  41. $res=json_decode($res,true);
  42. $res["token_time_out"]=time()+$res['expiresIn'];
  43. $res["refresh_token_time_out"]=time()+86400*7;
  44. session('token',$res);//
  45. return json_encode($res);
  46. }else{//鉴权
  47. return $this->Auth();
  48. }
  49. }
  50. //获取token
  51. public function gettoken(){
  52. //判断是否设置session以及session里的token是否过期
  53. if(session('?token')){
  54. $token=session('token');
  55. if($token['token_time_out']>=time()){//还没有过期直接返回token;
  56. return json_encode($token);
  57. }else{
  58. return $this->refreshtoken();
  59. }
  60. }else{
  61. return $token=$this->Auth();
  62. }
  63. }*/
  64. public function index()
  65. {
  66. $data_list =Db::table('device')->paginate();
  67. $pages = $data_list->render();
  68. $this->assign('data_list', $data_list);
  69. $this->assign('pages', $pages);
  70. return $this->fetch();
  71. }
  72. public function add()
  73. {
  74. $res=Db::name('device')->where('id',input('device_id'))->find();
  75. if(!$res){
  76. if ($this->request->isPost()) {
  77. $data=$_POST;
  78. //验证数据
  79. if(empty($data['device_id'])||empty($data['name'])){
  80. return $this->error("请填写好必要内容");
  81. }
  82. //调用电信接口
  83. $token=json_decode(gettoken(),true);
  84. $sendata=[
  85. "verifyCode"=>$data['device_id'],//"868744030067098"
  86. "nodeId"=>$data['device_id'],
  87. "timeout"=>0
  88. ];
  89. $header=[
  90. "Content-Type:application/json",
  91. "app_key:".$this->appId,
  92. "Authorization:Bearer ".$token["accessToken"]
  93. ];
  94. $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials?appId=".$this->appId,json_encode($sendata),$header);
  95. $res1=json_decode($res1,true);
  96. //添加
  97. $list=array();
  98. $list['device_id']= trim($data['device_id']);
  99. $list['name']= $data['name'];
  100. $list['iot_id']= $res1['deviceId'];
  101. $list['psk']= $res1['psk'];
  102. $list['addtime']= date('Y-m-d H:i:s');
  103. $list['updatetime']= date('Y-m-d H:i:s');
  104. //创建新表
  105. /* $device_table="hp_device_".$list['device_id'];
  106. $create_sql="
  107. CREATE TABLE `".$device_table."` (
  108. `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  109. `pressure` varchar(255) DEFAULT NULL,
  110. `status` tinyint(10) DEFAULT NULL,
  111. `addtime` varchar(255) DEFAULT NULL,
  112. PRIMARY KEY (`id`)
  113. ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
  114. ";
  115. $table= Db::execute($create_sql);
  116. */
  117. $res = Db::name('device')->insertGetId($list);
  118. if($res) {
  119. return $this->success('添加成功','index');
  120. }else{
  121. return $this->error("添加失败,请稍后再试");
  122. }
  123. }
  124. }else{
  125. return $this->error("改设备编号已经添加");
  126. }
  127. return $this->fetch();
  128. }
  129. public function edit()
  130. {
  131. if ($this->request->isPost()) {
  132. $data=$_POST;
  133. //验证数据
  134. if(empty($data['deviceType'])||empty($data['manufacturerName'])||empty($data['model'])||empty($data['location'])||empty($data['protocolType'])||empty($data['manufacturerName'])){
  135. return $this->error("请填写好必要内容");
  136. }
  137. //调用电信接口
  138. $token=json_decode(gettoken(),true);
  139. $sendata=[
  140. "name"=>$data["device_id"],
  141. "deviceType"=>$data['deviceType'],
  142. "manufacturerName"=>$data['manufacturerName'],
  143. "manufacturerId"=>$data['manufacturerId'],
  144. "model"=>$data['model'],
  145. "protocolType"=>$data['protocolType'],
  146. "location"=>trim($data['location']),
  147. ];
  148. $header=[
  149. "Content-Type:application/json",
  150. "app_key:".$this->appId,
  151. "Authorization:Bearer ".$token["accessToken"]
  152. ];
  153. //https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}
  154. $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/".$data['iot_id']."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT");
  155. $res1=json_decode($res1,true);
  156. //添加
  157. $list=array();
  158. $list['deviceType']= $data['deviceType'];//设备类型
  159. $list['name']= $data['name'];//设备类型
  160. $list['manufacturerName']= $data['manufacturerName'];//厂商名称
  161. $list['model']= $data['model'];//设备型号
  162. $list['isSecure']= $data['isSecure'];
  163. $list['manufacturerId']= $data['manufacturerId'];//设备型号
  164. $list['protocolType']= $data['protocolType'];
  165. $list['location']= $data['location'];
  166. $list['updatetime']= date('Y-m-d H:i:s');
  167. $res = Db::name('device')->where('id',$data['id'])->update($list);
  168. if($res) {
  169. return $this->success('编辑成功','index');
  170. }else{
  171. return $this->error("编辑失败,请稍后再试");
  172. }
  173. }
  174. $res=Db::name('device')->where('id',input('id'))->find();
  175. $this->assign('data', $res);
  176. return $this->fetch();
  177. }
  178. //删除设备
  179. public function delete()
  180. {
  181. }
  182. //查看状态
  183. public function status()
  184. {
  185. return 111;
  186. /*if ($this->request->isPost()) {
  187. $data=$_POST;
  188. //调用电信接口
  189. $token=json_decode($this->gettoken(),true);
  190. $header=[
  191. "Content-Type:application/json",
  192. "app_key:".$this->appId,
  193. "Authorization:Bearer ".$token["accessToken"]
  194. ];
  195. //https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  196. $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/".$data['iot_id']."?appId=".$this->appId,"",$header,"GET");
  197. return $res1;
  198. } */
  199. }
  200. public function testedit()
  201. {
  202. //https://server:port/iocm/app/dm/v1.4.0/devices/{deviceId}?appId={appId}
  203. //调用电信接口
  204. $token=json_decode(gettoken(),true);
  205. dump($token);
  206. /* $sendata=[
  207. "name"=>"test1",
  208. "deviceType"=>"HXKCNBDTU100",
  209. "manufacturerName"=>"SingPie",
  210. "model"=>"HXKC100NB",
  211. "location"=>"上海青浦",
  212. ];
  213. $header=[
  214. "Content-Type:application/json",
  215. "app_key:".$this->appId,
  216. "Authorization:Bearer ".$token["accessToken"]
  217. ];
  218. $res1=curl($this->host."iocm/app/dm/v1.4.0/devices/"."8d696965-8a63-4200-bc1f-202791e57883"."?appId=".$this->appId,json_encode($sendata,320),$header,"PUT");
  219. dump($res1);*/
  220. }
  221. public function teststatus()
  222. {
  223. // https://server:port/iocm/app/reg/v1.1.0/deviceCredentials/{deviceId}
  224. //调用电信接口
  225. $token=json_decode($this->gettoken(),true);
  226. $header=[
  227. "Content-Type:application/json",
  228. "app_key:".$this->appId,
  229. "Authorization:Bearer ".$token["accessToken"]
  230. ];
  231. $res1=curl($this->host."iocm/app/reg/v1.1.0/deviceCredentials/"."8d696965-8a63-4200-bc1f-202791e57883"."?appId=".$this->appId,"",$header,"GET");
  232. dump($res1);
  233. }
  234. }