123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- <?php
- namespace app\admin\controller;
- use think\Controller;
- use think\Session;
- use think\Db;
- use think\Request;
- class Order extends Controller
- {
- private $host="https://180.101.147.89:8743/";
- private $appId="1w5JN7f6h0dNF8Ryz1q5QYYW_2Ia";
- private $secret="ErjDPl30eKVESOQLk2vpfVQjdpYa";
-
- public function index(){
- $data_list =Db::name('order')->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;
- }
- }
|