123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Session;
- use think\Db;
- use think\Request;
- use think\Log;
- class Manage extends Controller
- {
- private $host="https://device.api.ct10649.com:8743/";
- private $appId="wvdOfp7JVe0ULwnFKwBF0edfbcAa";
- private $secret="Bihoe_rY9vebXMw6sFfpQy0jiRca";
- public function index(){
- $data_list =Db::table('managelog')->paginate();
- $pages = $data_list->render();
- $this->assign('data_list', $data_list);
- $this->assign('pages', $pages);
- //所有的服务类型
- $SetType=[
- 1=>"读取模块信息",
- 2=>"读取参数",
- 3=>"设置IOT平台IP地址",
- 4=>"设置参数",
- 5=>"复位模块",
- 6=>"配置下行波特率",
- 7=>"读取下行波特率配置",
- 8=>"设置DTU自动采集指令",
- 9=>"读取DTU自动采集指令",
- 10=>"设置APN",
- 11=>"读取APN设置",
- 12=>"AT指令",
- /* [1,"读取模块信息"],[2,"读取参数"],[3,"设置IOT平台IP地址"],[4,"设置参数"],[5,"复位模块"],[6,"配置下行波特率"],
- [7,"读取下行波特率配置"],[8,"设置DTU自动采集指令"],[9,"读取DTU自动采集指令"],[10,"设置APN"],[11,"读取APN设置"],
- [12,"AT指令"]*/
- ];
- $this->assign('SetType',$SetType);
-
- return $this->fetch();
- }
- //下发命令
- public function add(){
- if ($this->request->isPost()) {
- $data=input('');
- $list = array();
- if (is_array($data)) {
- $list['data'] = json_encode($data, 320);
- $getdata = json_decode($list['data'], true);//转成数组
- $list['type'] = 1;
- } else {
- $list['data'] = $data;
- $getdata = json_decode($list['data'], true);//转成数组
- $list['type'] = 2;
- }
- $json_string = json_encode($getdata, JSON_FORCE_OBJECT);
- Log::record('rawData:' .$json_string );
- $paras = array();
- if(!empty($getdata["dataType"])){
- if($getdata['dataType']=="setting"){
- $sql6="select * from sp_owner where id ={$getdata['sensorId']}";
- $longHuOwner = add($sql6);
- $deviceCode = '';
- $deviceArr = str_split($longHuOwner[0]['owner_code'],1);
- for ($i=0;$i<count($deviceArr);$i++){
- $deviceCode.='3'.$deviceArr[$i];
- }
- $deviceCode = $deviceCode.'30';
- $sendingInterval = "";//发送间隔
- if (!empty($getdata['sendTime'])) {
- if($getdata['sendTime']<1800){
- $sendingInterval = "020400000708";
- }else{
- $liuSendTime1 = dechex($getdata['sendTime']);
- $liuSendTime = str_pad($liuSendTime1,8,0,STR_PAD_LEFT);
- $sendingInterval = "0204".$liuSendTime;
- }
- }
- $lowThreshold = "";//告警下限
- if (!empty($getdata['lowThreshold'])) {
- $shiLowThreshold = $getdata['lowThreshold']*1000;
- $liuLowThreshold1 = dechex($shiLowThreshold);
- $liuLowThreshold = str_pad($liuLowThreshold1,4,0,STR_PAD_LEFT);
- $lowThreshold = "0602".$liuLowThreshold;
- }
- $highThreshold = "";//告警上限
- if (!empty($getdata['highThreshold'])) {
- $shiHighThreshold = $getdata['highThreshold']*1000;
- $liuHighThreshold1 = dechex($shiHighThreshold);
- $liuHighThreshold = str_pad($liuHighThreshold1,4,0,STR_PAD_LEFT);
- $highThreshold = "0702".$liuHighThreshold;
- }
- if($longHuOwner[0]['dwtype']==2){
- $dwType = "01";
- }elseif ($longHuOwner[0]['dwtype']==5){
- $dwType = "02";
- }
- $shiLength = strlen($dwType.$deviceCode.$sendingInterval.$lowThreshold.$highThreshold)/2;
- $liuLength = dechex($shiLength);
- $length = str_pad($liuLength,4,0,STR_PAD_LEFT);
- //调用电信接口
- $token=json_decode(gettoken(),true);
- $a = "7470736c04".$length.$dwType.$deviceCode.$sendingInterval.$lowThreshold.$highThreshold;
- $s = pack('H*',$a);
- $t = crc166($s);
- $t = unpack("H*", $s.$t);
- $paras['value'] = $t[1]."696f74";
- // $t = crc16($paras['value'],0X1021,0Xffff,0X0000,false,false);
- $device = Db::name('device')->where('device_id', trim($longHuOwner[0]['owner_code']))->find();
- $sendata=[
- "appId"=>$this->appId,
- "deviceId"=>$device['iot_id'],// c448d5e3-2ec5-4e42-99d7-0ad27668614f 137effbe-4980-4719-a768-0ae301fc5000
- "command"=>[
- "serviceId"=>"topsailSensorData",
- "method"=>"SET_DEVICE_LEVEL",
- "paras"=>$paras
- ],
- "callbackUrl"=>"http://iot.usky.cn:80/ytapi/admin/Manage/status",
- ];
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
- $rescurl=curl($this->host."iocm/app/cmd/v1.4.0/deviceCommands",json_encode($sendata),$header);
- // var_dump($rescurl);
- Log::record('nbmanage1:' .$rescurl[0].$sendata['command']['paras']['value'] );
- if($rescurl[0]==201){//成功
- $res1=json_decode($rescurl[1],true);
- // var_dump($res1);
- }else{//失败
- // $res2=json_decode($res1[1],true);
- // return $this->error($rescurl[1]);
- Log::record('nbmanageerror:' .json_encode($this->error($rescurl[1])));
- $returnInformation['code'] = 60001;
- $returnInformation['msg'] = "操作失败,请联系管理员";
- return json_encode($returnInformation);
- }
- }else if($getdata['dataType']=="bendi"){
- $deviceCode = '';
- $deviceArr = str_split($getdata['IMEI'],1);
- for ($i=0;$i<count($deviceArr);$i++){
- $deviceCode.='3'.$deviceArr[$i];
- }
- $deviceCode = $deviceCode.'30';
- //调用电信接口
- $collectionInterval = "";//发送间隔
- if (!empty($getdata['collectionTime'])) {
- $liuCollectionTime1 = dechex($getdata['collectionTime']);
- $liuCollectionTime = str_pad($liuCollectionTime1,8,0,STR_PAD_LEFT);
- $collectionInterval = "0104".$liuCollectionTime;
- }
- $sendingInterval = "";//发送间隔
- if (!empty($getdata['sendTime'])) {
- $liuSendTime1 = dechex($getdata['sendTime']);
- $liuSendTime = str_pad($liuSendTime1,8,0,STR_PAD_LEFT);
- $sendingInterval = "0204".$liuSendTime;
- }
- $lowThreshold = "";//告警下限
- if (!empty($getdata['alarmLowerLimit'])) {
- $shiLowThreshold = $getdata['alarmLowerLimit']*1000;
- $liuLowThreshold1 = dechex($shiLowThreshold);
- $liuLowThreshold = str_pad($liuLowThreshold1,4,0,STR_PAD_LEFT);
- $lowThreshold = "0602".$liuLowThreshold;
- }
- $highThreshold = "";//告警上限
- if (!empty($getdata['alarmUpperLimit'])) {
- $shiHighThreshold = $getdata['alarmUpperLimit']*1000;
- $liuHighThreshold1 = dechex($shiHighThreshold);
- $liuHighThreshold = str_pad($liuHighThreshold1,4,0,STR_PAD_LEFT);
- $highThreshold = "0702".$liuHighThreshold;
- }
- if($getdata['deviceType']==2){
- $dwType = "01";
- }elseif ($getdata['deviceType']==5){
- $dwType = "02";
- }
- $shiLength = strlen($dwType.$deviceCode.$collectionInterval.$sendingInterval.$lowThreshold.$highThreshold)/2;
- $liuLength = dechex($shiLength);
- $length = str_pad($liuLength,4,0,STR_PAD_LEFT);
- //调用电信接口
- $token=json_decode(gettoken(),true);
- $a = "7470736c04".$length.$dwType.$deviceCode.$collectionInterval.$sendingInterval.$lowThreshold.$highThreshold;
- $s = pack('H*',$a);
- $t = crc166($s);
- $t = unpack("H*", $s.$t);
- $paras['value'] = $t[1]."696f74";
- $sendata=[
- "appId"=>$this->appId,
- "deviceId"=>$getdata['deviceId'],// c448d5e3-2ec5-4e42-99d7-0ad27668614f 137effbe-4980-4719-a768-0ae301fc5000
- "command"=>[
- "serviceId"=>"topsailSensorData",
- "method"=>"SET_DEVICE_LEVEL",
- "paras"=>$paras
- ],
- "callbackUrl"=>"http://iot.usky.cn:80/ytapi/admin/Manage/status",
- ];
- $header=[
- "Content-Type:application/json",
- "app_key:".$this->appId,
- "Authorization:Bearer ".$token["accessToken"]
- ];
- $rescurl=curl($this->host."iocm/app/cmd/v1.4.0/deviceCommands",json_encode($sendata),$header);
- var_dump($rescurl);
- Log::record('nbmanage1:' .$rescurl[0].$sendata['command']['paras']['value'] );
- if($rescurl[0]==201){//成功
- $res1=json_decode($rescurl[1],true);
- // var_dump($res1);
- }else{//失败
- // $res2=json_decode($res1[1],true);
- return $this->error($rescurl[1]);
- }
- }else{
- $returnInformation['code'] = 60001;
- $returnInformation['msg'] = "操作失败,请联系管理员";
- return json_encode($returnInformation);
- }
- }else{
- $returnInformation['code'] = 60001;
- $returnInformation['msg'] = "操作失败,请联系管理员";
- return json_encode($returnInformation);
- }
- //将请求的结果存入managelog中
- $list=array();
- $list['commandId']=$res1['commandId'];
- $list['deviceId']=$res1['deviceId'];
- $list['status']=$res1['status'];
- $list['command']= json_encode($res1['command'],320) ;
- if($getdata['dataType']=="setting"){
- $list['IMEI']=$longHuOwner[0]['owner_code'];
- }else{
- $list['IMEI']=$getdata['IMEI'];
- }
- $list['HexStr']="";
- $list['settype']="";
- $list['addtime']= date('Y-m-d H:i:s');
- $res = Db::name('managelog')->insertGetId($list);//将推送的数据存入总表
- if($res) {
- $returnInformation['code'] = 200;
- $returnInformation['msg'] = "操作成功";
- if($getdata['dataType']=="setting"){
- return json_encode($returnInformation);
- }else{
- return $this->success('添加成功','index');
- }
- }else{
- $returnInformation['code'] = 60001;
- $returnInformation['msg'] = "操作失败,请联系管理员";
- if($getdata['dataType']=="setting"){
- return json_encode($returnInformation);
- }else{
- return $this->error("添加失败,请稍后再试");
- }
- }
- }
- if($this->request->isGet()){
- $deviceId=input('deviceId');
- $IMEI=input('IMEI');
- //所有的服务类型
- $SetType=[
- [1,"读取模块信息"],[2,"读取参数"],[3,"设置IOT平台IP地址"],[4,"设置参数"],[5,"复位模块"],[6,"配置下行波特率"],
- [7,"读取下行波特率配置"],[8,"设置DTU自动采集指令"],[9,"读取DTU自动采集指令"],[10,"设置APN"],[11,"读取APN设置"],
- [12,"AT指令"]
- ];
- $this->assign('SetType',$SetType);
- $this->assign('deviceId',$deviceId);
- $this->assign('IMEI', $IMEI);
- return $this->fetch();
- }
- }
- public function status(){
- $data=input('');
-
- if(is_array($data)){
- $getdata= json_encode($data,320);
- $getdata=json_decode($getdata,true);//转成数组
- $list['type']=1;
-
- }else{
- $getdata=$data;
- $getdata=json_decode($getdata,true);//转成数组
- $list['type']=2;
- }
-
- $list=array();
- $list['deviceId']=$getdata['deviceId'];
- $list['commandId']=$getdata['commandId'];
- $list['resultCode']=$getdata['result']['resultCode'];
- $list['result']= json_encode($getdata['result'],320) ;
- $list['addtime']= date('Y-m-d H:i:s');
- $res = Db::name('managedata')->insertGetId($list);//将推送的数据存入总表
- echo 111;
- }
- public function delete(){
- echo 111;
- }
- }
|