DpxfIndexAction.class.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. <?php
  2. class DpxfIndexAction extends \Jms\Network\WorkermanServerAction {
  3. public function index( ){
  4. $this->start("jsontext",10240,1);
  5. }
  6. protected function onAppLogin( $connection, $proto ){
  7. //检查imei地址
  8. if(!$proto->imei){
  9. $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  10. return;
  11. }
  12. if( $proto->addr ){ //设备序列号在控制台操作添加,会直接上报,2019年3月14日09:29:57
  13. $addr = $proto->addr;
  14. }else{
  15. $addr = MM('dpsb_device')->where(array('DeviceImei'=>$proto->imei))->getField('DeviceAddr');
  16. }
  17. if(!$addr){
  18. $device_id = saveDeviceInfo($proto);
  19. $this->respError($connection,'addr is empty ',$proto);
  20. return;
  21. }else{ //保存到设备表
  22. $device_id = saveDeviceInfo($proto);
  23. }
  24. //通过登录验证,加入连接池
  25. $connection->imei = $proto->imei;
  26. $connection->addr = $addr;
  27. $this->addToPool($addr,$connection);
  28. $this->logDebug('login success. should put into connection pool. imei = ' . $proto->imei);
  29. // 将数据更新到数据表中
  30. //$device_id = saveDeviceInfo($proto->imei,$proto->versionName,$addr,$proto->iccid,$proto->cellphone);
  31. if(!$device_id){
  32. $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  33. return;
  34. }
  35. /*
  36. if(!$proto->channel){
  37. $this->respError($connection,'channeldata is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  38. return;
  39. }*/
  40. $errmsg = saveChannel($device_id,$proto->channel);
  41. if($errmsg){
  42. $this->respError($connection,$errmsg,$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
  43. return;
  44. }
  45. // 新增:保存通道组信息 2019年1月8日11:46:49
  46. $result = saveChgroup($device_id,$proto->channel);
  47. if($result){
  48. $this->respError($connection,$errmsg,$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
  49. return;
  50. }
  51. // 以设备id-通道号为key,通道组号为值存入redis 2019年1月28日13:44:37
  52. addChgroupToRedis($device_id,$proto->channel);
  53. $where = array('DeviceAddr'=>$addr);
  54. $field = "Mobiles,ExceptContent,SmsInterval,HighTemp,IsUseHT,LowTemp,IsUseLT,SoundLightAlarmInterval,SoundLightDuration,SeveralExceptions,WorkUnitName";
  55. $result = M('dpsb_device')->where($where)->field($field)->find();
  56. $name = $result['WorkUnitName'];
  57. unset($result['WorkUnitName']);
  58. if(!$result){ //设备不存在
  59. json_fail('DeviceAddr not exists!');
  60. }
  61. $arr = array(
  62. 'method' => $proto->method.'Resp',
  63. 'Addr' => $addr,
  64. 'WorkUnitName' => $name,
  65. 'timestamp'=>time(date('Y-m-d H:i:s')),
  66. 'alarm'=>$result,
  67. );
  68. $connection->send($arr);
  69. }
  70. private function respError( $connection, $message, $proto, $data ){
  71. //判断$proto是否为字符串
  72. if(!is_string($proto)){
  73. $protostr = json_encode($proto);
  74. }else{
  75. $protostr = $proto;
  76. }
  77. log_error($message.' proto = '.$protostr);
  78. $array = array(
  79. 'success' => false,
  80. 'message' => $message,
  81. 'data' => $data,
  82. );
  83. //判断是否存在method
  84. if($proto->method){
  85. $array['method'] = $proto->method . 'Resp';
  86. }
  87. $connection->send($array);
  88. $connection->close();
  89. }
  90. protected function onApiSendControl( $connection, $proto ){
  91. /*返回结果:
  92. 1.失败,手动控制命令发送失败,地址码未设置
  93. 2.失败,手动控制命令发送失败,手动控制命令格式错误
  94. 3.失败,手动控制命令发送失败,设备未在线
  95. 4.失败,手动控制命令发送失败,可能设备掉线
  96. 5.失败,手动控制命令发送成功,但是设备回应超时
  97. 6.失败,手动控制命令发送成功,但是设备回应格式错误
  98. 7.失败,手动控制命令发送成功,但是设备回应错误信息
  99. 8.成功
  100. */
  101. //检查addr
  102. if(!$proto->Addr){
  103. $this->respError($connection,'失败,手动控制命令发送失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  104. return;
  105. }
  106. //给控制app终端发送手动控制信号,并等待回应
  107. $arr = array(
  108. "method"=> $proto->method,
  109. "Addr"=>$proto->Addr,
  110. "channelNumber"=>$proto->channelNumber,
  111. "status"=>$proto->status
  112. );
  113. $callback = function($errno,$resp) use ($connection){
  114. if($errno == self::ERRNO_SUCCESS){
  115. if(is_object($resp)){
  116. $res = $connection->send($resp);
  117. $connection->close();
  118. }
  119. else
  120. $this->respError($connection,'失败,手动控制命令发送成功,但是设备回应格式错误',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  121. }
  122. else{
  123. $this->respError($connection,'失败,手动控制命令发送失败,'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  124. }
  125. };
  126. $this->sendWait($proto->Addr,$arr,'appSendControlResp',$callback,10);
  127. }
  128. protected function onApiModifyAutoControlInfo( $connection, $proto ){
  129. //检查addr是否为空
  130. if(!$proto->Addr){
  131. $this->respError($connection,'发送失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::NOT_AUTH_DEVICE);
  132. return;
  133. }
  134. //检查channelNumber是否为空
  135. if(!$proto->channelNumber){
  136. $this->respError($connection,'发送失败,设备通道查询不到',$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
  137. return;
  138. }
  139. //检查channelNumber是否存在
  140. $device_id = MM('dpsb_device')->where(array('DeviceAddr'=>$proto->Addr))->getField('ID');
  141. $cond = array(
  142. 'DeviceId'=>$device_id,
  143. 'ChNumber'=>$proto->channelNumber,
  144. );
  145. $result = MM('dpsb_channel')->where($cond)->find();
  146. if(!$result){
  147. $this->respError($connection,'发送失败,设备通道未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  148. return;
  149. }
  150. //给app转发自动控制命令
  151. $arr = array(
  152. 'method'=>$proto->method,
  153. 'Addr'=>$proto->Addr,
  154. 'channelNumber'=>$proto->channelNumber,
  155. 'man'=>$proto->man,
  156. 'refChannel'=>$proto->refChannel,
  157. 'TimeEn'=>$proto->TimeEn,
  158. 'UpperLimit_Value'=>$proto->UpperLimit_Value,
  159. 'UpperLimit_State'=>$proto->UpperLimit_State,
  160. 'LowerLimit_Value'=>$proto->LowerLimit_Value,
  161. 'LowerLimit_State'=>$proto->LowerLimit_State,
  162. 'OnTime1'=>$proto->OnTime1,
  163. 'OffTime1'=>$proto->OffTime1,
  164. 'OnTime2'=>$proto->OnTime2,
  165. 'OffTime2'=>$proto->OffTime2,
  166. 'OnTime3'=>$proto->OnTime3,
  167. 'OffTime3'=>$proto->OffTime3,
  168. 'OnTime4'=>$proto->OnTime4,
  169. 'OffTime4'=>$proto->OffTime4,
  170. 'OnTime5'=>$proto->OnTime5,
  171. 'OffTime5'=>$proto->OffTime5,
  172. 'IssueTime'=>(int)$proto->IssueTime,
  173. );
  174. $callback = function($errno,$resp) use ($connection,$proto,$device_id){
  175. if($errno == \Zndp\Api\ResponseCode::SEND_SUCCESS){
  176. if(is_object($resp)){
  177. $connection->send($resp);
  178. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::SEND_SUCCESS);
  179. $connection->close();
  180. }
  181. else{
  182. $this->respError($connection,'失败,自动控制命令发送成功,但是设备回应格式错误',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  183. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  184. }
  185. }
  186. else{
  187. $this->respError($connection,'失败,自动控制命令发送失败,'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  188. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  189. }
  190. };
  191. $this->sendWait($proto->Addr,$arr,'appModifyAutoControlInfoResp',$callback,10);
  192. }
  193. private function saveModifyAutoData( $device_id, $proto, $connection, $msg ){
  194. $where = array(
  195. 'DeviceId'=> $device_id,
  196. 'Channel'=>$proto->channelNumber,
  197. );
  198. $res = MM('dpsb_policy')->where($where)->find();
  199. if($res){
  200. $saveData = array(
  201. 'Man'=> $proto->man,
  202. 'RefChannel'=> $proto->refChannel,
  203. 'IsTime'=> $proto->TimeEn,
  204. 'UpperLimitValue'=> $proto->UpperLimit_Value,
  205. 'UpperLimitState'=> $proto->UpperLimit_State,
  206. 'LowerLimitValue'=> $proto->LowerLimit_Value,
  207. 'LowerLimitState'=> $proto->LowerLimit_State,
  208. 'OnTime1'=> $proto->OnTime1,
  209. 'OffTime1'=> $proto->OffTime1,
  210. 'OnTime2'=> $proto->OnTime2,
  211. 'OffTime2'=> $proto->OffTime2,
  212. 'OnTime3'=> $proto->OnTime3,
  213. 'OffTime3'=> $proto->OffTime3,
  214. 'OnTime4'=> $proto->OnTime4,
  215. 'OffTime4'=> $proto->OffTime4,
  216. 'OnTime5'=> $proto->OnTime5,
  217. 'OffTime5'=> $proto->OffTime5,
  218. 'SendTime'=> date('Y-m-d H:i:s'),
  219. 'SendResult'=> $msg
  220. );
  221. $result = MM('dpsb_policy')->createSave($where,$saveData);
  222. }else{
  223. $addData = array(
  224. 'DeviceId'=> $device_id,
  225. 'Channel'=>$proto->channelNumber,
  226. 'Man'=> $proto->man,
  227. 'RefChannel'=> $proto->refChannel,
  228. 'IsTime'=> $proto->TimeEn,
  229. 'UpperLimitValue'=> $proto->UpperLimit_Value,
  230. 'UpperLimitState'=> $proto->UpperLimit_State,
  231. 'LowerLimitValue'=> $proto->LowerLimit_Value,
  232. 'LowerLimitState'=> $proto->LowerLimit_State,
  233. 'OnTime1'=> $proto->OnTime1,
  234. 'OffTime1'=> $proto->OffTime1,
  235. 'OnTime2'=> $proto->OnTime2,
  236. 'OffTime2'=> $proto->OffTime2,
  237. 'OnTime3'=> $proto->OnTime3,
  238. 'OffTime3'=> $proto->OffTime3,
  239. 'OnTime4'=> $proto->OnTime4,
  240. 'OffTime4'=> $proto->OffTime4,
  241. 'OnTime5'=> $proto->OnTime5,
  242. 'OffTime5'=> $proto->OffTime5,
  243. 'SendTime'=> date('Y-m-d H:i:s'),
  244. 'SendResult'=> $msg
  245. );
  246. $result = MM('dpsb_policy')->createAdd($addData);
  247. }
  248. if(!$result){
  249. $this->respError($connection,'channelNumber inexistence',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  250. return;
  251. }
  252. }
  253. protected function onAppSendRealTimeDeviceData( $connection, $proto ){
  254. //检查imei地址
  255. if(!$proto->imei){
  256. $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  257. return;
  258. }
  259. if(!$connection->addr){
  260. $this->respError($connection,'addr is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  261. return;
  262. }
  263. //检查连接池中是否存在addr,不存在添加回去
  264. if(!$this->getFromPool($connection->addr)){
  265. $connection->imei = $proto->imei;
  266. $this->addToPool($connection->addr,$connection);
  267. $this->logDebug('pool loss,reattach pool,imei = ' . $proto->imei);
  268. //log_debug('Data abnormal');
  269. echo 'Data abnormal'.PHP_EOL;
  270. }
  271. // 将数据更新到数据表中
  272. /*
  273. $data = $proto->data;
  274. $fix_device_info = MM('dpsb_device')->where(array('DeviceImei'=>$proto->imei))->select();
  275. $device_id = saveDeviceInfo($proto);
  276. //$device_id = saveDeviceInfo($proto->imei,$fix_device_info[0]['VersionName'],$connection->addr,$fix_device_info[0]['IccId'],$fix_device_info[0]['CellPhone']);
  277. */
  278. $device_id = MM('dpsb_device')->where(array('DeviceImei'=>$proto->imei))->getField('ID');
  279. if(!$device_id){
  280. $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  281. return;
  282. }
  283. if(!$proto->data){
  284. //$this->respError($connection,'data is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  285. //log_debug('Data is empty');
  286. echo 'Data is empty'.PHP_EOL;
  287. }
  288. if(!$proto->gathertime){
  289. $this->respError($connection,'gathertime is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  290. return;
  291. }
  292. $gathertime = date('Y-m-d H:i:s',$proto->gathertime);
  293. //检查数据是否无效
  294. if(isDeviceDataInvalid($proto->data)){
  295. //保存到无效数据表
  296. $device_data = array();
  297. $device_data['Addr'] = $connection->addr;
  298. $device_data['DeviceData'] = json_encode($proto->data);
  299. $device_data['GatherTime'] = $gathertime;
  300. $device_data['AddTime'] = date('Y-m-d H:i:s');
  301. MM('dpsj_invalid')->createAdd($device_data);
  302. //$this->respError($connection,'data invelid',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  303. //return;
  304. }
  305. //保存到通道表
  306. $errmsg2 = saveToChannel($device_id,$proto->data,$gathertime);
  307. if($errmsg2){
  308. $this->respError($connection,$errmsg2,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  309. return;
  310. }
  311. //保存数据到设备数据表
  312. if(!$proto->Dedata){
  313. //log_debug('Dedata is empty');
  314. echo 'Dedata is empty'.PHP_EOL;
  315. //$this->respError($connection,'Dedata is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  316. //return;
  317. }
  318. $dedata = $proto->Dedata;
  319. $errmsg1 = saveToDevice($device_id,$dedata,$gathertime);
  320. if($errmsg1){
  321. $this->respError($connection,$errmsg1,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  322. return;
  323. }
  324. //保存到通道数据表
  325. $errmsg3 = saveToChannelData($device_id,$proto->data,$gathertime);
  326. if($errmsg3){
  327. $this->respError($connection,$errmsg3,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  328. return;
  329. }
  330. // 检查告警,并推送到Redis 2019年1月29日10:09:59
  331. pushAlarmToRedis($device_id,$proto->data,$gathertime);
  332. // 推送数据到农科院
  333. $res = send_nongke($connection->addr,$proto->data,$gathertime);
  334. //$res = send_single_nongke($connection->addr,$data,$gathertime);
  335. $arr = array(
  336. 'method' => $proto->method.'Resp',
  337. "success"=>true,
  338. "message"=>"ok",
  339. "addtime"=>time(),
  340. );
  341. $connection->send($arr);
  342. }
  343. protected function onAppHeartbeat( $connection, $proto ){
  344. //直接返回心跳
  345. $arr = array(
  346. 'success'=>true
  347. );
  348. $connection->send($arr);
  349. }
  350. protected function onAppSendControlResp( ){
  351. }
  352. protected function onAppModifyAutoControlInfoResp( ){
  353. }
  354. protected function onApiUploadDebug( $connection, $proto ){
  355. if(!$proto->Addr){
  356. $this->respError($connection,'失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  357. return;
  358. }
  359. //给控制app终端发送手动控制信号,并等待回应
  360. $arr = array(
  361. "method"=> $proto->method,
  362. "Addr"=>$proto->Addr,
  363. );
  364. $callback = function($errno,$resp) use ($connection){
  365. if($errno == self::ERRNO_SUCCESS){
  366. if(is_object($resp)){
  367. $res = $connection->send($resp);
  368. $connection->close();
  369. }
  370. else
  371. $this->respError($connection,'失败,调试不存在',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  372. }
  373. else{
  374. $this->respError($connection,'失败,调试发送失败'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  375. }
  376. };
  377. $this->sendWait($proto->Addr,$arr,'appUploadDebugResp',$callback,10);
  378. }
  379. protected function onAppUploadDebugResp( ){
  380. }
  381. protected function onApiReboot( $connection, $proto ){
  382. if(!$proto->Addr){
  383. $this->respError($connection,'失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  384. return;
  385. }
  386. //给控制app终端发送手动控制信号,并等待回应
  387. $arr = array(
  388. "method"=> $proto->method,
  389. "Addr"=>$proto->Addr,
  390. );
  391. $callback = function($errno,$resp) use ($connection){
  392. if($errno == self::ERRNO_SUCCESS){
  393. if(is_object($resp)){
  394. $res = $connection->send($resp);
  395. $connection->close();
  396. }
  397. else
  398. $this->respError($connection,'失败,重启不存在',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  399. }
  400. else{
  401. $this->respError($connection,'失败,重启发送失败'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  402. }
  403. };
  404. $this->sendWait($proto->Addr,$arr,'appRebootResp',$callback,10);
  405. }
  406. protected function onApiUpgrade( $connection, $proto ){
  407. if(!$proto->Addr){
  408. $this->respError($connection,'失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  409. return;
  410. }
  411. //给控制app终端发送手动控制信号,并等待回应
  412. $arr = array(
  413. "method"=> $proto->method,
  414. "Addr"=>$proto->Addr,
  415. );
  416. $callback = function($errno,$resp) use ($connection){
  417. if($errno == self::ERRNO_SUCCESS){
  418. if(is_object($resp)){
  419. $res = $connection->send($resp);
  420. $connection->close();
  421. }
  422. else
  423. $this->respError($connection,'失败,升级不存在',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  424. }
  425. else{
  426. $this->respError($connection,'失败,升级发送失败'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  427. }
  428. };
  429. $this->sendWait($proto->Addr,$arr,'appUpgradeResp',$callback,10);
  430. }
  431. protected function onAppUpgradeResp( ){
  432. }
  433. protected function onAppRebootResp( ){
  434. }
  435. protected function onApiSysReboot( $connection, $proto ){
  436. if(!$proto->Addr){
  437. $this->respError($connection,'失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  438. return;
  439. }
  440. //给控制app终端发送手动控制信号,并等待回应
  441. $arr = array(
  442. "method"=> $proto->method,
  443. "Addr"=>$proto->Addr,
  444. );
  445. $callback = function($errno,$resp) use ($connection){
  446. if($errno == self::ERRNO_SUCCESS){
  447. if(is_object($resp)){
  448. $res = $connection->send($resp);
  449. $connection->close();
  450. }
  451. else
  452. $this->respError($connection,'失败,系统重启不存在',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  453. }
  454. else{
  455. $this->respError($connection,'失败,系统重启发送失败'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  456. }
  457. };
  458. $this->sendWait($proto->Addr,$arr,'appSysReboot',$callback,10);
  459. }
  460. public function onAppSysRebootResp( ){
  461. }
  462. protected function onFarmlandNum( $connection, $proto ){
  463. if( !$proto->addr ){
  464. $this->respError($connection,'失败,设置大棚编号命令发送失败,地址码未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  465. return;
  466. }
  467. if( $proto->channelGroup === ''){
  468. $this->respError($connection,'失败,设置大棚编号命令发送失败,通道组未设置',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  469. return;
  470. }
  471. //给控制app终端发送大棚编号,并等待回应
  472. $arr = array(
  473. "method" => $proto->method,
  474. "channelGroup" => (int)$proto->channelGroup,
  475. "number" => $proto->number
  476. );
  477. $callback = function($errno,$resp) use ($connection){
  478. if($errno == self::ERRNO_SUCCESS){
  479. if(is_object($resp)){
  480. $res = $connection->send($resp);
  481. $connection->close();
  482. }else{
  483. $this->respError($connection,'失败,设置大棚编号命令发送成功,但是设备回应格式错误',$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  484. }
  485. }else{
  486. $this->respError($connection,'失败,设置大棚编号命令发送失败,'.$this->getErrnoText($errno),$proto,\Zndp\Api\ResponseCode::OTHER_SERVER_ERROR);
  487. }
  488. };
  489. $this->sendWait($proto->addr,$arr,'farmlandNumResp',$callback,10);
  490. }
  491. protected function onFarmlandNumResp( $connection, $resp ){
  492. //var_dump($connection);
  493. var_dump($resp);
  494. }
  495. protected function onControlFinish( $connection, $proto ){
  496. /*
  497. {
  498. "method" : "controlFinish",
  499. "channelGroup" : 0, // 设备通道组
  500. "channelNum" : 1, // 卷膜机左/右
  501. "success" : true, // 操作成功,失败
  502. "isman" : 0, // 操作类型:(自动-0,手动-1)
  503. "operatTime" : 1552353910, // 操作时间戳
  504. "operat" : 1, // 当前操作,停止-0,打开-1,关闭-2,全打开-3,全关闭-4
  505. }
  506. */
  507. // 保存操作日志
  508. // 获取设备id
  509. $where = array('DeviceAddr' => $connection->addr);
  510. $device_id = MM('dpsb_device')->where($where)->getField('ID');
  511. if(!$device_id){
  512. $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  513. return;
  514. }
  515. // 获取大棚id
  516. $where = array('DeviceId'=>$device_id,'ChGroup'=>$proto->channelGroup);
  517. $farmland_id = MM('dpsb_chgroup')->where($where)->getField('FarmlandId');
  518. // 操作描述
  519. $control_status = array('自动控制','手动控制');
  520. $operat_status = array('停止','打开','关闭','全打开','全关闭');
  521. $operat_content = $control_status[$proto->isman] .'-'. $operat_status[$proto->operat];
  522. $log_data = array(
  523. 'ID' => create_guid(),
  524. 'IsMan' => $proto->isman,
  525. 'AddTime' => date('Y-m-d H:i:s',$proto->operatTime),
  526. 'DeviceId' => $device_id,
  527. 'FarmlandId' => $farmland_id,
  528. 'ChannelNumber' => $proto->channelNum,
  529. 'LogContent' => $operat_content,
  530. 'OperResult' => $proto->success ? '成功':'失败',
  531. );
  532. // 如果是控制台手动操作的,记录操作用户
  533. if( $proto->userName ){
  534. //获取UserId
  535. $where = array('UserName'=>$proto->userName);
  536. $log_data['UserId'] = MM('uc_user')->where($where)->getField('ID');
  537. }
  538. $result = MM('dpsj_oplog')->createAdd($log_data);
  539. $arr = array(
  540. 'method' => $proto->method.'Resp',
  541. 'message' => $result ? 'success':'fail',
  542. 'success' => (bool)$result
  543. );
  544. $connection->send($arr);
  545. }
  546. }