func.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. <?php
  2. function log_debug( $msg ){
  3. if (!APP_DEBUG)
  4. return;
  5. $logger = \Jiaruan\FileLogger::getInstance(SOLUTION_LOG_PATH . '/zndp/');
  6. $logger->log('',\Jiaruan\FileLogger::DEBUG,$msg);
  7. }
  8. function log_error( $msg ){
  9. $logger = \Jiaruan\FileLogger::getInstance(SOLUTION_LOG_PATH . '/zndp/');
  10. $logger->log('',\Jiaruan\FileLogger::ERROR,$msg);
  11. }
  12. function log_info( $msg ){
  13. if (!APP_DEBUG)
  14. return;
  15. $logger = \Jiaruan\FileLogger::getInstance(SOLUTION_LOG_PATH . '/zndp/');
  16. $logger->log('',\Jiaruan\FileLogger::INFO,$msg);
  17. }
  18. function saveDeviceInfo_old( $imei, $versionname, $addr, $iccid = '', $cellphone = '' ){
  19. $cond = array('DeviceImei'=>$imei);
  20. $is_exists = MM('dpsb_device')->where($cond)->getField('ID');
  21. if($is_exists){
  22. $where = array('ID'=>$is_exists);
  23. $data = array(
  24. 'VersionName'=>$versionname,
  25. 'DeviceAddr' => $addr,
  26. 'IccId'=>$iccid,
  27. 'CellPhone'=>$cellphone,
  28. );
  29. $res = MM('dpsb_device')->createSave($where,$data);
  30. if(!$res)
  31. json_fail('设备版本号添加到设备表失败!');
  32. return $is_exists;
  33. }else{
  34. $data = array(
  35. 'DeviceImei'=>$imei,
  36. 'DeviceAddr' => $addr,
  37. 'IccId'=>$iccid,
  38. 'CellPhone'=>$cellphone,
  39. 'AddTime'=>date("Y-m-d H:i:s"),
  40. 'VersionName'=>$versionname
  41. );
  42. $id = MM('dpsb_device')->createAdd($data);
  43. if(!$id)
  44. json_fail('设备IMEI添加到设备表失败!');
  45. return $id;
  46. }
  47. }
  48. function saveChannel( $device_id, $data ){
  49. $model = MM('dpsb_channel');
  50. $where = array('DeviceId'=>$device_id);
  51. // 获取该设备下目前已有的通道
  52. $addr = MM('dpsb_device')->where(array('ID'=>$device_id))->getField('DeviceAddr');
  53. $channel_list = $model->where($where)->field('ChNumber')->select();
  54. $channel_arr = array();
  55. if($channel_list){
  56. foreach($channel_list as $channel){
  57. array_push($channel_arr,$channel['ChNumber']);
  58. }
  59. }
  60. //对象转数组
  61. $data = json_decode(json_encode($data),TRUE);
  62. // 获取通道名称
  63. $display_name = new \Zndp\Device\ChannelType;
  64. foreach($data as $v){
  65. if( in_array($v['Column'],$channel_arr) ){
  66. $where['ChNumber'] = $v['Column'];
  67. $saveData = array(
  68. 'ChGroup'=>$v['Group'],
  69. 'ChType'=>$v['Type'],
  70. 'DisplayName'=>$display_name->getDisplayName($v['Type'],$v['Group'],$v['Column']),
  71. );
  72. $result = $model->createSave($where,$saveData);
  73. if(!$result){
  74. return 'update channel fail';
  75. }
  76. }else{
  77. $saveData = array(
  78. 'ChGroup'=>$v['Group'],
  79. 'ChNumber'=>$v['Column'],
  80. 'ChNumberText'=>'column_'.$v['Column'],
  81. 'DisplayName'=>$display_name->getDisplayName($v['Type'],$v['Group'],$v['Column']),
  82. 'DeviceId'=>$device_id,
  83. 'ChType'=>$v['Type'],
  84. );
  85. $result = $model->createAdd($saveData);
  86. if(!$result){
  87. return 'add channel fail';
  88. }
  89. }
  90. }
  91. }
  92. function saveToChannel( $device_id, $data, $gathertime ){
  93. $cond = array('DeviceId'=>$device_id);
  94. // 获取该设备下目前已有的通道
  95. $channel_list = MM('dpsb_channel')->where($cond)->field('ChNumber')->select();
  96. $channel_arr = array();
  97. if($channel_list){
  98. foreach($channel_list as $channel){
  99. array_push($channel_arr,$channel['ChNumber']);
  100. }
  101. }
  102. //var_dump($channel_arr);
  103. //对象转数组
  104. $data = json_decode(json_encode($data),TRUE);
  105. // 获取通道名称
  106. $display_name = new \Zndp\Device\ChannelType;
  107. foreach($data as $v){
  108. if( in_array($v['column'],$channel_arr) ){
  109. $where = array(
  110. 'DeviceId' => $device_id,
  111. 'ChNumber' => $v['column'],
  112. );
  113. //检测通道类型
  114. $channeltype = MM('dpsb_channel')->where($where)->getField('ChType');
  115. if($channeltype != $v['type']){
  116. return 'type inconsistency';
  117. }
  118. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  119. $v['value'] = round( $v['value']/1000,1 );
  120. }
  121. $saveData = array(
  122. 'ChValue'=>floatval($v['value']),
  123. 'GatherTime'=>$gathertime,
  124. );
  125. $result = MM('dpsb_channel')->createSave($where,$saveData);
  126. if(!$result){
  127. return 'update channeldata fail';
  128. }
  129. }else{
  130. return 'no channeldata updata';
  131. }
  132. }
  133. }
  134. function saveToChannelData( $device_id, $data, $gathertime ){
  135. //对象转数组
  136. $data = json_decode(json_encode($data),TRUE);
  137. // 获取通道名称
  138. $display_name = new \Zndp\Device\ChannelType;
  139. foreach($data as $v){
  140. $device_data_id = MM('dpsj_device')->where(array('DeviceId'=>$device_id))->order('ID desc')->getField('ID');
  141. $group = MM('dpsb_channel')->where(array('ChNumber'=>$v['column'],'DeviceId'=>$device_id))->getField('ChGroup');
  142. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  143. $v['value'] = round( $v['value']/1000,1 );
  144. }
  145. $addData = array(
  146. 'DeviceDataId'=>$device_data_id,
  147. 'ChNumber'=>$v['column'],
  148. 'DisplayName'=>$display_name->getDisplayName($v['type'],$group,$v['column']),
  149. 'DeviceId'=>$device_id,
  150. 'ChValue'=>floatval($v['value']),
  151. 'ChType'=>$v['type'],
  152. 'GatherTime'=>$gathertime,
  153. );
  154. $result = MM('dpsj_channel')->createAdd($addData);
  155. if(!$result){
  156. return 'add channeldata fail';
  157. }
  158. }
  159. }
  160. function saveToDevice( $device_id, $dedata, $gathertime ){
  161. $data = json_decode(json_encode($dedata),TRUE);
  162. // 获取通道名称
  163. $addData = array(
  164. 'ID'=>create_guid(),
  165. 'DeviceId'=>$device_id,
  166. 'GatherTime'=>$gathertime,
  167. 'Signal'=>$data['Signal'],
  168. 'PwrState'=>$data['PwrState']
  169. );
  170. if(is_numeric($data['Voltage'])){
  171. $addData['Voltage'] = $data['Voltage'];
  172. }
  173. if(is_numeric($data['WorkTemp'])){
  174. $addData['WorkTemp'] = $data['WorkTemp'];
  175. }
  176. $result = MM('dpsj_device')->createAdd($addData);
  177. if(!$result){
  178. return 'add devicedata fail';
  179. }
  180. $saveDate = array(
  181. 'GatherTime'=>$gathertime,
  182. 'OnlineTime'=>date('Y-m-d H:i:s'),
  183. 'Signal'=>$data['Signal'],
  184. 'PwrState'=>$data['PwrState']
  185. );
  186. if(is_numeric($data['Voltage'])){
  187. $saveDate['Voltage'] = $data['Voltage'];
  188. }
  189. if(is_numeric($data['WorkTemp'])){
  190. $saveDate['WorkTemp'] = $data['WorkTemp'];
  191. }
  192. $where = array(
  193. 'ID'=>$device_id,
  194. );
  195. $res = MM('dpsb_device')->createSave($where,$saveDate);
  196. if(!$res){
  197. return 'update onlinetime fail';
  198. }
  199. }
  200. function isDeviceDataInvalid( $data ){
  201. //对象转数组
  202. $data_arr = json_decode(json_encode($data),TRUE);
  203. foreach($data_arr as $channel){
  204. if( $channel['value'] == '-' ){
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. function send_nongke( $addr, $data, $gathertime ){
  211. //对象转数组
  212. $data = json_decode(json_encode($data),TRUE);
  213. $value_list1 = array();
  214. $value_list2 = array();
  215. $value_list3 = array();
  216. $value_list4 = array();
  217. $array1 = array(2,3,4);
  218. $array2 = array(7,8,9);
  219. $array3 = array(12,13,14);
  220. $array4 = array(17,18,19);
  221. foreach($data as $v){
  222. if(in_array($v['column'],$array1)){
  223. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  224. $v['value'] = round( $v['value']/1000,1 );
  225. $value_list1['gz'] = $v['value'];
  226. }
  227. if($v['type'] == \Zndp\Device\ChannelType::TEMPERATURE){
  228. $value_list1['wd'] = $v['value'];
  229. }
  230. if($v['type'] == \Zndp\Device\ChannelType::HUMIDITY){
  231. $value_list1['sd'] = $v['value'];
  232. }
  233. }
  234. if(in_array($v['column'],$array2)){
  235. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  236. $v['value'] = round( $v['value']/1000,1 );
  237. $value_list2['gz'] = $v['value'];
  238. }
  239. if($v['type'] == \Zndp\Device\ChannelType::TEMPERATURE){
  240. $value_list2['wd'] = $v['value'];
  241. }
  242. if($v['type'] == \Zndp\Device\ChannelType::HUMIDITY){
  243. $value_list2['sd'] = $v['value'];
  244. }
  245. }
  246. if(in_array($v['column'],$array3)){
  247. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  248. $v['value'] = round( $v['value']/1000,1 );
  249. $value_list3['gz'] = $v['value'];
  250. }
  251. if($v['type'] == \Zndp\Device\ChannelType::TEMPERATURE){
  252. $value_list3['wd'] = $v['value'];
  253. }
  254. if($v['type'] == \Zndp\Device\ChannelType::HUMIDITY){
  255. $value_list3['sd'] = $v['value'];
  256. }
  257. }
  258. if(in_array($v['column'],$array4)){
  259. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  260. $v['value'] = round( $v['value']/1000,1 );
  261. $value_list4['gz'] = $v['value'];
  262. }
  263. if($v['type'] == \Zndp\Device\ChannelType::TEMPERATURE){
  264. $value_list4['wd'] = $v['value'];
  265. }
  266. if($v['type'] == \Zndp\Device\ChannelType::HUMIDITY){
  267. $value_list4['sd'] = $v['value'];
  268. }
  269. }
  270. }
  271. //传到农科院设备
  272. $channel_data1 = array($value_list1['wd'],$value_list1['sd'],$value_list1['gz']);
  273. $channel_data2 = array($value_list2['wd'],$value_list2['sd'],$value_list2['gz']);
  274. $channel_data3 = array($value_list3['wd'],$value_list3['sd'],$value_list3['gz']);
  275. $channel_data4 = array($value_list4['wd'],$value_list4['sd'],$value_list4['gz']);
  276. $channel_list = array();
  277. if($value_list1['wd']){
  278. $channel_list1 = array('addr'=>"1",'value'=>$channel_data1);
  279. array_push($channel_list,$channel_list1);
  280. }
  281. if($value_list2['wd']){
  282. $channel_list2 = array('addr'=>2,'value'=>$channel_data2);
  283. array_push($channel_list,$channel_list2);
  284. }
  285. if($value_list3['wd']){
  286. $channel_list3 = array('addr'=>3,'value'=>$channel_data3);
  287. array_push($channel_list,$channel_list3);
  288. }
  289. if($value_list4['wd']){
  290. $channel_list4 = array('addr'=>4,'value'=>$channel_data4);
  291. array_push($channel_list,$channel_list4);
  292. }
  293. $data = array(
  294. "gatewayid" => $addr,
  295. "data" => $channel_list,
  296. "sessionkey" => 123456,
  297. );
  298. $dataurl = urlencode(json_encode($data));
  299. //$url = "http://210.12.220.220:8888/dataswitch-inner-http/inner/send/gateway.html?req=".$dataurl;
  300. $url = "http://218.22.201.174:8083/dataswitch-inner-http/inner/send/gateway.html?req=".$dataurl;
  301. //$last_time = S('nongke_last_time');
  302. $dpsb_device = MM('dpsb_device');
  303. $where = array('DeviceAddr'=>$addr);
  304. $dpsb_device_info = $dpsb_device->where($where)->find();
  305. $send_time = $dpsb_device_info['NkSendTime'];
  306. if( (strtotime($gathertime) - strtotime($send_time)) >= 600){
  307. $res = curl_json_post($url,array());
  308. //$where = array('DeviceAddr'=>$addr);
  309. $nkdata = array('NkSengResult'=>$res['status'], 'NkMessage'=>$res['msg'], 'NkSendTime'=>$gathertime);
  310. $nkres = $dpsb_device->createSave($where,$nkdata);
  311. if(!$nkres){
  312. log_debug('保存农科发送数据失败');
  313. }
  314. if($res['status']){
  315. log_debug($res['msg']);
  316. //S('nongke_last_time',strtotime($gathertime));
  317. }else{
  318. log_info($res['msg'].': '.json_encode($channel_list));
  319. //S('nongke_last_time',strtotime($gathertime));
  320. }
  321. }
  322. }
  323. function send_single_nongke( $addr, $data, $gathertime ){
  324. //对象转数组
  325. $data = json_decode(json_encode($data),TRUE);
  326. $value_list = array();
  327. foreach($data as $v){
  328. if($v['type'] == \Zndp\Device\ChannelType::ILLINATIONUM){ //修改光照值
  329. $v['value'] = round( $v['value']/1000,1 );
  330. $value_list['gz'] = $v['value'];
  331. }
  332. if($v['type'] == \Zndp\Device\ChannelType::TEMPERATURE){
  333. $value_list['wd'] = $v['value'];
  334. }
  335. if($v['type'] == \Zndp\Device\ChannelType::HUMIDITY){
  336. $value_list['sd'] = $v['value'];
  337. }
  338. }
  339. //传到农科院设备
  340. $channel_data = array($value_list['wd'],$value_list['sd'],$value_list['gz']);
  341. $data = array(
  342. "deviceid" => "617261",
  343. "data" => $channel_data,
  344. "sessionkey" => 123456,
  345. );
  346. $dataurl = urlencode(json_encode($data));
  347. $url = "http://218.22.201.174:8083/dataswitch-inner-http/inner/send/single.html?req=".$dataurl;
  348. /*$last_time = S('nongke_last_time');
  349. if( strtotime($gathertime) - $last_time >= 600){
  350. $res = curl_json_post($url,array());
  351. S('nongke_last_time',strtotime($gathertime));
  352. if(isset($res['success']) && !$res['success']){
  353. log_error($res['message']);
  354. return;
  355. }
  356. if($res['status']){
  357. log_error($res['msg']);
  358. }else{
  359. log_info($res['msg'].': '.json_encode($channel_data));
  360. }
  361. }*/
  362. $last_time = S('nongke_last_time');
  363. if( strtotime($gathertime) - $last_time >= 600){
  364. $res = curl_json_post($url,array());
  365. if($res['status']){
  366. S('nongke_last_time',strtotime($gathertime));
  367. log_debug($res['msg']);
  368. }else{
  369. S('nongke_last_time',strtotime($gathertime));
  370. log_info($res['msg'].': '.json_encode($channel_data));
  371. }
  372. }
  373. }
  374. function saveChgroup( $device_id, $data ){
  375. $chgroup = MM('dpsb_chgroup');
  376. // 为了方便设备接入后可以直接绑定大棚,不用等所有通道组都上报数据,2019年3月12日15:36:57
  377. for( $i=0; $i<=1; $i++ ){
  378. $where = array( 'DeviceId'=> $device_id,'ChGroup' => $i );
  379. $count = $chgroup->where($where)->count();
  380. if($count > 0){ //通道组存在,跳过
  381. continue;
  382. }
  383. //保存通道组信息
  384. $save_data = array(
  385. 'ID' => create_guid(),
  386. 'DeviceId' => $device_id,
  387. 'ChGroup' => $i
  388. );
  389. $result = $chgroup->createAdd($save_data);
  390. if( $result === false ){
  391. return 'add channel group failed';
  392. }
  393. }
  394. /*
  395. foreach($data as $ch){ //遍历通道数据,获取通道组信息
  396. $where = array( 'DeviceId'=> $device_id,'ChGroup' => $ch->Group );
  397. $count = $chgroup->where($where)->count();
  398. if($count > 0){ //通道组存在,跳过
  399. continue;
  400. }
  401. //保存通道组信息
  402. $save_data = array(
  403. 'ID' => create_guid(),
  404. 'DeviceId' => $device_id,
  405. 'ChGroup' => $ch->Group
  406. );
  407. $result = $chgroup->createAdd($save_data);
  408. if(!$result){
  409. return 'add channel group failed';
  410. }
  411. }
  412. */
  413. }
  414. function pushAlarmToRedis( $device_id, $data, $gathertime ){
  415. $path = SOLUTION_LOG_PATH . 'push_alarm_to_redis.log';
  416. if(!$device_id || !$gathertime || empty($data)){ //缺少参数或没数据
  417. $msg = '['.date('Y-m-d H:i:s').'] DeviceId:'.$device_id.',GatherTime:'.$gathertime.',Data:'.$data .PHP_EOL;
  418. file_put_contents($path,$msg,FILE_APPEND);
  419. }
  420. $redis_chgroup = Redis('dpxf_chgroup','hash'); // 查通道组
  421. $redis_farmland_list = Redis("dpsb_farmland_list","hash"); // 查大棚id
  422. $redis_alarm_set = Redis('dpyh_alarm_set','hash'); // 查告警设置
  423. $redis_alarm_list = Redis('dpxf_alarm_list','list'); // 告警列表
  424. $dpyh_farmland = MM('dpyh_farmland');
  425. foreach($data as $ch){//遍历通道数据
  426. $key = 'DeviceId-'.$device_id .'-ChNumber-'.$ch->column;
  427. $chgroup = $redis_chgroup->get($key); //查出通道组
  428. if($chgroup === false){ //不存在的
  429. continue;
  430. }
  431. $key = 'DeviceId-'.$device_id .'-ChGroup-'.$chgroup;
  432. $farmland_id = $redis_farmland_list->get($key);
  433. if( !$farmland_id){//没有大棚id记录
  434. continue;
  435. }
  436. // 获取告警设置信息
  437. $key = 'FarmlandId-'.$farmland_id;
  438. $alarm_data = $redis_alarm_set->get($key);
  439. if(!$alarm_data){//没有设置信息
  440. continue;
  441. }
  442. $alarm_data = json_decode($alarm_data,true);
  443. if( $alarm_data['IsUseTempLimit'] == 0){ //没有启用告警
  444. continue;
  445. }
  446. // 判断是否达到设定温度告警阈值
  447. if( $ch->type == \Zndp\Device\ChannelType::TEMPERATURE ){ // 温度类传感器
  448. $result = false;
  449. $key = $alarm_data['FarmId'];
  450. $val = pushTempAlarm($ch, $alarm_data, $dpyh_farmland, $farmland_id, $gathertime);
  451. if($val['success']){ // 有值推送到redis
  452. $list = array($key => $val);
  453. $result = $redis_alarm_list->lpush($list);
  454. }
  455. if( !$result ){
  456. $msg = '[failed]['.date('Y-m-d H:i:s').']Input to dpxf_alarm_list : DeviceId-'.$device_id .'-ChNumber-'. $ch->column .' => '. $val['message'] .PHP_EOL;
  457. file_put_contents($path,$msg,FILE_APPEND);
  458. }
  459. }
  460. }
  461. }
  462. function addChgroupToRedis( $device_id, $data ){
  463. $redis = Redis("dpxf_chgroup","hash");
  464. foreach($data as $ch){
  465. // 保存每个通道对应的通道组
  466. $key = 'DeviceId-'.$device_id .'-ChNumber-'.$ch->Column;
  467. $val = $ch->Group;
  468. $hash = array($key => $val );
  469. $result = $redis->add($hash);
  470. if(!$result){
  471. $path = SOLUTION_LOG_PATH . 'add_chgroup_to_redis.log';
  472. $msg = '['.date('Y-m-d H:i:s').']DeviceId-'.$device_id .'-ChNumber-'.$ch->Column.'添加Redis表dpxf_chgroup失败'.PHP_EOL;
  473. file_put_contents($path,$msg,FILE_APPEND);
  474. }
  475. }
  476. }
  477. function pushTempAlarm( $ch, $alarm_data, $dpyh_farmland, $farmland_id, $gathertime ){
  478. if(!$ch || !$alarm_data || !$dpyh_farmland || !$farmland_id || !$gathertime){
  479. return array(
  480. 'success' => false,
  481. 'message' => '缺少参数',
  482. );
  483. }
  484. if(!is_numeric($ch->value)){ //非数字
  485. return array(
  486. 'success' => false,
  487. 'message' => "非数字值:".$ch->value,
  488. );
  489. }
  490. if( $ch->value < $alarm_data['HighTempLimit'] && $ch->value > $alarm_data['LowTempLimit'] ){ // 判断是否恢复正常,状态 0
  491. $where = array('ID'=>$farmland_id);
  492. $alarm_state = $dpyh_farmland->where($where)->getField('AlarmState');
  493. if( $alarm_state == 0 ){ //当前告警状态为0,表示之前未报警,跳过
  494. return array(
  495. 'success' => false,
  496. 'message' => '温度正常',
  497. );
  498. }
  499. $content = $alarm_data['FarmlandName'].'育苗大棚温度恢复正常,实际温度:'.$ch->value.'℃';
  500. $val = array(
  501. 'alarm_state' => 0,
  502. 'content' => $content,
  503. 'farmland_id' => $farmland_id,
  504. 'gathertime' => $gathertime,
  505. );
  506. }
  507. elseif( $ch->value >= $alarm_data['MaxTempExceed'] || $ch->value <= $alarm_data['LowTempExceed'] ){ // 判断有没有超过极限温度,状态 2
  508. $content = $alarm_data['FarmlandName'].'育苗大棚温度已达到极限阈值,实际温度:'.$ch->value .'℃,请立即前往排查。';
  509. $val = array(
  510. 'alarm_state' => 2,
  511. 'content' => $content,
  512. 'farmland_id' => $farmland_id,
  513. 'gathertime' => $gathertime,
  514. );
  515. }
  516. else{ // 超过上下限,状态 1
  517. $content = $alarm_data['FarmlandName'].'育苗大棚温度异常,实际温度:'.$ch->value .'℃,请立即前往排查。';
  518. $val = array(
  519. 'alarm_state' => 1,
  520. 'content' => $content,
  521. 'farmland_id' => $farmland_id,
  522. 'gathertime' => $gathertime,
  523. );
  524. }
  525. return array(
  526. 'success' => true,
  527. 'message' => 'success',
  528. 'data' => $val
  529. );
  530. }
  531. function saveDeviceInfo( $proto ){
  532. $cond = array('DeviceImei'=>$proto->imei);
  533. $device_id = MM('dpsb_device')->where($cond)->getField('ID');
  534. // 根据传过来的设备型号,到型号表里去查,有则添加 DeviceTypeId
  535. $cond = array('DeviceModel'=>$proto->model);
  536. $type_id = ( MM('dpsb_type')->where($cond)->getField('ID') ) ? : '';
  537. if($device_id){ // 已经保存过了,更新可能会变更的信息
  538. $where = array('ID'=>$device_id);
  539. $data = array(
  540. 'VersionName' => $proto->versionName,
  541. 'DeviceTypeId' => $type_id,
  542. 'DeviceAddr' => $proto->addr,
  543. 'IccId' => $proto->iccid,
  544. 'CellPhone' => $proto->cellphone,
  545. );
  546. $res = MM('dpsb_device')->createSave($where,$data);
  547. if(!$res){
  548. json_fail('设备版本号添加到设备表失败!');
  549. }
  550. return $device_id;
  551. }else{ // 不存在,保存所有信息
  552. $data = array(
  553. 'DeviceImei' => $proto->imei,
  554. 'DeviceAddr' => $proto->addr,
  555. 'DeviceTypeId' => $type_id,
  556. 'IccId' => $proto->iccid,
  557. 'CellPhone' => $proto->cellphone,
  558. 'AddTime' => date("Y-m-d H:i:s"),
  559. 'VersionName' => $proto->versionName,
  560. );
  561. $device_id = MM('dpsb_device')->createAdd($data);
  562. if(!$device_id){
  563. json_fail('设备IMEI添加到设备表失败!');
  564. }
  565. return $device_id;
  566. }
  567. }