Order.php.bak 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Controller;
  4. use think\Session;
  5. use think\Db;
  6. use think\Request;
  7. class Order extends Controller
  8. {
  9. private $host="https://180.101.147.89:8743/";
  10. private $appId="1w5JN7f6h0dNF8Ryz1q5QYYW_2Ia";
  11. private $secret="ErjDPl30eKVESOQLk2vpfVQjdpYa";
  12. public function index(){
  13. $data_list =Db::name('order')->paginate();
  14. $pages = $data_list->render();
  15. $this->assign('data_list', $data_list);
  16. $this->assign('pages', $pages);
  17. return $this->fetch();
  18. }
  19. public function add(){
  20. $res=Db::name('order')->where('notifyType',input('notifyType'))->find();
  21. if(!$res){
  22. if ($this->request->isPost()) {
  23. $data=$_POST;
  24. //验证数据
  25. if(empty($data['notifyType'])||empty($data['callbackUrl'])){
  26. return $this->error("请填写好必要内容");
  27. }
  28. $token=json_decode(gettoken(),true);
  29. $sendata=[
  30. "notifyType"=>$data['notifyType'],
  31. "callbackUrl"=>$data['callbackUrl'],
  32. "appId"=>$this->appId,
  33. ];
  34. $header=[
  35. "Content-Type:application/json",
  36. "app_key:".$this->appId,
  37. "Authorization:Bearer ".$token["accessToken"]
  38. ];
  39. //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag}
  40. $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header);
  41. dump($rescurl);
  42. if($rescurl[0]==201){//成功
  43. $res1=json_decode($rescurl[1],true);
  44. }else{//失败
  45. // $res2=json_decode($res1[1],true);
  46. return $this->error($rescurl[1]);
  47. }
  48. //添加
  49. $list=array();
  50. $list['notifyType']= $data['notifyType'];
  51. $list['callbackUrl']= $data['callbackUrl'];
  52. $list['subscriptionId']= $res1['subscriptionId'];
  53. $list['addtime']= date('Y-m-d H:i:s');
  54. $list['updatetime']= date('Y-m-d H:i:s');
  55. $res = Db::name('order')->insertGetId($list);
  56. if($res) {
  57. return $this->success('添加成功','index');
  58. }else{
  59. return $this->error("添加失败,请稍后再试");
  60. }
  61. }
  62. }else{
  63. return $this->error("该订阅类型已经填写");
  64. }
  65. $notifyType=Db::table('notifyType')->select();
  66. $this->assign('notifyType', $notifyType);
  67. return $this->fetch();
  68. }
  69. /* public function edit(){
  70. if ($this->request->isPost()) {
  71. $data=$_POST;
  72. //验证数据
  73. if(empty($data['callbackUrl'])){
  74. return $this->error("请填写好必要内容");
  75. }
  76. $token=json_decode(gettoken(),true);
  77. $sendata=[
  78. "notifyType"=>$data['notifyType'],
  79. "callbackUrl"=>$data['callbackUrl'],
  80. "appId"=>$this->appId,
  81. ];
  82. $header=[
  83. "Content-Type:application/json",
  84. "app_key:".$this->appId,
  85. "Authorization:Bearer ".$token["accessToken"]
  86. ];
  87. //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag}
  88. $res1=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header);
  89. $res1=json_decode($res1,true);
  90. $list=array();
  91. $list['notifyType']= $data['notifyType'];
  92. $list['callbackUrl']= $data['callbackUrl'];
  93. $list['subscriptionId']= $res1['subscriptionId'];
  94. $list['updatetime']= date('Y-m-d H:i:s');
  95. $res = Db::name('order')->where('id',$data['id'])->update($list);
  96. if($res) {
  97. return $this->success('编辑成功','index');
  98. }else{
  99. return $this->error("编辑失败,请稍后再试");
  100. }
  101. }
  102. $notifyType=Db::table('notifyType')->select();
  103. $this->assign('notifyType', $notifyType);
  104. $res=Db::name('order')->where('id',input('id'))->find();
  105. $this->assign('data', $res);
  106. return $this->fetch();
  107. }*/
  108. //删除订阅
  109. public function delete(){
  110. if ($this->request->isPost()) {
  111. $data=$_POST;
  112. $token=json_decode(gettoken(),true);
  113. $header=[
  114. "Content-Type:application/json",
  115. "app_key:".$this->appId,
  116. "Authorization:Bearer ".$token["accessToken"]
  117. ];
  118. //https://server:port/iocm/app/sub/v1.2.0/subscriptions/{subscriptionId}
  119. $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions/".$data['subscriptionId'],"",$header,"DELETE");
  120. if($rescurl[0]==204){//成功
  121. Db::name('order')->where('subscriptionId',$data['subscriptionId'])->delete();
  122. return "1111";
  123. //将数据库的订阅数据也删掉
  124. }else{//失败
  125. return $this->error($rescurl[1]);//
  126. }
  127. }
  128. }
  129. public function adddata(){
  130. if ($this->request->isPost()) {
  131. $data=input('');
  132. /* $rand=rand(0,2);
  133. $header=["Content-Type:application/json"];
  134. //将数据分配给不同的服务
  135. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  136. $server=$httpserver[$rand];
  137. //将数据随机转发到别的平台做处理
  138. $res=curltest($server,$data,$header);
  139. */
  140. $list=array();
  141. if(is_array($data)){
  142. $list['data']= json_encode($data,320);
  143. $getdata=json_decode($list['data'],true);//转成数组
  144. $list['type']=1;
  145. }else{
  146. $list['data']=$data;
  147. $getdata=json_decode($list['data'],true);//转成数组
  148. $list['type']=2;
  149. }
  150. // $list['data']=11;
  151. $list['response']= "111";
  152. $list['notifyType']=$getdata["notifyType"];
  153. $list['deviceId']=$getdata["deviceId"];
  154. $list['addtime']= date('Y-m-d H:i:s');
  155. $res = Db::name('noticedata')->insertGetId($list);//将推送的数据存入总表
  156. //在详细表里插入数据
  157. //根据设备id查找到device里的device_id
  158. if(trim($getdata["notifyType"])=="deviceDataChanged"){
  159. $device=Db::name('device')->where('iot_id',trim($getdata["deviceId"]))->find();
  160. $datainfo=array();
  161. $datainfo['notifyType']=$getdata["notifyType"];
  162. $datainfo['device_id']=$device["device_id"];
  163. $datainfo['iot_id']=$getdata["deviceId"];
  164. $datainfo['service']=json_encode($getdata["service"]);
  165. $datainfo['addtime']=date('Y-m-d H:i:s');
  166. $res1 = Db::table('dev_'.$device["device_id"])->insertGetId($datainfo);
  167. }
  168. }
  169. return json_encode($data,320);
  170. }
  171. public function testdata1(){
  172. $rand=rand(0,2);
  173. $data=[
  174. '{"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"}}',
  175. '{"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"}}',
  176. '{"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"}}',
  177. ];
  178. $header=["Content-Type:application/json"];
  179. //将数据分配给不同的服务
  180. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  181. $senddata=$data[$rand];
  182. $server=$httpserver[$rand];
  183. $res=curltest($server,$senddata,$header);
  184. dump($res);
  185. /* //设备所属公司
  186. $owner=Db::name('device')->field("owner_id")->where('iot_id',$iot_id)->find();
  187. $ownerhost=Db::name('owner')->where('id',$owner["owner_id"])->find();
  188. $ipaddr=$ownerhost['host'].":".$ownerhost['port'];
  189. $res=curltest($ipaddr,$senddata);
  190. dump($res);*/
  191. //将数据转发到不同的接口中
  192. /* $sendapi="senddata".$rand;
  193. echo $this->$sendapi($data[$rand],$devies[$rand]);*/
  194. }
  195. public function senddata0($data){
  196. //将数据存到总表
  197. //将数据存到设备详细表
  198. //查看当前设备属于哪家业主
  199. //查找业主的服务器
  200. //给服务器发数据
  201. }
  202. public function senddata1($data){
  203. echo "m2".$data;
  204. }
  205. public function senddata2($data){
  206. echo "m3".$data;
  207. }
  208. }