Manage.php.bak.20201223 15 KB

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