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(){ } }