Order.php 96 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794
  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. $waterData["SubType"] = 2;
  655. $waterData["DeviceId"] = $data666['IMEI'];
  656. $waterData["InsertId"] = $insert_id66[0]['id'];
  657. $waterData["Confirmed"] = false;
  658. $data6 = json_encode($waterData);
  659. $rep_url = "http://47.98.201.187:55335/report";
  660. $res = $this->curl($rep_url, $data6);
  661. // $url_wx_push2 = "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Waterwarn/bj";
  662. foreach($op as $key => $o){
  663. if (!empty($o['openid']))
  664. {
  665. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  666. // s_curl($url_wx_push2,$data88);
  667. $Message['openid']=$o['openid'];
  668. $arr[$key]=$Message;
  669. }
  670. }
  671. }
  672. }
  673. }
  674. }
  675. if ($device['deviceType']=='Smoke') {
  676. if ($getdata["service"]['data']["DeviceStatus"]!== 7){
  677. $arr_bit = array(
  678. "","报警","报警静音","保留","低压","故障","保留"
  679. );
  680. $deviceStatus=$getdata["service"]['data']["DeviceStatus"];
  681. $data3 = array();
  682. $data3['device_code'] = $device["device_id"];
  683. $data3['time'] = date('Y-m-d H:i:s');
  684. $data3['status'] = 'y'.'0'.$deviceStatus;
  685. $data3['address'] = '180.101.147.89';
  686. $data3['compartment'] = '';
  687. $data3['fireprocess'] = '';
  688. $data3['flowendtime'] = '0000-00-00 00:00:00';
  689. $data3['ncmd'] = 'y'.'0'.$deviceStatus;
  690. $data3['data1'] = 'y'.'0'.$deviceStatus;
  691. $data3['data2'] = $arr_bit["{$deviceStatus}"];
  692. $data3['data3'] = '';
  693. $data3['data4'] = '烟雾'.$arr_bit["{$deviceStatus}"];
  694. $data3['data5'] = '';
  695. $sql = "INSERT INTO sp_hj2017 VALUES ('','','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}','{$data3['compartment']}',
  696. '{$data3['fireprocess']}', '{$data3['flowendtime']}', '{$data3['ncmd']}','{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','',''
  697. ,'','','','','','')";
  698. $res5 = addDataInfo($sql);
  699. $insert_id_sql="select id from sp_hj2017 where device_code='".$data3['device_code']."' and time='".$data3['time']."'";
  700. Log::record('sql66886688'.':'.$insert_id_sql);
  701. $insert_id66=add($insert_id_sql);
  702. // $sql66="update sp_devices_status set statustime=".$data3['time'];
  703. // Log::record('sql66'.':'.$sql66);
  704. // $update_id = addDataInfo($sql66);
  705. }
  706. $arr_h=array(1,2);
  707. if (in_array($getdata["service"]['data']["DeviceStatus"],$arr_h)){
  708. $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"]}'";
  709. Log::record('sql3'.':'.$sql3);
  710. $company = add($sql3);
  711. Log::record('company'.':'.$company[0]['company']);
  712. $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%'";
  713. Log::record('sql4'.':'.$sql4);
  714. $union_sql=add($sql4);
  715. $openids=$this->array_column1($union_sql,"phone");
  716. if (empty($openids))
  717. {
  718. echo 'open is null';
  719. exit;
  720. }
  721. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  722. $op=add($sql5);
  723. $access_token=GetAccess_Token();
  724. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  725. $deviceid=$device["device_id"];
  726. $type='烟雾';
  727. $time=$data3['time'];
  728. $remark='设备名称:'.$company[0]['owner_name'];
  729. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/bj";
  730. Log::record('data666_url_wx_push2'.':'.$url_wx_push2);
  731. $data666['phone']=$openids;
  732. Log::record('data666_phone'.':'.$data666['phone']);
  733. $data666['IMEI']=$device["device_id"];
  734. Log::record('data666_IMEI'.':'.$data666['IMEI']);
  735. $data666['time']=$data3['time'];
  736. Log::record('data666_time'.':'.$data666['time']);
  737. $data666['evt']=$data3['data1'];
  738. Log::record('data666_evt'.':'.$data666['evt']);
  739. $data666['insert_id']=$insert_id66[0]['id'];
  740. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  741. $this->curl($url_wx_push2,$data666);
  742. foreach($op as $key => $o){
  743. if (!empty($o['openid']))
  744. {
  745. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  746. // s_curl($url_wx_push2,$data88);
  747. $Message['openid']=$o['openid'];
  748. $arr[$key]=$Message;
  749. }
  750. }
  751. }
  752. } elseif($device['deviceType']=='MultiSensor') {
  753. if($getdata["service"]['data']["alarmBoolean"]== 17){
  754. // $sql1="SELECT * from 'dev_' . {$device["device_id"]} where addtime=(select max(addtime) from 'dev_' . {$device["device_id"]})";
  755. // $Model = M();
  756. // $result = $Model->query($sql1);
  757. // $service=json_decode($result['']['service']);
  758. $unit=dechex($getdata["service"]['data']["unit"]);
  759. $data3 = array();
  760. $data3['device_code'] = $device["device_id"];
  761. $data3['time'] = date('Y-m-d H:i:s');
  762. $data3['status'] = $getdata["service"]['data']["alarmBoolean"];
  763. $data3['address'] = '180.101.147.89';
  764. $data3['ncmd'] = '';
  765. $data3['unit'] =$getdata["service"]['data']["unit"];
  766. $arr_y=array('10','11','20','21','22');
  767. $sql11="SELECT dwtype FROM sp_owner WHERE owner_code ='{$device["device_id"]}'";
  768. $dwtype = add($sql11);
  769. if ($dwtype[0]['dwtype'] == 5){
  770. if ($data3['unit']== 1){
  771. $data3['data1']='LL2';
  772. }elseif($data3['unit']== 2){
  773. $data3['data1']='LL1';
  774. }else{
  775. $data3['data1']='LL0';
  776. }
  777. }else {
  778. if ($data3['unit']== 1){
  779. $data3['data1']='WP2';
  780. }elseif($data3['unit']== 2){
  781. $data3['data1']='WP1';
  782. }else{
  783. $data3['data1']='WP0';
  784. }
  785. }
  786. $data3['data2'] = $getdata["service"]['data']["batteyValue"];
  787. $data3['data3'] = '';
  788. switch ($unit){
  789. case '10':
  790. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  791. break;
  792. case '11':
  793. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  794. break;
  795. case '20':
  796. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  797. break;
  798. case '21':
  799. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  800. break;
  801. case '22':
  802. $data3['data4'] = $getdata["service"]['data']["data"];
  803. break;
  804. case '30':
  805. $data3['data4'] = $getdata["service"]['data']["data"]*0.0000001;
  806. break;
  807. case '31':
  808. $data3['data4'] = $getdata["service"]['data']["data"]*0.000001;
  809. break;
  810. case '40':
  811. $data3['data4'] = $getdata["service"]['data']["data"]*0.00001;
  812. break;
  813. case '41':
  814. $data3['data4'] = $getdata["service"]['data']["data"]*0.0001;
  815. break;
  816. case '42':
  817. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  818. break;
  819. case '50':
  820. $data3['data4'] = $getdata["service"]['data']["data"]*0.01;
  821. break;
  822. case '51':
  823. $data3['data4'] = $getdata["service"]['data']["data"]*0.1;
  824. break;
  825. case '52':
  826. $data3['data4'] = $getdata["service"]['data']["data"];
  827. break;
  828. case '2':
  829. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  830. break;
  831. case '1':
  832. $data3['data4'] = $getdata["service"]['data']["data"]*0.001;
  833. break;
  834. }
  835. $data3['data5'] = '';
  836. $sql = "INSERT INTO sp_sj2017 VALUES ('','tp_water_pressure','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}', '{$data3['ncmd']}'
  837. ,'{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','','','','','','','','')";
  838. // Log::record('sql123'.':'.$sql);
  839. $insert_id = addDataInfo($sql);
  840. $insert_id_sql="select id from sp_sj2017 where device_code='".$data3['device_id']."' and time='".$data3['time']."'";
  841. Log::record('sql987253232'.':'.$insert_id_sql);
  842. $insert_id66=add($insert_id_sql);
  843. if ($dwtype[0]['dwtype'] == 5) {
  844. if ($getdata["service"]['data']["unit"] == '1') {
  845. $msg['evt'] = "WP2";
  846. $msg['msg'] = "高水位";
  847. } elseif ($getdata["service"]['data']["unit"] == '2') { // high
  848. $msg['evt'] = "WP1";
  849. $msg['msg'] = "低水位";
  850. }
  851. }else{
  852. if ($getdata["service"]['data']["unit"] == '1') {
  853. $msg['evt'] = "WP2";
  854. $msg['msg'] = "高压";
  855. } elseif ($getdata["service"]['data']["unit"] == '2') { // high
  856. $msg['evt'] = "WP1";
  857. $msg['msg'] = "低压";
  858. }
  859. }
  860. if ( $msg['evt']=='WP1' || $msg['evt']=='WP2' ) {
  861. $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"]}'";
  862. $company = add($sql3);
  863. $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%'";
  864. $union_sql=add($sql4);
  865. Log::record('sql12345'.':'.$sql4);
  866. $openids=$this->array_column1($union_sql,"phone");
  867. if (empty($openids))
  868. {
  869. echo 'open is null';
  870. exit;
  871. }
  872. Log::record('data666_phone'.':'.$openids);
  873. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  874. $op=add($sql5);
  875. $access_token=GetAccess_Token();
  876. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  877. $deviceid=$device["device_id"];
  878. $type=$msg['msg'];
  879. $time=$data3['time'];
  880. $remark='设备名称:'.$company[0]['owner_name'];
  881. //
  882. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/message_water";
  883. Log::record('data666_url_wx_push2'.':'. $url_wx_push2);
  884. $data666['IMEI']=$device["device_id"];
  885. Log::record('data666_IMET'.':'.$data666['IMEI']);
  886. $data666['phone']=$openids;
  887. Log::record('data666_phone'.':'.$data666['phone']);
  888. $data666['evt']=$data3['data1'];
  889. Log::record('data666_evt'.':'.$data666['evt']);
  890. $data666['insert_id']=$insert_id66[0]['id'];
  891. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  892. $data666['time']=$data3['time'];
  893. // Log::record('data666'.':'.$data666);
  894. $sql88="UPDATE sp_devices_status SET statustime ='".$data3['time']."' where deviceid='".$data666['IMEI']."'";
  895. addDataInfo($sql88);
  896. $this->curl($url_wx_push2,$data666);
  897. // $url_wx_push2 = "http://www.jd-ioe.com/jdxf/wxapp2.php/Home/Waterwarn/bj";
  898. foreach($op as $key => $o){
  899. if (!empty($o['openid']))
  900. {
  901. // $data88['openid']=$o['openid'];
  902. // $data88['address']=$address;
  903. // $data88['deviceid']=$deviceid;
  904. // $data88['type']=$type;
  905. // $data88['time']=$time;
  906. // $data88['remark']=$remark;
  907. //token,微信id,地址,设备号,类型,时间,设备名称(或者其他内容)
  908. //{{first.DATA}}$address
  909. //设备号:{{keyword1.DATA}}$deviceid
  910. //报警类型:{{keyword2.DATA}}$type
  911. //时间:{{keyword3.DATA}}$time
  912. //{{remark.DATA}}$remark
  913. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  914. // s_curl($url_wx_push2,$data88);
  915. $Message['openid']=$o['openid'];
  916. $arr[$key]=$Message;
  917. }
  918. }
  919. }
  920. }
  921. }
  922. }
  923. return json_encode($data, 320);
  924. }
  925. }
  926. public function addtuobaodata(){
  927. if ($this->request->isPost()) {
  928. $data = input('');
  929. $list = array();
  930. if (is_array($data)) {
  931. $list['data'] = json_encode($data, 320);
  932. $getdata = json_decode($list['data'], true);//转成数组
  933. $list['type'] = 1;
  934. } else {
  935. $list['data'] = $data;
  936. $getdata = json_decode($list['data'], true);//转成数组
  937. $list['type'] = 2;
  938. }
  939. // $list['data']=11;
  940. $list['response'] = "222";
  941. $list['notifyType'] = $getdata["notifyType"];
  942. $list['deviceId'] = $getdata["deviceId"];
  943. $list['addtime'] = date('Y-m-d H:i:s');
  944. $res = Db::name('noticedata')->insertGetId($list);//将推送的数据存入总表
  945. if (trim($getdata["notifyType"]) == "deviceDataChanged") {
  946. $device = Db::name('device')->where('iot_id', trim($getdata["deviceId"]))->find();
  947. Log::record('tuobaoceshi123' . ':' . $device["device_id"]);
  948. $datainfo = array();
  949. $datainfo['notifyType'] = $getdata["notifyType"];
  950. $datainfo['device_id'] = $device["device_id"];
  951. $datainfo['iot_id'] = $getdata["deviceId"];
  952. $datainfo['service'] = json_encode($getdata["service"]);
  953. $datainfo['addtime'] = date('Y-m-d H:i:s');
  954. $res1 = Db::table('dev_' . $device["device_id"])->insertGetId($datainfo);
  955. if ($device['deviceType']=='SaiTeSmoke') {
  956. $data3 = array();
  957. $data3['device_id'] = $device["device_id"];
  958. $data3['RSRQ'] = $getdata["service"]['data']['RSRP'];
  959. $data3['CSQ'] = $getdata["service"]['data']["CSQ"];
  960. $data3['DeviceType'] = 1;
  961. if ($getdata["service"]['data']["Alarm_Status"]!== 7) {
  962. $data3['DeviceStatus'] = 1;
  963. }else{
  964. $data3['DeviceStatus'] = 7;
  965. }
  966. $data3['BatteryLevel'] = $getdata["service"]['data']["Battery_Level"];
  967. $data3['SensorLevel'] = $getdata["service"]['data']["AlarmLevel"];
  968. $data3['TData'] = $getdata["service"]['data']["TData"];
  969. $data3['eventTime'] = $getdata["service"]["eventTime"];
  970. $data3['storeTime'] = date('Y-m-d H:i:s');
  971. $sql = "INSERT INTO sp_y{$data3['device_id']} VALUES ('','{$data3['device_id']}',
  972. '{$data3['RSRQ']}', '{$data3['CSQ']}', '{$data3['DeviceType']}','{$data3['DeviceStatus']}','{$data3['BatteryLevel']}','{$data3['SensorLevel']}','{$data3['eventTime']}','{$data3['storeTime']}','{$data3['TData']}')";
  973. $res3 = addDataInfo($sql);
  974. if ($getdata["service"]['data']["Alarm_Status"]!== 7){
  975. $arr_bit = array(
  976. "","报警","静音","保留","低压","故障","数据异常","","设备收到单次静音指令","设备收到连续静音指令","拆卸报警","拆卸恢复","","","模拟报警"
  977. );
  978. $deviceStatus=$getdata["service"]['data']["Alarm_Status"];
  979. $data3 = array();
  980. $data3['device_code'] = $device["device_id"];
  981. $data3['time'] = date('Y-m-d H:i:s');
  982. $data3['status'] = 'y'.'0'.$deviceStatus;
  983. $data3['address'] = '180.101.147.89';
  984. $data3['compartment'] = '';
  985. $data3['fireprocess'] = '';
  986. $data3['flowendtime'] = '0000-00-00 00:00:00';
  987. $data3['ncmd'] = 'y'.'01';
  988. $data3['data1'] = 'y'.'01';
  989. $data3['data2'] = $arr_bit["{$deviceStatus}"];
  990. $data3['data3'] = '';
  991. $data3['data4'] = '烟雾'.$arr_bit["{$deviceStatus}"];
  992. $data3['data5'] = '';
  993. $sql = "INSERT INTO sp_hj2017 VALUES ('','','{$data3['device_code']}','{$data3['time']}','{$data3['status']}','{$data3['address']}','{$data3['compartment']}',
  994. '{$data3['fireprocess']}', '{$data3['flowendtime']}', '{$data3['ncmd']}','{$data3['data1']}','{$data3['data2']}','{$data3['data3']}','{$data3['data4']}','{$data3['data5']}','','',''
  995. ,'','','','','','')";
  996. $res5 = addDataInfo($sql);
  997. $insert_id_sql="select id from sp_hj2017 where device_code='".$data3['device_code']."' and time='".$data3['time']."'";
  998. Log::record('sql6688668866'.':'.$insert_id_sql);
  999. $insert_id66=add($insert_id_sql);
  1000. $arr_h=array(1,2,6,8,9,10,11,14);
  1001. if (in_array($getdata["service"]['data']["Alarm_Status"],$arr_h)){
  1002. $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"]}'";
  1003. Log::record('sql3'.':'.$sql3);
  1004. $company = add($sql3);
  1005. Log::record('company'.':'.$company[0]['company']);
  1006. $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%'";
  1007. Log::record('sql4'.':'.$sql4);
  1008. $union_sql=add($sql4);
  1009. $openids=$this->array_column1($union_sql,"phone");
  1010. if (empty($openids))
  1011. {
  1012. echo 'open is null';
  1013. exit;
  1014. }
  1015. $sql5="SELECT openid FROM sp_wx_customuser WHERE type='wx' and idphone in($openids)";
  1016. $op=add($sql5);
  1017. $access_token=GetAccess_Token();
  1018. $address='警报地址:'.$company[0]['owner_address'].$company[0]['unitinfo'];
  1019. $deviceid=$device["device_id"];
  1020. $type='烟雾';
  1021. $time=$data3['time'];
  1022. $remark='设备名称:'.$company[0]['owner_name'];
  1023. if($getdata["service"]['data']["Alarm_Status"]==5){
  1024. $deviceStatus1='y05';
  1025. }elseif ($getdata["service"]['data']["Alarm_Status"]==1){
  1026. $deviceStatus1='y01';
  1027. }elseif ($getdata["service"]['data']["Alarm_Status"]==10){
  1028. $deviceStatus1='y10';
  1029. }elseif ($getdata["service"]['data']["Alarm_Status"]==14){
  1030. $deviceStatus1='y12';
  1031. }
  1032. $url_wx_push2 = "https://iot.usky.cn/jdxf/wxapp2.php/Home/Waterwarn/bj";
  1033. Log::record('data666_url_wx_push2'.':'.$url_wx_push2);
  1034. $data666['phone']=$openids;
  1035. Log::record('data666_phone'.':'.$data666['phone']);
  1036. $data666['IMEI']=$device["device_id"];
  1037. Log::record('data666_IMEI'.':'.$data666['IMEI']);
  1038. $data666['time']=$data3['time'];
  1039. Log::record('data666_time'.':'.$data666['time']);
  1040. $data666['evt']=$deviceStatus1;
  1041. Log::record('data666_evt'.':'.$data666['evt']);
  1042. $data666['insert_id']=$insert_id66[0]['id'];
  1043. Log::record('data666_insert_id'.':'.$data666['insert_id']);
  1044. $this->curl($url_wx_push2,$data666);
  1045. foreach($op as $key => $o){
  1046. if (!empty($o['openid']))
  1047. {
  1048. $Message=$this->SendDeviceAlarm($access_token,$o['openid'],$address,$deviceid,$type,$time,$remark,$url='');
  1049. // s_curl($url_wx_push2,$data88);
  1050. $Message['openid']=$o['openid'];
  1051. $arr[$key]=$Message;
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. }
  1058. }
  1059. }
  1060. public function jingan(){
  1061. if ($this->request->isPost()) {
  1062. Log::record('jinganxiaofang' . ':ceshi');
  1063. $data = input('');
  1064. /* $rand=rand(0,2);
  1065. $header=["Content-Type:application/json"];
  1066. //将数据分配给不同的服务
  1067. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1068. $server=$httpserver[$rand];
  1069. //将数据随机转发到别的平台做处理
  1070. $res=curltest($server,$data,$header);
  1071. */
  1072. $list = array();
  1073. if (is_array($data)) {
  1074. $list['data'] = json_encode($data, 320);
  1075. $getdata = json_decode($list['data'], true);//转成数组
  1076. } else {
  1077. $list['data'] = $data;
  1078. $getdata = json_decode($list['data'], true);//转成数组
  1079. }
  1080. // $list['data']=11;
  1081. // $list['response'] = "111";
  1082. //
  1083. // $list['notifyType'] = $getdata["notifyType"];
  1084. // $list['deviceId'] = $getdata["deviceId"];
  1085. // $list['addtime'] = date('Y-m-d H:i:s');
  1086. $res = Db::name('jingandata')->insertGetId($list);//将推送的数据存入总表
  1087. Log::record('jinganxiaofang' . ':' . $res);
  1088. $Data = array();
  1089. $Data['app_key'] = $getdata["app_key"];
  1090. $Data['timestamp'] = date('Y-m-d H:i:s',$getdata["timestamp"]);
  1091. $Data['format'] = $getdata["format"];
  1092. $Data['v'] = $getdata["v"];
  1093. $Data['sign_method'] = $getdata["sign_method"];
  1094. $Data['pid'] = $getdata["pid"];
  1095. $Data['bid'] = $getdata["bid"];
  1096. $Data['fid'] = $getdata["fid"];
  1097. $Data['supervise_id'] = $getdata["supervise_id"];
  1098. if (empty($getdata["supervise_code"])){
  1099. $Data['supervise_code']="";
  1100. }else{
  1101. $Data['supervise_code'] = $getdata["supervise_code"];
  1102. }
  1103. $Data['status'] = $getdata["status"];
  1104. $Data['content'] = $getdata["content"];
  1105. $Data['type'] = $getdata["type"];
  1106. if (empty($getdata["device_id"])){
  1107. $Data['device_id']="";
  1108. }else{
  1109. $Data['device_id'] = $getdata["device_id"];
  1110. }
  1111. if (empty($getdata["aid"])){
  1112. $Data['aid']="";
  1113. }else{
  1114. $Data['aid'] = $getdata["aid"];
  1115. }
  1116. if (empty($getdata["loop_number"])){
  1117. $Data['loop_number']="";
  1118. }else{
  1119. $Data['loop_number'] = $getdata["loop_number"];
  1120. }
  1121. if (empty($getdata["position_number"])){
  1122. $Data['position_number']="";
  1123. }else{
  1124. $Data['position_number'] = $getdata["position_number"];
  1125. }
  1126. if (empty($getdata["mainframe_id"])){
  1127. $Data['mainframe_id']="";
  1128. }else{
  1129. $Data['mainframe_id'] = $getdata["mainframe_id"];
  1130. }
  1131. $Data['sign'] = $getdata["sign"];
  1132. if ($Data['app_key']=="f8bdea7926bcc7cb827d2eccf67fa559"){
  1133. $sql3="select device_name,company_code from sp_huangpu_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1134. }else if ($Data['app_key']=="9e555b8b2244ebc1f59695824b86c5a0"){
  1135. $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";
  1136. }
  1137. $company = add($sql3);
  1138. $Data['company_code'] = $company[0]['company_code'];
  1139. $res1 = Db::name('jinganAnalysisData')->insertGetId($Data);
  1140. $url_wx_data = "https://iot.usky.cn/jdxf/wxapp2.php/Home/WorkOrderNotice/message_workordernotice";
  1141. $data_wx['company_code'] = $company[0]['company_code'];
  1142. $data_wx['time']=$Data['timestamp'];
  1143. $data_wx['title']=$Data['content'];
  1144. $data_wx['id']=$res1;
  1145. $data_wx['types']=2;
  1146. if($getdata["status"]==0){
  1147. $this->s_curl($url_wx_data,$data_wx);
  1148. }
  1149. return "success";
  1150. }
  1151. }
  1152. public function jinganPerson(){
  1153. if ($this->request->isPost()) {
  1154. Log::record('jinganxiaofangren' . ':ceshi');
  1155. $data = input('');
  1156. $list = array();
  1157. $data_list = array();
  1158. if (is_array($data)) {
  1159. $list['data'] = json_encode($data, 320);
  1160. $getdata = json_decode($list['data'], true);//转成数组
  1161. } else {
  1162. $list['data'] = $data;
  1163. $getdata = json_decode($list['data'], true);//转成数组
  1164. }
  1165. // $list['data']=11;
  1166. // $list['response'] = "111";
  1167. $res = Db::name('jinganperson')->insertGetId($list);//将推送的数据存入总表
  1168. Log::record('jinganxiaofangren' . ':' . $res);
  1169. $data_list['app_key'] = $getdata["app_key"];
  1170. $data_list['timestamp'] = date('Y-m-d H:i:s',$getdata["timestamp"]);
  1171. $data_list['format'] = $getdata["format"];
  1172. $data_list['version'] = $getdata["v"];
  1173. $data_list['sign_method'] = $getdata["sign_method"];
  1174. $data_list['artificial_id'] = $getdata["artificial_id"];
  1175. $data_list['pid'] = $getdata["pid"];
  1176. $data_list['artificial_code'] = $getdata["artificial_code"];
  1177. $data_list['company_id'] = $getdata["company_id"];
  1178. $data_list['content'] = $getdata["content"];
  1179. $data_list['status'] = $getdata["status"];
  1180. $data_list['sign'] = $getdata["sign"];
  1181. $device = Db::name('person_data')->where('artificial_id', trim( $data_list['artificial_id']))->find();
  1182. if($device){
  1183. return "success";
  1184. }else{
  1185. if ($data_list['app_key']=="f8bdea7926bcc7cb827d2eccf67fa559"){
  1186. $sql3="select device_name,company_code from sp_huangpu_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1187. }else if ($data_list['app_key']=="9e555b8b2244ebc1f59695824b86c5a0"){
  1188. $sql3="select device_name,company_code from sp_jingan_share where pid='{$getdata["pid"]}' GROUP BY company_code";
  1189. }
  1190. $company = add($sql3);
  1191. $data_list['company_code'] = $company[0]['company_code'];
  1192. $res1 = Db::name('person_data')->insertGetId($data_list);//将推送的数据存入解析表
  1193. Log::record('person_data_list'.':'.$getdata["content"]);
  1194. $url_wx_data = "https://iot.usky.cn/jdxf/wxapp2.php/Home/WorkOrderNotice/message_workordernotice";
  1195. $data_wx['company_code']=$company[0]['company_code'];
  1196. $data_wx['time']=$data_list['timestamp'];
  1197. $data_wx['title']=$data_list['content'];
  1198. $data_wx['id']=$res1;
  1199. $data_wx['types']=1;
  1200. $this->s_curl($url_wx_data,$data_wx);
  1201. return "success";
  1202. }
  1203. }
  1204. }
  1205. public function aepceshi(){
  1206. if ($this->request->isPost()) {
  1207. Log::record('aep' . ':ceshi');
  1208. $data = input('');
  1209. /* $rand=rand(0,2);
  1210. $header=["Content-Type:application/json"];
  1211. //将数据分配给不同的服务
  1212. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1213. $server=$httpserver[$rand];
  1214. //将数据随机转发到别的平台做处理
  1215. $res=curltest($server,$data,$header);
  1216. */
  1217. $list = array();
  1218. if (is_array($data)) {
  1219. $list['data'] = json_encode($data, 320);
  1220. $getdata = json_decode($list['data'], true);//转成数组
  1221. } else {
  1222. $list['data'] = $data;
  1223. $getdata = json_decode($list['data'], true);//转成数组
  1224. }
  1225. // $list['data']=11;
  1226. // $list['response'] = "111";
  1227. //
  1228. // $list['notifyType'] = $getdata["notifyType"];
  1229. // $list['deviceId'] = $getdata["deviceId"];
  1230. // $list['addtime'] = date('Y-m-d H:i:s');
  1231. $res = Db::name('nb_aep')->insertGetId($list);//将推送的数据存入总表
  1232. Log::record('aep' . ':' . $res);
  1233. return json_encode($data, 320);
  1234. }
  1235. }
  1236. public function longHuConfigure(){
  1237. if ($this->request->isPost()) {
  1238. $data = input('');
  1239. $list = array();
  1240. $set_data = array();
  1241. if (is_array($data)) {
  1242. $list['data'] = json_encode($data, 320);
  1243. $getdata = json_decode($list['data'], true);//转成数组
  1244. } else {
  1245. $list['data'] = $data;
  1246. $getdata = json_decode($list['data'], true);//转成数组
  1247. }
  1248. if ($getdata["dataType"]=="setting"){
  1249. $sql="SELECT owner_code FROM sp_owner WHERE id ={$getdata["sensorId"]}";
  1250. $owner_code = add($sql);
  1251. $imei='&imei='.$owner_code[0]['owner_code'];
  1252. if ($getdata["deviceType"]=="XiaoFangShuiYa"){
  1253. $deviceType='&deviceType='.'1';
  1254. }elseif ($getdata["deviceType"]=="YeWei"){
  1255. $deviceType='&deviceType='.'2';
  1256. }
  1257. $sendingGap = '';
  1258. if (!empty($getdata["sendTime"])){
  1259. $sendingGap = '&sendingGap='.$getdata["sendTime"];
  1260. }
  1261. $unipathThresholdLimit = '';
  1262. if (!empty($getdata["lowThreshold"])){
  1263. $unipathThresholdLimit = '&unipathThresholdLimit='.$getdata["lowThreshold"]*1000;
  1264. }
  1265. $unipathThresholdUpper = '';
  1266. if (!empty($getdata["highThreshold"])){
  1267. $unipathThresholdUpper = '&unipathThresholdUpper='.$getdata["highThreshold"]*1000;
  1268. }
  1269. $data6 = json_encode($set_data);
  1270. $rep_url = "https://api.topsailiot.com/dmp/deviceConfig?appkey=3a3e533818a9604dfec0388b2d817dbc2f9f7b05dd4b153c403897cd93a9c04d0edfe3d86ebd3ad0&userid=TS2020120110122980052".$imei.$deviceType.$sendingGap.$unipathThresholdLimit.$unipathThresholdUpper;
  1271. $res = $this->s_curl($rep_url,'');
  1272. Log::record('longhu' . ':' . $rep_url);
  1273. return $res;
  1274. }else{
  1275. $res_arr = array();
  1276. $res_arr['code'] = 60001;
  1277. $res_arr['msg'] = "dataType参数有误";
  1278. $res_arr['data'] = "";
  1279. $res = json_encode($res_arr);
  1280. Log::record('longhuerror' . ':' . $res);
  1281. return $res;
  1282. }
  1283. }
  1284. }
  1285. public function longhudata(){
  1286. if ($this->request->isPost()) {
  1287. $data = input('');
  1288. $list = array();
  1289. $set_data = array();
  1290. if (is_array($data)) {
  1291. $list['data'] = json_encode($data, 320);
  1292. $getdata = json_decode($list['data'], true);//转成数组
  1293. } else {
  1294. $list['data'] = $data;
  1295. $getdata = json_decode($list['data'], true);//转成数组
  1296. }
  1297. if ($getdata["user_from"]=="longHuWater"){
  1298. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1299. $longHuCompany = add($sql6);
  1300. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  1301. $sensorList['status'] = 0;
  1302. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力正常';
  1303. $data4['deviceType'] = 'XiaoFangShuiYa';
  1304. $sensorList['unit'] = 'MPa';
  1305. $data4['dataType'] = 'deviceInfo';
  1306. $data4['provider'] = 'Usky';
  1307. $data4['softVersion'] = '1.00.00';
  1308. $data4['hardVersion'] = '0.1';
  1309. $sensorList['pointValue'] = (double)$getdata['device_value'];
  1310. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1311. if (!empty($deviceConfigure)) {
  1312. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1313. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1314. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1315. } else {
  1316. $sensorList['lowThreshold'] = 0;
  1317. $sensorList['highThreshold'] = 0;
  1318. $sensorList['sendTime'] = 1800;
  1319. }
  1320. $sensorListArray[0] = $sensorList;
  1321. $data4['sensorList'] = $sensorListArray;
  1322. if ($longHuCompany[0]['company']=='10232'){
  1323. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1324. }else{
  1325. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db6';
  1326. }
  1327. $data5['action'] = 'transpush';
  1328. $data5['data'] = $data4;
  1329. $data6 = json_encode($data5);
  1330. $rep_url = "http://47.98.201.73:55126/report";
  1331. $res = $this->curl($rep_url, $data6);
  1332. Log::record('minhanglonghuduijie' . ':' . $res);
  1333. $res_arr = array();
  1334. $res_arr['code'] = 200;
  1335. $res_arr['msg'] = "操作成功";
  1336. $res_arr['data'] = "";
  1337. $res1 = json_encode($res_arr);
  1338. return $res1;
  1339. }else if ($getdata["user_from"]=="longHuWaterAlarm"){
  1340. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1341. $longHuCompany = add($sql6);
  1342. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  1343. $sql7="select * from sp_sj2017 where id ={$getdata['device_value']}";
  1344. $longHuAlarm = add($sql7);
  1345. if ($longHuAlarm[0]['data1']=='WP1'){
  1346. $sensorList['status'] = 3;
  1347. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力低于下限';
  1348. }elseif ($longHuAlarm[0]['data1']=='WP2'){
  1349. $sensorList['status'] = 4;
  1350. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-压力超过上限';
  1351. }elseif ($longHuAlarm[0]['data1']=='WP3'){
  1352. $sensorList['status'] = 1;
  1353. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$longHuCompany[0]['owner_name'].'-设备故障';
  1354. }
  1355. $data4['deviceType'] = 'XiaoFangShuiYa';
  1356. $sensorList['unit'] = 'MPa';
  1357. $data4['dataType'] = 'deviceInfo';
  1358. $data4['softVersion'] = '1.00.00';
  1359. $data4['hardVersion'] = '0.1';
  1360. $sensorList['pointValue'] = (double)$longHuAlarm[0]['data4'];
  1361. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1362. if (!empty($deviceConfigure)) {
  1363. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1364. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1365. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1366. } else {
  1367. $sensorList['lowThreshold'] = 0;
  1368. $sensorList['highThreshold'] = 0;
  1369. $sensorList['sendTime'] = 1800;
  1370. }
  1371. $sensorListArray[0] = $sensorList;
  1372. $data4['sensorList'] = $sensorListArray;
  1373. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1374. $data5['action'] = 'transpush';
  1375. $data5['data'] = $data4;
  1376. $data6 = json_encode($data5);
  1377. $rep_url = "http://47.98.201.73:55126/report";
  1378. $res = $this->curl($rep_url, $data6);
  1379. Log::record('minhanglonghuduijie' . ':' . $res);
  1380. $res_arr = array();
  1381. $res_arr['code'] = 200;
  1382. $res_arr['msg'] = "操作成功";
  1383. $res_arr['data'] = "";
  1384. $res1 = json_encode($res_arr);
  1385. return $res1;
  1386. }else if ($getdata["user_from"]=="xingYueHuiRtuWater"){
  1387. Log::record('xingyuehui' . ':' . $getdata["device_code"]);
  1388. $sql6="select * from sp_owner where owner_code ='{$getdata["device_code"]}'";
  1389. $longHuCompany = add($sql6);
  1390. if($getdata["device_type"]=="water"){
  1391. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'];
  1392. }else{
  1393. $sensorList['sensorId'] = (int)$longHuCompany[0]['id'].(int)$getdata["port"];
  1394. }
  1395. $sensorList['status'] = 0;
  1396. $sensorList['desc'] = $longHuCompany[0]['unitinfo'].'-'.$getdata["port_name"].'-压力正常';
  1397. $data4['deviceType'] = 'XiaoFangShuiYa';
  1398. $sensorList['unit'] = 'MPa';
  1399. $data4['dataType'] = 'deviceInfo';
  1400. $data4['provider'] = 'Usky';
  1401. $data4['softVersion'] = '1.00.00';
  1402. $data4['hardVersion'] = '0.1';
  1403. $sensorList['pointValue'] = (double)$getdata['device_value'];
  1404. $deviceConfigure = Db::name('deviceConfigure')->where('device_code', trim($getdata["device_code"]))->find();
  1405. if (!empty($deviceConfigure)) {
  1406. $sensorList['lowThreshold'] = (double)$deviceConfigure['low_threshold'];
  1407. $sensorList['highThreshold'] = (double)$deviceConfigure['high_threshold'];
  1408. $sensorList['sendTime'] = (int)$deviceConfigure['send_time'];
  1409. } else {
  1410. $sensorList['lowThreshold'] = 0;
  1411. $sensorList['highThreshold'] = 0;
  1412. $sensorList['sendTime'] = 1800;
  1413. }
  1414. $sensorListArray[0] = $sensorList;
  1415. $data4['sensorList'] = $sensorListArray;
  1416. if ($longHuCompany[0]['company']=='10232'){
  1417. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db5';
  1418. }else{
  1419. $data5['mqttId'] = 'a71c40bb-5b02-4741-881b-24449f699db6';
  1420. }
  1421. $data5['action'] = 'transpush';
  1422. $data5['data'] = $data4;
  1423. $data6 = json_encode($data5);
  1424. $rep_url = "http://47.98.201.73:55126/report";
  1425. $res = $this->curl($rep_url, $data6);
  1426. Log::record('minhanglonghuduijie' . ':' . $res);
  1427. $res_arr = array();
  1428. $res_arr['code'] = 200;
  1429. $res_arr['msg'] = "操作成功";
  1430. $res_arr['data'] = "";
  1431. $res1 = json_encode($res_arr);
  1432. return $res1;
  1433. }else{
  1434. $res_arr = array();
  1435. $res_arr['code'] = 60001;
  1436. $res_arr['msg'] = "user_from参数有误";
  1437. $res_arr['data'] = "";
  1438. $res = json_encode($res_arr);
  1439. Log::record('longhuerror' . ':' . $res);
  1440. return $res;
  1441. }
  1442. }
  1443. }
  1444. public function testdata1(){
  1445. $rand=rand(0,2);
  1446. $data=[
  1447. '{"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"}}',
  1448. '{"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"}}',
  1449. '{"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"}}',
  1450. ];
  1451. $header=["Content-Type:application/json"];
  1452. //将数据分配给不同的服务
  1453. $httpserver=["http://47.98.153.212:1101","http://47.98.153.212:1102","http://47.98.153.212:1103"];
  1454. $senddata=$data[$rand];
  1455. $server=$httpserver[$rand];
  1456. $res=curltest($server,$senddata,$header);
  1457. dump($res);
  1458. /* //设备所属公司
  1459. $owner=Db::name('device')->field("owner_id")->where('iot_id',$iot_id)->find();
  1460. $ownerhost=Db::name('owner')->where('id',$owner["owner_id"])->find();
  1461. $ipaddr=$ownerhost['host'].":".$ownerhost['port'];
  1462. $res=curltest($ipaddr,$senddata);
  1463. dump($res);*/
  1464. //将数据转发到不同的接口中
  1465. /* $sendapi="senddata".$rand;
  1466. echo $this->$sendapi($data[$rand],$devies[$rand]);*/
  1467. }
  1468. public function tuoBaoUrl(){
  1469. if ($this->request->isPost()) {
  1470. $data = input('');
  1471. $list = array();
  1472. if (is_array($data)) {
  1473. $list['data'] = json_encode($data, 320);
  1474. $getdata = json_decode($list['data'], true);//转成数组
  1475. } else {
  1476. $list['data'] = $data;
  1477. $getdata = json_decode($list['data'], true);//转成数组
  1478. }
  1479. $res = Db::name('tuobao_data')->insertGetId($list);//将推送的数据存入总表
  1480. Log::record('tuoBaoLora' . ':' . $res);
  1481. $tuoBaoData['deviceId'] = $getdata['deviceId'];
  1482. if(!empty($getdata['parseMsg'])){
  1483. Log::record('tuoBaoLoraParseMsg' . ':' . $getdata['parseMsg'][0]['method']);
  1484. }
  1485. $tuoBaoData['deviceId'] = $getdata['deviceId'];
  1486. return json_encode($data, 320);
  1487. }else{
  1488. $msg = input('msg');
  1489. return $msg;
  1490. }
  1491. }
  1492. public function senddata0($data){
  1493. //将数据存到总表
  1494. //将数据存到设备详细表
  1495. //查看当前设备属于哪家业主
  1496. //查找业主的服务器
  1497. //给服务器发数据
  1498. }
  1499. public function senddata1($data){
  1500. echo "m2".$data;
  1501. }
  1502. public function senddata2($data){
  1503. echo "m3".$data;
  1504. }
  1505. public function array_column1($rows, $column_key, $index_key = null) {
  1506. $data = array();
  1507. if (empty($index_key)) {
  1508. foreach ($rows as $row) {
  1509. $data[] = $row[$column_key];
  1510. }
  1511. $data=implode(",",$data);
  1512. } else {
  1513. foreach ($rows as $row) {
  1514. $data[$row[$index_key]] = $row[$column_key];
  1515. }
  1516. }
  1517. return $data;
  1518. }
  1519. public function SendDeviceAlarm($access_token,$openid,$address,$deviceid,$type,$time,$remark,$url){
  1520. $data = array(
  1521. "touser"=> $openid,
  1522. "template_id"=>"JkQYUiScfIuH9O4vvUgtgYxPIOBOY7Zh9w2DcWTgm50",
  1523. //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
  1524. "data"=> array(
  1525. "first"=> array(
  1526. "value"=> $address,
  1527. "color"=>"#44b549"
  1528. ),
  1529. "keyword1"=>array(
  1530. "value"=> $deviceid,
  1531. "color"=>"#173177"
  1532. ),
  1533. "keyword2"=> array(
  1534. "value"=> $type,
  1535. "color"=>"#173177"
  1536. ),
  1537. "keyword3"=> array(
  1538. "value"=> $time,
  1539. "color"=>"#173177"
  1540. ),
  1541. "remark"=>array(
  1542. "value"=> $remark,
  1543. "color"=>"#aaaaaa"
  1544. )
  1545. )
  1546. );
  1547. //{{first.DATA}}
  1548. //设备号:{{keyword1.DATA}}
  1549. //报警类型:{{keyword2.DATA}}
  1550. //时间:{{keyword3.DATA}}
  1551. //{{remark.DATA}}
  1552. $data = json_encode($data);
  1553. $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
  1554. $res = $this->curl($QUEST_URL,$data);
  1555. return json_decode($res,true);
  1556. }
  1557. public function curl($url,$data=''){
  1558. $ch = curl_init();
  1559. curl_setopt($ch, CURLOPT_URL, $url);
  1560. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  1561. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1562. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1563. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1564. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1565. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1566. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1567. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1568. $info = curl_exec($ch);
  1569. if (curl_errno($ch)) {
  1570. $info = 'ERROR: ' . curl_error($ch);
  1571. }
  1572. curl_close($ch);
  1573. return $info;
  1574. }
  1575. public function s_curl($url, $data = '')
  1576. {
  1577. $ch = curl_init();
  1578. curl_setopt($ch, CURLOPT_URL, $url);
  1579. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  1580. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1581. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1582. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1583. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1584. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1585. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1586. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1587. $info = curl_exec($ch);
  1588. if (curl_errno($ch)) {
  1589. $info = 'ERROR: ' . curl_error($ch);
  1590. }
  1591. curl_close($ch);
  1592. return $info;
  1593. }
  1594. public function get_curl($url, $data = '')
  1595. {
  1596. $ch = curl_init();
  1597. curl_setopt($ch, CURLOPT_URL, $url);
  1598. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
  1599. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  1600. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  1601. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  1602. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  1603. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  1604. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1605. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1606. $info = curl_exec($ch);
  1607. if (curl_errno($ch)) {
  1608. $info = 'ERROR: ' . curl_error($ch);
  1609. }
  1610. curl_close($ch);
  1611. return $info;
  1612. }
  1613. public function message_push_mh($topic,$msg,$qos){
  1614. vendor('phpMQTT.phpMQTT');
  1615. $server = '47.98.201.73'; // 服务器IP
  1616. $port = 1883; // 服务器端口
  1617. $username = 'usky'; // 用户名
  1618. $password = 'usky'; // 密码
  1619. $client_id = 'pub_' . uniqid();
  1620. $mqtt = new \phpMQTT($server, $port, $client_id);
  1621. if ($mqtt->connect(true, NULL, $username, $password)) {
  1622. $mqtt->publish($topic, $msg, $qos);
  1623. usleep(100000);
  1624. $mqtt->close();
  1625. Log::record('MH—NB-MQTT-Push success!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  1626. } else {
  1627. Log::record('MH—NB-MQTT-Time out!('.$topic.')['.date('Y-m-d H:i:s').']:'.json_encode($msg));
  1628. }
  1629. }
  1630. public function mqqt_data_recombination($type, $device_code, $device_name, $electric_quantity, $signal, $water_gage,$dwtype)
  1631. {
  1632. if ($type == 'INFO') {
  1633. $status_valie = "0";
  1634. } else {
  1635. $status_valie = "1";
  1636. }
  1637. if ($dwtype==2){
  1638. $property = "水压";
  1639. }else{
  1640. $property = "水位";
  1641. }
  1642. $array = [
  1643. "devId" => $device_code,
  1644. "connType" => "G",
  1645. "deviceName" => $device_name,
  1646. "devs" => [
  1647. [
  1648. "dp" => [
  1649. [
  1650. "property" => "电量",
  1651. "serial" => "201",
  1652. "value" => '' . $electric_quantity
  1653. ],
  1654. [
  1655. "property" => "信号",
  1656. "serial" => "202",
  1657. "value" => '' . $signal
  1658. ],
  1659. [
  1660. "property" => $property,
  1661. "serial" => "203",
  1662. "value" => '' . $water_gage
  1663. ],
  1664. [
  1665. "property" => "状态",
  1666. "serial" => "204",
  1667. "value" => $status_valie
  1668. ]
  1669. ],
  1670. "deviceName" => $device_name,
  1671. "timeStamp" => "".time(),
  1672. "deviceModel" => "",
  1673. "deviceType" => "".$dwtype,
  1674. "devId" => $device_code
  1675. ]
  1676. ],
  1677. "type" => $type,
  1678. "timeStamp" => "".time()
  1679. ];
  1680. $json_data = json_encode($array);
  1681. return $json_data;
  1682. }
  1683. }