paginate(); $pages = $data_list->render(); $this->assign('data_list', $data_list); $this->assign('pages', $pages); return $this->fetch(); } public function add(){ $res=Db::name('order')->where('notifyType',input('notifyType'))->find(); if(!$res){ if ($this->request->isPost()) { $data=$_POST; //验证数据 if(empty($data['notifyType'])||empty($data['callbackUrl'])){ return $this->error("请填写好必要内容"); } $token=json_decode(gettoken(),true); $sendata=[ "notifyType"=>$data['notifyType'], "callbackUrl"=>$data['callbackUrl'], "appId"=>$this->appId, ]; $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag} $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header); dump($rescurl); if($rescurl[0]==201){//成功 $res1=json_decode($rescurl[1],true); }else{//失败 // $res2=json_decode($res1[1],true); return $this->error($rescurl[1]); } //添加 $list=array(); $list['notifyType']= $data['notifyType']; $list['callbackUrl']= $data['callbackUrl']; $list['subscriptionId']= $res1['subscriptionId']; $list['addtime']= date('Y-m-d H:i:s'); $list['updatetime']= date('Y-m-d H:i:s'); $res = Db::name('order')->insertGetId($list); if($res) { return $this->success('添加成功','index'); }else{ return $this->error("添加失败,请稍后再试"); } } }else{ return $this->error("该订阅类型已经填写"); } $notifyType=Db::table('notifyType')->select(); $this->assign('notifyType', $notifyType); return $this->fetch(); } /* public function edit(){ if ($this->request->isPost()) { $data=$_POST; //验证数据 if(empty($data['callbackUrl'])){ return $this->error("请填写好必要内容"); } $token=json_decode(gettoken(),true); $sendata=[ "notifyType"=>$data['notifyType'], "callbackUrl"=>$data['callbackUrl'], "appId"=>$this->appId, ]; $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag} $res1=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header); $res1=json_decode($res1,true); $list=array(); $list['notifyType']= $data['notifyType']; $list['callbackUrl']= $data['callbackUrl']; $list['subscriptionId']= $res1['subscriptionId']; $list['updatetime']= date('Y-m-d H:i:s'); $res = Db::name('order')->where('id',$data['id'])->update($list); if($res) { return $this->success('编辑成功','index'); }else{ return $this->error("编辑失败,请稍后再试"); } } $notifyType=Db::table('notifyType')->select(); $this->assign('notifyType', $notifyType); $res=Db::name('order')->where('id',input('id'))->find(); $this->assign('data', $res); return $this->fetch(); }*/ //删除订阅 public function delete(){ if ($this->request->isPost()) { $data=$_POST; $token=json_decode(gettoken(),true); $header=[ "Content-Type:application/json", "app_key:".$this->appId, "Authorization:Bearer ".$token["accessToken"] ]; //https://server:port/iocm/app/sub/v1.2.0/subscriptions/{subscriptionId} $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions/".$data['subscriptionId'],"",$header,"DELETE"); if($rescurl[0]==204){//成功 Db::name('order')->where('subscriptionId',$data['subscriptionId'])->delete(); return "1111"; //将数据库的订阅数据也删掉 }else{//失败 return $this->error($rescurl[1]);// } } } public function adddata(){ if ($this->request->isPost()) { $data=input(''); /* $rand=rand(0,2); $header=["Content-Type:application/json"]; //将数据分配给不同的服务 $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"]; $server=$httpserver[$rand]; //将数据随机转发到别的平台做处理 $res=curltest($server,$data,$header); */ $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; } // $list['data']=11; $list['response']= "111"; $list['notifyType']=$getdata["notifyType"]; $list['deviceId']=$getdata["deviceId"]; $list['addtime']= date('Y-m-d H:i:s'); $res = Db::name('noticedata')->insertGetId($list);//将推送的数据存入总表 //在详细表里插入数据 //根据设备id查找到device里的device_id if(trim($getdata["notifyType"])=="deviceDataChanged"){ $device=Db::name('device')->where('iot_id',trim($getdata["deviceId"]))->find(); $datainfo=array(); $datainfo['notifyType']=$getdata["notifyType"]; $datainfo['device_id']=$device["device_id"]; $datainfo['iot_id']=$getdata["deviceId"]; $datainfo['service']=json_encode($getdata["service"]); $datainfo['addtime']=date('Y-m-d H:i:s'); $res1 = Db::table('dev_'.$device["device_id"])->insertGetId($datainfo); } } return json_encode($data,320); } public function testdata1(){ $rand=rand(0,2); $data=[ '{"notifyType":"deviceDataChanged","deviceId":"a1d88da9-5156-4507-995a-bb20af09001a","gatewayId":"a1d88da9-5156-4507-995a-bb20af09001a","requestId":null,"service":{"serviceId":"hxkcnbdtu","serviceType":"hxkcnbdtu","data":{"deviceAddress":"868744030057305","framenumber":0,"framenum":0,"frameser":0,"voltage":3.6,"rsrp":-99.9,"snr":67,"ecl":0,"csq":11,"cellid":"0125145939","reportway":0,"cycle":1600,"setmode":3,"settime":"3-00","freezday":"28-02","nbtime":"18-09-21 10:30:15","pressure":0.7455,"alarm_en":0,"alarm_time":60,"down_alarm_pre":0,"up_alarm_pre":0,"pre_status":0,"log":"7E64E7834300000011395914250100064003030028021809211030151811112222011F1D0000003C00000000000000000000000000","eventTime":"2018-09-21 10:30:17"},"eventTime":"20180921T023017Z"}}', '{"notifyType":"deviceDataChanged","deviceId":"a1d88da9-5156-4507-995a-bb20af09001a","gatewayId":"a1d88da9-5156-4507-995a-bb20af09001a","requestId":null,"service":{"serviceId":"hxkcnbdtu","serviceType":"hxkcnbdtu","data":{"deviceAddress":"868744030057305","framenumber":0,"framenum":0,"frameser":0,"voltage":3.6,"rsrp":-99.9,"snr":67,"ecl":0,"csq":11,"cellid":"0125145939","reportway":0,"cycle":1600,"setmode":3,"settime":"3-00","freezday":"28-02","nbtime":"18-09-21 10:30:15","pressure":0.7455,"alarm_en":0,"alarm_time":60,"down_alarm_pre":0,"up_alarm_pre":0,"pre_status":0,"log":"7E64E7834300000011395914250100064003030028021809211030151811112222011F1D0000003C00000000000000000000000000","eventTime":"2018-09-21 10:30:17"},"eventTime":"20180921T023017Z"}}', '{"notifyType":"deviceDataChanged","deviceId":"a1d88da9-5156-4507-995a-bb20af09001a","gatewayId":"a1d88da9-5156-4507-995a-bb20af09001a","requestId":null,"service":{"serviceId":"hxkcnbdtu","serviceType":"hxkcnbdtu","data":{"deviceAddress":"868744030057305","framenumber":0,"framenum":0,"frameser":0,"voltage":3.6,"rsrp":-99.9,"snr":67,"ecl":0,"csq":11,"cellid":"0125145939","reportway":0,"cycle":1600,"setmode":3,"settime":"3-00","freezday":"28-02","nbtime":"18-09-21 10:30:15","pressure":0.7455,"alarm_en":0,"alarm_time":60,"down_alarm_pre":0,"up_alarm_pre":0,"pre_status":0,"log":"7E64E7834300000011395914250100064003030028021809211030151811112222011F1D0000003C00000000000000000000000000","eventTime":"2018-09-21 10:30:17"},"eventTime":"20180921T023017Z"}}', ]; $header=["Content-Type:application/json"]; //将数据分配给不同的服务 $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"]; $senddata=$data[$rand]; $server=$httpserver[$rand]; $res=curltest($server,$senddata,$header); dump($res); /* //设备所属公司 $owner=Db::name('device')->field("owner_id")->where('iot_id',$iot_id)->find(); $ownerhost=Db::name('owner')->where('id',$owner["owner_id"])->find(); $ipaddr=$ownerhost['host'].":".$ownerhost['port']; $res=curltest($ipaddr,$senddata); dump($res);*/ //将数据转发到不同的接口中 /* $sendapi="senddata".$rand; echo $this->$sendapi($data[$rand],$devies[$rand]);*/ } public function senddata0($data){ //将数据存到总表 //将数据存到设备详细表 //查看当前设备属于哪家业主 //查找业主的服务器 //给服务器发数据 } public function senddata1($data){ echo "m2".$data; } public function senddata2($data){ echo "m3".$data; } }