DpxfIndex1Action.class.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. class DpxfIndex1Action extends \Jms\Network\WorkermanServerAction {
  3. /*
  4. */
  5. public function index(){
  6. $this->start("jsontext",10240,1);
  7. }
  8. /*
  9. 参数说明:
  10. connection:
  11. proto:
  12. */
  13. protected function onAppLogin($connection, $proto){
  14. //检查imei地址
  15. if(!$proto->imei){
  16. $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  17. return;
  18. }
  19. $addr = MM('dpsb_device')->where(array('DeviceImei'=>$proto->imei))->getField('DeviceAddr');
  20. if(!$addr){
  21. $this->respError($connection,'addr is empty ',$proto);
  22. return;
  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,$addr);
  31. if(!$device_id){
  32. $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  33. return;
  34. }
  35. if(!$proto->channel){
  36. $this->respError($connection,'channeldata is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  37. return;
  38. }
  39. $errmsg = saveChannel($device_id,$proto->channel);
  40. if($errmsg){
  41. $this->respError($connection,$errmsg,$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
  42. return;
  43. }
  44. $arr = array(
  45. 'method' => $proto->method.'Resp',
  46. 'Addr' => $addr
  47. );
  48. $connection->send($arr);
  49. }
  50. /*
  51. 参数说明:
  52. connection:
  53. message:
  54. proto:
  55. data:
  56. */
  57. private function respError($connection, $message, $proto, $data){
  58. //判断$proto是否为字符串
  59. if(!is_string($proto)){
  60. $protostr = json_encode($proto);
  61. }else{
  62. $protostr = $proto;
  63. }
  64. log_error($message.' proto = '.$protostr);
  65. $array = array(
  66. 'success' => false,
  67. 'message' => $message,
  68. 'data' => $data,
  69. );
  70. //判断是否存在method
  71. if($proto->method){
  72. $array['method'] = $proto->method . 'Resp';
  73. }
  74. $connection->send($array);
  75. $connection->close();
  76. }
  77. /*
  78. 参数说明:
  79. connection:
  80. proto:
  81. */
  82. protected function onApiSendControl($connection, $proto){
  83. /*返回结果:
  84. 1.失败,手动控制命令发送失败,地址码未设置
  85. 2.失败,手动控制命令发送失败,手动控制命令格式错误
  86. 3.失败,手动控制命令发送失败,设备未在线
  87. 4.失败,手动控制命令发送失败,可能设备掉线
  88. 5.失败,手动控制命令发送成功,但是设备回应超时
  89. 6.失败,手动控制命令发送成功,但是设备回应格式错误
  90. 7.失败,手动控制命令发送成功,但是设备回应错误信息
  91. 8.成功
  92. */
  93. //检查addr
  94. if(!$proto->Addr){
  95. $this->respError($connection,'addr is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  96. return;
  97. }
  98. //给控制app终端发送手动控制信号,并等待回应
  99. $arr = array(
  100. "method"=> $proto->method,
  101. "Addr"=>$proto->Addr,
  102. "channelNumber"=>$proto->channelNumber,
  103. "status"=>$proto->status
  104. );
  105. $callback = function($errno,$resp) use ($connection){
  106. if($errno == \Zndp\Api\ResponseCode::SEND_SUCCESS){
  107. if(is_object($resp)){
  108. $res = $connection->send($resp);
  109. $connection->close();
  110. }
  111. else
  112. $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  113. }
  114. else{
  115. $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  116. }
  117. };
  118. $this->sendWait($proto->Addr,$arr,'appSendControlResp',$callback,3);
  119. }
  120. /*
  121. 参数说明:
  122. connection:
  123. proto:
  124. */
  125. protected function onApiModifyAutoControlInfo($connection, $proto){
  126. //检查addr是否为空
  127. if(!$proto->Addr){
  128. $this->respError($connection,'addr is empty',$proto,\Zndp\Api\ResponseCode::NOT_AUTH_DEVICE);
  129. return;
  130. }
  131. //检查channelNumber是否为空
  132. if(!$proto->channelNumber){
  133. $this->respError($connection,'channelNumber empty',$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
  134. return;
  135. }
  136. //检查channelNumber是否存在
  137. $device_id = MM('dpsb_device')->where(array('DeviceAddr'=>$proto->Addr))->getField('ID');
  138. $cond = array(
  139. 'DeviceId'=>$device_id,
  140. 'ChNumber'=>$proto->channelNumber,
  141. );
  142. $result = MM('dpsb_channel')->where($cond)->find();
  143. if(!$result){
  144. $this->respError($connection,'channelNumber inexistence',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  145. return;
  146. }
  147. //给app转发自动控制命令
  148. $arr = array(
  149. 'method'=>$proto->method,
  150. 'Addr'=>$proto->Addr,
  151. 'channelNumber'=>$proto->channelNumber,
  152. 'man'=>$proto->man,
  153. 'refChannel'=>$proto->refChannel,
  154. 'TimeEn'=>$proto->TimeEn,
  155. 'UpperLimit_Value'=>$proto->UpperLimit_Value,
  156. 'UpperLimit_State'=>$proto->UpperLimit_State,
  157. 'LowerLimit_Value'=>$proto->LowerLimit_Value,
  158. 'LowerLimit_State'=>$proto->LowerLimit_State,
  159. 'OnTime1'=>$proto->OnTime1,
  160. 'OffTime1'=>$proto->OffTime1,
  161. 'OnTime2'=>$proto->OnTime2,
  162. 'OffTime2'=>$proto->OffTime2,
  163. 'OnTime3'=>$proto->OnTime3,
  164. 'OffTime3'=>$proto->OffTime3,
  165. 'OnTime4'=>$proto->OnTime4,
  166. 'OffTime4'=>$proto->OffTime4,
  167. 'OnTime5'=>$proto->OnTime5,
  168. 'OffTime5'=>$proto->OffTime5,
  169. );
  170. $callback = function($errno,$resp) use ($connection){
  171. if($errno == \Zndp\Api\ResponseCode::SEND_SUCCESS){
  172. if(is_object($resp)){
  173. $connection->send($resp);
  174. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::SEND_SUCCESS);
  175. $connection->close();
  176. }
  177. else{
  178. $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  179. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  180. }
  181. }
  182. else{
  183. $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  184. $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  185. }
  186. };
  187. $this->sendWait($proto->Addr,$arr,'AppModifyAutoControlInfoResp',$callback,3);
  188. }
  189. /*
  190. 参数说明:
  191. device_id:
  192. proto:
  193. connection:
  194. msg:
  195. */
  196. private function saveModifyAutoData($device_id, $proto, $connection, $msg){
  197. $where = array(
  198. 'DeviceId'=> $device_id,
  199. 'Channel'=>$proto->channelNumber,
  200. );
  201. $res = MM('dpsb_policy')->where($where)->find();
  202. if($res){
  203. $saveData = array(
  204. 'Man'=> $proto->man,
  205. 'RefChannel'=> $proto->refChannel,
  206. 'IsTime'=> $proto->TimeEn,
  207. 'UpperLimitValue'=> $proto->UpperLimit_Value,
  208. 'UpperLimitState'=> $proto->UpperLimit_State,
  209. 'LowerLimitValue'=> $proto->LowerLimit_Value,
  210. 'LowerLimitState'=> $proto->LowerLimit_State,
  211. 'OnTime1'=> $proto->OnTime1,
  212. 'OffTime1'=> $proto->OffTime1,
  213. 'OnTime2'=> $proto->OnTime2,
  214. 'OffTime2'=> $proto->OffTime2,
  215. 'OnTime3'=> $proto->OnTime3,
  216. 'OffTime3'=> $proto->OffTime3,
  217. 'OnTime4'=> $proto->OnTime4,
  218. 'OffTime4'=> $proto->OffTime4,
  219. 'OnTime5'=> $proto->OnTime5,
  220. 'OffTime5'=> $proto->OffTime5,
  221. 'SendTime'=> date('Y-m-d H:i:s'),
  222. 'SendResult'=> $msg
  223. );
  224. $result = MM('dpsb_policy')->createSave($where,$saveData);
  225. }else{
  226. $addData = array(
  227. 'DeviceId'=> $device_id,
  228. 'Channel'=>$proto->channelNumber,
  229. 'Man'=> $proto->man,
  230. 'RefChannel'=> $proto->refChannel,
  231. 'IsTime'=> $proto->TimeEn,
  232. 'UpperLimitValue'=> $proto->UpperLimit_Value,
  233. 'UpperLimitState'=> $proto->UpperLimit_State,
  234. 'LowerLimitValue'=> $proto->LowerLimit_Value,
  235. 'LowerLimitState'=> $proto->LowerLimit_State,
  236. 'OnTime1'=> $proto->OnTime1,
  237. 'OffTime1'=> $proto->OffTime1,
  238. 'OnTime2'=> $proto->OnTime2,
  239. 'OffTime2'=> $proto->OffTime2,
  240. 'OnTime3'=> $proto->OnTime3,
  241. 'OffTime3'=> $proto->OffTime3,
  242. 'OnTime4'=> $proto->OnTime4,
  243. 'OffTime4'=> $proto->OffTime4,
  244. 'OnTime5'=> $proto->OnTime5,
  245. 'OffTime5'=> $proto->OffTime5,
  246. 'SendTime'=> date('Y-m-d H:i:s'),
  247. 'SendResult'=> $msg
  248. );
  249. $result = MM('dpsb_policy')->createAdd($addData);
  250. }
  251. if(!$result){
  252. $this->respError($connection,'channelNumber inexistence',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  253. return;
  254. }
  255. }
  256. /*
  257. 参数说明:
  258. connection:
  259. proto:
  260. */
  261. protected function onAppSendRealTimeDeviceData($connection, $proto){
  262. //检查imei地址
  263. if(!$proto->imei){
  264. $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  265. return;
  266. }
  267. if(!$connection->addr){
  268. $this->respError($connection,'addr is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  269. return;
  270. }
  271. // 将数据更新到数据表中
  272. $data = $proto->data;
  273. $gathertime = date('Y-m-d H:i:s',$proto->gathertime);
  274. $device_id = saveDeviceInfo($proto->imei,$connection->addr);
  275. if(!$device_id){
  276. $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
  277. return;
  278. }
  279. if(!$proto->data){
  280. $this->respError($connection,'data is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  281. return;
  282. }
  283. if(!$proto->gathertime){
  284. $this->respError($connection,'gathertime is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
  285. return;
  286. }
  287. $errmsg1 = saveToDevice($device_id,$gathertime);
  288. if($errmsg1){
  289. $this->respError($connection,$errmsg1,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  290. return;
  291. }
  292. $errmsg2 = saveToChannel($device_id,$data,$gathertime);
  293. if($errmsg2){
  294. $this->respError($connection,$errmsg2,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  295. return;
  296. }
  297. $errmsg3 = saveToChannelData($device_id,$data,$gathertime);
  298. if($errmsg3){
  299. $this->respError($connection,$errmsg3,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
  300. return;
  301. }
  302. $arr = array(
  303. 'method' => $proto->method.'Resp',
  304. "success"=>true,
  305. "message"=>"ok",
  306. "addtime"=>time(),
  307. );
  308. $connection->send($arr);
  309. }
  310. /*
  311. 参数说明:
  312. connection:
  313. proto:
  314. */
  315. protected function onAppHeartbeat($connection, $proto){
  316. //直接返回心跳
  317. $arr = array(
  318. 'success'=>true
  319. );
  320. $connection->send($arr);
  321. }
  322. /*
  323. */
  324. protected function onAppSendControlResp(){
  325. }
  326. /*
  327. */
  328. protected function onAppModifyAutoControlInfoResp(){
  329. }
  330. }