123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- <?php
- namespace Jiaruan;
- class ProtoHeartbeat {
-
-
- public function resp_black1( $req ){
- $lock_cfg = array(
- "subtype" => 5,
- "mac"=> substr($req->mac,-6),
- "elable" => "411681111138",
- "op" => 1 //1-添加黑车 2-删除黑车
- );
- $item = array(
- "subtype" => 5,
- "elable" => '',
- "op" => 1 //1-添加黑车 2-删除黑车
- );
-
- $lock_cfg = array(
- );
- for($i=138;$i<338;$i++){
- $item['elable'] = "411681111" . $i;
- array_push($lock_cfg,$item);
- }
-
- return $this->create_resp( $req,$lock_cfg );
- }
-
-
- public function resp_black2( $req ){
- $lock_cfg = array(
- );
- $item = array(
- "subtype" => 5,
- "elable" => '',
- "op" => 2 //1-添加黑车 2-删除黑车
- );
- for($i=138;$i<238;$i++){
- $item['elable'] = "411681111" . $i;
- array_push($lock_cfg,$item);
- }
- return $this->create_resp( $req,$lock_cfg );
- }
-
-
- private function resp_all_blacklist( $req ){
- $cityId = get_station_cityid($req->mac);
- if ($cityId == 0) {
- log_debug("第一次心跳布控,没有查询到对应的城市id, 基站MAC = " . $req->mac);
- return $this->create_resp($req, array() );
- }
-
- //
- $model = M("jzgl_blacklist");
- $where = array(
- 'CityId' => $cityId,
- 'MonitorStatus' => C("布控状态_布控")
- );
- $data = $model->field("DeviceNumber")->where($where)->select();
-
- if ($data) {
- log_debug("第一次心跳布控车辆为" . json_encode($data));
- } else {
- log_debug("第一次心跳布控车辆为0");
- }
- $monit_cfg = array();
- $item = array(
- "subtype" => 5,
- "elable" => '',
- "op" => 1 //1-添加黑车 2-删除黑车
- );
- foreach ($data as $value) {
- $item['elable'] = $value['DeviceNumber'];
- array_push($monit_cfg, $item);
- }
- return $this->create_resp($req, $monit_cfg);
- }
-
-
- public function proc( $req, $param ){
- //第一次心跳下发所有的布控车辆
- if($param['heartbeat'] == 1){
- return $this->resp_all_blacklist($req);
- }
-
-
- //返回被盗配置
- /*
- static $i = 0;
- if($i == 0){
- $resp = $this->resp_black1($req);
- $i = 1;
- }
- else{
- $resp = $this->resp_black2($req);
- $i = 0;
- }
- return $resp;
- */
- /*
- 上锁解锁配置
- $resp = $this->resp_lockddc($req);
- if($resp)
- return $resp;
- */
- /*
- //重启配置
- $resp = $this->resp_reboot($req);
- if($resp)
- return $resp;
- //升级配置
- $resp = $this->resp_upgrade($req);
- if($resp)
- return $resp;
- //命令配置
- $resp = $this->resp_cmd($req);
- if($resp)
- return $resp;
- //上锁配置
- $resp = $this->resp_lockddc($req);
- if($resp)
- return $resp;
- //实时上报配置
- $resp = $this->resp_rtreport($req);
- if($resp)
- return $resp;
- return null;
- */
- //返回被盗配置
- //$resp = $this->resp_black($req);
- //return $resp;
- //返回空配置包
- $resp = $this->resp_empty($req);
- return $resp;
- }
-
-
- public function sign_req( $req ){
- $str = $req->type;
- $str.= $req->msgid;
- $str.= $req->mac;
- $str.= $req->interval;
- $str.= $req->uptime;
- $str.= C('协议签名密钥');
- return strtoupper(md5(urlencode($str)));
- }
-
-
- public function resp_reboot( $req ){
- $reboot_cfg = array(
- "subtype"=> 1,
- "cmd"=> "reboot"
- );
- return $this->create_resp( $req,array($reboot_cfg) );
- }
-
-
- public function resp_upgrade( ){
- return null;
- $upgrade_cfg = array(
- "subtype"=> 3,
- "version"=> "",
- "ftpip"=> "",
- "ftpport"=> "",
- "name"=> "",
- "pssword"=> "",
- "filename"=> "",
- "filesize"=> "",
- );
- return $this->create_resp( $req,array($upgrade_cfg) );
- }
-
-
- public function resp_lockddc( $req ){
- /*$lock_cfg1 = array(
- "subtype" => 4,
- "base_mac"=> substr($req->mac,-6),
- "elable" => "411681111114",
- "lock" => 0
- );
- $lock_cfg2 = array(
- "subtype" => 4,
- "base_mac"=> substr($req->mac,-6),
- "elable" => "411681111115",
- "lock" => 0
- );*/
- //获取基站待下发配置数据
- $cond = array(
- 'StationMac' => $req->mac,
- 'IsSent' => 0,
- 'ConfigType' => C('配置类型_解锁或锁车')
- );
- $list = M('sc_station_cfg')->where($cond)->select();
- $conf_arr = array();
- foreach($list as $key=>$row){
- $conf_data = json_decode($row['ConfigData'],true);
- if($conf_data){
- array_push($conf_arr,$conf_data);
- //变更待下发基站状态
- $cond = array('ID'=>$row['ID']);
- $data = array('IsSent'=>1,'SendTime'=>date('Y-m-d H:i:s'));
- if(!M('sc_station_cfg')->createSave($cond,$data)){
- echo '变更基站配置发送状态失败'. PHP_EOL;
- return;
- }
- //变更车辆锁定状态
- if(!$conf_data['lock']){
- //解锁车辆
- $LockStatus = C('车辆锁定状态_待解锁');
- $cond = array('VehicleLabel'=>$row['VehicleLabel'],'LockStationMac'=>$row['StationMac'],'LockStatus'=>$LockStatus);
- $lock_info = M('sc_lock_vehicle')->where($cond)->find();
- if(!$lock_info){
- echo '未找到待解锁车辆记录'. PHP_EOL;
- return;
- }
- $cond = array('ID'=>$lock_info['ID']);
- $data = array('LockStatus'=>C('车辆锁定状态_已解锁'),'UnlockTime'=>date('Y-m-d H:i:s'));
- if(!M('sc_lock_vehicle')->createSave($cond,$data)){
- echo '设置锁定状态为已解锁失败'. PHP_EOL;
- return;
- }
- }else{
- //锁定车辆
- $LockStatus = C('车辆锁定状态_待锁定');
- $cond = array('VehicleLabel'=>$row['VehicleLabel'],'LockStationMac'=>$row['StationMac'],'LockStatus'=>$LockStatus);
- $lock_info = M('sc_lock_vehicle')->where($cond)->find();
- if(!$lock_info){
- echo '未找到待锁定车辆记录'. PHP_EOL;
- return;
- }
- $cond = array('ID'=>$lock_info['ID']);
- $data = array('LockStatus'=>C('车辆锁定状态_已锁定'),'LockTime'=>date('Y-m-d H:i:s'));
- if(!M('sc_lock_vehicle')->createSave($cond,$data)){
- echo '设置锁定状态为已锁定失败'. PHP_EOL;
- return;
- }
- }
- }
- }
- return $this->create_resp( $req,$conf_arr );
-
- }
-
-
- public function resp_rtreport( $req ){
- $rtreport_cfg1 = array(
- "subtype"=> 5,
- "base_mac"=> substr($req->mac,-6),
- "elable" => "411681111114",
- "op"=> 2
- );
- $rtreport_cfg2 = array(
- "subtype"=> 5,
- "base_mac"=> substr($req->mac,-6),
- "elable" => "411681111115",
- "op"=> 2
- );
- return $this->create_resp( $req,array($rtreport_cfg1,$rtreport_cfg2) );
- }
-
-
- public function resp_cmd( $req ){
- $cmd_cfg = array(
- "subtype"=> 1,
- "cmd"=> "echo 123 >> /tmp/1.txt"
- );
- return $this->create_resp( $req,array($cmd_cfg) );
- }
-
-
- public function sign_resp( $resp ){
- $str = $resp['type'];
- $str.= $resp['msgid'];
- $str.= $resp['num'];
- foreach($resp['config'] as $cfg){
- if( $cfg['subtype'] == C('配置类型_执行命令') ){
- $str.= $cfg['version'];
- $str.= $cfg['ftpip'];
- $str.= $cfg['ftpport'];
- $str.= $cfg['name'];
- $str.= $cfg['password'];
- $str.= $cfg['filename'];
- $str.= $cfg['filesize'];
- }
- else if( $cfg['subtype'] == C('配置类型_升级基站') ){
- $str.= $cfg['subtype'];
- $str.= $cfg['cmd'];
- }
- else if( $cfg['subtype'] == C('配置类型_重启基站') ){
- $str.= $cfg['subtype'];
- $str.= $cfg['cmd'];
- }
- else if( $cfg['subtype'] == C('配置类型_解锁或锁车') ){
- }
- else if( $cfg['subtype'] == C('配置类型_被盗实时上报') ){
- }
- else{
- }
- }
- $str.= C('协议签名密钥');
- return strtoupper(md5(urlencode($str)));
- }
-
-
- public function create_resp( $req, $config ){
- $resp = array(
- 'type' => 4,
- 'msgid' => $req->msgid,
- 'mac' => $req->mac,
- 'num' => count($config),
- 'config' => $config,
- );
- return $resp;
- }
-
-
- public function resp_black( $req ){
- $lock_cfg = array(
- "subtype" => 5,
- "mac"=> substr($req->mac,-6),
- "elable" => "411681111138",
- "op" => 1 //1-添加黑车 2-删除黑车
- );
- return $this->create_resp( $req,array($lock_cfg) );
- }
-
-
- public function resp_empty( $req ){
- $empty_cfg = array();
- return $this->create_resp( $req,$empty_cfg );
- }
-
- }
|