Manage.php.bak.20201204shangxiaxianweiruku 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 Manage 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. $data_list =Db::table('managelog')->paginate();
  15. $pages = $data_list->render();
  16. $this->assign('data_list', $data_list);
  17. $this->assign('pages', $pages);
  18. //所有的服务类型
  19. $SetType=[
  20. 1=>"读取模块信息",
  21. 2=>"读取参数",
  22. 3=>"设置IOT平台IP地址",
  23. 4=>"设置参数",
  24. 5=>"复位模块",
  25. 6=>"配置下行波特率",
  26. 7=>"读取下行波特率配置",
  27. 8=>"设置DTU自动采集指令",
  28. 9=>"读取DTU自动采集指令",
  29. 10=>"设置APN",
  30. 11=>"读取APN设置",
  31. 12=>"AT指令",
  32. /* [1,"读取模块信息"],[2,"读取参数"],[3,"设置IOT平台IP地址"],[4,"设置参数"],[5,"复位模块"],[6,"配置下行波特率"],
  33. [7,"读取下行波特率配置"],[8,"设置DTU自动采集指令"],[9,"读取DTU自动采集指令"],[10,"设置APN"],[11,"读取APN设置"],
  34. [12,"AT指令"]*/
  35. ];
  36. $this->assign('SetType',$SetType);
  37. return $this->fetch();
  38. }
  39. //下发命令
  40. public function add(){
  41. if ($this->request->isPost()) {
  42. $data=input('');
  43. $list = array();
  44. if (is_array($data)) {
  45. $list['data'] = json_encode($data, 320);
  46. $getdata = json_decode($list['data'], true);//转成数组
  47. $list['type'] = 1;
  48. } else {
  49. $list['data'] = $data;
  50. $getdata = json_decode($list['data'], true);//转成数组
  51. $list['type'] = 2;
  52. }
  53. $json_string = json_encode($getdata, JSON_FORCE_OBJECT);
  54. Log::record('rawData:' .$json_string );
  55. $paras = array();
  56. if(!empty($getdata["dataType"])){
  57. if($getdata['dataType']=="setting"){
  58. $sql6="select * from sp_owner where id ={$getdata['sensorId']}";
  59. $longHuOwner = add($sql6);
  60. $deviceCode = '';
  61. $deviceArr = str_split($longHuOwner[0]['owner_code'],1);
  62. for ($i=0;$i<count($deviceArr);$i++){
  63. $deviceCode.='3'.$deviceArr[$i];
  64. }
  65. $deviceCode = $deviceCode.'30';
  66. $sendingInterval = "";//发送间隔
  67. if (!empty($getdata['sendTime'])) {
  68. if($getdata['sendTime']<1800){
  69. $sendingInterval = "020400000708";
  70. }else{
  71. $liuSendTime1 = dechex($getdata['sendTime']);
  72. $liuSendTime = str_pad($liuSendTime1,8,0,STR_PAD_LEFT);
  73. $sendingInterval = "0204".$liuSendTime;
  74. }
  75. }
  76. $lowThreshold = "";//告警下限
  77. if (!empty($getdata['lowThreshold'])) {
  78. $shiLowThreshold = $getdata['lowThreshold']*1000;
  79. $liuLowThreshold1 = dechex($shiLowThreshold);
  80. $liuLowThreshold = str_pad($liuLowThreshold1,4,0,STR_PAD_LEFT);
  81. $lowThreshold = "0602".$liuLowThreshold;
  82. }
  83. $highThreshold = "";//告警上限
  84. if (!empty($getdata['highThreshold'])) {
  85. $shiHighThreshold = $getdata['highThreshold']*1000;
  86. $liuHighThreshold1 = dechex($shiHighThreshold);
  87. $liuHighThreshold = str_pad($liuHighThreshold1,4,0,STR_PAD_LEFT);
  88. $highThreshold = "0702".$liuHighThreshold;
  89. }
  90. if($longHuOwner[0]['dwtype']==2){
  91. $dwType = "01";
  92. }elseif ($longHuOwner[0]['dwtype']==5){
  93. $dwType = "02";
  94. }
  95. $shiLength = strlen($dwType.$deviceCode.$sendingInterval.$lowThreshold.$highThreshold)/2;
  96. $liuLength = dechex($shiLength);
  97. $length = str_pad($liuLength,4,0,STR_PAD_LEFT);
  98. //调用电信接口
  99. $token=json_decode(gettoken(),true);
  100. $a = "7470736c04".$length.$dwType.$deviceCode.$sendingInterval.$lowThreshold.$highThreshold;
  101. $s = pack('H*',$a);
  102. $t = crc166($s);
  103. $t = unpack("H*", $s.$t);
  104. $paras['value'] = $t[1]."696f74";
  105. // $t = crc16($paras['value'],0X1021,0Xffff,0X0000,false,false);
  106. $device = Db::name('device')->where('device_id', trim($longHuOwner[0]['owner_code']))->find();
  107. $sendata=[
  108. "appId"=>$this->appId,
  109. "deviceId"=>$device['iot_id'],// c448d5e3-2ec5-4e42-99d7-0ad27668614f 137effbe-4980-4719-a768-0ae301fc5000
  110. "command"=>[
  111. "serviceId"=>"topsailSensorData",
  112. "method"=>"SET_DEVICE_LEVEL",
  113. "paras"=>$paras
  114. ],
  115. "callbackUrl"=>"http://iot.usky.cn:80/ytapi/admin/Manage/status",
  116. ];
  117. $header=[
  118. "Content-Type:application/json",
  119. "app_key:".$this->appId,
  120. "Authorization:Bearer ".$token["accessToken"]
  121. ];
  122. $rescurl=curl($this->host."iocm/app/cmd/v1.4.0/deviceCommands",json_encode($sendata),$header);
  123. // var_dump($rescurl);
  124. Log::record('nbmanage1:' .$rescurl[0].$sendata['command']['paras']['value'] );
  125. if($rescurl[0]==201){//成功
  126. $res1=json_decode($rescurl[1],true);
  127. // var_dump($res1);
  128. }else{//失败
  129. // $res2=json_decode($res1[1],true);
  130. // return $this->error($rescurl[1]);
  131. Log::record('nbmanageerror:' .json_encode($this->error($rescurl[1])));
  132. $returnInformation['code'] = 60001;
  133. $returnInformation['msg'] = "操作失败,请联系管理员";
  134. return json_encode($returnInformation);
  135. }
  136. }else if($getdata['dataType']=="bendi"){
  137. $deviceCode = '';
  138. $deviceArr = str_split($getdata['IMEI'],1);
  139. for ($i=0;$i<count($deviceArr);$i++){
  140. $deviceCode.='3'.$deviceArr[$i];
  141. }
  142. $deviceCode = $deviceCode.'30';
  143. //调用电信接口
  144. $collectionInterval = "";//发送间隔
  145. if (!empty($getdata['collectionTime'])) {
  146. $liuCollectionTime1 = dechex($getdata['collectionTime']);
  147. $liuCollectionTime = str_pad($liuCollectionTime1,8,0,STR_PAD_LEFT);
  148. $collectionInterval = "0104".$liuCollectionTime;
  149. }
  150. $sendingInterval = "";//发送间隔
  151. if (!empty($getdata['sendTime'])) {
  152. $liuSendTime1 = dechex($getdata['sendTime']);
  153. $liuSendTime = str_pad($liuSendTime1,8,0,STR_PAD_LEFT);
  154. $sendingInterval = "0204".$liuSendTime;
  155. }
  156. $lowThreshold = "";//告警下限
  157. if (!empty($getdata['alarmLowerLimit'])) {
  158. $shiLowThreshold = $getdata['alarmLowerLimit']*1000;
  159. $liuLowThreshold1 = dechex($shiLowThreshold);
  160. $liuLowThreshold = str_pad($liuLowThreshold1,4,0,STR_PAD_LEFT);
  161. $lowThreshold = "0602".$liuLowThreshold;
  162. }
  163. $highThreshold = "";//告警上限
  164. if (!empty($getdata['alarmUpperLimit'])) {
  165. $shiHighThreshold = $getdata['alarmUpperLimit']*1000;
  166. $liuHighThreshold1 = dechex($shiHighThreshold);
  167. $liuHighThreshold = str_pad($liuHighThreshold1,4,0,STR_PAD_LEFT);
  168. $highThreshold = "0702".$liuHighThreshold;
  169. }
  170. if($getdata['deviceType']==2){
  171. $dwType = "01";
  172. }elseif ($getdata['deviceType']==5){
  173. $dwType = "02";
  174. }
  175. $shiLength = strlen($dwType.$deviceCode.$collectionInterval.$sendingInterval.$lowThreshold.$highThreshold)/2;
  176. $liuLength = dechex($shiLength);
  177. $length = str_pad($liuLength,4,0,STR_PAD_LEFT);
  178. //调用电信接口
  179. $token=json_decode(gettoken(),true);
  180. $a = "7470736c04".$length.$dwType.$deviceCode.$collectionInterval.$sendingInterval.$lowThreshold.$highThreshold;
  181. $s = pack('H*',$a);
  182. $t = crc166($s);
  183. $t = unpack("H*", $s.$t);
  184. $paras['value'] = $t[1]."696f74";
  185. $sendata=[
  186. "appId"=>$this->appId,
  187. "deviceId"=>$getdata['deviceId'],// c448d5e3-2ec5-4e42-99d7-0ad27668614f 137effbe-4980-4719-a768-0ae301fc5000
  188. "command"=>[
  189. "serviceId"=>"topsailSensorData",
  190. "method"=>"SET_DEVICE_LEVEL",
  191. "paras"=>$paras
  192. ],
  193. "callbackUrl"=>"http://iot.usky.cn:80/ytapi/admin/Manage/status",
  194. ];
  195. $header=[
  196. "Content-Type:application/json",
  197. "app_key:".$this->appId,
  198. "Authorization:Bearer ".$token["accessToken"]
  199. ];
  200. $rescurl=curl($this->host."iocm/app/cmd/v1.4.0/deviceCommands",json_encode($sendata),$header);
  201. var_dump($rescurl);
  202. Log::record('nbmanage1:' .$rescurl[0].$sendata['command']['paras']['value'] );
  203. if($rescurl[0]==201){//成功
  204. $res1=json_decode($rescurl[1],true);
  205. // var_dump($res1);
  206. }else{//失败
  207. // $res2=json_decode($res1[1],true);
  208. return $this->error($rescurl[1]);
  209. }
  210. }else{
  211. $returnInformation['code'] = 60001;
  212. $returnInformation['msg'] = "操作失败,请联系管理员";
  213. return json_encode($returnInformation);
  214. }
  215. }else{
  216. $returnInformation['code'] = 60001;
  217. $returnInformation['msg'] = "操作失败,请联系管理员";
  218. return json_encode($returnInformation);
  219. }
  220. //将请求的结果存入managelog中
  221. $list=array();
  222. $list['commandId']=$res1['commandId'];
  223. $list['deviceId']=$res1['deviceId'];
  224. $list['status']=$res1['status'];
  225. $list['command']= json_encode($res1['command'],320) ;
  226. if($getdata['dataType']=="setting"){
  227. $list['IMEI']=$longHuOwner[0]['owner_code'];
  228. }else{
  229. $list['IMEI']=$getdata['IMEI'];
  230. }
  231. $list['HexStr']="";
  232. $list['settype']="";
  233. $list['addtime']= date('Y-m-d H:i:s');
  234. $res = Db::name('managelog')->insertGetId($list);//将推送的数据存入总表
  235. if($res) {
  236. $returnInformation['code'] = 200;
  237. $returnInformation['msg'] = "操作成功";
  238. if($getdata['dataType']=="setting"){
  239. return json_encode($returnInformation);
  240. }else{
  241. return $this->success('添加成功','index');
  242. }
  243. }else{
  244. $returnInformation['code'] = 60001;
  245. $returnInformation['msg'] = "操作失败,请联系管理员";
  246. if($getdata['dataType']=="setting"){
  247. return json_encode($returnInformation);
  248. }else{
  249. return $this->error("添加失败,请稍后再试");
  250. }
  251. }
  252. }
  253. if($this->request->isGet()){
  254. $deviceId=input('deviceId');
  255. $IMEI=input('IMEI');
  256. //所有的服务类型
  257. $SetType=[
  258. [1,"读取模块信息"],[2,"读取参数"],[3,"设置IOT平台IP地址"],[4,"设置参数"],[5,"复位模块"],[6,"配置下行波特率"],
  259. [7,"读取下行波特率配置"],[8,"设置DTU自动采集指令"],[9,"读取DTU自动采集指令"],[10,"设置APN"],[11,"读取APN设置"],
  260. [12,"AT指令"]
  261. ];
  262. $this->assign('SetType',$SetType);
  263. $this->assign('deviceId',$deviceId);
  264. $this->assign('IMEI', $IMEI);
  265. return $this->fetch();
  266. }
  267. }
  268. public function status(){
  269. $data=input('');
  270. if(is_array($data)){
  271. $getdata= json_encode($data,320);
  272. $getdata=json_decode($getdata,true);//转成数组
  273. $list['type']=1;
  274. }else{
  275. $getdata=$data;
  276. $getdata=json_decode($getdata,true);//转成数组
  277. $list['type']=2;
  278. }
  279. $list=array();
  280. $list['deviceId']=$getdata['deviceId'];
  281. $list['commandId']=$getdata['commandId'];
  282. $list['resultCode']=$getdata['result']['resultCode'];
  283. $list['result']= json_encode($getdata['result'],320) ;
  284. $list['addtime']= date('Y-m-d H:i:s');
  285. $res = Db::name('managedata')->insertGetId($list);//将推送的数据存入总表
  286. echo 111;
  287. }
  288. public function delete(){
  289. echo 111;
  290. }
  291. }