Order.php 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  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 Order 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::name('order')->paginate();
  15. $pages = $data_list->render();
  16. $this->assign('data_list', $data_list);
  17. $this->assign('pages', $pages);
  18. return $this->fetch();
  19. }
  20. public function add(){
  21. $res=Db::name('order')->where('notifyType',input('notifyType'))->find();
  22. if(!$res){
  23. if ($this->request->isPost()) {
  24. $data=$_POST;
  25. //验证数据
  26. if(empty($data['notifyType'])||empty($data['callbackUrl'])){
  27. return $this->error("请填写好必要内容");
  28. }
  29. $token=json_decode(gettoken(),true);
  30. $sendata=[
  31. "notifyType"=>$data['notifyType'],
  32. "callbackUrl"=>$data['callbackUrl'],
  33. "appId"=>$this->appId,
  34. ];
  35. $header=[
  36. "Content-Type:application/json",
  37. "app_key:".$this->appId,
  38. "Authorization:Bearer ".$token["accessToken"]
  39. ];
  40. //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag}
  41. $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header);
  42. dump($rescurl);
  43. if($rescurl[0]==201){//成功
  44. $res1=json_decode($rescurl[1],true);
  45. }else{//失败
  46. // $res2=json_decode($res1[1],true);
  47. return $this->error($rescurl[1]);
  48. }
  49. //添加
  50. $list=array();
  51. $list['notifyType']= $data['notifyType'];
  52. $list['callbackUrl']= $data['callbackUrl'];
  53. $list['subscriptionId']= $res1['subscriptionId'];
  54. $list['addtime']= date('Y-m-d H:i:s');
  55. $list['updatetime']= date('Y-m-d H:i:s');
  56. $res = Db::name('order')->insertGetId($list);
  57. if($res) {
  58. return $this->success('添加成功','index');
  59. }else{
  60. return $this->error("添加失败,请稍后再试");
  61. }
  62. }
  63. }else{
  64. return $this->error("该订阅类型已经填写");
  65. }
  66. $notifyType=Db::table('notifyType')->select();
  67. $this->assign('notifyType', $notifyType);
  68. return $this->fetch();
  69. }
  70. /* public function edit(){
  71. if ($this->request->isPost()) {
  72. $data=$_POST;
  73. //验证数据
  74. if(empty($data['callbackUrl'])){
  75. return $this->error("请填写好必要内容");
  76. }
  77. $token=json_decode(gettoken(),true);
  78. $sendata=[
  79. "notifyType"=>$data['notifyType'],
  80. "callbackUrl"=>$data['callbackUrl'],
  81. "appId"=>$this->appId,
  82. ];
  83. $header=[
  84. "Content-Type:application/json",
  85. "app_key:".$this->appId,
  86. "Authorization:Bearer ".$token["accessToken"]
  87. ];
  88. //https://server:port/iocm/app/sub/v1.2.0/subscriptions?ownerFlag={ownerFlag}
  89. $res1=curl($this->host."iocm/app/sub/v1.2.0/subscriptions",json_encode($sendata),$header);
  90. $res1=json_decode($res1,true);
  91. $list=array();
  92. $list['notifyType']= $data['notifyType'];
  93. $list['callbackUrl']= $data['callbackUrl'];
  94. $list['subscriptionId']= $res1['subscriptionId'];
  95. $list['updatetime']= date('Y-m-d H:i:s');
  96. $res = Db::name('order')->where('id',$data['id'])->update($list);
  97. if($res) {
  98. return $this->success('编辑成功','index');
  99. }else{
  100. return $this->error("编辑失败,请稍后再试");
  101. }
  102. }
  103. $notifyType=Db::table('notifyType')->select();
  104. $this->assign('notifyType', $notifyType);
  105. $res=Db::name('order')->where('id',input('id'))->find();
  106. $this->assign('data', $res);
  107. return $this->fetch();
  108. }*/
  109. //删除订阅
  110. public function delete(){
  111. if ($this->request->isPost()) {
  112. $data=$_POST;
  113. $token=json_decode(gettoken(),true);
  114. $header=[
  115. "Content-Type:application/json",
  116. "app_key:".$this->appId,
  117. "Authorization:Bearer ".$token["accessToken"]
  118. ];
  119. //https://server:port/iocm/app/sub/v1.2.0/subscriptions/{subscriptionId}
  120. $rescurl=curl($this->host."iocm/app/sub/v1.2.0/subscriptions/".$data['subscriptionId'],"",$header,"DELETE");
  121. if($rescurl[0]==204){//成功
  122. Db::name('order')->where('subscriptionId',$data['subscriptionId'])->delete();
  123. return "1111";
  124. //将数据库的订阅数据也删掉
  125. }else{//失败
  126. return $this->error($rescurl[1]);//
  127. }
  128. }
  129. }
  130. public function adddata(){
  131. if ($this->request->isPost()) {
  132. $data = input('');
  133. /* $rand=rand(0,2);
  134. $header=["Content-Type:application/json"];
  135. //将数据分配给不同的服务
  136. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  137. $server=$httpserver[$rand];
  138. //将数据随机转发到别的平台做处理
  139. $res=curltest($server,$data,$header);
  140. */
  141. $list = array();
  142. if (is_array($data)) {
  143. $list['data'] = json_encode($data, 320);
  144. $getdata = json_decode($list['data'], true);//转成数组
  145. $list['type'] = 1;
  146. } else {
  147. $list['data'] = $data;
  148. $getdata = json_decode($list['data'], true);//转成数组
  149. $list['type'] = 2;
  150. }
  151. // $list['data']=11;
  152. $list['response'] = "111";
  153. $list['notifyType'] = $getdata["notifyType"];
  154. $list['deviceId'] = $getdata["deviceId"];
  155. $list['addtime'] = date('Y-m-d H:i:s');
  156. $res = Db::name('noticedata')->insertGetId($list);//将推送的数据存入总表
  157. Log::record('deviceId' . ':' . $getdata["service"]["eventTime"]);
  158. //在详细表里插入数据
  159. //根据设备id查找到device里的device_id
  160. if (trim($getdata["notifyType"]) == "deviceDataChanged") {
  161. $device = Db::name('device')->where('iot_id', trim($getdata["deviceId"]))->find();
  162. $datainfo = array();
  163. $datainfo['notifyType'] = $getdata["notifyType"];
  164. $datainfo['device_id'] = $device["device_id"];
  165. $datainfo['iot_id'] = $getdata["deviceId"];
  166. $datainfo['service'] = json_encode($getdata["service"]);
  167. $datainfo['addtime'] = date('Y-m-d H:i:s');
  168. $res1 = Db::table('dev_' . $device["device_id"])->insertGetId($datainfo);
  169. $data2=array();
  170. $data2['devicestatus'] = 0;
  171. $data2['true_status'] = '0';
  172. $data2['lastcommtime'] = date('Y-m-d H:i:s');
  173. $sql2="UPDATE sp_devices_status SET statustime = {$data2['lastcommtime']} devicestatus = {$data2['devicestatus']},true_status={$data2['true_status']},lastcommtime='{$data2['lastcommtime']}' WHERE deviceid='{$device['device_id']}'";
  174. $res2 = addDataInfo($sql2);
  175. if ($device['deviceType']=='Smoke') {
  176. $data3 = array();
  177. $data3['device_id'] = $device["device_id"];
  178. $data3['RSRQ'] = $getdata["service"]['data']['RSRQ'];
  179. $data3['CSQ'] = $getdata["service"]['data']["CSQ"];
  180. $data3['DeviceType'] = $getdata["service"]['data']["DeviceType"];
  181. $data3['DeviceStatus'] = $getdata["service"]['data']["DeviceStatus"];
  182. $data3['BatteryLevel'] = $getdata["service"]['data']["BatteryLevel"];
  183. $data3['SensorLevel'] = $getdata["service"]['data']["SensorLevel"];
  184. $data3['eventTime'] = $getdata["service"]["eventTime"];
  185. $data3['storeTime'] = date('Y-m-d H:i:s');
  186. $sql = "INSERT INTO sp_y{$data3['device_id']} VALUES ('','{$data3['device_id']}',
  187. '{$data3['RSRQ']}', '{$data3['CSQ']}', '{$data3['DeviceType']}','{$data3['DeviceStatus']}','{$data3['BatteryLevel']}','{$data3['SensorLevel']}','{$data3['eventTime']}','{$data3['storeTime']}')";
  188. $res3 = addDataInfo($sql);
  189. } elseif($device['deviceType']=='MultiSensor') {
  190. $data3 = array();
  191. $data3['device_id'] = $device["device_id"];
  192. $data3['port'] = 'tp_water_pressure';
  193. $data3['storeTime'] = date('Y-m-d H:i:s');
  194. $data3['unit'] =$getdata["service"]['data']["unit"];
  195. // $data3['messageId'] = $getdata["service"]['data']["messageId"];
  196. $data3['batteyValue'] = $getdata["service"]['data']["batteyValue"];
  197. $data3['alarmBoolean'] = $getdata["service"]['data']["alarmBoolean"];
  198. $unit=dechex($getdata["service"]['data']["unit"]);
  199. Log::record('sql1234'.':'.$unit);
  200. $sql11="SELECT dwtype FROM sp_owner WHERE owner_code ='{$device["device_id"]}'";
  201. $dwtype = add($sql11);
  202. $arr_y=array('10','11','20','21','22');
  203. if ($dwtype[0]['dwtype'] == 5){
  204. if ($data3['unit']== 1){
  205. $data3['data1']='LL2';
  206. }elseif($data3['unit']== 2){
  207. $data3['data1']='LL1';
  208. }else{
  209. $data3['data1']='LL0';
  210. }
  211. }else {
  212. if ($data3['unit']== 1){
  213. $data3['data1']='WP2';
  214. }elseif($data3['unit']== 2){
  215. $data3['data1']='WP1';
  216. }else{
  217. $data3['data1']='WP0';
  218. }
  219. }
  220. switch ($unit){
  221. case '10':
  222. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  223. break;
  224. case '11':
  225. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  226. break;
  227. case '20':
  228. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  229. break;
  230. case '21':
  231. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  232. break;
  233. case '22':
  234. $data3['data4'] = $getdata["service"]['data']["data"];
  235. break;
  236. case '30':
  237. $data3['data4'] = $getdata["service"]['data']["data"]*0.0000001;
  238. break;
  239. case '31':
  240. $data3['data4'] = $getdata["service"]['data']["data"]*0.000001;
  241. break;
  242. case '40':
  243. $data3['data4'] = $getdata["service"]['data']["data"]*0.00001;
  244. break;
  245. case '41':
  246. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  247. break;
  248. case '42':
  249. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  250. break;
  251. case '50':
  252. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  253. break;
  254. case '51':
  255. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  256. break;
  257. case '52':
  258. $data3['data4'] = $getdata["service"]['data']["data"];
  259. break;
  260. case '2':
  261. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  262. break;
  263. case '1':
  264. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  265. break;
  266. }
  267. $data3['data'] = $getdata["service"]['data']["data"];
  268. $sql = "INSERT INTO sp_d{$data3['device_id']} VALUES ('','{$data3['device_id']}','{$data3['port']}','{$data3['storeTime']}',
  269. '','','','{$data3['data1']}','{$data3['batteyValue']}','5','{$data3['data4']}','11111111111')";
  270. // Log::record('sql1234'.':'.$sql);
  271. $res4 = addDataInfo($sql);
  272. }elseif ($device['deviceType']=='WaterMeter'){
  273. //宇洁容情
  274. Log::record('xinshuibiaozhi'.':'.$getdata["service"]['data']['data']);
  275. $data3 = array();
  276. $data3['device_id'] = $device["device_id"];
  277. $data3['port'] = 'tp_water_pressure';
  278. $data3['storeTime'] = date('Y-m-d H:i:s');
  279. $data3['batteyValue'] = dechex($getdata["service"]['data']["battery"]);
  280. $data3['unit'] =dechex($getdata["service"]['data']['data']);
  281. $data3['device_type'] =dechex($getdata["service"]['data']['device_type']);
  282. $data3['address'] = '180.101.147.89';
  283. $data3['alarmBoolean'] = $getdata["service"]['data']["alarm"];
  284. $alarm=dechex($data3['alarmBoolean']);
  285. $data3['sign'] = round($getdata["service"]['data']["signal"]/6);
  286. $data3['coeffice'] = dechex($getdata["service"]['data']["coeffice"]);
  287. if ($data3['alarmBoolean']!== 0){
  288. if ($data3['alarmBoolean']== 161){
  289. $data3['data1']='WP1';
  290. }else{
  291. $data3['data1']='WP2';
  292. }
  293. }else {
  294. $data3['data1'] = 'WP0';
  295. }
  296. if ($data3['device_type']=='ae'){
  297. switch ($data3['coeffice']){
  298. case 'd3':
  299. $data3['data4'] = $data3['unit']*0.001;
  300. break;
  301. case 'd2':
  302. $data3['data4'] = $data3['unit']*0.01;
  303. break;
  304. case 'd1':
  305. $data3['data4'] = $data3['unit']*0.1;
  306. break;
  307. case 'e0':
  308. $data3['data4'] = $data3['unit'];
  309. break;
  310. case 'e1':
  311. $data3['data4'] = $data3['unit']*10;
  312. break;
  313. case 'e2':
  314. $data3['data4'] = $data3['unit']*100;
  315. break;
  316. }
  317. $sql = "INSERT INTO sp_d{$data3['device_id']} VALUES ('','{$data3['device_id']}','{$data3['port']}','{$data3['storeTime']}',
  318. '','','','{$data3['data1']}','{$data3['batteyValue']}','{$data3['sign']}','{$data3['data4']}','11111111111')";
  319. $res5 = addDataInfo($sql);
  320. if ($data3['alarmBoolean']!== 0){
  321. $sql = "INSERT INTO sp_sj2017 VALUES ('','tp_water_pressure','{$data3['device_id']}','{$data3['storeTime']}','$alarm','{$data3['address']}', ''
  322. ,'{$data3['data1']}','{$data3['batteyValue']}','{$data3['sign']}','{$data3['data4']}','','','','','','','','','','')";
  323. $insert_id = addDataInfo($sql);
  324. // $sql66="update sp_devices_status set statustime=".$data3['storeTime'];
  325. Log::record('yujie'.':'.$insert_id);
  326. // $update_id = addDataInfo($sql66);
  327. $insert_id_sql="select id from sp_sj2017 where device_code='".$data3['device_id']."' and time='".$data3['storeTime']."'";
  328. Log::record('sql987'.':'.$insert_id_sql);
  329. $insert_id66=add($insert_id_sql);
  330. Log::record('sql253232'.':'.$insert_id66[0]['id']);
  331. if ($data3['alarmBoolean']== 161){
  332. $msg['evt'] = "WP1";
  333. $msg['msg'] = "低压";
  334. }else{ // high
  335. $msg['evt'] = "WP2";
  336. $msg['msg'] = "高压";
  337. }
  338. if ( $msg['evt']=='WP1' || $msg['evt']=='WP2' ) {
  339. $sql3="select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='{$device["device_id"]}'";
  340. $company = add($sql3);
  341. $sql4="select phone from sp_owner_phone WHERE owner_code='".$company[0]['company']."' AND data2 like '%3%' union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='".$company[0]['company']."') AND data2 like '%3%'";
  342. $union_sql=add($sql4);
  343. // Log::record('sql12345'.':'.$sql4);
  344. $openids=$this->array_column1($union_sql,"phone");
  345. if (empty($openids))
  346. {
  347. echo 'open is null';
  348. exit;
  349. }
  350. // Log::record('data666_phone'.':'.$openids);
  351. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  352. $op=add($sql5);
  353. $access_token=GetAccess_Token();
  354. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  355. $deviceid=$device["device_id"];
  356. $type=$msg['msg'];
  357. $time=$data3['storeTime'];
  358. $remark='设备名称:'.$company[0]['owner_name'];
  359. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  360. Log::record('data666_url_wx_push2'.':'. $url_wx_push2);
  361. $data666['IMEI']=$device["device_id"];
  362. Log::record('data666_IMET'.':'.$data666['IMEI']);
  363. $data666['phone']=$openids;
  364. Log::record('data666_phone'.':'.$data666['phone']);
  365. $data666['evt']=$data3['data1'];
  366. Log::record('data666_evt'.':'.$data666['evt']);
  367. $data666['insert_id']=$insert_id66[0]['id'];
  368. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  369. $data666['time']=$data3['storeTime'];
  370. // Log::record('data666'.':'.$data666);
  371. $sql88="UPDATE sp_devices_status SET statustime ='".$data3['storeTime']."' where deviceid='".$data666['IMEI']."'";
  372. addDataInfo($sql88);
  373. $this->curl($url_wx_push2,$data666);
  374. $waterData["SubType"] = 2;
  375. $waterData["DeviceId"] = $data666['IMEI'];
  376. $waterData["InsertId"] = $insert_id66[0]['id'];
  377. $waterData["Confirmed"] = false;
  378. $data6 = json_encode($waterData);
  379. $rep_url = "http://47.98.201.187:55335/report";
  380. $res = $this->curl($rep_url, $data6);
  381. // $url_wx_push2 = "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Waterwarn/bj";
  382. foreach($op as $key => $o){
  383. if (!empty($o['openid']))
  384. {
  385. // $data88['openid']=$o['openid'];
  386. // $data88['address']=$address;
  387. // $data88['deviceid']=$deviceid;
  388. // $data88['type']=$type;
  389. // $data88['time']=$time;
  390. // $data88['remark']=$remark;
  391. //token,微信id,地址,设备号,类型,时间,设备名称(或者其他内容)
  392. //{{first.DATA}}$address
  393. //设备号:{{keyword1.DATA}}$deviceid
  394. //报警类型:{{keyword2.DATA}}$type
  395. //时间:{{keyword3.DATA}}$time
  396. //{{remark.DATA}}$remark
  397. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  398. // s_curl($url_wx_push2,$data88);
  399. $Message['openid']=$o['openid'];
  400. $arr[$key]=$Message;
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }elseif ($device['deviceType']=='topsailSensor'){
  407. //拓普索尔
  408. Log::record('tuopusuoershuibiao'.':'.$getdata["service"]['data']['Imei']);
  409. $data3 = array();
  410. $data4 = array();
  411. $data5 = array();
  412. $sensorList = array();
  413. $sensorListArray = array();
  414. $data4['provider'] = 'Usky';
  415. $data3['device_id'] = $device["device_id"];
  416. $data3['port'] = 'TuoPuSuoEr-TSM-04P';
  417. $data3['storeTime'] = date('Y-m-d H:i:s');
  418. $data3['batteyValue'] = $getdata["service"]['data']["batteryLevel"];
  419. $data3['data'] =substr($getdata["service"]['data']["sampleData"],-5);
  420. $data3['unit'] =substr($getdata["service"]['data']["sampleData"],0,1);
  421. $data3['device_type'] =$getdata["service"]['data']['deviceType'];
  422. $data3['sendTime'] =$getdata["service"]['data']['sendTime'];
  423. $data3['address'] = '180.101.147.89';
  424. $data3['alarmBoolean'] = $getdata["service"]['data']["alarmType"];
  425. $data3['sign'] = round($getdata["service"]['data']["signalStrength"]/6);
  426. $sql6="select * from sp_owner where owner_code ='{$device["device_id"]}'";
  427. $longHuCompany = add($sql6);
  428. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  429. if($data3['device_type']==1 || $data3['device_type']==4){
  430. if ($data3['alarmBoolean']!== '0'){
  431. if ($data3['alarmBoolean']== '1'){
  432. $data3['data1']='WP1';
  433. $sensorList['status'] = 3;
  434. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力低于下限';
  435. $topic = 'mh/water/alert';
  436. $ytTMhwater = Db::name('yt_t_mhwater')->where('device_code', trim($device["device_id"]))->find();
  437. if (!empty($ytTMhwater)) {
  438. $mhMqtt['deviceCode']=$ytTMhwater['mh_device_code'];
  439. $mhMqtt['addr']='上海市';
  440. $mhMqtt['eventCode1']='02';
  441. $mhMqtt['eventName1']='预警事件';
  442. $mhMqtt['eventCode2']='02008';
  443. $mhMqtt['eventName2']='水压报警';
  444. $mhMqtt['description']='低压';
  445. $mhMqtt['lon']=(double)$ytTMhwater['longitude'];
  446. $mhMqtt['lat']=(double)$ytTMhwater['latitude'];
  447. $mhMqtt['createdTime']=time();
  448. $param = ["platform_cy"=>$ytTMhwater['platform_cy'],"scenetype_cy"=>$ytTMhwater['scenetype_cy']];
  449. $mhMqtt['param']=$param;
  450. $mhMqttJson = json_encode($mhMqtt);
  451. $this->message_push_mh($topic,$mhMqttJson,0);
  452. }
  453. }elseif ($data3['alarmBoolean']== '2'){
  454. $data3['data1']='WP2';
  455. $sensorList['status'] = 4;
  456. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力超过上限';
  457. $topic = 'mh/water/alert';
  458. $ytTMhwater = Db::name('yt_t_mhwater')->where('device_code', trim($device["device_id"]))->find();
  459. if (!empty($ytTMhwater)) {
  460. $mhMqtt['deviceCode']=$ytTMhwater['mh_device_code'];
  461. $mhMqtt['addr']='上海市';
  462. $mhMqtt['eventCode1']='02';
  463. $mhMqtt['eventName1']='预警事件';
  464. $mhMqtt['eventCode2']='02008';
  465. $mhMqtt['eventName2']='水压报警';
  466. $mhMqtt['description']='高压';
  467. $mhMqtt['lon']=(double)$ytTMhwater['longitude'];
  468. $mhMqtt['lat']=(double)$ytTMhwater['latitude'];
  469. $mhMqtt['createdTime']=time();
  470. $param = ["platform_cy"=>$ytTMhwater['platform_cy'],"scenetype_cy"=>$ytTMhwater['scenetype_cy']];
  471. $mhMqtt['param']=$param;
  472. $mhMqttJson = json_encode($mhMqtt);
  473. $this->message_push_mh($topic,$mhMqttJson,0);
  474. }
  475. }
  476. }else {
  477. $topic = 'mh/water/info';
  478. $ytTMhwater = Db::name('yt_t_mhwater')->where('device_code', trim($device["device_id"]))->find();
  479. if (!empty($ytTMhwater)) {
  480. $mhMqtt['deviceCode']=$ytTMhwater['mh_device_code'];
  481. $mhMqtt['signalLevel']=(int)$data3['sign']*20;
  482. $mhMqtt['volt']=(int)$data3['batteyValue'];
  483. $mhMqtt['lon']=(double)$ytTMhwater['longitude'];
  484. $mhMqtt['lat']=(double)$ytTMhwater['latitude'];
  485. $mhMqtt['createdTime']=time();
  486. $param = ["platform_cy"=>$ytTMhwater['platform_cy'],"scenetype_cy"=>$ytTMhwater['scenetype_cy']];
  487. $mhMqtt['param']=$param;
  488. $mhMqttJson = json_encode($mhMqtt);
  489. $this->message_push_mh($topic,$mhMqttJson,0);
  490. }
  491. $data3['data1'] = 'WP0';
  492. $sensorList['status'] = 0;
  493. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力正常';
  494. }
  495. $data4['deviceType'] = 'XiaoFangShuiYa';
  496. $sensorList['unit'] = 'MPa';
  497. }elseif ($data3['device_type']==2){
  498. if ($data3['alarmBoolean']!== '0'){
  499. if ($data3['alarmBoolean']== '1'){
  500. $data3['data1']='LL1';
  501. $sensorList['status'] = 3;
  502. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-液位低于下限';
  503. }elseif ($data3['alarmBoolean']== '2'){
  504. $data3['data1']='LL2';
  505. $sensorList['status'] = 4;
  506. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-液位超过上限';
  507. }
  508. }else {
  509. $data3['data1'] = 'LL0';
  510. $sensorList['status'] = 0;
  511. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-液位正常';
  512. }
  513. $data4['deviceType'] = 'YeWei';
  514. $sensorList['unit'] = 'm-米';
  515. }
  516. if (!empty($data3['sendTime'])) {
  517. switch ($data3['unit']) {
  518. case '0':
  519. $data3['data4'] = $data3['data'] * 1;
  520. break;
  521. case '1':
  522. $data3['data4'] = $data3['data'] * 0.1;
  523. break;
  524. case '2':
  525. $data3['data4'] = $data3['data'] * 0.01;
  526. break;
  527. case '3':
  528. $data3['data4'] = $data3['data'] * 0.001;
  529. break;
  530. case '4':
  531. $data3['data4'] = $data3['data'] * 0.0001;
  532. break;
  533. }
  534. $sql = "INSERT INTO sp_d{$data3['device_id']} VALUES ('','{$data3['device_id']}','{$data3['port']}','{$data3['storeTime']}',
  535. '','','','{$data3['data1']}','{$data3['batteyValue']}','{$data3['sign']}','{$data3['data4']}','11111111111')";
  536. $res5 = addDataInfo($sql);
  537. if ($data3['alarmBoolean']== '0'){
  538. $json_data_qone = $this->mqqt_data_recombination("INFO", $data3['device_id'], $longHuCompany[0]['owner_name'], $data3['batteyValue'], $data3['sign'], $data3['data4'],$longHuCompany[0]['dwtype']);
  539. if (!empty($json_data_qone)){
  540. $topicYp_one = '/usky/ytDP0002/'.$longHuCompany[0]['company'].'/'.$data3['device_id'].'/info';
  541. $this->message_push_mh($topicYp_one, $json_data_qone, 0);
  542. }
  543. }else{
  544. $json_data_qtwo = $this->mqqt_data_recombination("ALARM", $data3['device_id'], $longHuCompany[0]['owner_name'], $data3['batteyValue'], $data3['sign'], $data3['data4'],$longHuCompany[0]['dwtype']);
  545. if (!empty($json_data_qtwo)){
  546. $topicYp_two = '/usky/ytDP0002/'.$longHuCompany[0]['company'].'/'.$data3['device_id'].'/alarm';
  547. $this->message_push_mh($topicYp_two, $json_data_qtwo, 0);
  548. }
  549. }
  550. if ($longHuCompany[0]['company']=='10232'|| $longHuCompany[0]['company']=='10318'){
  551. $data4['dataType'] = 'deviceInfo';
  552. $data4['softVersion'] = '1.00.00';
  553. $data4['hardVersion'] = '0.1';
  554. $sensorList['pointValue'] = $data3['data4'];
  555. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($device["device_id"]))->find();
  556. if(!empty($deviceConfigure)){
  557. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  558. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  559. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  560. }else{
  561. $sensorList['lowThreshold'] = 0;
  562. $sensorList['highThreshold'] = 0;
  563. $sensorList['sendTime'] = 1800;
  564. }
  565. $sensorListArray[0] = $sensorList;
  566. $data4['sensorList'] = $sensorListArray;
  567. if ($longHuCompany[0]['company']=='10232'){
  568. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  569. }else{
  570. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db6';
  571. }
  572. $data5['action'] = 'transpush';
  573. $data5['data'] = $data4;
  574. $data6 = json_encode($data5);
  575. $rep_url = "http://47.98.201.73:55126/report";
  576. $res = $this->curl($rep_url,$data6);
  577. Log::record('minhanglonghuduijie'.':'.$res);
  578. }
  579. if ($data3['alarmBoolean']!== '0'){
  580. $sql = "INSERT INTO sp_sj2017 VALUES ('','TuoPuSuoEr-TSM-04P','{$data3['device_id']}','{$data3['storeTime']}','{$data3['alarmBoolean']}','{$data3['address']}', ''
  581. ,'{$data3['data1']}','{$data3['batteyValue']}','{$data3['sign']}','{$data3['data4']}','','','','','','','','','','')";
  582. $insert_id = addDataInfo($sql);
  583. // $sql66="update sp_devices_status set statustime=".$data3['storeTime'];
  584. // Log::record('sql66'.':'.$sql66);
  585. // $update_id = addDataInfo($sql66);
  586. $insert_id_sql="select id from sp_sj2017 where device_code='".$data3['device_id']."' and time='".$data3['storeTime']."'";
  587. Log::record('sql98725'.':'.$insert_id_sql);
  588. $insert_id66=add($insert_id_sql);
  589. if($data3['device_type']==1 || $data3['device_type']==4){
  590. if ($data3['alarmBoolean']== '1'){
  591. $msg['evt'] = "WP1";
  592. $msg['msg'] = "低压";
  593. }elseif ($data3['alarmBoolean']== '2'){ // high
  594. $msg['evt'] = "WP2";
  595. $msg['msg'] = "高压";
  596. }
  597. }elseif ($data3['device_type']==2){
  598. if ($data3['alarmBoolean']== '1'){
  599. $msg['evt'] = "LL1";
  600. $msg['msg'] = "低水位";
  601. }elseif ($data3['alarmBoolean']== '2'){ // high
  602. $msg['evt'] = "LL2";
  603. $msg['msg'] = "高水位";
  604. }
  605. }
  606. if ( $msg['evt']=='WP1' || $msg['evt']=='WP2' || $msg['evt']=='LL2' || $msg['evt']=='LL1') {
  607. $sql3="select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='{$device["device_id"]}'";
  608. $company = add($sql3);
  609. $sql4="select phone from sp_owner_phone WHERE owner_code='".$company[0]['company']."' AND data2 like '%3%' union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='".$company[0]['company']."') AND data2 like '%3%'";
  610. $union_sql=add($sql4);
  611. // Log::record('sql12345'.':'.$sql4);
  612. $openids=$this->array_column1($union_sql,"phone");
  613. if (empty($openids))
  614. {
  615. echo 'open is null';
  616. exit;
  617. }
  618. // Log::record('data666_phone'.':'.$openids);
  619. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  620. $op=add($sql5);
  621. $access_token=GetAccess_Token();
  622. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  623. $deviceid=$device["device_id"];
  624. $type=$msg['msg'];
  625. $time=$data3['storeTime'];
  626. $remark='设备名称:'.$company[0]['owner_name'];
  627. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  628. Log::record('data666_url_wx_push2'.':'. $url_wx_push2);
  629. $data666['IMEI']=$device["device_id"];
  630. Log::record('data666_IMET'.':'.$data666['IMEI']);
  631. $data666['phone']=$openids;
  632. Log::record('data666_phone'.':'.$data666['phone']);
  633. $data666['evt']=$data3['data1'];
  634. Log::record('data666_evt'.':'.$data666['evt']);
  635. $data666['insert_id']=$insert_id66[0]['id'];
  636. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  637. $data666['time']=$data3['storeTime'];
  638. // Log::record('data666'.':'.$data666);
  639. $sql88="UPDATE sp_devices_status SET statustime ='".$data3['storeTime']."' where deviceid='".$data666['IMEI']."'";
  640. addDataInfo($sql88);
  641. $this->curl($url_wx_push2,$data666);
  642. $rep['SubType']=2;
  643. $rep['DeviceId']=$device["device_id"];
  644. $rep['InsertId']=$insert_id66[0]['id'];
  645. $rep['Confirmed']=0;
  646. $rep['CompanyCode']=$company[0]['company'];
  647. $rep['evt_name']=$data3['data1'];
  648. $rep['evt']=0;
  649. $rep['alarm_time']=$data3['storeTime'];
  650. $rep2 = json_encode($rep);
  651. $rep_url = "http://47.98.201.73:55125/report";
  652. $res = $this->curl($rep_url,$rep2);
  653. Log::record('TuoPuSuoEr-TSM-04P'.':'.$res);
  654. // $url_wx_push2 = "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Waterwarn/bj";
  655. foreach($op as $key => $o){
  656. if (!empty($o['openid']))
  657. {
  658. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  659. // s_curl($url_wx_push2,$data88);
  660. $Message['openid']=$o['openid'];
  661. $arr[$key]=$Message;
  662. }
  663. }
  664. }
  665. }
  666. }
  667. }
  668. if ($device['deviceType']=='Smoke') {
  669. if ($getdata["service"]['data']["DeviceStatus"]!== 7){
  670. $arr_bit = array(
  671. "","报警","报警静音","保留","低压","故障","保留"
  672. );
  673. $deviceStatus=$getdata["service"]['data']["DeviceStatus"];
  674. $data3 = array();
  675. $data3['device_code'] = $device["device_id"];
  676. $data3['time'] = date('Y-m-d H:i:s');
  677. $data3['status'] = 'y'.'0'.$deviceStatus;
  678. $data3['address'] = '180.101.147.89';
  679. $data3['compartment'] = '';
  680. $data3['fireprocess'] = '';
  681. $data3['flowendtime'] = '0000-00-00 00:00:00';
  682. $data3['ncmd'] = 'y'.'0'.$deviceStatus;
  683. $data3['data1'] = 'y'.'0'.$deviceStatus;
  684. $data3['data2'] = $arr_bit["{$deviceStatus}"];
  685. $data3['data3'] = '';
  686. $data3['data4'] = '烟雾'.$arr_bit["{$deviceStatus}"];
  687. $data3['data5'] = '';
  688. $sql = "INSERT INTO sp_hj2017 VALUES ('','','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}','{$data3['compartment']}',
  689. '{$data3['fireprocess']}', '{$data3['flowendtime']}', '{$data3['ncmd']}','{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','',''
  690. ,'','','','','','')";
  691. $res5 = addDataInfo($sql);
  692. $insert_id_sql="select id from sp_hj2017 where device_code='".$data3['device_code']."' and time='".$data3['time']."'";
  693. Log::record('sql66886688'.':'.$insert_id_sql);
  694. $insert_id66=add($insert_id_sql);
  695. // $sql66="update sp_devices_status set statustime=".$data3['time'];
  696. // Log::record('sql66'.':'.$sql66);
  697. // $update_id = addDataInfo($sql66);
  698. }
  699. $arr_h=array(1,2);
  700. if (in_array($getdata["service"]['data']["DeviceStatus"],$arr_h)){
  701. $sql3="select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='{$device["device_id"]}'";
  702. Log::record('sql3'.':'.$sql3);
  703. $company = add($sql3);
  704. Log::record('company'.':'.$company[0]['company']);
  705. $sql4="select phone from sp_owner_phone WHERE owner_code='".$company[0]['company']."' AND data2 like '%6%' union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='".$company[0]['company']."') AND data2 like '%6%'";
  706. Log::record('sql4'.':'.$sql4);
  707. $union_sql=add($sql4);
  708. $openids=$this->array_column1($union_sql,"phone");
  709. if (empty($openids))
  710. {
  711. echo 'open is null';
  712. exit;
  713. }
  714. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  715. $op=add($sql5);
  716. $access_token=GetAccess_Token();
  717. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  718. $deviceid=$device["device_id"];
  719. $type='烟雾';
  720. $time=$data3['time'];
  721. $remark='设备名称:'.$company[0]['owner_name'];
  722. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/bj";
  723. Log::record('data666_url_wx_push2'.':'.$url_wx_push2);
  724. $data666['phone']=$openids;
  725. Log::record('data666_phone'.':'.$data666['phone']);
  726. $data666['IMEI']=$device["device_id"];
  727. Log::record('data666_IMEI'.':'.$data666['IMEI']);
  728. $data666['time']=$data3['time'];
  729. Log::record('data666_time'.':'.$data666['time']);
  730. $data666['evt']=$data3['data1'];
  731. Log::record('data666_evt'.':'.$data666['evt']);
  732. $data666['insert_id']=$insert_id66[0]['id'];
  733. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  734. $this->curl($url_wx_push2,$data666);
  735. foreach($op as $key => $o){
  736. if (!empty($o['openid']))
  737. {
  738. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  739. // s_curl($url_wx_push2,$data88);
  740. $Message['openid']=$o['openid'];
  741. $arr[$key]=$Message;
  742. }
  743. }
  744. }
  745. } elseif($device['deviceType']=='MultiSensor') {
  746. if($getdata["service"]['data']["alarmBoolean"]== 17){
  747. // $sql1="SELECT * from 'dev_' . {$device["device_id"]} where addtime=(select max(addtime) from 'dev_' . {$device["device_id"]})";
  748. // $Model = M();
  749. // $result = $Model->query($sql1);
  750. // $service=json_decode($result['']['service']);
  751. $unit=dechex($getdata["service"]['data']["unit"]);
  752. $data3 = array();
  753. $data3['device_code'] = $device["device_id"];
  754. $data3['time'] = date('Y-m-d H:i:s');
  755. $data3['status'] = $getdata["service"]['data']["alarmBoolean"];
  756. $data3['address'] = '180.101.147.89';
  757. $data3['ncmd'] = '';
  758. $data3['unit'] =$getdata["service"]['data']["unit"];
  759. $arr_y=array('10','11','20','21','22');
  760. $sql11="SELECT dwtype FROM sp_owner WHERE owner_code ='{$device["device_id"]}'";
  761. $dwtype = add($sql11);
  762. if ($dwtype[0]['dwtype'] == 5){
  763. if ($data3['unit']== 1){
  764. $data3['data1']='LL2';
  765. }elseif($data3['unit']== 2){
  766. $data3['data1']='LL1';
  767. }else{
  768. $data3['data1']='LL0';
  769. }
  770. }else {
  771. if ($data3['unit']== 1){
  772. $data3['data1']='WP2';
  773. }elseif($data3['unit']== 2){
  774. $data3['data1']='WP1';
  775. }else{
  776. $data3['data1']='WP0';
  777. }
  778. }
  779. $data3['data2'] = $getdata["service"]['data']["batteyValue"];
  780. $data3['data3'] = '';
  781. switch ($unit){
  782. case '10':
  783. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  784. break;
  785. case '11':
  786. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  787. break;
  788. case '20':
  789. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  790. break;
  791. case '21':
  792. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  793. break;
  794. case '22':
  795. $data3['data4'] = $getdata["service"]['data']["data"];
  796. break;
  797. case '30':
  798. $data3['data4'] = $getdata["service"]['data']["data"]*0.0000001;
  799. break;
  800. case '31':
  801. $data3['data4'] = $getdata["service"]['data']["data"]*0.000001;
  802. break;
  803. case '40':
  804. $data3['data4'] = $getdata["service"]['data']["data"]*0.00001;
  805. break;
  806. case '41':
  807. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  808. break;
  809. case '42':
  810. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  811. break;
  812. case '50':
  813. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  814. break;
  815. case '51':
  816. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  817. break;
  818. case '52':
  819. $data3['data4'] = $getdata["service"]['data']["data"];
  820. break;
  821. case '2':
  822. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  823. break;
  824. case '1':
  825. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  826. break;
  827. }
  828. $data3['data5'] = '';
  829. $sql = "INSERT INTO sp_sj2017 VALUES ('','tp_water_pressure','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}', '{$data3['ncmd']}'
  830. ,'{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','','','','','','','','')";
  831. // Log::record('sql123'.':'.$sql);
  832. $insert_id = addDataInfo($sql);
  833. $insert_id_sql="select id from sp_sj2017 where device_code='".$data3['device_id']."' and time='".$data3['time']."'";
  834. Log::record('sql987253232'.':'.$insert_id_sql);
  835. $insert_id66=add($insert_id_sql);
  836. if ($dwtype[0]['dwtype'] == 5) {
  837. if ($getdata["service"]['data']["unit"] == '1') {
  838. $msg['evt'] = "WP2";
  839. $msg['msg'] = "高水位";
  840. } elseif ($getdata["service"]['data']["unit"] == '2') { // high
  841. $msg['evt'] = "WP1";
  842. $msg['msg'] = "低水位";
  843. }
  844. }else{
  845. if ($getdata["service"]['data']["unit"] == '1') {
  846. $msg['evt'] = "WP2";
  847. $msg['msg'] = "高压";
  848. } elseif ($getdata["service"]['data']["unit"] == '2') { // high
  849. $msg['evt'] = "WP1";
  850. $msg['msg'] = "低压";
  851. }
  852. }
  853. if ( $msg['evt']=='WP1' || $msg['evt']=='WP2' ) {
  854. $sql3="select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='{$device["device_id"]}'";
  855. $company = add($sql3);
  856. $sql4="select phone from sp_owner_phone WHERE owner_code='".$company[0]['company']."' AND data2 like '%3%' union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='".$company[0]['company']."') AND data2 like '%3%'";
  857. $union_sql=add($sql4);
  858. Log::record('sql12345'.':'.$sql4);
  859. $openids=$this->array_column1($union_sql,"phone");
  860. if (empty($openids))
  861. {
  862. echo 'open is null';
  863. exit;
  864. }
  865. Log::record('data666_phone'.':'.$openids);
  866. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  867. $op=add($sql5);
  868. $access_token=GetAccess_Token();
  869. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  870. $deviceid=$device["device_id"];
  871. $type=$msg['msg'];
  872. $time=$data3['time'];
  873. $remark='设备名称:'.$company[0]['owner_name'];
  874. //
  875. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  876. Log::record('data666_url_wx_push2'.':'. $url_wx_push2);
  877. $data666['IMEI']=$device["device_id"];
  878. Log::record('data666_IMET'.':'.$data666['IMEI']);
  879. $data666['phone']=$openids;
  880. Log::record('data666_phone'.':'.$data666['phone']);
  881. $data666['evt']=$data3['data1'];
  882. Log::record('data666_evt'.':'.$data666['evt']);
  883. $data666['insert_id']=$insert_id66[0]['id'];
  884. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  885. $data666['time']=$data3['time'];
  886. // Log::record('data666'.':'.$data666);
  887. $sql88="UPDATE sp_devices_status SET statustime ='".$data3['time']."' where deviceid='".$data666['IMEI']."'";
  888. addDataInfo($sql88);
  889. $this->curl($url_wx_push2,$data666);
  890. // $url_wx_push2 = "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Waterwarn/bj";
  891. foreach($op as $key => $o){
  892. if (!empty($o['openid']))
  893. {
  894. // $data88['openid']=$o['openid'];
  895. // $data88['address']=$address;
  896. // $data88['deviceid']=$deviceid;
  897. // $data88['type']=$type;
  898. // $data88['time']=$time;
  899. // $data88['remark']=$remark;
  900. //token,微信id,地址,设备号,类型,时间,设备名称(或者其他内容)
  901. //{{first.DATA}}$address
  902. //设备号:{{keyword1.DATA}}$deviceid
  903. //报警类型:{{keyword2.DATA}}$type
  904. //时间:{{keyword3.DATA}}$time
  905. //{{remark.DATA}}$remark
  906. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  907. // s_curl($url_wx_push2,$data88);
  908. $Message['openid']=$o['openid'];
  909. $arr[$key]=$Message;
  910. }
  911. }
  912. }
  913. }
  914. }
  915. }
  916. return json_encode($data, 320);
  917. }
  918. }
  919. public function addtuobaodata(){
  920. if ($this->request->isPost()) {
  921. $data = input('');
  922. $list = array();
  923. if (is_array($data)) {
  924. $list['data'] = json_encode($data, 320);
  925. $getdata = json_decode($list['data'], true);//转成数组
  926. $list['type'] = 1;
  927. } else {
  928. $list['data'] = $data;
  929. $getdata = json_decode($list['data'], true);//转成数组
  930. $list['type'] = 2;
  931. }
  932. // $list['data']=11;
  933. $list['response'] = "222";
  934. $list['notifyType'] = $getdata["notifyType"];
  935. $list['deviceId'] = $getdata["deviceId"];
  936. $list['addtime'] = date('Y-m-d H:i:s');
  937. $res = Db::name('noticedata')->insertGetId($list);//将推送的数据存入总表
  938. if (trim($getdata["notifyType"]) == "deviceDataChanged") {
  939. $device = Db::name('device')->where('iot_id', trim($getdata["deviceId"]))->find();
  940. Log::record('tuobaoceshi123' . ':' . $device["device_id"]);
  941. $datainfo = array();
  942. $datainfo['notifyType'] = $getdata["notifyType"];
  943. $datainfo['device_id'] = $device["device_id"];
  944. $datainfo['iot_id'] = $getdata["deviceId"];
  945. $datainfo['service'] = json_encode($getdata["service"]);
  946. $datainfo['addtime'] = date('Y-m-d H:i:s');
  947. $res1 = Db::table('dev_' . $device["device_id"])->insertGetId($datainfo);
  948. if ($device['deviceType']=='SaiTeSmoke') {
  949. $data3 = array();
  950. $data3['device_id'] = $device["device_id"];
  951. $data3['RSRQ'] = $getdata["service"]['data']['RSRP'];
  952. $data3['CSQ'] = $getdata["service"]['data']["CSQ"];
  953. $data3['DeviceType'] = 1;
  954. if ($getdata["service"]['data']["Alarm_Status"]!== 7) {
  955. $data3['DeviceStatus'] = 1;
  956. }else{
  957. $data3['DeviceStatus'] = 7;
  958. }
  959. $data3['BatteryLevel'] = $getdata["service"]['data']["Battery_Level"];
  960. $data3['SensorLevel'] = $getdata["service"]['data']["AlarmLevel"];
  961. $data3['TData'] = $getdata["service"]['data']["TData"];
  962. $data3['eventTime'] = $getdata["service"]["eventTime"];
  963. $data3['storeTime'] = date('Y-m-d H:i:s');
  964. $sql = "INSERT INTO sp_y{$data3['device_id']} VALUES ('','{$data3['device_id']}',
  965. '{$data3['RSRQ']}', '{$data3['CSQ']}', '{$data3['DeviceType']}','{$data3['DeviceStatus']}','{$data3['BatteryLevel']}','{$data3['SensorLevel']}','{$data3['eventTime']}','{$data3['storeTime']}','{$data3['TData']}')";
  966. $res3 = addDataInfo($sql);
  967. if ($getdata["service"]['data']["Alarm_Status"]!== 7){
  968. $arr_bit = array(
  969. "","报警","静音","保留","低压","故障","数据异常","","设备收到单次静音指令","设备收到连续静音指令","拆卸报警","拆卸恢复","","","模拟报警"
  970. );
  971. $deviceStatus=$getdata["service"]['data']["Alarm_Status"];
  972. $data3 = array();
  973. $data3['device_code'] = $device["device_id"];
  974. $data3['time'] = date('Y-m-d H:i:s');
  975. $data3['status'] = 'y'.'0'.$deviceStatus;
  976. $data3['address'] = '180.101.147.89';
  977. $data3['compartment'] = '';
  978. $data3['fireprocess'] = '';
  979. $data3['flowendtime'] = '0000-00-00 00:00:00';
  980. $data3['ncmd'] = 'y'.'01';
  981. $data3['data1'] = 'y'.'01';
  982. $data3['data2'] = $arr_bit["{$deviceStatus}"];
  983. $data3['data3'] = '';
  984. $data3['data4'] = '烟雾'.$arr_bit["{$deviceStatus}"];
  985. $data3['data5'] = '';
  986. $sql = "INSERT INTO sp_hj2017 VALUES ('','','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}','{$data3['compartment']}',
  987. '{$data3['fireprocess']}', '{$data3['flowendtime']}', '{$data3['ncmd']}','{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','',''
  988. ,'','','','','','')";
  989. $res5 = addDataInfo($sql);
  990. $insert_id_sql="select id from sp_hj2017 where device_code='".$data3['device_code']."' and time='".$data3['time']."'";
  991. Log::record('sql6688668866'.':'.$insert_id_sql);
  992. $insert_id66=add($insert_id_sql);
  993. $arr_h=array(1,2,6,8,9,10,11,14);
  994. if (in_array($getdata["service"]['data']["Alarm_Status"],$arr_h)){
  995. $sql3="select s.owner_name,s.unitinfo,s.company,c.owner_address from sp_owner s ,sp_owner_company c where s.company=c.owner_id and s.owner_code ='{$device["device_id"]}'";
  996. Log::record('sql3'.':'.$sql3);
  997. $company = add($sql3);
  998. Log::record('company'.':'.$company[0]['company']);
  999. $sql4="select phone from sp_owner_phone WHERE owner_code='".$company[0]['company']."' AND data2 like '%6%' union select phone from sp_owner_phone WHERE owner_code in (select other_code from sp_owner_fujin where owner_code='".$company[0]['company']."') AND data2 like '%6%'";
  1000. Log::record('sql4'.':'.$sql4);
  1001. $union_sql=add($sql4);
  1002. $openids=$this->array_column1($union_sql,"phone");
  1003. if (empty($openids))
  1004. {
  1005. echo 'open is null';
  1006. exit;
  1007. }
  1008. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  1009. $op=add($sql5);
  1010. $access_token=GetAccess_Token();
  1011. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  1012. $deviceid=$device["device_id"];
  1013. $type='烟雾';
  1014. $time=$data3['time'];
  1015. $remark='设备名称:'.$company[0]['owner_name'];
  1016. if($getdata["service"]['data']["Alarm_Status"]==5){
  1017. $deviceStatus1='y05';
  1018. }elseif ($getdata["service"]['data']["Alarm_Status"]==1){
  1019. $deviceStatus1='y01';
  1020. }elseif ($getdata["service"]['data']["Alarm_Status"]==10){
  1021. $deviceStatus1='y10';
  1022. }elseif ($getdata["service"]['data']["Alarm_Status"]==14){
  1023. $deviceStatus1='y12';
  1024. }
  1025. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/bj";
  1026. Log::record('data666_url_wx_push2'.':'.$url_wx_push2);
  1027. $data666['phone']=$openids;
  1028. Log::record('data666_phone'.':'.$data666['phone']);
  1029. $data666['IMEI']=$device["device_id"];
  1030. Log::record('data666_IMEI'.':'.$data666['IMEI']);
  1031. $data666['time']=$data3['time'];
  1032. Log::record('data666_time'.':'.$data666['time']);
  1033. $data666['evt']=$deviceStatus1;
  1034. Log::record('data666_evt'.':'.$data666['evt']);
  1035. $data666['insert_id']=$insert_id66[0]['id'];
  1036. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  1037. $this->curl($url_wx_push2,$data666);
  1038. foreach($op as $key => $o){
  1039. if (!empty($o['openid']))
  1040. {
  1041. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  1042. // s_curl($url_wx_push2,$data88);
  1043. $Message['openid']=$o['openid'];
  1044. $arr[$key]=$Message;
  1045. }
  1046. }
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. }
  1053. public function jingan(){
  1054. if ($this->request->isPost()) {
  1055. Log::record('jinganxiaofang' . ':ceshi');
  1056. $data = input('');
  1057. /* $rand=rand(0,2);
  1058. $header=["Content-Type:application/json"];
  1059. //将数据分配给不同的服务
  1060. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1061. $server=$httpserver[$rand];
  1062. //将数据随机转发到别的平台做处理
  1063. $res=curltest($server,$data,$header);
  1064. */
  1065. $list = array();
  1066. if (is_array($data)) {
  1067. $list['data'] = json_encode($data, 320);
  1068. $getdata = json_decode($list['data'], true);//转成数组
  1069. } else {
  1070. $list['data'] = $data;
  1071. $getdata = json_decode($list['data'], true);//转成数组
  1072. }
  1073. // $list['data']=11;
  1074. // $list['response'] = "111";
  1075. //
  1076. // $list['notifyType'] = $getdata["notifyType"];
  1077. // $list['deviceId'] = $getdata["deviceId"];
  1078. // $list['addtime'] = date('Y-m-d H:i:s');
  1079. $res = Db::name('jingandata')->insertGetId($list);//将推送的数据存入总表
  1080. Log::record('jinganxiaofang' . ':' . $res);
  1081. $Data = array();
  1082. $Data['app_key'] = $getdata["app_key"];
  1083. $Data['timestamp'] = date('Y-m-d H:i:s',$getdata["timestamp"]);
  1084. $Data['format'] = $getdata["format"];
  1085. $Data['v'] = $getdata["v"];
  1086. $Data['sign_method'] = $getdata["sign_method"];
  1087. $Data['pid'] = $getdata["pid"];
  1088. $Data['bid'] = $getdata["bid"];
  1089. $Data['fid'] = $getdata["fid"];
  1090. $Data['supervise_id'] = $getdata["supervise_id"];
  1091. if (empty($getdata["supervise_code"])){
  1092. $Data['supervise_code']="";
  1093. }else{
  1094. $Data['supervise_code'] = $getdata["supervise_code"];
  1095. }
  1096. $Data['status'] = $getdata["status"];
  1097. $Data['content'] = $getdata["content"];
  1098. $Data['type'] = $getdata["type"];
  1099. if (empty($getdata["device_id"])){
  1100. $Data['device_id']="";
  1101. }else{
  1102. $Data['device_id'] = $getdata["device_id"];
  1103. }
  1104. if (empty($getdata["aid"])){
  1105. $Data['aid']="";
  1106. }else{
  1107. $Data['aid'] = $getdata["aid"];
  1108. }
  1109. if (empty($getdata["loop_number"])){
  1110. $Data['loop_number']="";
  1111. }else{
  1112. $Data['loop_number'] = $getdata["loop_number"];
  1113. }
  1114. if (empty($getdata["position_number"])){
  1115. $Data['position_number']="";
  1116. }else{
  1117. $Data['position_number'] = $getdata["position_number"];
  1118. }
  1119. if (empty($getdata["mainframe_id"])){
  1120. $Data['mainframe_id']="";
  1121. }else{
  1122. $Data['mainframe_id'] = $getdata["mainframe_id"];
  1123. }
  1124. $Data['sign'] = $getdata["sign"];
  1125. if ($Data['app_key']=="f8bdea7926bcc7cb827d2eccf67fa559"){
  1126. $sql3="select device_name,company_code from sp_huangpu_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1127. }else if ($Data['app_key']=="9e555b8b2244ebc1f59695824b86c5a0"){
  1128. $sql3="select device_name,company_code from sp_jingan_share where pid='{$getdata["pid"]}' and bid='{$getdata["bid"]}' and fid='{$getdata["fid"]}' GROUP BY company_code";
  1129. }
  1130. $company = add($sql3);
  1131. $Data['company_code'] = $company[0]['company_code'];
  1132. $res1 = Db::name('jinganAnalysisData')->insertGetId($Data);
  1133. $url_wx_data = "https://iot.usky.cn/jdxf/wxapp2.php/Home/WorkOrderNotice/message_workordernotice";
  1134. $data_wx['company_code'] = $company[0]['company_code'];
  1135. $data_wx['time']=$Data['timestamp'];
  1136. $data_wx['title']=$Data['content'];
  1137. $data_wx['id']=$res1;
  1138. $data_wx['types']=2;
  1139. if($getdata["status"]==0){
  1140. $this->s_curl($url_wx_data,$data_wx);
  1141. }
  1142. return "success";
  1143. }
  1144. }
  1145. public function jinganPerson(){
  1146. if ($this->request->isPost()) {
  1147. Log::record('jinganxiaofangren' . ':ceshi');
  1148. $data = input('');
  1149. $list = array();
  1150. $data_list = array();
  1151. if (is_array($data)) {
  1152. $list['data'] = json_encode($data, 320);
  1153. $getdata = json_decode($list['data'], true);//转成数组
  1154. } else {
  1155. $list['data'] = $data;
  1156. $getdata = json_decode($list['data'], true);//转成数组
  1157. }
  1158. // $list['data']=11;
  1159. // $list['response'] = "111";
  1160. $res = Db::name('jinganperson')->insertGetId($list);//将推送的数据存入总表
  1161. Log::record('jinganxiaofangren' . ':' . $res);
  1162. $data_list['app_key'] = $getdata["app_key"];
  1163. $data_list['timestamp'] = date('Y-m-d H:i:s',$getdata["timestamp"]);
  1164. $data_list['format'] = $getdata["format"];
  1165. $data_list['version'] = $getdata["v"];
  1166. $data_list['sign_method'] = $getdata["sign_method"];
  1167. $data_list['artificial_id'] = $getdata["artificial_id"];
  1168. $data_list['pid'] = $getdata["pid"];
  1169. $data_list['artificial_code'] = $getdata["artificial_code"];
  1170. $data_list['company_id'] = $getdata["company_id"];
  1171. $data_list['content'] = $getdata["content"];
  1172. $data_list['status'] = $getdata["status"];
  1173. $data_list['sign'] = $getdata["sign"];
  1174. $device = Db::name('person_data')->where('artificial_id', trim( $data_list['artificial_id']))->find();
  1175. if($device){
  1176. return "success";
  1177. }else{
  1178. if ($data_list['app_key']=="f8bdea7926bcc7cb827d2eccf67fa559"){
  1179. $sql3="select device_name,company_code from sp_huangpu_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1180. }else if ($data_list['app_key']=="9e555b8b2244ebc1f59695824b86c5a0"){
  1181. $sql3="select device_name,company_code from sp_jingan_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1182. }
  1183. $company = add($sql3);
  1184. $data_list['company_code'] = $company[0]['company_code'];
  1185. $res1 = Db::name('person_data')->insertGetId($data_list);//将推送的数据存入解析表
  1186. Log::record('person_data_list'.':'.$getdata["content"]);
  1187. $url_wx_data = "https://iot.usky.cn/jdxf/wxapp2.php/Home/WorkOrderNotice/message_workordernotice";
  1188. $data_wx['company_code']=$company[0]['company_code'];
  1189. $data_wx['time']=$data_list['timestamp'];
  1190. $data_wx['title']=$data_list['content'];
  1191. $data_wx['id']=$res1;
  1192. $data_wx['types']=1;
  1193. $this->s_curl($url_wx_data,$data_wx);
  1194. return "success";
  1195. }
  1196. }
  1197. }
  1198. public function aepceshi(){
  1199. if ($this->request->isPost()) {
  1200. Log::record('aep' . ':ceshi');
  1201. $data = input('');
  1202. /* $rand=rand(0,2);
  1203. $header=["Content-Type:application/json"];
  1204. //将数据分配给不同的服务
  1205. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1206. $server=$httpserver[$rand];
  1207. //将数据随机转发到别的平台做处理
  1208. $res=curltest($server,$data,$header);
  1209. */
  1210. $list = array();
  1211. if (is_array($data)) {
  1212. $list['data'] = json_encode($data, 320);
  1213. $getdata = json_decode($list['data'], true);//转成数组
  1214. } else {
  1215. $list['data'] = $data;
  1216. $getdata = json_decode($list['data'], true);//转成数组
  1217. }
  1218. // $list['data']=11;
  1219. // $list['response'] = "111";
  1220. //
  1221. // $list['notifyType'] = $getdata["notifyType"];
  1222. // $list['deviceId'] = $getdata["deviceId"];
  1223. // $list['addtime'] = date('Y-m-d H:i:s');
  1224. $res = Db::name('nb_aep')->insertGetId($list);//将推送的数据存入总表
  1225. Log::record('aep' . ':' . $res);
  1226. return json_encode($data, 320);
  1227. }
  1228. }
  1229. public function longHuConfigure(){
  1230. if ($this->request->isPost()) {
  1231. $data = input('');
  1232. $list = array();
  1233. $set_data = array();
  1234. if (is_array($data)) {
  1235. $list['data'] = json_encode($data, 320);
  1236. $getdata = json_decode($list['data'], true);//转成数组
  1237. } else {
  1238. $list['data'] = $data;
  1239. $getdata = json_decode($list['data'], true);//转成数组
  1240. }
  1241. if ($getdata["dataType"]=="setting"){
  1242. $sql="SELECT owner_code FROM sp_owner WHERE id ={$getdata["sensorId"]}";
  1243. $owner_code = add($sql);
  1244. $imei='&imei='.$owner_code[0]['owner_code'];
  1245. if ($getdata["deviceType"]=="XiaoFangShuiYa"){
  1246. $deviceType='&deviceType='.'1';
  1247. }elseif ($getdata["deviceType"]=="YeWei"){
  1248. $deviceType='&deviceType='.'2';
  1249. }
  1250. $sendingGap = '';
  1251. if (!empty($getdata["sendTime"])){
  1252. $sendingGap = '&sendingGap='.$getdata["sendTime"];
  1253. }
  1254. $unipathThresholdLimit = '';
  1255. if (!empty($getdata["lowThreshold"])){
  1256. $unipathThresholdLimit = '&unipathThresholdLimit='.$getdata["lowThreshold"]*1000;
  1257. }
  1258. $unipathThresholdUpper = '';
  1259. if (!empty($getdata["highThreshold"])){
  1260. $unipathThresholdUpper = '&unipathThresholdUpper='.$getdata["highThreshold"]*1000;
  1261. }
  1262. $data6 = json_encode($set_data);
  1263. $rep_url = "https://api.topsailiot.com/dmp/deviceConfig?appkey=3a3e533818a9604dfec0388b2d817dbc2f9f7b05dd4b153c403897cd93a9c04d0edfe3d86ebd3ad0&userid=TS2020120110122980052".$imei.$deviceType.$sendingGap.$unipathThresholdLimit.$unipathThresholdUpper;
  1264. $res = $this->s_curl($rep_url,'');
  1265. Log::record('longhu' . ':' . $rep_url);
  1266. return $res;
  1267. }else{
  1268. $res_arr = array();
  1269. $res_arr['code'] = 60001;
  1270. $res_arr['msg'] = "dataType参数有误";
  1271. $res_arr['data'] = "";
  1272. $res = json_encode($res_arr);
  1273. Log::record('longhuerror' . ':' . $res);
  1274. return $res;
  1275. }
  1276. }
  1277. }
  1278. public function longhudata(){
  1279. if ($this->request->isPost()) {
  1280. $data = input('');
  1281. $list = array();
  1282. $set_data = array();
  1283. if (is_array($data)) {
  1284. $list['data'] = json_encode($data, 320);
  1285. $getdata = json_decode($list['data'], true);//转成数组
  1286. } else {
  1287. $list['data'] = $data;
  1288. $getdata = json_decode($list['data'], true);//转成数组
  1289. }
  1290. if ($getdata["user_from"]=="longHuWater"){
  1291. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1292. $longHuCompany = add($sql6);
  1293. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  1294. $sensorList['status'] = 0;
  1295. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力正常';
  1296. $data4['deviceType'] = 'XiaoFangShuiYa';
  1297. $sensorList['unit'] = 'MPa';
  1298. $data4['dataType'] = 'deviceInfo';
  1299. $data4['provider'] = 'Usky';
  1300. $data4['softVersion'] = '1.00.00';
  1301. $data4['hardVersion'] = '0.1';
  1302. $sensorList['pointValue'] = (double)$getdata['device_value'];
  1303. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1304. if (!empty($deviceConfigure)) {
  1305. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1306. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1307. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1308. } else {
  1309. $sensorList['lowThreshold'] = 0;
  1310. $sensorList['highThreshold'] = 0;
  1311. $sensorList['sendTime'] = 1800;
  1312. }
  1313. $sensorListArray[0] = $sensorList;
  1314. $data4['sensorList'] = $sensorListArray;
  1315. if ($longHuCompany[0]['company']=='10232'){
  1316. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1317. }else{
  1318. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db6';
  1319. }
  1320. $data5['action'] = 'transpush';
  1321. $data5['data'] = $data4;
  1322. $data6 = json_encode($data5);
  1323. $rep_url = "http://47.98.201.73:55126/report";
  1324. $res = $this->curl($rep_url, $data6);
  1325. Log::record('minhanglonghuduijie' . ':' . $res);
  1326. $res_arr = array();
  1327. $res_arr['code'] = 200;
  1328. $res_arr['msg'] = "操作成功";
  1329. $res_arr['data'] = "";
  1330. $res1 = json_encode($res_arr);
  1331. return $res1;
  1332. }else if ($getdata["user_from"]=="longHuWaterAlarm"){
  1333. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1334. $longHuCompany = add($sql6);
  1335. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  1336. $sql7="select * from sp_sj2017 where id ={$getdata['device_value']}";
  1337. $longHuAlarm = add($sql7);
  1338. if ($longHuAlarm[0]['data1']=='WP1'){
  1339. $sensorList['status'] = 3;
  1340. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力低于下限';
  1341. }elseif ($longHuAlarm[0]['data1']=='WP2'){
  1342. $sensorList['status'] = 4;
  1343. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力超过上限';
  1344. }elseif ($longHuAlarm[0]['data1']=='WP3'){
  1345. $sensorList['status'] = 1;
  1346. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-设备故障';
  1347. }
  1348. $data4['deviceType'] = 'XiaoFangShuiYa';
  1349. $sensorList['unit'] = 'MPa';
  1350. $data4['dataType'] = 'deviceInfo';
  1351. $data4['softVersion'] = '1.00.00';
  1352. $data4['hardVersion'] = '0.1';
  1353. $sensorList['pointValue'] = (double)$longHuAlarm[0]['data4'];
  1354. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1355. if (!empty($deviceConfigure)) {
  1356. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1357. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1358. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1359. } else {
  1360. $sensorList['lowThreshold'] = 0;
  1361. $sensorList['highThreshold'] = 0;
  1362. $sensorList['sendTime'] = 1800;
  1363. }
  1364. $sensorListArray[0] = $sensorList;
  1365. $data4['sensorList'] = $sensorListArray;
  1366. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1367. $data5['action'] = 'transpush';
  1368. $data5['data'] = $data4;
  1369. $data6 = json_encode($data5);
  1370. $rep_url = "http://47.98.201.73:55126/report";
  1371. $res = $this->curl($rep_url, $data6);
  1372. Log::record('minhanglonghuduijie' . ':' . $res);
  1373. $res_arr = array();
  1374. $res_arr['code'] = 200;
  1375. $res_arr['msg'] = "操作成功";
  1376. $res_arr['data'] = "";
  1377. $res1 = json_encode($res_arr);
  1378. return $res1;
  1379. }else if ($getdata["user_from"]=="xingYueHuiRtuWater"){
  1380. Log::record('xingyuehui' . ':' . $getdata["device_code"]);
  1381. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1382. $longHuCompany = add($sql6);
  1383. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'].(int)$getdata["port"];
  1384. $sensorList['status'] = 0;
  1385. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$getdata["port_name"].'-压力正常';
  1386. $data4['deviceType'] = 'XiaoFangShuiYa';
  1387. $sensorList['unit'] = 'MPa';
  1388. $data4['dataType'] = 'deviceInfo';
  1389. $data4['provider'] = 'Usky';
  1390. $data4['softVersion'] = '1.00.00';
  1391. $data4['hardVersion'] = '0.1';
  1392. $sensorList['pointValue'] = (double)$getdata['device_value'];
  1393. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1394. if (!empty($deviceConfigure)) {
  1395. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1396. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1397. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1398. } else {
  1399. $sensorList['lowThreshold'] = 0;
  1400. $sensorList['highThreshold'] = 0;
  1401. $sensorList['sendTime'] = 1800;
  1402. }
  1403. $sensorListArray[0] = $sensorList;
  1404. $data4['sensorList'] = $sensorListArray;
  1405. if ($longHuCompany[0]['company']=='10232'){
  1406. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1407. }else{
  1408. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db6';
  1409. }
  1410. $data5['action'] = 'transpush';
  1411. $data5['data'] = $data4;
  1412. $data6 = json_encode($data5);
  1413. $rep_url = "http://47.98.201.73:55126/report";
  1414. $res = $this->curl($rep_url, $data6);
  1415. Log::record('minhanglonghuduijie' . ':' . $res);
  1416. $res_arr = array();
  1417. $res_arr['code'] = 200;
  1418. $res_arr['msg'] = "操作成功";
  1419. $res_arr['data'] = "";
  1420. $res1 = json_encode($res_arr);
  1421. return $res1;
  1422. }else{
  1423. $res_arr = array();
  1424. $res_arr['code'] = 60001;
  1425. $res_arr['msg'] = "user_from参数有误";
  1426. $res_arr['data'] = "";
  1427. $res = json_encode($res_arr);
  1428. Log::record('longhuerror' . ':' . $res);
  1429. return $res;
  1430. }
  1431. }
  1432. }
  1433. public function testdata1(){
  1434. $rand=rand(0,2);
  1435. $data=[
  1436. '{"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"}}',
  1437. '{"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"}}',
  1438. '{"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"}}',
  1439. ];
  1440. $header=["Content-Type:application/json"];
  1441. //将数据分配给不同的服务
  1442. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1443. $senddata=$data[$rand];
  1444. $server=$httpserver[$rand];
  1445. $res=curltest($server,$senddata,$header);
  1446. dump($res);
  1447. /* //设备所属公司
  1448. $owner=Db::name('device')->field("owner_id")->where('iot_id',$iot_id)->find();
  1449. $ownerhost=Db::name('owner')->where('id',$owner["owner_id"])->find();
  1450. $ipaddr=$ownerhost['host'].":".$ownerhost['port'];
  1451. $res=curltest($ipaddr,$senddata);
  1452. dump($res);*/
  1453. //将数据转发到不同的接口中
  1454. /* $sendapi="senddata".$rand;
  1455. echo $this->$sendapi($data[$rand],$devies[$rand]);*/
  1456. }
  1457. public function tuoBaoUrl(){
  1458. if ($this->request->isPost()) {
  1459. $data = input('');
  1460. $list = array();
  1461. if (is_array($data)) {
  1462. $list['data'] = json_encode($data, 320);
  1463. $getdata = json_decode($list['data'], true);//转成数组
  1464. } else {
  1465. $list['data'] = $data;
  1466. $getdata = json_decode($list['data'], true);//转成数组
  1467. }
  1468. $res = Db::name('tuobao_data')->insertGetId($list);//将推送的数据存入总表
  1469. Log::record('tuoBaoLora' . ':' . $res);
  1470. $tuoBaoData['deviceId'] = $getdata['deviceId'];
  1471. if(!empty($getdata['parseMsg'])){
  1472. Log::record('tuoBaoLoraParseMsg' . ':' . $getdata['parseMsg'][0]['method']);
  1473. }
  1474. $tuoBaoData['deviceId'] = $getdata['deviceId'];
  1475. return json_encode($data, 320);
  1476. }else{
  1477. $msg = input('msg');
  1478. return $msg;
  1479. }
  1480. }
  1481. public function senddata0($data){
  1482. //将数据存到总表
  1483. //将数据存到设备详细表
  1484. //查看当前设备属于哪家业主
  1485. //查找业主的服务器
  1486. //给服务器发数据
  1487. }
  1488. public function senddata1($data){
  1489. echo "m2".$data;
  1490. }
  1491. public function senddata2($data){
  1492. echo "m3".$data;
  1493. }
  1494. public function array_column1($rows, $column_key, $index_key = null) {
  1495. $data = array();
  1496. if (empty($index_key)) {
  1497. foreach ($rows as $row) {
  1498. $data[] = $row[$column_key];
  1499. }
  1500. $data=implode(",",$data);
  1501. } else {
  1502. foreach ($rows as $row) {
  1503. $data[$row[$index_key]] = $row[$column_key];
  1504. }
  1505. }
  1506. return $data;
  1507. }
  1508. public function SendDeviceAlarm($access_token,$openid,$address,$deviceid,$type,$time,$remark,$url){
  1509. $data = array(
  1510. "touser"=> $openid,
  1511. "template_id"=>"JkQYUiScfIuH9O4vvUgtgYxPIOBOY7Zh9w2DcWTgm50",
  1512. //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
  1513. "data"=> array(
  1514. "first"=> array(
  1515. "value"=> $address,
  1516. "color"=>"#44b549"
  1517. ),
  1518. "keyword1"=>array(
  1519. "value"=> $deviceid,
  1520. "color"=>"#173177"
  1521. ),
  1522. "keyword2"=> array(
  1523. "value"=> $type,
  1524. "color"=>"#173177"
  1525. ),
  1526. "keyword3"=> array(
  1527. "value"=> $time,
  1528. "color"=>"#173177"
  1529. ),
  1530. "remark"=>array(
  1531. "value"=> $remark,
  1532. "color"=>"#aaaaaa"
  1533. )
  1534. )
  1535. );
  1536. //{{first.DATA}}
  1537. //设备号:{{keyword1.DATA}}
  1538. //报警类型:{{keyword2.DATA}}
  1539. //时间:{{keyword3.DATA}}
  1540. //{{remark.DATA}}
  1541. $data = json_encode($data);
  1542. $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
  1543. $res = $this->curl($QUEST_URL,$data);
  1544. return json_decode($res,true);
  1545. }
  1546. public function curl($url,$data=''){
  1547. $ch = curl_init();
  1548. curl_setopt($ch, CURLOPT_URL, $url);
  1549. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  1550. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1551. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1552. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1553. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1554. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1555. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1556. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1557. $info = curl_exec($ch);
  1558. if (curl_errno($ch)) {
  1559. $info = 'ERROR: ' . curl_error($ch);
  1560. }
  1561. curl_close($ch);
  1562. return $info;
  1563. }
  1564. public function s_curl($url, $data = '')
  1565. {
  1566. $ch = curl_init();
  1567. curl_setopt($ch, CURLOPT_URL, $url);
  1568. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  1569. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1570. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1571. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1572. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1573. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1574. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1575. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1576. $info = curl_exec($ch);
  1577. if (curl_errno($ch)) {
  1578. $info = 'ERROR: ' . curl_error($ch);
  1579. }
  1580. curl_close($ch);
  1581. return $info;
  1582. }
  1583. public function get_curl($url, $data = '')
  1584. {
  1585. $ch = curl_init();
  1586. curl_setopt($ch, CURLOPT_URL, $url);
  1587. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  1588. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1589. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1590. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1591. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1592. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1593. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1594. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1595. $info = curl_exec($ch);
  1596. if (curl_errno($ch)) {
  1597. $info = 'ERROR: ' . curl_error($ch);
  1598. }
  1599. curl_close($ch);
  1600. return $info;
  1601. }
  1602. public function message_push_mh($topic,$msg,$qos){
  1603. vendor('phpMQTT.phpMQTT');
  1604. $server = '47.98.201.73'; // 服务器IP
  1605. $port = 1883; // 服务器端口
  1606. $username = 'usky'; // 用户名
  1607. $password = 'usky'; // 密码
  1608. $client_id = 'pub_' . uniqid();
  1609. $mqtt = new \phpMQTT($server, $port, $client_id);
  1610. if ($mqtt->connect(true, NULL, $username, $password)) {
  1611. $mqtt->publish($topic, $msg, $qos);
  1612. usleep(100000);
  1613. $mqtt->close();
  1614. Log::record('MH—NB-MQTT-Push success!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  1615. } else {
  1616. Log::record('MH—NB-MQTT-Time out!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  1617. }
  1618. }
  1619. public function mqqt_data_recombination($type, $device_code, $device_name, $electric_quantity, $signal, $water_gage,$dwtype)
  1620. {
  1621. if ($type == 'INFO') {
  1622. $status_valie = "0";
  1623. } else {
  1624. $status_valie = "1";
  1625. }
  1626. if ($dwtype==2){
  1627. $property = "水压";
  1628. }else{
  1629. $property = "水位";
  1630. }
  1631. $array = [
  1632. "devId" => $device_code,
  1633. "connType" => "G",
  1634. "deviceName" => $device_name,
  1635. "devs" => [
  1636. [
  1637. "dp" => [
  1638. [
  1639. "property" => "电量",
  1640. "serial" => "201",
  1641. "value" => '' . $electric_quantity
  1642. ],
  1643. [
  1644. "property" => "信号",
  1645. "serial" => "202",
  1646. "value" => '' . $signal
  1647. ],
  1648. [
  1649. "property" => $property,
  1650. "serial" => "203",
  1651. "value" => '' . $water_gage
  1652. ],
  1653. [
  1654. "property" => "状态",
  1655. "serial" => "204",
  1656. "value" => $status_valie
  1657. ]
  1658. ],
  1659. "deviceName" => $device_name,
  1660. "timeStamp" => "".time(),
  1661. "deviceModel" => "",
  1662. "deviceType" => "".$dwtype,
  1663. "devId" => $device_code
  1664. ]
  1665. ],
  1666. "type" => $type,
  1667. "timeStamp" => "".time()
  1668. ];
  1669. $json_data = json_encode($array);
  1670. return $json_data;
  1671. }
  1672. }