ProtoHeartbeat.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. namespace Jiaruan;
  3. class ProtoHeartbeat {
  4. public function resp_black1( $req ){
  5. $lock_cfg = array(
  6. "subtype" => 5,
  7. "mac"=> substr($req->mac,-6),
  8. "elable" => "411681111138",
  9. "op" => 1 //1-添加黑车 2-删除黑车
  10. );
  11. $item = array(
  12. "subtype" => 5,
  13. "elable" => '',
  14. "op" => 1 //1-添加黑车 2-删除黑车
  15. );
  16. $lock_cfg = array(
  17. );
  18. for($i=138;$i<338;$i++){
  19. $item['elable'] = "411681111" . $i;
  20. array_push($lock_cfg,$item);
  21. }
  22. return $this->create_resp( $req,$lock_cfg );
  23. }
  24. public function resp_black2( $req ){
  25. $lock_cfg = array(
  26. );
  27. $item = array(
  28. "subtype" => 5,
  29. "elable" => '',
  30. "op" => 2 //1-添加黑车 2-删除黑车
  31. );
  32. for($i=138;$i<238;$i++){
  33. $item['elable'] = "411681111" . $i;
  34. array_push($lock_cfg,$item);
  35. }
  36. return $this->create_resp( $req,$lock_cfg );
  37. }
  38. private function resp_all_blacklist( $req ){
  39. $cityId = get_station_cityid($req->mac);
  40. if ($cityId == 0) {
  41. log_debug("第一次心跳布控,没有查询到对应的城市id, 基站MAC = " . $req->mac);
  42. return $this->create_resp($req, array() );
  43. }
  44. //
  45. $model = M("jzgl_blacklist");
  46. $where = array(
  47. 'CityId' => $cityId,
  48. 'MonitorStatus' => C("布控状态_布控")
  49. );
  50. $data = $model->field("DeviceNumber")->where($where)->select();
  51. if ($data) {
  52. log_debug("第一次心跳布控车辆为" . json_encode($data));
  53. } else {
  54. log_debug("第一次心跳布控车辆为0");
  55. }
  56. $monit_cfg = array();
  57. $item = array(
  58. "subtype" => 5,
  59. "elable" => '',
  60. "op" => 1 //1-添加黑车 2-删除黑车
  61. );
  62. foreach ($data as $value) {
  63. $item['elable'] = $value['DeviceNumber'];
  64. array_push($monit_cfg, $item);
  65. }
  66. return $this->create_resp($req, $monit_cfg);
  67. }
  68. public function proc( $req, $param ){
  69. //第一次心跳下发所有的布控车辆
  70. if($param['heartbeat'] == 1){
  71. return $this->resp_all_blacklist($req);
  72. }
  73. //返回被盗配置
  74. /*
  75. static $i = 0;
  76. if($i == 0){
  77. $resp = $this->resp_black1($req);
  78. $i = 1;
  79. }
  80. else{
  81. $resp = $this->resp_black2($req);
  82. $i = 0;
  83. }
  84. return $resp;
  85. */
  86. /*
  87. 上锁解锁配置
  88. $resp = $this->resp_lockddc($req);
  89. if($resp)
  90. return $resp;
  91. */
  92. /*
  93. //重启配置
  94. $resp = $this->resp_reboot($req);
  95. if($resp)
  96. return $resp;
  97. //升级配置
  98. $resp = $this->resp_upgrade($req);
  99. if($resp)
  100. return $resp;
  101. //命令配置
  102. $resp = $this->resp_cmd($req);
  103. if($resp)
  104. return $resp;
  105. //上锁配置
  106. $resp = $this->resp_lockddc($req);
  107. if($resp)
  108. return $resp;
  109. //实时上报配置
  110. $resp = $this->resp_rtreport($req);
  111. if($resp)
  112. return $resp;
  113. return null;
  114. */
  115. //返回被盗配置
  116. //$resp = $this->resp_black($req);
  117. //return $resp;
  118. //返回空配置包
  119. $resp = $this->resp_empty($req);
  120. return $resp;
  121. }
  122. public function sign_req( $req ){
  123. $str = $req->type;
  124. $str.= $req->msgid;
  125. $str.= $req->mac;
  126. $str.= $req->interval;
  127. $str.= $req->uptime;
  128. $str.= C('协议签名密钥');
  129. return strtoupper(md5(urlencode($str)));
  130. }
  131. public function resp_reboot( $req ){
  132. $reboot_cfg = array(
  133. "subtype"=> 1,
  134. "cmd"=> "reboot"
  135. );
  136. return $this->create_resp( $req,array($reboot_cfg) );
  137. }
  138. public function resp_upgrade( ){
  139. return null;
  140. $upgrade_cfg = array(
  141. "subtype"=> 3,
  142. "version"=> "",
  143. "ftpip"=> "",
  144. "ftpport"=> "",
  145. "name"=> "",
  146. "pssword"=> "",
  147. "filename"=> "",
  148. "filesize"=> "",
  149. );
  150. return $this->create_resp( $req,array($upgrade_cfg) );
  151. }
  152. public function resp_lockddc( $req ){
  153. /*$lock_cfg1 = array(
  154. "subtype" => 4,
  155. "base_mac"=> substr($req->mac,-6),
  156. "elable" => "411681111114",
  157. "lock" => 0
  158. );
  159. $lock_cfg2 = array(
  160. "subtype" => 4,
  161. "base_mac"=> substr($req->mac,-6),
  162. "elable" => "411681111115",
  163. "lock" => 0
  164. );*/
  165. //获取基站待下发配置数据
  166. $cond = array(
  167. 'StationMac' => $req->mac,
  168. 'IsSent' => 0,
  169. 'ConfigType' => C('配置类型_解锁或锁车')
  170. );
  171. $list = M('sc_station_cfg')->where($cond)->select();
  172. $conf_arr = array();
  173. foreach($list as $key=>$row){
  174. $conf_data = json_decode($row['ConfigData'],true);
  175. if($conf_data){
  176. array_push($conf_arr,$conf_data);
  177. //变更待下发基站状态
  178. $cond = array('ID'=>$row['ID']);
  179. $data = array('IsSent'=>1,'SendTime'=>date('Y-m-d H:i:s'));
  180. if(!M('sc_station_cfg')->createSave($cond,$data)){
  181. echo '变更基站配置发送状态失败'. PHP_EOL;
  182. return;
  183. }
  184. //变更车辆锁定状态
  185. if(!$conf_data['lock']){
  186. //解锁车辆
  187. $LockStatus = C('车辆锁定状态_待解锁');
  188. $cond = array('VehicleLabel'=>$row['VehicleLabel'],'LockStationMac'=>$row['StationMac'],'LockStatus'=>$LockStatus);
  189. $lock_info = M('sc_lock_vehicle')->where($cond)->find();
  190. if(!$lock_info){
  191. echo '未找到待解锁车辆记录'. PHP_EOL;
  192. return;
  193. }
  194. $cond = array('ID'=>$lock_info['ID']);
  195. $data = array('LockStatus'=>C('车辆锁定状态_已解锁'),'UnlockTime'=>date('Y-m-d H:i:s'));
  196. if(!M('sc_lock_vehicle')->createSave($cond,$data)){
  197. echo '设置锁定状态为已解锁失败'. PHP_EOL;
  198. return;
  199. }
  200. }else{
  201. //锁定车辆
  202. $LockStatus = C('车辆锁定状态_待锁定');
  203. $cond = array('VehicleLabel'=>$row['VehicleLabel'],'LockStationMac'=>$row['StationMac'],'LockStatus'=>$LockStatus);
  204. $lock_info = M('sc_lock_vehicle')->where($cond)->find();
  205. if(!$lock_info){
  206. echo '未找到待锁定车辆记录'. PHP_EOL;
  207. return;
  208. }
  209. $cond = array('ID'=>$lock_info['ID']);
  210. $data = array('LockStatus'=>C('车辆锁定状态_已锁定'),'LockTime'=>date('Y-m-d H:i:s'));
  211. if(!M('sc_lock_vehicle')->createSave($cond,$data)){
  212. echo '设置锁定状态为已锁定失败'. PHP_EOL;
  213. return;
  214. }
  215. }
  216. }
  217. }
  218. return $this->create_resp( $req,$conf_arr );
  219. }
  220. public function resp_rtreport( $req ){
  221. $rtreport_cfg1 = array(
  222. "subtype"=> 5,
  223. "base_mac"=> substr($req->mac,-6),
  224. "elable" => "411681111114",
  225. "op"=> 2
  226. );
  227. $rtreport_cfg2 = array(
  228. "subtype"=> 5,
  229. "base_mac"=> substr($req->mac,-6),
  230. "elable" => "411681111115",
  231. "op"=> 2
  232. );
  233. return $this->create_resp( $req,array($rtreport_cfg1,$rtreport_cfg2) );
  234. }
  235. public function resp_cmd( $req ){
  236. $cmd_cfg = array(
  237. "subtype"=> 1,
  238. "cmd"=> "echo 123 >> /tmp/1.txt"
  239. );
  240. return $this->create_resp( $req,array($cmd_cfg) );
  241. }
  242. public function sign_resp( $resp ){
  243. $str = $resp['type'];
  244. $str.= $resp['msgid'];
  245. $str.= $resp['num'];
  246. foreach($resp['config'] as $cfg){
  247. if( $cfg['subtype'] == C('配置类型_执行命令') ){
  248. $str.= $cfg['version'];
  249. $str.= $cfg['ftpip'];
  250. $str.= $cfg['ftpport'];
  251. $str.= $cfg['name'];
  252. $str.= $cfg['password'];
  253. $str.= $cfg['filename'];
  254. $str.= $cfg['filesize'];
  255. }
  256. else if( $cfg['subtype'] == C('配置类型_升级基站') ){
  257. $str.= $cfg['subtype'];
  258. $str.= $cfg['cmd'];
  259. }
  260. else if( $cfg['subtype'] == C('配置类型_重启基站') ){
  261. $str.= $cfg['subtype'];
  262. $str.= $cfg['cmd'];
  263. }
  264. else if( $cfg['subtype'] == C('配置类型_解锁或锁车') ){
  265. }
  266. else if( $cfg['subtype'] == C('配置类型_被盗实时上报') ){
  267. }
  268. else{
  269. }
  270. }
  271. $str.= C('协议签名密钥');
  272. return strtoupper(md5(urlencode($str)));
  273. }
  274. public function create_resp( $req, $config ){
  275. $resp = array(
  276. 'type' => 4,
  277. 'msgid' => $req->msgid,
  278. 'mac' => $req->mac,
  279. 'num' => count($config),
  280. 'config' => $config,
  281. );
  282. return $resp;
  283. }
  284. public function resp_black( $req ){
  285. $lock_cfg = array(
  286. "subtype" => 5,
  287. "mac"=> substr($req->mac,-6),
  288. "elable" => "411681111138",
  289. "op" => 1 //1-添加黑车 2-删除黑车
  290. );
  291. return $this->create_resp( $req,array($lock_cfg) );
  292. }
  293. public function resp_empty( $req ){
  294. $empty_cfg = array();
  295. return $this->create_resp( $req,$empty_cfg );
  296. }
  297. }