123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- class DpxfIndex1Action extends \Jms\Network\WorkermanServerAction {
- /*
- */
- public function index(){
- $this->start("jsontext",10240,1);
- }
- /*
- 参数说明:
- connection:
- proto:
- */
- protected function onAppLogin($connection, $proto){
- //检查imei地址
- if(!$proto->imei){
- $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
-
- $addr = MM('dpsb_device')->where(array('DeviceImei'=>$proto->imei))->getField('DeviceAddr');
- if(!$addr){
- $this->respError($connection,'addr is empty ',$proto);
- return;
- }
- //通过登录验证,加入连接池
- $connection->imei = $proto->imei;
- $connection->addr = $addr;
- $this->addToPool($addr,$connection);
- $this->logDebug('login success. should put into connection pool. imei = ' . $proto->imei);
-
- // 将数据更新到数据表中
- $device_id = saveDeviceInfo($proto->imei,$addr);
- if(!$device_id){
- $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
- if(!$proto->channel){
- $this->respError($connection,'channeldata is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
- $errmsg = saveChannel($device_id,$proto->channel);
- if($errmsg){
- $this->respError($connection,$errmsg,$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
- return;
- }
- $arr = array(
- 'method' => $proto->method.'Resp',
- 'Addr' => $addr
- );
- $connection->send($arr);
- }
- /*
- 参数说明:
- connection:
- message:
- proto:
- data:
- */
- private function respError($connection, $message, $proto, $data){
- //判断$proto是否为字符串
- if(!is_string($proto)){
- $protostr = json_encode($proto);
- }else{
- $protostr = $proto;
- }
- log_error($message.' proto = '.$protostr);
- $array = array(
- 'success' => false,
- 'message' => $message,
- 'data' => $data,
- );
- //判断是否存在method
- if($proto->method){
- $array['method'] = $proto->method . 'Resp';
- }
-
- $connection->send($array);
- $connection->close();
- }
- /*
- 参数说明:
- connection:
- proto:
- */
- protected function onApiSendControl($connection, $proto){
- /*返回结果:
- 1.失败,手动控制命令发送失败,地址码未设置
- 2.失败,手动控制命令发送失败,手动控制命令格式错误
- 3.失败,手动控制命令发送失败,设备未在线
- 4.失败,手动控制命令发送失败,可能设备掉线
- 5.失败,手动控制命令发送成功,但是设备回应超时
- 6.失败,手动控制命令发送成功,但是设备回应格式错误
- 7.失败,手动控制命令发送成功,但是设备回应错误信息
- 8.成功
- */
- //检查addr
- if(!$proto->Addr){
- $this->respError($connection,'addr is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
-
- //给控制app终端发送手动控制信号,并等待回应
- $arr = array(
- "method"=> $proto->method,
- "Addr"=>$proto->Addr,
- "channelNumber"=>$proto->channelNumber,
- "status"=>$proto->status
- );
- $callback = function($errno,$resp) use ($connection){
- if($errno == \Zndp\Api\ResponseCode::SEND_SUCCESS){
- if(is_object($resp)){
- $res = $connection->send($resp);
- $connection->close();
- }
- else
- $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- }
- else{
- $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- }
- };
- $this->sendWait($proto->Addr,$arr,'appSendControlResp',$callback,3);
- }
- /*
- 参数说明:
- connection:
- proto:
- */
- protected function onApiModifyAutoControlInfo($connection, $proto){
- //检查addr是否为空
- if(!$proto->Addr){
- $this->respError($connection,'addr is empty',$proto,\Zndp\Api\ResponseCode::NOT_AUTH_DEVICE);
- return;
- }
- //检查channelNumber是否为空
- if(!$proto->channelNumber){
- $this->respError($connection,'channelNumber empty',$proto,\Zndp\Api\ResponseCode::DEVICE_LOST_CONNECTION);
- return;
- }
- //检查channelNumber是否存在
- $device_id = MM('dpsb_device')->where(array('DeviceAddr'=>$proto->Addr))->getField('ID');
- $cond = array(
- 'DeviceId'=>$device_id,
- 'ChNumber'=>$proto->channelNumber,
- );
- $result = MM('dpsb_channel')->where($cond)->find();
- if(!$result){
- $this->respError($connection,'channelNumber inexistence',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
-
- //给app转发自动控制命令
- $arr = array(
- 'method'=>$proto->method,
- 'Addr'=>$proto->Addr,
- 'channelNumber'=>$proto->channelNumber,
- 'man'=>$proto->man,
- 'refChannel'=>$proto->refChannel,
- 'TimeEn'=>$proto->TimeEn,
- 'UpperLimit_Value'=>$proto->UpperLimit_Value,
- 'UpperLimit_State'=>$proto->UpperLimit_State,
- 'LowerLimit_Value'=>$proto->LowerLimit_Value,
- 'LowerLimit_State'=>$proto->LowerLimit_State,
- 'OnTime1'=>$proto->OnTime1,
- 'OffTime1'=>$proto->OffTime1,
- 'OnTime2'=>$proto->OnTime2,
- 'OffTime2'=>$proto->OffTime2,
- 'OnTime3'=>$proto->OnTime3,
- 'OffTime3'=>$proto->OffTime3,
- 'OnTime4'=>$proto->OnTime4,
- 'OffTime4'=>$proto->OffTime4,
- 'OnTime5'=>$proto->OnTime5,
- 'OffTime5'=>$proto->OffTime5,
- );
- $callback = function($errno,$resp) use ($connection){
- if($errno == \Zndp\Api\ResponseCode::SEND_SUCCESS){
- if(is_object($resp)){
- $connection->send($resp);
- $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::SEND_SUCCESS);
- $connection->close();
- }
- else{
- $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- }
- }
- else{
- $this->respError($connection,$errno,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- $this->saveModifyAutoData($device_id,$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- }
- };
- $this->sendWait($proto->Addr,$arr,'AppModifyAutoControlInfoResp',$callback,3);
-
-
- }
- /*
- 参数说明:
- device_id:
- proto:
- connection:
- msg:
- */
- private function saveModifyAutoData($device_id, $proto, $connection, $msg){
- $where = array(
- 'DeviceId'=> $device_id,
- 'Channel'=>$proto->channelNumber,
- );
- $res = MM('dpsb_policy')->where($where)->find();
- if($res){
- $saveData = array(
- 'Man'=> $proto->man,
- 'RefChannel'=> $proto->refChannel,
- 'IsTime'=> $proto->TimeEn,
- 'UpperLimitValue'=> $proto->UpperLimit_Value,
- 'UpperLimitState'=> $proto->UpperLimit_State,
- 'LowerLimitValue'=> $proto->LowerLimit_Value,
- 'LowerLimitState'=> $proto->LowerLimit_State,
- 'OnTime1'=> $proto->OnTime1,
- 'OffTime1'=> $proto->OffTime1,
- 'OnTime2'=> $proto->OnTime2,
- 'OffTime2'=> $proto->OffTime2,
- 'OnTime3'=> $proto->OnTime3,
- 'OffTime3'=> $proto->OffTime3,
- 'OnTime4'=> $proto->OnTime4,
- 'OffTime4'=> $proto->OffTime4,
- 'OnTime5'=> $proto->OnTime5,
- 'OffTime5'=> $proto->OffTime5,
- 'SendTime'=> date('Y-m-d H:i:s'),
- 'SendResult'=> $msg
- );
- $result = MM('dpsb_policy')->createSave($where,$saveData);
- }else{
- $addData = array(
- 'DeviceId'=> $device_id,
- 'Channel'=>$proto->channelNumber,
- 'Man'=> $proto->man,
- 'RefChannel'=> $proto->refChannel,
- 'IsTime'=> $proto->TimeEn,
- 'UpperLimitValue'=> $proto->UpperLimit_Value,
- 'UpperLimitState'=> $proto->UpperLimit_State,
- 'LowerLimitValue'=> $proto->LowerLimit_Value,
- 'LowerLimitState'=> $proto->LowerLimit_State,
- 'OnTime1'=> $proto->OnTime1,
- 'OffTime1'=> $proto->OffTime1,
- 'OnTime2'=> $proto->OnTime2,
- 'OffTime2'=> $proto->OffTime2,
- 'OnTime3'=> $proto->OnTime3,
- 'OffTime3'=> $proto->OffTime3,
- 'OnTime4'=> $proto->OnTime4,
- 'OffTime4'=> $proto->OffTime4,
- 'OnTime5'=> $proto->OnTime5,
- 'OffTime5'=> $proto->OffTime5,
- 'SendTime'=> date('Y-m-d H:i:s'),
- 'SendResult'=> $msg
- );
- $result = MM('dpsb_policy')->createAdd($addData);
- }
- if(!$result){
- $this->respError($connection,'channelNumber inexistence',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
- }
- /*
- 参数说明:
- connection:
- proto:
- */
- protected function onAppSendRealTimeDeviceData($connection, $proto){
- //检查imei地址
- if(!$proto->imei){
- $this->respError($connection,'imei is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
- if(!$connection->addr){
- $this->respError($connection,'addr is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
-
- // 将数据更新到数据表中
- $data = $proto->data;
- $gathertime = date('Y-m-d H:i:s',$proto->gathertime);
- $device_id = saveDeviceInfo($proto->imei,$connection->addr);
- if(!$device_id){
- $this->respError($connection,'device_id is empty ',$proto,\Zndp\Api\ResponseCode::DEVICE_NOT_EXISTS);
- return;
- }
- if(!$proto->data){
- $this->respError($connection,'data is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
- return;
- }
- if(!$proto->gathertime){
- $this->respError($connection,'gathertime is empty ',$proto,\Zndp\Api\ResponseCode::NO_DATA);
- return;
- }
- $errmsg1 = saveToDevice($device_id,$gathertime);
- if($errmsg1){
- $this->respError($connection,$errmsg1,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
- return;
- }
- $errmsg2 = saveToChannel($device_id,$data,$gathertime);
- if($errmsg2){
- $this->respError($connection,$errmsg2,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
- return;
- }
- $errmsg3 = saveToChannelData($device_id,$data,$gathertime);
- if($errmsg3){
- $this->respError($connection,$errmsg3,$proto,\Zndp\Api\ResponseCode::DEVICE_OPRATE_DISALLOWED);
- return;
- }
- $arr = array(
- 'method' => $proto->method.'Resp',
- "success"=>true,
- "message"=>"ok",
- "addtime"=>time(),
- );
- $connection->send($arr);
- }
- /*
- 参数说明:
- connection:
- proto:
- */
- protected function onAppHeartbeat($connection, $proto){
- //直接返回心跳
- $arr = array(
- 'success'=>true
- );
- $connection->send($arr);
- }
- /*
- */
- protected function onAppSendControlResp(){
-
- }
- /*
- */
- protected function onAppModifyAutoControlInfoResp(){
-
- }
- }
|