Commit 5a78b34aaa910929eaa06662aa5d60c4e225f40a

Authored by 王通
1 parent 841c9a3e

1.针对线路预设区间的处理

Too many changes to show.

To preserve performance only 16 of 37 files are displayed.

src/main/java/com/bsth/data/directive/DayOfDirectives.java
1   -package com.bsth.data.directive;
2   -
3   -import com.alibaba.fastjson.JSONObject;
4   -import com.bsth.data.LineConfigData;
5   -import com.bsth.entity.directive.D60;
6   -import com.bsth.entity.directive.D64;
7   -import com.bsth.entity.directive.Directive;
8   -import com.bsth.entity.directive.DirectiveReponse;
9   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
10   -import com.bsth.service.directive.DirectiveService;
11   -import com.bsth.websocket.handler.SendUtils;
12   -import org.slf4j.Logger;
13   -import org.slf4j.LoggerFactory;
14   -import org.springframework.beans.factory.annotation.Autowired;
15   -import org.springframework.stereotype.Component;
16   -
17   -import java.util.*;
18   -import java.util.concurrent.ConcurrentHashMap;
19   -import java.util.concurrent.ConcurrentLinkedQueue;
20   -import java.util.concurrent.ConcurrentMap;
21   -
22   -/**
23   - *
24   - * @ClassName: CurrDayDirectives
25   - * @Description: TODO(当天指令数据)
26   - * @author PanZhao
27   - * @date 2016年8月14日 下午5:23:59
28   - *
29   - */
30   -@Component
31   -public class DayOfDirectives {
32   -
33   - // 当日60指令缓存
34   - private static ConcurrentMap<Integer, D60> d60Map;
35   -
36   - // 线路切换指令 64
37   - public static ConcurrentMap<String, D64> d64Map;
38   -
39   - //等待插入的指令
40   - public static ConcurrentLinkedQueue<Directive> pstDirectives;
41   - //等待更新的指令
42   - public static ConcurrentLinkedQueue<D60> pstD60s;
43   -
44   - @Autowired
45   - DirectiveService directiveService;
46   -
47   - @Autowired
48   - SendUtils sendUtils;
49   -
50   - @Autowired
51   - LineConfigData lineConfigData;
52   -
53   - static Logger logger = LoggerFactory.getLogger(DayOfDirectives.class);
54   -
55   -
56   - static{
57   - d60Map = new ConcurrentHashMap<>();
58   - d64Map = new ConcurrentHashMap<>();
59   - pstDirectives = new ConcurrentLinkedQueue<>();
60   - pstD60s = new ConcurrentLinkedQueue<>();
61   - }
62   -
63   - public void put60(D60 d60) {
64   - d60Map.put(d60.getMsgId(), d60);
65   - //等待持久化
66   - pstDirectives.add(d60);
67   - }
68   -
69   - public void put64(D64 d64) {
70   - d64Map.put(d64.getKey(), d64);
71   - //等待持久化
72   - pstDirectives.add(d64);
73   - }
74   -
75   - /**
76   - *
77   - * @Title: reply @Description: TODO(指令 46,47 响应) @throws
78   - */
79   - public void reply(DirectiveReponse res) {
80   - Integer msgId = res.getMsgId();
81   - if (msgId == null) {
82   - logger.error("reply error , msgId is null.");
83   - return;
84   - }
85   -
86   - D60 d60 = d60Map.get(msgId);
87   -
88   - if (null == d60) {
89   - logger.error("找不到msgId: " + msgId + " 对应的指令数据");
90   - return;
91   - }
92   -
93   - switch (res.getStatus()) {
94   - case 0:
95   - d60.setReply46((short) -1);// 失败
96   - break;
97   - case 1:
98   - d60.setReply46((short) 0);// 发送成功
99   - d60.setReply46Time(System.currentTimeMillis());
100   - break;
101   - case 2:
102   - d60.setReply47((short) 0);// 驾驶员阅读
103   - d60.setReply47Time(System.currentTimeMillis());
104   - break;
105   - }
106   -
107   - //更新60数据
108   - pstD60s.add(d60);
109   -
110   - ScheduleRealInfo sch = d60.getSch();
111   - if (null == sch)
112   - return;
113   -
114   - if (d60.isDispatch()) {
115   - // 更新班次状态
116   - sch.setDirectiveState(res.getStatus() * 100);
117   - // 通知页面
118   - sendUtils.sendDirectiveToPage(sch);
119   - }
120   - }
121   -
122   - /**
123   - *
124   - * @Title: reply64 @Description: TODO(64 协议响应) @throws
125   - */
126   - public void reply64(JSONObject json) {
127   - String key = json.getString("deviceId") + "_" + json.getString("timestamp");
128   -
129   - D64 d64 = d64Map.get(key);
130   -
131   - if (null == d64)
132   - logger.warn("64响应 -找不到请求源,json: " + json);
133   - else {
134   - JSONObject data = json.getJSONObject("data");
135   -
136   - if (null == data)
137   - logger.warn("64响应 data is null ,json: " + json);
138   - else {
139   - logger.info(d64.getDeviceId() + "_" + d64.getData().getLineId() + "响应:" + data.getShort("requestAck"));
140   - /*d64.setRespAck(data.getShort("requestAck"));
141   - // 持久化*/
142   - //64 响应不入库了...
143   - }
144   - }
145   - }
146   -
147   - @Autowired
148   - DirectivesPstThread directivesPstThread;
149   - public void clearAll(){
150   - d60Map = new ConcurrentHashMap<>();
151   - d64Map = new ConcurrentHashMap<>();
152   - logger.info("清除指令数据 ,,,");
153   - }
154   -
155   - public Collection<D60> all60(){
156   - return d60Map.values();
157   - }
158   -
159   - public Collection<D64> all64(){
160   - return d64Map.values();
161   - }
162   -
163   - public D60 get(Integer msgId){
164   - return d60Map.get(msgId);
165   - }
166   -
167   - public Collection<Directive> all(){
168   - List<Directive> all = new ArrayList<>();
169   - all.addAll(d60Map.values());
170   - all.addAll(d64Map.values());
171   -
172   - return all;
173   - }
174   -
175   - public static class DComparator implements Comparator<Directive>{
176   -
177   - @Override
178   - public int compare(Directive d1, Directive d2) {
179   - return (int) (d2.getTimestamp() - d1.getTimestamp());
180   - }
181   - }
182   - }
  1 +package com.bsth.data.directive;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import com.bsth.data.LineConfigData;
  5 +import com.bsth.entity.directive.D60;
  6 +import com.bsth.entity.directive.D64;
  7 +import com.bsth.entity.directive.Directive;
  8 +import com.bsth.entity.directive.DirectiveReponse;
  9 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  10 +import com.bsth.service.directive.DirectiveService;
  11 +import com.bsth.websocket.handler.SendUtils;
  12 +import org.slf4j.Logger;
  13 +import org.slf4j.LoggerFactory;
  14 +import org.springframework.beans.factory.annotation.Autowired;
  15 +import org.springframework.stereotype.Component;
  16 +
  17 +import java.util.*;
  18 +import java.util.concurrent.ConcurrentHashMap;
  19 +import java.util.concurrent.ConcurrentLinkedQueue;
  20 +import java.util.concurrent.ConcurrentMap;
  21 +
  22 +/**
  23 + *
  24 + * @ClassName: CurrDayDirectives
  25 + * @Description: TODO(当天指令数据)
  26 + * @author PanZhao
  27 + * @date 2016年8月14日 下午5:23:59
  28 + *
  29 + */
  30 +@Component
  31 +public class DayOfDirectives {
  32 +
  33 + // 当日60指令缓存
  34 + private static ConcurrentMap<Integer, D60> d60Map;
  35 +
  36 + // 线路切换指令 64
  37 + public static ConcurrentMap<String, D64> d64Map;
  38 +
  39 + //等待插入的指令
  40 + public static ConcurrentLinkedQueue<Directive> pstDirectives;
  41 + //等待更新的指令
  42 + public static ConcurrentLinkedQueue<D60> pstD60s;
  43 +
  44 + @Autowired
  45 + DirectiveService directiveService;
  46 +
  47 + @Autowired
  48 + SendUtils sendUtils;
  49 +
  50 + @Autowired
  51 + LineConfigData lineConfigData;
  52 +
  53 + static Logger logger = LoggerFactory.getLogger(DayOfDirectives.class);
  54 +
  55 +
  56 + static{
  57 + d60Map = new ConcurrentHashMap<>();
  58 + d64Map = new ConcurrentHashMap<>();
  59 + pstDirectives = new ConcurrentLinkedQueue<>();
  60 + pstD60s = new ConcurrentLinkedQueue<>();
  61 + }
  62 +
  63 + public void put60(D60 d60) {
  64 + d60Map.put(d60.getMsgId(), d60);
  65 + //等待持久化
  66 + pstDirectives.add(d60);
  67 + }
  68 +
  69 + public void put64(D64 d64) {
  70 + d64Map.put(d64.getKey(), d64);
  71 + //等待持久化
  72 + pstDirectives.add(d64);
  73 + }
  74 +
  75 + /**
  76 + *
  77 + * @Title: reply @Description: TODO(指令 46,47 响应) @throws
  78 + */
  79 + public void reply(DirectiveReponse res) {
  80 + Integer msgId = res.getMsgId();
  81 + if (msgId == null) {
  82 + logger.error("reply error , msgId is null.");
  83 + return;
  84 + }
  85 +
  86 + D60 d60 = d60Map.get(msgId);
  87 +
  88 + if (null == d60) {
  89 + logger.error("找不到msgId: " + msgId + " 对应的指令数据");
  90 + return;
  91 + }
  92 +
  93 + switch (res.getStatus()) {
  94 + case 0:
  95 + d60.setReply46((short) -1);// 失败
  96 + break;
  97 + case 1:
  98 + d60.setReply46((short) 0);// 发送成功
  99 + d60.setReply46Time(System.currentTimeMillis());
  100 + break;
  101 + case 2:
  102 + d60.setReply47((short) 0);// 驾驶员阅读
  103 + d60.setReply47Time(System.currentTimeMillis());
  104 + break;
  105 + }
  106 +
  107 + //更新60数据
  108 + pstD60s.add(d60);
  109 +
  110 + ScheduleRealInfo sch = d60.getSch();
  111 + if (null == sch)
  112 + return;
  113 +
  114 + if (d60.isDispatch()) {
  115 + // 更新班次状态
  116 + sch.setDirectiveState(res.getStatus() * 100);
  117 + // 通知页面
  118 + sendUtils.sendDirectiveToPage(sch);
  119 + }
  120 + }
  121 +
  122 + /**
  123 + *
  124 + * @Title: reply64 @Description: TODO(64 协议响应) @throws
  125 + */
  126 + public void reply64(JSONObject json) {
  127 + String key = json.getString("deviceId") + "_" + json.getString("timestamp");
  128 +
  129 + D64 d64 = d64Map.get(key);
  130 +
  131 + if (null == d64)
  132 + logger.warn("64响应 -找不到请求源,json: " + json);
  133 + else {
  134 + JSONObject data = json.getJSONObject("data");
  135 +
  136 + if (null == data)
  137 + logger.warn("64响应 data is null ,json: " + json);
  138 + else {
  139 + logger.info(d64.getDeviceId() + "_" + d64.getData().getLineId() + "响应:" + data.getShort("requestAck"));
  140 + ScheduleRealInfo sch = d64.getSch();
  141 + if (sch != null) {
  142 + sch.setRegionSwitchState(1);
  143 + }
  144 + /*d64.setRespAck(data.getShort("requestAck"));
  145 + // 持久化*/
  146 + //64 响应不入库了...
  147 + }
  148 + }
  149 + }
  150 +
  151 + @Autowired
  152 + DirectivesPstThread directivesPstThread;
  153 + public void clearAll(){
  154 + d60Map = new ConcurrentHashMap<>();
  155 + d64Map = new ConcurrentHashMap<>();
  156 + logger.info("清除指令数据 ,,,");
  157 + }
  158 +
  159 + public Collection<D60> all60(){
  160 + return d60Map.values();
  161 + }
  162 +
  163 + public Collection<D64> all64(){
  164 + return d64Map.values();
  165 + }
  166 +
  167 + public D60 get(Integer msgId){
  168 + return d60Map.get(msgId);
  169 + }
  170 +
  171 + public Collection<Directive> all(){
  172 + List<Directive> all = new ArrayList<>();
  173 + all.addAll(d60Map.values());
  174 + all.addAll(d64Map.values());
  175 +
  176 + return all;
  177 + }
  178 +
  179 + public static class DComparator implements Comparator<Directive>{
  180 +
  181 + @Override
  182 + public int compare(Directive d1, Directive d2) {
  183 + return (int) (d2.getTimestamp() - d1.getTimestamp());
  184 + }
  185 + }
  186 + }
... ...
src/main/java/com/bsth/data/directive/DirectiveCreator.java
1   -package com.bsth.data.directive;
2   -
3   -import com.alibaba.fastjson.JSON;
4   -import com.bsth.data.BasicData;
5   -import com.bsth.entity.directive.D60;
6   -import com.bsth.entity.directive.D60.D60Data;
7   -import com.bsth.entity.directive.D64;
8   -import com.bsth.entity.directive.D64.D64Data;
9   -import org.slf4j.Logger;
10   -import org.slf4j.LoggerFactory;
11   -
12   -import java.text.SimpleDateFormat;
13   -import java.util.Date;
14   -import java.util.HashMap;
15   -import java.util.Map;
16   -
17   -/**
18   - *
19   - * @ClassName: DirectiveCreator
20   - * @Description: TODO(指令数据生成)
21   - * @author PanZhao
22   - * @date 2016年8月14日 下午9:57:07
23   - *
24   - */
25   -public class DirectiveCreator {
26   -
27   - // 城市代码
28   - static final short cityCode = 22;
29   -
30   - static Logger logger = LoggerFactory.getLogger(DirectiveCreator.class);
31   -
32   - /**
33   - *
34   - * @Title: createDirective60
35   - * @Description: TODO(创建60数据包)
36   - * @param @param nbbm 车辆内部编码
37   - * @param @param text 文本
38   - * @param @param dispatchInstruct 指令类型(0X00表示信息短语,0X01表示取消上次指令+调度指令(闹钟有效),0x02表示为调度指令(闹钟有效); 0x03表示运营状态指令(闹钟无效);0x04表示其他指令)
39   - * @param @param upDown 上下行(0 上行 1 下行)
40   - * @param @param state 营运状态(0 营运 1 非营运)
41   - * @param @return 设定文件
42   - * @return Directive60 返回类型
43   - * @throws
44   - */
45   - public D60 createD60(String nbbm, String text, Short dispatchInstruct, int upDown, int state,String lineCode) {
46   - Long timestamp = System.currentTimeMillis();
47   -
48   - Short company = Short.parseShort(BasicData.nbbm2CompanyCodeMap.get(nbbm));
49   - String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
50   -
51   - int msgId = MsgIdGenerator.getMsgId();
52   -
53   - D60 directive = new D60();
54   - D60Data data = new D60Data();
55   - // 一级协议
56   - directive.setOperCode((short) 0x60);
57   - // 设备号
58   - directive.setDeviceId(deviceId);
59   - // 时间戳
60   - directive.setTimestamp(timestamp);
61   - directive.setMsgId(msgId);
62   - // 构造数据
63   - data.setDeviceId(deviceId);
64   - data.setDispatchInstruct(dispatchInstruct);
65   - data.setTimestamp(timestamp);
66   - data.setCompanyCode(company);
67   - data.setMsgId(msgId);
68   - directive.setData(data);
69   - directive.setLineCode(lineCode);
70   - long serviceState;
71   - try {
72   - serviceState = Consts.SERVICE_STATE[upDown][state];
73   - } catch (IndexOutOfBoundsException e) {
74   - // 未知营运状态的直接默认为上行非营运
75   - serviceState = Consts.SERVICE_STATE[0][1];
76   - }
77   - data.setServiceState(serviceState);
78   - data.setTxtContent(text);
79   -
80   - return directive;
81   - }
82   -
83   -
84   - public D60 createD60_02(String nbbm, String text, int upDown, int state, Date alarmTime){
85   - SimpleDateFormat sdfMMddHHmm = new SimpleDateFormat("MMddHHmm");
86   -
87   - Long timestamp = System.currentTimeMillis();
88   -
89   - Short company = Short.parseShort(BasicData.nbbm2CompanyCodeMap.get(nbbm));
90   - String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
91   -
92   - int msgId = MsgIdGenerator.getMsgId();
93   -
94   - D60 directive = new D60();
95   - D60Data data = new D60Data();
96   - // 一级协议
97   - directive.setOperCode((short) 0x60);
98   - // 设备号
99   - directive.setDeviceId(deviceId);
100   - // 时间戳
101   - directive.setTimestamp(timestamp);
102   - directive.setMsgId(msgId);
103   - // 构造数据
104   - data.setDeviceId(deviceId);
105   - data.setDispatchInstruct((short) 0x02);
106   - data.setTimestamp(timestamp);
107   - data.setCompanyCode(company);
108   - data.setMsgId(msgId);
109   - data.setAlarmTime(Long.parseLong(sdfMMddHHmm.format(alarmTime)));
110   - directive.setData(data);
111   - long serviceState;
112   - try {
113   - serviceState = Consts.SERVICE_STATE[upDown][state];
114   - } catch (IndexOutOfBoundsException e) {
115   - // 未知营运状态的直接默认为上行非营运
116   - serviceState = Consts.SERVICE_STATE[0][1];
117   - }
118   - data.setServiceState(serviceState);
119   - data.setTxtContent(text);
120   - return directive;
121   - }
122   -
123   - /**
124   - *
125   - * @Title: createD64
126   - * @Description: TODO(创建线路切换指令 64)
127   - * @param @param nbbm 车辆内部编码
128   - * @param @param lineId 线路编码
129   - * @param @param t 时间戳
130   - * @throws
131   -
132   - public D64 createD64(String nbbm, String lineCode, long t){
133   - String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
134   - return create64(deviceId, lineCode, t);
135   - }*/
136   -
137   - public D64 create64(String deviceId, String lineCode, long t){
138   - D64 change = new D64();
139   - D64Data data = new D64Data();
140   - data.setCityCode(cityCode);
141   - data.setDeviceId(deviceId);
142   -
143   - //线路编码补满6位数
144   - String lineCodeStr = padLeft(lineCode, 6, '0');
145   - data.setLineId(lineCodeStr);
146   -
147   - change.setDeviceId(deviceId);
148   - change.setOperCode((short) 0X64);
149   - change.setTimestamp(t);
150   - change.setData(data);
151   -
152   - return change;
153   - }
154   -
155   - /**
156   - *
157   - * @Title: createDeviceRefreshData
158   - * @Description: TODO(线路刷新指令(用于切换线路后,要求设置重新下载线路文件))
159   - * @param @param deviceId 设备编号
160   - * @param @param lineId 线路ID
161   - * @throws
162   - */
163   - public String createDeviceRefreshData(String deviceId, String lineCode) {
164   - Long t = System.currentTimeMillis();
165   - Map<String, Object> param = new HashMap<>();
166   - param.put("deviceId", deviceId);
167   - param.put("timestamp", t);
168   - param.put("operCode", 0Xc0);
169   -
170   - Map<String, Object> data = new HashMap<>();
171   - data.put("operCode2", 0xa1);
172   - data.put("cityCode", cityCode);
173   - data.put("deviceId", deviceId);
174   - data.put("timestamp", t);
175   - data.put("centerId", 1);
176   -
177   - //线路编码补满6位数
178   - String lineCodeStr = padLeft(lineCode, 6, '0');
179   -
180   - data.put("lineId", lineCodeStr);
181   - data.put("lineVersion", 0);
182   - data.put("carparkDataVersion", 0);
183   - param.put("data", data);
184   -
185   - return JSON.toJSONString(param);
186   - }
187   -
188   - public String padLeft(String oriStr,int len,char alexin){
189   - String str = "";
190   - int strlen = oriStr.length();
191   - if(strlen < len){
192   - for(int i=0;i<len-strlen;i++){
193   - str = str+alexin;
194   - }
195   - }
196   - str = str + oriStr;
197   - return str;
198   - }
199   -}
  1 +package com.bsth.data.directive;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.bsth.data.BasicData;
  5 +import com.bsth.entity.directive.D60;
  6 +import com.bsth.entity.directive.D60.D60Data;
  7 +import com.bsth.entity.directive.D64;
  8 +import com.bsth.entity.directive.D64.D64Data;
  9 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.util.StringUtils;
  13 +
  14 +import java.text.SimpleDateFormat;
  15 +import java.util.Date;
  16 +import java.util.HashMap;
  17 +import java.util.Map;
  18 +
  19 +/**
  20 + *
  21 + * @ClassName: DirectiveCreator
  22 + * @Description: TODO(指令数据生成)
  23 + * @author PanZhao
  24 + * @date 2016年8月14日 下午9:57:07
  25 + *
  26 + */
  27 +public class DirectiveCreator {
  28 +
  29 + // 城市代码
  30 + static final short cityCode = 22;
  31 +
  32 + static Logger logger = LoggerFactory.getLogger(DirectiveCreator.class);
  33 +
  34 + /**
  35 + *
  36 + * @Title: createDirective60
  37 + * @Description: TODO(创建60数据包)
  38 + * @param @param nbbm 车辆内部编码
  39 + * @param @param text 文本
  40 + * @param @param dispatchInstruct 指令类型(0X00表示信息短语,0X01表示取消上次指令+调度指令(闹钟有效),0x02表示为调度指令(闹钟有效); 0x03表示运营状态指令(闹钟无效);0x04表示其他指令)
  41 + * @param @param upDown 上下行(0 上行 1 下行)
  42 + * @param @param state 营运状态(0 营运 1 非营运)
  43 + * @param @return 设定文件
  44 + * @return Directive60 返回类型
  45 + * @throws
  46 + */
  47 + public D60 createD60(String nbbm, String text, Short dispatchInstruct, int upDown, int state,String lineCode) {
  48 + Long timestamp = System.currentTimeMillis();
  49 +
  50 + Short company = Short.parseShort(BasicData.nbbm2CompanyCodeMap.get(nbbm));
  51 + String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
  52 +
  53 + int msgId = MsgIdGenerator.getMsgId();
  54 +
  55 + D60 directive = new D60();
  56 + D60Data data = new D60Data();
  57 + // 一级协议
  58 + directive.setOperCode((short) 0x60);
  59 + // 设备号
  60 + directive.setDeviceId(deviceId);
  61 + // 时间戳
  62 + directive.setTimestamp(timestamp);
  63 + directive.setMsgId(msgId);
  64 + // 构造数据
  65 + data.setDeviceId(deviceId);
  66 + data.setDispatchInstruct(dispatchInstruct);
  67 + data.setTimestamp(timestamp);
  68 + data.setCompanyCode(company);
  69 + data.setMsgId(msgId);
  70 + directive.setData(data);
  71 + directive.setLineCode(lineCode);
  72 + long serviceState;
  73 + try {
  74 + serviceState = Consts.SERVICE_STATE[upDown][state];
  75 + } catch (IndexOutOfBoundsException e) {
  76 + // 未知营运状态的直接默认为上行非营运
  77 + serviceState = Consts.SERVICE_STATE[0][1];
  78 + }
  79 + data.setServiceState(serviceState);
  80 + data.setTxtContent(text);
  81 +
  82 + return directive;
  83 + }
  84 +
  85 +
  86 + public D60 createD60_02(String nbbm, String text, int upDown, int state, Date alarmTime){
  87 + SimpleDateFormat sdfMMddHHmm = new SimpleDateFormat("MMddHHmm");
  88 +
  89 + Long timestamp = System.currentTimeMillis();
  90 +
  91 + Short company = Short.parseShort(BasicData.nbbm2CompanyCodeMap.get(nbbm));
  92 + String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
  93 +
  94 + int msgId = MsgIdGenerator.getMsgId();
  95 +
  96 + D60 directive = new D60();
  97 + D60Data data = new D60Data();
  98 + // 一级协议
  99 + directive.setOperCode((short) 0x60);
  100 + // 设备号
  101 + directive.setDeviceId(deviceId);
  102 + // 时间戳
  103 + directive.setTimestamp(timestamp);
  104 + directive.setMsgId(msgId);
  105 + // 构造数据
  106 + data.setDeviceId(deviceId);
  107 + data.setDispatchInstruct((short) 0x02);
  108 + data.setTimestamp(timestamp);
  109 + data.setCompanyCode(company);
  110 + data.setMsgId(msgId);
  111 + data.setAlarmTime(Long.parseLong(sdfMMddHHmm.format(alarmTime)));
  112 + directive.setData(data);
  113 + long serviceState;
  114 + try {
  115 + serviceState = Consts.SERVICE_STATE[upDown][state];
  116 + } catch (IndexOutOfBoundsException e) {
  117 + // 未知营运状态的直接默认为上行非营运
  118 + serviceState = Consts.SERVICE_STATE[0][1];
  119 + }
  120 + data.setServiceState(serviceState);
  121 + data.setTxtContent(text);
  122 + return directive;
  123 + }
  124 +
  125 + /**
  126 + *
  127 + * @Title: createD64
  128 + * @Description: TODO(创建线路切换指令 64)
  129 + * @param @param nbbm 车辆内部编码
  130 + * @param @param lineId 线路编码
  131 + * @param @param t 时间戳
  132 + * @throws
  133 +
  134 + public D64 createD64(String nbbm, String lineCode, long t){
  135 + String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
  136 + return create64(deviceId, lineCode, t);
  137 + }*/
  138 +
  139 + public D64 create64(String deviceId, String lineCode, ScheduleRealInfo sch, long t){
  140 + D64 change = new D64();
  141 + D64Data data = new D64Data();
  142 + data.setCityCode(cityCode);
  143 + data.setDeviceId(deviceId);
  144 +
  145 + //线路编码补满6位数
  146 + String lineCodeStr = padLeft(lineCode, 6, '0');
  147 + data.setLineId(lineCodeStr);
  148 + int seq = 0;
  149 + if (sch != null) {
  150 + String regionId = sch.getRegionId();
  151 + if (StringUtils.hasText(regionId)) {
  152 + seq = Integer.parseInt(regionId.split("_")[1]);
  153 + }
  154 + }
  155 + data.setSeq(seq);
  156 +
  157 + change.setDeviceId(deviceId);
  158 + change.setOperCode((short) 0X64);
  159 + change.setTimestamp(t);
  160 + change.setData(data);
  161 + change.setSch(sch);
  162 +
  163 + return change;
  164 + }
  165 +
  166 + /**
  167 + *
  168 + * @Title: createDeviceRefreshData
  169 + * @Description: TODO(线路刷新指令(用于切换线路后,要求设置重新下载线路文件))
  170 + * @param @param deviceId 设备编号
  171 + * @param @param lineId 线路ID
  172 + * @throws
  173 + */
  174 + public String createDeviceRefreshData(String deviceId, String lineCode) {
  175 + Long t = System.currentTimeMillis();
  176 + Map<String, Object> param = new HashMap<>();
  177 + param.put("deviceId", deviceId);
  178 + param.put("timestamp", t);
  179 + param.put("operCode", 0Xc0);
  180 +
  181 + Map<String, Object> data = new HashMap<>();
  182 + data.put("operCode2", 0xa1);
  183 + data.put("cityCode", cityCode);
  184 + data.put("deviceId", deviceId);
  185 + data.put("timestamp", t);
  186 + data.put("centerId", 1);
  187 +
  188 + //线路编码补满6位数
  189 + String lineCodeStr = padLeft(lineCode, 6, '0');
  190 +
  191 + data.put("lineId", lineCodeStr);
  192 + data.put("lineVersion", 0);
  193 + data.put("carparkDataVersion", 0);
  194 + param.put("data", data);
  195 +
  196 + return JSON.toJSONString(param);
  197 + }
  198 +
  199 + public String padLeft(String oriStr,int len,char alexin){
  200 + String str = "";
  201 + int strlen = oriStr.length();
  202 + if(strlen < len){
  203 + for(int i=0;i<len-strlen;i++){
  204 + str = str+alexin;
  205 + }
  206 + }
  207 + str = str + oriStr;
  208 + return str;
  209 + }
  210 +}
... ...
src/main/java/com/bsth/data/gpsdata_v2/handlers/GpsStateProcess.java
1   -package com.bsth.data.gpsdata_v2.handlers;
2   -
3   -import com.bsth.data.gpsdata_v2.entity.GpsEntity;
4   -import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
5   -import com.bsth.data.schedule.DayOfSchedule;
6   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
7   -import org.springframework.beans.factory.annotation.Autowired;
8   -import org.springframework.stereotype.Component;
9   -
10   -import java.util.concurrent.ConcurrentHashMap;
11   -import java.util.concurrent.ConcurrentMap;
12   -
13   -/**
14   - * GPS 状态处理
15   - * Created by panzhao on 2017/11/15.
16   - */
17   -@Component
18   -public class GpsStateProcess {
19   -
20   - @Autowired
21   - DayOfSchedule dayOfSchedule;
22   -
23   - @Autowired
24   - GpsStatusManager gpsStatusManager;
25   -
26   - /**
27   - * 设置状态差异连续次数
28   - */
29   - private static ConcurrentMap<String, Integer> stateDiffMap = new ConcurrentHashMap<>();
30   -
31   - private final static int CHANGE_THRESHOLD = 2;
32   -
33   - public void process(GpsEntity gps) {
34   - //在执行的任务
35   - ScheduleRealInfo sch = dayOfSchedule.executeCurr(gps.getNbbm());
36   -
37   - if (null == sch)
38   - return;
39   -
40   - int upDown = Integer.parseInt(sch.getXlDir());
41   - int schState = dayOfSchedule.emptyService(sch)?1:0;
42   - String device = gps.getDeviceId();
43   - /**
44   - * 网关在进终点的时候,会直接将当前点位状态改变
45   - * 为避免出现单个点的状态跳动,设置一下切换阈值
46   - */
47   - if(gps.getState() != schState || gps.getUpDown() != upDown){
48   - Integer count = 0;
49   - if(stateDiffMap.containsKey(device))
50   - count = stateDiffMap.get(device);
51   -
52   - count ++;
53   -
54   - if(count >= CHANGE_THRESHOLD){
55   - //下发指令纠正车载的 营运状态 和 走向
56   - gpsStatusManager.changeServiceState(gps.getNbbm(), upDown, schState, "同步@系统");
57   - count = 0;
58   - }
59   -
60   - stateDiffMap.put(device, count);
61   -
62   - //记录原始设备状态
63   - gps.setOrigStateStr(gps.getUpDown() + "_" + gps.getState());
64   - }
65   - else
66   - stateDiffMap.put(device, 0);
67   -
68   - if (gps.getUpDown() != upDown) {
69   - gps.setUpDown((byte) upDown);//修正走向
70   - }
71   -
72   - if(gps.getState() != schState){
73   - gps.setState(schState);//修正营运状态
74   - }
75   -
76   - if (!sch.getXlBm().equals(gps.getLineId())) {
77   - //切换车载的 线路编码
78   - gpsStatusManager.changeLine(gps.getNbbm(), sch.getXlBm(), "同步@系统");
79   - }
80   - }
81   -}
  1 +package com.bsth.data.gpsdata_v2.handlers;
  2 +
  3 +import com.bsth.data.gpsdata_v2.entity.GpsEntity;
  4 +import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
  5 +import com.bsth.data.schedule.DayOfSchedule;
  6 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.stereotype.Component;
  9 +import org.springframework.util.StringUtils;
  10 +
  11 +import java.util.concurrent.ConcurrentHashMap;
  12 +import java.util.concurrent.ConcurrentMap;
  13 +
  14 +/**
  15 + * GPS 状态处理
  16 + * Created by panzhao on 2017/11/15.
  17 + */
  18 +@Component
  19 +public class GpsStateProcess {
  20 +
  21 + @Autowired
  22 + DayOfSchedule dayOfSchedule;
  23 +
  24 + @Autowired
  25 + GpsStatusManager gpsStatusManager;
  26 +
  27 + /**
  28 + * 设置状态差异连续次数
  29 + */
  30 + private static ConcurrentMap<String, Integer> stateDiffMap = new ConcurrentHashMap<>();
  31 +
  32 + private final static int CHANGE_THRESHOLD = 2;
  33 +
  34 + public void process(GpsEntity gps) {
  35 + if ("W7G-083".equals(gps.getNbbm())) {
  36 + System.out.println("a");
  37 + }
  38 + //在执行的任务
  39 + ScheduleRealInfo sch = dayOfSchedule.executeCurr(gps.getNbbm());
  40 +
  41 + if (null == sch)
  42 + return;
  43 +
  44 + int upDown = Integer.parseInt(sch.getXlDir());
  45 + int schState = dayOfSchedule.emptyService(sch)?1:0;
  46 + String device = gps.getDeviceId();
  47 + /**
  48 + * 网关在进终点的时候,会直接将当前点位状态改变
  49 + * 为避免出现单个点的状态跳动,设置一下切换阈值
  50 + */
  51 + if(gps.getState() != schState || gps.getUpDown() != upDown){
  52 + Integer count = 0;
  53 + if(stateDiffMap.containsKey(device))
  54 + count = stateDiffMap.get(device);
  55 +
  56 + count ++;
  57 +
  58 + if(count >= CHANGE_THRESHOLD){
  59 + //下发指令纠正车载的 营运状态 和 走向
  60 + gpsStatusManager.changeServiceState(gps.getNbbm(), upDown, schState, "同步@系统");
  61 + count = 0;
  62 + }
  63 +
  64 + stateDiffMap.put(device, count);
  65 +
  66 + //记录原始设备状态
  67 + gps.setOrigStateStr(gps.getUpDown() + "_" + gps.getState());
  68 + }
  69 + else
  70 + stateDiffMap.put(device, 0);
  71 +
  72 + if (gps.getUpDown() != upDown) {
  73 + gps.setUpDown((byte) upDown);//修正走向
  74 + }
  75 +
  76 + if(gps.getState() != schState){
  77 + gps.setState(schState);//修正营运状态
  78 + }
  79 +
  80 + // 线路编码不一致或者未接收到切换区间线的响应
  81 + if (!sch.getXlBm().equals(gps.getLineId()) || StringUtils.hasText(sch.getRegionId()) && sch.getRegionSwitchState() == 0) {
  82 + //切换车载的 线路编码
  83 + gpsStatusManager.changeLine(gps.getNbbm(), sch, "同步@系统");
  84 + }
  85 + }
  86 +}
... ...
src/main/java/com/bsth/data/gpsdata_v2/handlers/InStationProcess.java
... ... @@ -183,7 +183,7 @@ public class InStationProcess {
183 183  
184 184 //套跑 -下发线路切换指令
185 185 if (null != next && !next.getXlBm().equals(sch.getXlBm())) {
186   - gpsStatusManager.changeLine(next.getClZbh(), next.getXlBm(), "套跑@系统");
  186 + gpsStatusManager.changeLine(next.getClZbh(), next, "套跑@系统");
187 187 }
188 188  
189 189 /**
... ...
src/main/java/com/bsth/data/gpsdata_v2/rfid/handle/RfidSignalHandle.java
1   -package com.bsth.data.gpsdata_v2.rfid.handle;
2   -
3   -import com.bsth.data.gpsdata_v2.rfid.entity.RfidInoutStation;
4   -import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
5   -import com.bsth.data.msg_queue.DirectivePushQueue;
6   -import com.bsth.data.schedule.DayOfSchedule;
7   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
8   -import com.bsth.websocket.handler.SendUtils;
9   -import org.apache.commons.lang3.StringUtils;
10   -import org.slf4j.Logger;
11   -import org.slf4j.LoggerFactory;
12   -import org.springframework.beans.factory.annotation.Autowired;
13   -import org.springframework.stereotype.Component;
14   -
15   -import java.util.List;
16   -
17   -/**
18   - * RFID信号处理
19   - * Created by panzhao on 2017/11/22.
20   - */
21   -@Component
22   -public class RfidSignalHandle {
23   -
24   - @Autowired
25   - DayOfSchedule dayOfSchedule;
26   -
27   - @Autowired
28   - SendUtils sendUtils;
29   -
30   - @Autowired
31   - GpsStatusManager gpsStatusManager;
32   -
33   - private final static int MAX_TIME_DIFF = 1000 * 60 * 60 * 2;
34   -
35   - Logger logger = LoggerFactory.getLogger(this.getClass());
36   -
37   - public void handle(List<RfidInoutStation> list){
38   - for(RfidInoutStation signal : list){
39   - if(signal.getType()==2)
40   - in(signal);
41   - else if(signal.getType()==4)
42   - out(signal);
43   - }
44   - }
45   -
46   - /**
47   - * 出
48   - * @param signal
49   - */
50   - private void out(RfidInoutStation signal) {
51   - try{
52   - String nbbm = signal.getNbbm();
53   - ScheduleRealInfo sch = dayOfSchedule.executeCurr(nbbm);
54   -
55   - if(null == sch)
56   - return;
57   -
58   - //最大时间差
59   - if(Math.abs(sch.getDfsjT() - signal.getT()) > MAX_TIME_DIFF)
60   - return;
61   -
62   - if(sch.getQdzCode().equals(signal.getStation())
63   - && StringUtils.isEmpty(sch.getFcsjActual())){
64   -
65   - //班次发车
66   - sch.setFcsjActualAll(signal.getT());
67   -
68   - //webSocket
69   - sendUtils.sendFcsj(sch);
70   -
71   - //持久化
72   - dayOfSchedule.save(sch);
73   -
74   - logger.info("RFID; 车辆:" + sch.getClZbh() + " 班次:" + sch.getDfsj() + "发车, 时间:" + sch.getFcsjActual());
75   - }
76   - }catch (Exception e){
77   - logger.error("", e);
78   - }
79   - }
80   -
81   - /**
82   - * 进
83   - * @param signal
84   - */
85   - private void in(RfidInoutStation signal) {
86   - try{
87   - String nbbm = signal.getNbbm();
88   - ScheduleRealInfo sch = dayOfSchedule.executeCurr(nbbm);
89   -
90   - if(null == sch)
91   - return;
92   -
93   - //最大时间差
94   - if(Math.abs(sch.getDfsjT() - signal.getT()) > MAX_TIME_DIFF)
95   - return;
96   -
97   - if(sch.getZdzCode().equals(signal.getStation())
98   - && StringUtils.isEmpty(sch.getZdsjActual())){
99   -
100   - sch.setZdsjActualAll(signal.getT());
101   -
102   - //持久化
103   - dayOfSchedule.save(sch);
104   -
105   - //车辆的下一个班次
106   - ScheduleRealInfo next = dayOfSchedule.next(sch);
107   - if(next != null){
108   - dayOfSchedule.addExecPlan(next);
109   - }
110   -
111   - //路牌的下一个班次,页面显示起点实际到达时间
112   - ScheduleRealInfo lpNext = dayOfSchedule.nextByLp(sch);
113   - if(lpNext != null){
114   - lpNext.setQdzArrDatesj(sch.getZdsjActual());
115   - }
116   -
117   - //已完成班次数
118   - int doneSum = dayOfSchedule.doneSum(nbbm);
119   -
120   - //webSocket
121   - sendUtils.sendZdsj(sch, lpNext, doneSum);
122   -
123   - logger.info("RFID; 车辆:" + nbbm + " 班次:" + sch.getDfsj() + "到终点, 时间:" + sch.getZdsjActual());
124   -
125   - //下发调度指令
126   - DirectivePushQueue.put6002(next, doneSum, "rfid@系统", "");
127   -
128   - //套跑 -下发线路切换指令
129   - if(null != next && !next.getXlBm().equals(sch.getXlBm())){
130   - gpsStatusManager.changeLine(next.getClZbh(), next.getXlBm(), "rfid@系统");
131   - }
132   -
133   - if(null == next)
134   - nonService(sch, "rfid1@系统");//班次结束
135   - else if(null != next && dayOfSchedule.emptyService(next))
136   - nonService(sch, "rfid2@系统");//下一班非营运
137   - }
138   - }catch (Exception e){
139   - logger.error("", e);
140   - }
141   - }
142   -
143   - /**
144   - * 将车载设备切换为非营运状态
145   - * @param sch
146   - * @param sender
147   - */
148   - private void nonService(ScheduleRealInfo sch, String sender){
149   - gpsStatusManager.changeServiceState(sch.getClZbh(), sch.getXlDir(), 1, sender);
150   - }
  1 +package com.bsth.data.gpsdata_v2.rfid.handle;
  2 +
  3 +import com.bsth.data.gpsdata_v2.rfid.entity.RfidInoutStation;
  4 +import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
  5 +import com.bsth.data.msg_queue.DirectivePushQueue;
  6 +import com.bsth.data.schedule.DayOfSchedule;
  7 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  8 +import com.bsth.websocket.handler.SendUtils;
  9 +import org.apache.commons.lang3.StringUtils;
  10 +import org.slf4j.Logger;
  11 +import org.slf4j.LoggerFactory;
  12 +import org.springframework.beans.factory.annotation.Autowired;
  13 +import org.springframework.stereotype.Component;
  14 +
  15 +import java.util.List;
  16 +
  17 +/**
  18 + * RFID信号处理
  19 + * Created by panzhao on 2017/11/22.
  20 + */
  21 +@Component
  22 +public class RfidSignalHandle {
  23 +
  24 + @Autowired
  25 + DayOfSchedule dayOfSchedule;
  26 +
  27 + @Autowired
  28 + SendUtils sendUtils;
  29 +
  30 + @Autowired
  31 + GpsStatusManager gpsStatusManager;
  32 +
  33 + private final static int MAX_TIME_DIFF = 1000 * 60 * 60 * 2;
  34 +
  35 + Logger logger = LoggerFactory.getLogger(this.getClass());
  36 +
  37 + public void handle(List<RfidInoutStation> list){
  38 + for(RfidInoutStation signal : list){
  39 + if(signal.getType()==2)
  40 + in(signal);
  41 + else if(signal.getType()==4)
  42 + out(signal);
  43 + }
  44 + }
  45 +
  46 + /**
  47 + * 出
  48 + * @param signal
  49 + */
  50 + private void out(RfidInoutStation signal) {
  51 + try{
  52 + String nbbm = signal.getNbbm();
  53 + ScheduleRealInfo sch = dayOfSchedule.executeCurr(nbbm);
  54 +
  55 + if(null == sch)
  56 + return;
  57 +
  58 + //最大时间差
  59 + if(Math.abs(sch.getDfsjT() - signal.getT()) > MAX_TIME_DIFF)
  60 + return;
  61 +
  62 + if(sch.getQdzCode().equals(signal.getStation())
  63 + && StringUtils.isEmpty(sch.getFcsjActual())){
  64 +
  65 + //班次发车
  66 + sch.setFcsjActualAll(signal.getT());
  67 +
  68 + //webSocket
  69 + sendUtils.sendFcsj(sch);
  70 +
  71 + //持久化
  72 + dayOfSchedule.save(sch);
  73 +
  74 + logger.info("RFID; 车辆:" + sch.getClZbh() + " 班次:" + sch.getDfsj() + "发车, 时间:" + sch.getFcsjActual());
  75 + }
  76 + }catch (Exception e){
  77 + logger.error("", e);
  78 + }
  79 + }
  80 +
  81 + /**
  82 + * 进
  83 + * @param signal
  84 + */
  85 + private void in(RfidInoutStation signal) {
  86 + try{
  87 + String nbbm = signal.getNbbm();
  88 + ScheduleRealInfo sch = dayOfSchedule.executeCurr(nbbm);
  89 +
  90 + if(null == sch)
  91 + return;
  92 +
  93 + //最大时间差
  94 + if(Math.abs(sch.getDfsjT() - signal.getT()) > MAX_TIME_DIFF)
  95 + return;
  96 +
  97 + if(sch.getZdzCode().equals(signal.getStation())
  98 + && StringUtils.isEmpty(sch.getZdsjActual())){
  99 +
  100 + sch.setZdsjActualAll(signal.getT());
  101 +
  102 + //持久化
  103 + dayOfSchedule.save(sch);
  104 +
  105 + //车辆的下一个班次
  106 + ScheduleRealInfo next = dayOfSchedule.next(sch);
  107 + if(next != null){
  108 + dayOfSchedule.addExecPlan(next);
  109 + }
  110 +
  111 + //路牌的下一个班次,页面显示起点实际到达时间
  112 + ScheduleRealInfo lpNext = dayOfSchedule.nextByLp(sch);
  113 + if(lpNext != null){
  114 + lpNext.setQdzArrDatesj(sch.getZdsjActual());
  115 + }
  116 +
  117 + //已完成班次数
  118 + int doneSum = dayOfSchedule.doneSum(nbbm);
  119 +
  120 + //webSocket
  121 + sendUtils.sendZdsj(sch, lpNext, doneSum);
  122 +
  123 + logger.info("RFID; 车辆:" + nbbm + " 班次:" + sch.getDfsj() + "到终点, 时间:" + sch.getZdsjActual());
  124 +
  125 + //下发调度指令
  126 + DirectivePushQueue.put6002(next, doneSum, "rfid@系统", "");
  127 +
  128 + //套跑 -下发线路切换指令
  129 + if(null != next && !next.getXlBm().equals(sch.getXlBm())){
  130 + gpsStatusManager.changeLine(next.getClZbh(), next, "rfid@系统");
  131 + }
  132 +
  133 + if(null == next)
  134 + nonService(sch, "rfid1@系统");//班次结束
  135 + else if(null != next && dayOfSchedule.emptyService(next))
  136 + nonService(sch, "rfid2@系统");//下一班非营运
  137 + }
  138 + }catch (Exception e){
  139 + logger.error("", e);
  140 + }
  141 + }
  142 +
  143 + /**
  144 + * 将车载设备切换为非营运状态
  145 + * @param sch
  146 + * @param sender
  147 + */
  148 + private void nonService(ScheduleRealInfo sch, String sender){
  149 + gpsStatusManager.changeServiceState(sch.getClZbh(), sch.getXlDir(), 1, sender);
  150 + }
151 151 }
152 152 \ No newline at end of file
... ...
src/main/java/com/bsth/data/gpsdata_v2/status_manager/GpsStatusManager.java
1   -package com.bsth.data.gpsdata_v2.status_manager;
2   -
3   -import com.bsth.Application;
4   -import com.bsth.data.gpsdata_v2.status_manager.gps_line_state.LineStateHandle;
5   -import com.bsth.data.gpsdata_v2.status_manager.gps_service_state.ServiceStateHandle;
6   -import org.springframework.beans.factory.annotation.Autowired;
7   -import org.springframework.boot.CommandLineRunner;
8   -import org.springframework.stereotype.Component;
9   -
10   -import java.util.concurrent.TimeUnit;
11   -
12   -/**
13   - * GPS 状态管理
14   - * Created by panzhao on 2017/7/13.
15   - */
16   -@Component
17   -public class GpsStatusManager implements CommandLineRunner {
18   -
19   - @Autowired
20   - StatusCheckThread checkThread;
21   -
22   - @Autowired
23   - LineStateHandle lineStateHandle;
24   -
25   - @Autowired
26   - ServiceStateHandle serviceStateHandle;
27   -
28   - /**
29   - * 切换线路
30   - * @param nbbm
31   - * @param lineCode
32   - * @param sender
33   - */
34   - public void changeLine(String nbbm, String lineCode, String sender){
35   - lineStateHandle.changeLine(nbbm, lineCode, sender);
36   - }
37   -
38   - /**
39   - * 切换营运状态
40   - * @param nbbm
41   - * @param state 0 营运, 1:非营运
42   - * @param sender
43   - */
44   - public void changeServiceState(String nbbm, String upDown,int state, String sender){
45   - changeServiceState(nbbm, Integer.parseInt(upDown), state, sender);
46   - }
47   -
48   - /**
49   - * 切换营运状态
50   - * @param nbbm
51   - * @param state 0 营运, 1:非营运
52   - * @param sender
53   - */
54   - public void changeServiceState(String nbbm, int upDown,int state, String sender){
55   - serviceStateHandle.changeState(nbbm, upDown, state, sender);
56   - }
57   -
58   - @Override
59   - public void run(String... strings) throws Exception {
60   - Application.mainServices.scheduleWithFixedDelay(checkThread, 120, 120, TimeUnit.SECONDS);
61   - }
62   -
63   - @Component
64   - public static class StatusCheckThread extends Thread{
65   -
66   - @Autowired
67   - LineStateHandle lineStateHandle;
68   -
69   - @Autowired
70   - ServiceStateHandle serviceStateHandle;
71   -
72   - @Override
73   - public void run() {
74   - /** 检查线路切换结果 */
75   - lineStateHandle.checkResultAll();
76   - /** 检查营运状态切换结果 */
77   - serviceStateHandle.checkResultAll();
78   - }
79   - }
80   -}
  1 +package com.bsth.data.gpsdata_v2.status_manager;
  2 +
  3 +import com.bsth.Application;
  4 +import com.bsth.data.gpsdata_v2.status_manager.gps_line_state.LineStateHandle;
  5 +import com.bsth.data.gpsdata_v2.status_manager.gps_service_state.ServiceStateHandle;
  6 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  7 +import org.springframework.beans.factory.annotation.Autowired;
  8 +import org.springframework.boot.CommandLineRunner;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.util.concurrent.TimeUnit;
  12 +
  13 +/**
  14 + * GPS 状态管理
  15 + * Created by panzhao on 2017/7/13.
  16 + */
  17 +@Component
  18 +public class GpsStatusManager implements CommandLineRunner {
  19 +
  20 + @Autowired
  21 + StatusCheckThread checkThread;
  22 +
  23 + @Autowired
  24 + LineStateHandle lineStateHandle;
  25 +
  26 + @Autowired
  27 + ServiceStateHandle serviceStateHandle;
  28 +
  29 + /**
  30 + * 切换线路
  31 + * @param nbbm
  32 + * @param lineCode
  33 + * @param sender
  34 + */
  35 + public void changeLine(String nbbm, String lineCode, String sender){
  36 + lineStateHandle.changeLine(nbbm, lineCode, sender);
  37 + }
  38 +
  39 + public void changeLine(String nbbm, ScheduleRealInfo sch, String sender){
  40 + lineStateHandle.changeLine(nbbm, sch, sender);
  41 + }
  42 +
  43 + /**
  44 + * 切换营运状态
  45 + * @param nbbm
  46 + * @param state 0 营运, 1:非营运
  47 + * @param sender
  48 + */
  49 + public void changeServiceState(String nbbm, String upDown,int state, String sender){
  50 + changeServiceState(nbbm, Integer.parseInt(upDown), state, sender);
  51 + }
  52 +
  53 + /**
  54 + * 切换营运状态
  55 + * @param nbbm
  56 + * @param state 0 营运, 1:非营运
  57 + * @param sender
  58 + */
  59 + public void changeServiceState(String nbbm, int upDown,int state, String sender){
  60 + serviceStateHandle.changeState(nbbm, upDown, state, sender);
  61 + }
  62 +
  63 + @Override
  64 + public void run(String... strings) throws Exception {
  65 + Application.mainServices.scheduleWithFixedDelay(checkThread, 120, 120, TimeUnit.SECONDS);
  66 + }
  67 +
  68 + @Component
  69 + public static class StatusCheckThread extends Thread{
  70 +
  71 + @Autowired
  72 + LineStateHandle lineStateHandle;
  73 +
  74 + @Autowired
  75 + ServiceStateHandle serviceStateHandle;
  76 +
  77 + @Override
  78 + public void run() {
  79 + /** 检查线路切换结果 */
  80 + lineStateHandle.checkResultAll();
  81 + /** 检查营运状态切换结果 */
  82 + serviceStateHandle.checkResultAll();
  83 + }
  84 + }
  85 +}
... ...
src/main/java/com/bsth/data/gpsdata_v2/status_manager/gps_line_state/ChangeBean.java
1   -package com.bsth.data.gpsdata_v2.status_manager.gps_line_state;
2   -
3   -/**
4   - * Created by panzhao on 2017/7/13.
5   - */
6   -public class ChangeBean {
7   -
8   - /**
9   - * 车辆自编号
10   - */
11   - private String nbbm;
12   -
13   - /**
14   - * 要切换到的线路
15   - */
16   - private String lineCode;
17   -
18   - /**
19   - * 指令发送次数
20   - */
21   - private int sendCount;
22   -
23   - /**
24   - * 上次指令时间
25   - */
26   - private long st;
27   -
28   - /**发送人 */
29   - private String sender;
30   -
31   - public static ChangeBean getInstance(String nbbm, String lineCode, String sender){
32   - ChangeBean cb = new ChangeBean();
33   - cb.setNbbm(nbbm);
34   - cb.setLineCode(lineCode);
35   - cb.setSendCount(0);
36   - cb.setSender(sender);
37   - return cb;
38   - }
39   -
40   - public void countPlus(){
41   - sendCount ++;
42   - }
43   -
44   - public String getLineCode() {
45   - return lineCode;
46   - }
47   -
48   - public void setLineCode(String lineCode) {
49   - this.lineCode = lineCode;
50   - }
51   -
52   - public int getSendCount() {
53   - return sendCount;
54   - }
55   -
56   - public void setSendCount(int sendCount) {
57   - this.sendCount = sendCount;
58   - }
59   -
60   - public long getSt() {
61   - return st;
62   - }
63   -
64   - public void setSt(long st) {
65   - this.st = st;
66   - }
67   -
68   - public String getSender() {
69   - return sender;
70   - }
71   -
72   - public void setSender(String sender) {
73   - this.sender = sender;
74   - }
75   -
76   - public String getNbbm() {
77   - return nbbm;
78   - }
79   -
80   - public void setNbbm(String nbbm) {
81   - this.nbbm = nbbm;
82   - }
83   -}
  1 +package com.bsth.data.gpsdata_v2.status_manager.gps_line_state;
  2 +
  3 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  4 +
  5 +/**
  6 + * Created by panzhao on 2017/7/13.
  7 + */
  8 +public class ChangeBean {
  9 +
  10 + /**
  11 + * 车辆自编号
  12 + */
  13 + private String nbbm;
  14 +
  15 + /**
  16 + * 要切换到的线路
  17 + */
  18 + private String lineCode;
  19 +
  20 + /**
  21 + * 班次
  22 + */
  23 + private ScheduleRealInfo sch;
  24 +
  25 + /**
  26 + * 指令发送次数
  27 + */
  28 + private int sendCount;
  29 +
  30 + /**
  31 + * 上次指令时间
  32 + */
  33 + private long st;
  34 +
  35 + /**发送人 */
  36 + private String sender;
  37 +
  38 + public static ChangeBean getInstance(String nbbm, String lineCode, String sender){
  39 + ChangeBean cb = new ChangeBean();
  40 + cb.setNbbm(nbbm);
  41 + cb.setLineCode(lineCode);
  42 + cb.setSendCount(0);
  43 + cb.setSender(sender);
  44 + return cb;
  45 + }
  46 +
  47 + public void countPlus(){
  48 + sendCount ++;
  49 + }
  50 +
  51 + public String getLineCode() {
  52 + return lineCode;
  53 + }
  54 +
  55 + public void setLineCode(String lineCode) {
  56 + this.lineCode = lineCode;
  57 + }
  58 +
  59 + public ScheduleRealInfo getSch() {
  60 + return sch;
  61 + }
  62 +
  63 + public void setSch(ScheduleRealInfo sch) {
  64 + this.sch = sch;
  65 + }
  66 +
  67 + public int getSendCount() {
  68 + return sendCount;
  69 + }
  70 +
  71 + public void setSendCount(int sendCount) {
  72 + this.sendCount = sendCount;
  73 + }
  74 +
  75 + public long getSt() {
  76 + return st;
  77 + }
  78 +
  79 + public void setSt(long st) {
  80 + this.st = st;
  81 + }
  82 +
  83 + public String getSender() {
  84 + return sender;
  85 + }
  86 +
  87 + public void setSender(String sender) {
  88 + this.sender = sender;
  89 + }
  90 +
  91 + public String getNbbm() {
  92 + return nbbm;
  93 + }
  94 +
  95 + public void setNbbm(String nbbm) {
  96 + this.nbbm = nbbm;
  97 + }
  98 +}
... ...
src/main/java/com/bsth/data/gpsdata_v2/status_manager/gps_line_state/LineStateHandle.java
1   -package com.bsth.data.gpsdata_v2.status_manager.gps_line_state;
2   -
3   -import com.bsth.data.gpsdata_v2.GpsRealData;
4   -import com.bsth.data.gpsdata_v2.entity.GpsEntity;
5   -import com.bsth.data.msg_queue.DirectivePushQueue;
6   -import com.bsth.service.directive.DirectiveService;
7   -import org.slf4j.Logger;
8   -import org.slf4j.LoggerFactory;
9   -import org.springframework.beans.factory.annotation.Autowired;
10   -import org.springframework.stereotype.Component;
11   -
12   -import java.util.Collection;
13   -import java.util.concurrent.ConcurrentHashMap;
14   -
15   -/**
16   - * 设备线路状态处理
17   - * Created by panzhao on 2017/7/13.
18   - */
19   -@Component
20   -public class LineStateHandle {
21   -
22   - private static ConcurrentHashMap<String, ChangeBean> map;
23   -
24   - @Autowired
25   - DirectiveService directiveService;
26   - @Autowired
27   - GpsRealData gpsRealData;
28   -
29   - Logger logger = LoggerFactory.getLogger(this.getClass());
30   -
31   - /** 重发次数 */
32   - private final static int MAX_SEND_COUNT=3;
33   - /** 重发间隔 */
34   - private final static int SEND_SPACE=1000 * 60 * 6;
35   - /** 最大有效时间 */
36   - private final static int MAX_AVAIL_TIME=1000 * 60 * 60 * 2;
37   -
38   - static{
39   - map = new ConcurrentHashMap();
40   - }
41   -
42   - public void changeLine(String nbbm, String lineCode, String sender){
43   - ChangeBean cb = map.get(nbbm);
44   - if(cb != null && cb.getLineCode().equals(lineCode)){
45   - return;
46   - }
47   -
48   - cb = ChangeBean.getInstance(nbbm, lineCode, sender);
49   - map.put(nbbm, cb);
50   -
51   - changeLine(cb);
52   - }
53   -
54   - private void changeLine(ChangeBean cb){
55   - cb.setSt(System.currentTimeMillis());
56   - cb.countPlus();
57   - DirectivePushQueue.put64(cb.getNbbm(), cb.getLineCode(), cb.getSender());
58   - }
59   -
60   -
61   - public void checkResultAll(){
62   - Collection<ChangeBean> cbs = map.values();
63   - for(ChangeBean cb : cbs){
64   - checkResult(cb);
65   - }
66   - }
67   -
68   - private void checkResult(ChangeBean cb){
69   - try{
70   - GpsEntity gps = gpsRealData.getByNbbm(cb.getNbbm());
71   - if(gps == null)
72   - return;
73   -
74   - if(cb.getLineCode().equals(gps.getLineId())){
75   - map.remove(cb.getNbbm());
76   - logger.info("线路切换成功," + cb.getNbbm() + "、" + cb.getLineCode());
77   - }
78   - else{
79   - reSend(cb);
80   - }
81   - }catch (Exception e){
82   - logger.error("", e);
83   - }
84   - }
85   -
86   - private void reSend(ChangeBean cb){
87   - if(cb.getSendCount() >= MAX_SEND_COUNT){
88   - map.remove(cb.getNbbm());
89   - logger.info("超过重发次数," + cb.getNbbm() + "、" + cb.getLineCode());
90   - return;
91   - }
92   -
93   - long diff = System.currentTimeMillis() - cb.getSt();
94   - if(diff >= MAX_AVAIL_TIME){
95   - map.remove(cb.getNbbm());
96   - logger.info("超过有效时间," + cb.getNbbm() + "、" + cb.getLineCode() + "、" + cb.getSt());
97   - return;
98   - }
99   -
100   - if(diff >= SEND_SPACE){
101   - cb.setSender("补发@系统");
102   - changeLine(cb);
103   - logger.info("重发线路切换指令," + cb.getNbbm() + "、" + cb.getLineCode());
104   - return;
105   - }
106   - }
107   -}
  1 +package com.bsth.data.gpsdata_v2.status_manager.gps_line_state;
  2 +
  3 +import com.bsth.data.gpsdata_v2.GpsRealData;
  4 +import com.bsth.data.gpsdata_v2.entity.GpsEntity;
  5 +import com.bsth.data.msg_queue.DirectivePushQueue;
  6 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  7 +import com.bsth.service.directive.DirectiveService;
  8 +import org.slf4j.Logger;
  9 +import org.slf4j.LoggerFactory;
  10 +import org.springframework.beans.factory.annotation.Autowired;
  11 +import org.springframework.stereotype.Component;
  12 +import org.springframework.util.StringUtils;
  13 +
  14 +import java.util.Collection;
  15 +import java.util.concurrent.ConcurrentHashMap;
  16 +
  17 +/**
  18 + * 设备线路状态处理
  19 + * Created by panzhao on 2017/7/13.
  20 + */
  21 +@Component
  22 +public class LineStateHandle {
  23 +
  24 + private static ConcurrentHashMap<String, ChangeBean> map;
  25 +
  26 + @Autowired
  27 + DirectiveService directiveService;
  28 + @Autowired
  29 + GpsRealData gpsRealData;
  30 +
  31 + Logger logger = LoggerFactory.getLogger(this.getClass());
  32 +
  33 + /** 重发次数 */
  34 + private final static int MAX_SEND_COUNT=3;
  35 + /** 重发间隔 */
  36 + private final static int SEND_SPACE=1000 * 60 * 3;
  37 + /** 最大有效时间 */
  38 + private final static int MAX_AVAIL_TIME=1000 * 60 * 60 * 2;
  39 +
  40 + static{
  41 + map = new ConcurrentHashMap();
  42 + }
  43 +
  44 + public void changeLine(String nbbm, String lineCode, String sender){
  45 + ChangeBean cb = map.get(nbbm);
  46 + if(cb != null && cb.getLineCode().equals(lineCode)){
  47 + return;
  48 + }
  49 +
  50 + cb = ChangeBean.getInstance(nbbm, lineCode, sender);
  51 + map.put(nbbm, cb);
  52 +
  53 + changeLine(cb);
  54 + }
  55 +
  56 + public void changeLine(String nbbm, ScheduleRealInfo sch, String sender){
  57 + ChangeBean cb = map.get(nbbm);
  58 + if(cb != null && cb.getLineCode().equals(sch.getXlBm())){
  59 + return;
  60 + }
  61 +
  62 + cb = ChangeBean.getInstance(nbbm, sch.getXlBm(), sender);
  63 + cb.setSch(sch);
  64 + map.put(nbbm, cb);
  65 +
  66 + changeLine(cb);
  67 + }
  68 +
  69 + private void changeLine(ChangeBean cb){
  70 + cb.setSt(System.currentTimeMillis());
  71 + cb.countPlus();
  72 + DirectivePushQueue.put64(cb.getNbbm(), cb.getLineCode(), cb.getSch(), cb.getSender());
  73 + }
  74 +
  75 +
  76 + public void checkResultAll(){
  77 + Collection<ChangeBean> cbs = map.values();
  78 + for(ChangeBean cb : cbs){
  79 + checkResult(cb);
  80 + }
  81 + }
  82 +
  83 + private void checkResult(ChangeBean cb){
  84 + try{
  85 + GpsEntity gps = gpsRealData.getByNbbm(cb.getNbbm());
  86 + if(gps == null)
  87 + return;
  88 +
  89 + if(cb.getLineCode().equals(gps.getLineId())){
  90 + map.remove(cb.getNbbm());
  91 + logger.info("线路切换成功," + cb.getNbbm() + "、" + cb.getLineCode());
  92 + }
  93 + else{
  94 + reSend(cb);
  95 + }
  96 + }catch (Exception e){
  97 + logger.error("", e);
  98 + }
  99 + }
  100 +
  101 + private void reSend(ChangeBean cb){
  102 + if(cb.getSendCount() >= MAX_SEND_COUNT){
  103 + map.remove(cb.getNbbm());
  104 + logger.info("超过重发次数," + cb.getNbbm() + "、" + cb.getLineCode());
  105 + return;
  106 + }
  107 +
  108 + long diff = System.currentTimeMillis() - cb.getSt();
  109 + if(diff >= MAX_AVAIL_TIME){
  110 + map.remove(cb.getNbbm());
  111 + logger.info("超过有效时间," + cb.getNbbm() + "、" + cb.getLineCode() + "、" + cb.getSt());
  112 + return;
  113 + }
  114 +
  115 + if(diff >= SEND_SPACE){
  116 + cb.setSender("补发@系统");
  117 + changeLine(cb);
  118 + logger.info("重发线路切换指令," + cb.getNbbm() + "、" + cb.getLineCode());
  119 + return;
  120 + }
  121 + }
  122 +}
... ...
src/main/java/com/bsth/data/msg_queue/DirectivePushQueue.java
1   -package com.bsth.data.msg_queue;
2   -
3   -import com.bsth.data.schedule.DayOfSchedule;
4   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
5   -import com.bsth.service.directive.DirectiveService;
6   -import org.slf4j.Logger;
7   -import org.slf4j.LoggerFactory;
8   -import org.springframework.beans.BeansException;
9   -import org.springframework.context.ApplicationContext;
10   -import org.springframework.context.ApplicationContextAware;
11   -import org.springframework.stereotype.Component;
12   -
13   -import java.util.concurrent.ConcurrentHashMap;
14   -import java.util.concurrent.ConcurrentLinkedQueue;
15   -import java.util.concurrent.ConcurrentMap;
16   -
17   -/**
18   - * 到网关的指令推送队列 (系统发送的队列, 用户手动发送的不走这里)
19   - * Created by panzhao on 2017/5/11.
20   - */
21   -@Component
22   -public class DirectivePushQueue implements ApplicationContextAware {
23   -
24   - static ConcurrentLinkedQueue<QueueData_Directive> linkedList;
25   - static DataPushThread thread;
26   - static DirectiveService directiveService;
27   - static long threadT;
28   -
29   - /**
30   - * 下发运营指令6003的最小间隔时间
31   - */
32   - static final int MIN_SEND6003_SPACE = 1000 * 30;
33   -
34   - /**
35   - * 车辆 ——> 上次下发6003的时间
36   - */
37   - static ConcurrentMap<String, Long> lastSend60TimeMap;
38   -
39   - static {
40   - linkedList = new ConcurrentLinkedQueue<>();
41   - lastSend60TimeMap = new ConcurrentHashMap<>();
42   - }
43   -
44   - public static void put6002(ScheduleRealInfo sch, int finish, String sender, String txtPrefix){
45   - if(null == sch)
46   - return;
47   - QueueData_Directive qd6002 = new QueueData_Directive();
48   - qd6002.setSch(sch);
49   - qd6002.setFinish(finish);
50   - qd6002.setSender(sender);
51   - qd6002.setCode("60_02");
52   - qd6002.setTxtPrefix(txtPrefix);
53   -
54   - linkedList.add(qd6002);
55   - lastSend60TimeMap.put(sch.getClZbh(), System.currentTimeMillis());
56   - }
57   - //单独出厂请求设计
58   - public static void put60_002(ScheduleRealInfo sch, int finish, String sender, String txtPrefix){
59   - if(null == sch)
60   - return;
61   - QueueData_Directive qd6002 = new QueueData_Directive();
62   - qd6002.setSch(sch);
63   - qd6002.setFinish(finish);
64   - qd6002.setSender(sender);
65   - qd6002.setCode("60_002");
66   - qd6002.setTxtPrefix(txtPrefix);
67   -
68   - linkedList.add(qd6002);
69   - lastSend60TimeMap.put(sch.getClZbh(), System.currentTimeMillis());
70   - }
71   -
72   -
73   -
74   -
75   - public static void put6003(String nbbm, int state, int upDown, String sender){
76   - long t = System.currentTimeMillis();
77   - if(lastSend60TimeMap.containsKey(nbbm)
78   - && t - lastSend60TimeMap.get(nbbm) < MIN_SEND6003_SPACE)
79   - return; //最短下发间隔
80   -
81   - QueueData_Directive qd6003 = new QueueData_Directive();
82   - qd6003.setNbbm(nbbm);
83   - qd6003.setState(state);
84   - qd6003.setUpDown(upDown);
85   - qd6003.setSender(sender);
86   - qd6003.setCode("60_03");
87   -
88   - linkedList.add(qd6003);
89   - lastSend60TimeMap.put(nbbm, t);
90   - }
91   -
92   - public static void put6003(ScheduleRealInfo sch, String sender){
93   - if(null == sch)
94   - return;
95   - int state = 0;//营运状态
96   - if(DayOfSchedule.emptyService(sch))
97   - state = 1;
98   -
99   - put6003(sch.getClZbh(), state, Integer.parseInt(sch.getXlDir()), sender);
100   - }
101   -
102   - public static void put64(String nbbm, String lineCode, String sender){
103   - QueueData_Directive qd64 = new QueueData_Directive();
104   - qd64.setNbbm(nbbm);
105   - qd64.setLineCode(lineCode);
106   - qd64.setSender(sender);
107   -
108   - qd64.setCode("64");
109   -
110   - linkedList.add(qd64);
111   - }
112   -
113   - public static void start(){
114   - if(thread != null){
115   - thread.interrupt();
116   - }
117   - linkedList.clear();
118   - thread = new DataPushThread();
119   - thread.start();
120   - }
121   -
122   - public static int size(){
123   - return linkedList.size();
124   - }
125   -
126   - @Override
127   - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
128   - directiveService = applicationContext.getBean(DirectiveService.class);
129   - }
130   -
131   - public static class DataPushThread extends Thread {
132   -
133   - Logger log = LoggerFactory.getLogger(this.getClass());
134   -
135   - @Override
136   - public void run() {
137   - boolean sleepFlag = false;
138   - QueueData_Directive qd;
139   - String code;
140   - while (true) {
141   - try {
142   - qd = linkedList.poll();
143   - if (qd != null) {
144   - sleepFlag = false;
145   - code = qd.getCode();
146   -
147   - if(code.equals("60_02")){
148   - directiveService.send60Dispatch(qd.getSch(), qd.getFinish(), qd.getSender(), qd.getTxtPrefix());
149   - log.info("directive 60_02 sch id: " + qd.getSch().getId());
150   - }
151   - else if(code.equals("60_002")){ //出场请求只写出场
152   - directiveService.send60Dispatchcc(qd.getSch(), qd.getFinish(), qd.getSender(), qd.getTxtPrefix());
153   - log.info("directive出场 60_002 nbbm: " + qd.getNbbm() + " lineCode: " + qd.getLineCode());
154   - }
155   - else if(code.equals("60_03")){
156   - directiveService.send60Operation(qd.getNbbm(), qd.getState(), qd.getUpDown(), qd.getSender());
157   - log.info("directive 60_03 nbbm: " + qd.getNbbm());
158   - }
159   - else if(code.equals("64")){
160   - directiveService.lineChange(qd.getNbbm(), qd.getLineCode(), qd.getSender());
161   - log.info("directive 64 nbbm: " + qd.getNbbm() + " lineCode: " + qd.getLineCode());
162   - }
163   -
164   -
165   - } else{
166   - Thread.sleep(500);
167   - if(!sleepFlag){
168   - log.info("sleep...");
169   - sleepFlag = true;
170   - }
171   - }
172   - threadT = System.currentTimeMillis();
173   - }
174   - catch(InterruptedException e){
175   - log.error("", e);
176   - break;
177   - }
178   - catch (Exception e) {
179   - log.error("", e);
180   - }
181   - }
182   -
183   - log.warn("DirectivePushQueue is break...");
184   - }
185   - }
186   -}
  1 +package com.bsth.data.msg_queue;
  2 +
  3 +import com.bsth.data.schedule.DayOfSchedule;
  4 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  5 +import com.bsth.service.directive.DirectiveService;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.BeansException;
  9 +import org.springframework.context.ApplicationContext;
  10 +import org.springframework.context.ApplicationContextAware;
  11 +import org.springframework.stereotype.Component;
  12 +import org.springframework.util.StringUtils;
  13 +
  14 +import java.util.concurrent.ConcurrentHashMap;
  15 +import java.util.concurrent.ConcurrentLinkedQueue;
  16 +import java.util.concurrent.ConcurrentMap;
  17 +
  18 +/**
  19 + * 到网关的指令推送队列 (系统发送的队列, 用户手动发送的不走这里)
  20 + * Created by panzhao on 2017/5/11.
  21 + */
  22 +@Component
  23 +public class DirectivePushQueue implements ApplicationContextAware {
  24 +
  25 + static ConcurrentLinkedQueue<QueueData_Directive> linkedList;
  26 + static DataPushThread thread;
  27 + static DirectiveService directiveService;
  28 + static long threadT;
  29 +
  30 + /**
  31 + * 下发运营指令6003的最小间隔时间
  32 + */
  33 + static final int MIN_SEND6003_SPACE = 1000 * 30;
  34 +
  35 + /**
  36 + * 车辆 ——> 上次下发6003的时间
  37 + */
  38 + static ConcurrentMap<String, Long> lastSend60TimeMap;
  39 +
  40 + static {
  41 + linkedList = new ConcurrentLinkedQueue<>();
  42 + lastSend60TimeMap = new ConcurrentHashMap<>();
  43 + }
  44 +
  45 + public static void put6002(ScheduleRealInfo sch, int finish, String sender, String txtPrefix){
  46 + if(null == sch)
  47 + return;
  48 + QueueData_Directive qd6002 = new QueueData_Directive();
  49 + qd6002.setSch(sch);
  50 + qd6002.setFinish(finish);
  51 + qd6002.setSender(sender);
  52 + qd6002.setCode("60_02");
  53 + qd6002.setTxtPrefix(txtPrefix);
  54 +
  55 + linkedList.add(qd6002);
  56 + lastSend60TimeMap.put(sch.getClZbh(), System.currentTimeMillis());
  57 + }
  58 + //单独出厂请求设计
  59 + public static void put60_002(ScheduleRealInfo sch, int finish, String sender, String txtPrefix){
  60 + if(null == sch)
  61 + return;
  62 + QueueData_Directive qd6002 = new QueueData_Directive();
  63 + qd6002.setSch(sch);
  64 + qd6002.setFinish(finish);
  65 + qd6002.setSender(sender);
  66 + qd6002.setCode("60_002");
  67 + qd6002.setTxtPrefix(txtPrefix);
  68 +
  69 + linkedList.add(qd6002);
  70 + lastSend60TimeMap.put(sch.getClZbh(), System.currentTimeMillis());
  71 + }
  72 +
  73 +
  74 +
  75 +
  76 + public static void put6003(String nbbm, int state, int upDown, String sender){
  77 + long t = System.currentTimeMillis();
  78 + if(lastSend60TimeMap.containsKey(nbbm)
  79 + && t - lastSend60TimeMap.get(nbbm) < MIN_SEND6003_SPACE)
  80 + return; //最短下发间隔
  81 +
  82 + QueueData_Directive qd6003 = new QueueData_Directive();
  83 + qd6003.setNbbm(nbbm);
  84 + qd6003.setState(state);
  85 + qd6003.setUpDown(upDown);
  86 + qd6003.setSender(sender);
  87 + qd6003.setCode("60_03");
  88 +
  89 + linkedList.add(qd6003);
  90 + lastSend60TimeMap.put(nbbm, t);
  91 + }
  92 +
  93 + public static void put6003(ScheduleRealInfo sch, String sender){
  94 + if(null == sch)
  95 + return;
  96 + int state = 0;//营运状态
  97 + if(DayOfSchedule.emptyService(sch))
  98 + state = 1;
  99 +
  100 + put6003(sch.getClZbh(), state, Integer.parseInt(sch.getXlDir()), sender);
  101 + }
  102 +
  103 + public static void put64(String nbbm, String lineCode, ScheduleRealInfo sch, String sender){
  104 + QueueData_Directive qd64 = new QueueData_Directive();
  105 + qd64.setNbbm(nbbm);
  106 + qd64.setLineCode(lineCode);
  107 + qd64.setSch(sch);
  108 + qd64.setSender(sender);
  109 +
  110 + qd64.setCode("64");
  111 +
  112 + linkedList.add(qd64);
  113 + }
  114 +
  115 + public static void start(){
  116 + if(thread != null){
  117 + thread.interrupt();
  118 + }
  119 + linkedList.clear();
  120 + thread = new DataPushThread();
  121 + thread.start();
  122 + }
  123 +
  124 + public static int size(){
  125 + return linkedList.size();
  126 + }
  127 +
  128 + @Override
  129 + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  130 + directiveService = applicationContext.getBean(DirectiveService.class);
  131 + }
  132 +
  133 + public static class DataPushThread extends Thread {
  134 +
  135 + Logger log = LoggerFactory.getLogger(this.getClass());
  136 +
  137 + @Override
  138 + public void run() {
  139 + boolean sleepFlag = false;
  140 + QueueData_Directive qd;
  141 + String code;
  142 + while (true) {
  143 + try {
  144 + qd = linkedList.poll();
  145 + if (qd != null) {
  146 + sleepFlag = false;
  147 + code = qd.getCode();
  148 +
  149 + if(code.equals("60_02")){
  150 + directiveService.send60Dispatch(qd.getSch(), qd.getFinish(), qd.getSender(), qd.getTxtPrefix());
  151 + log.info("directive 60_02 sch id: " + qd.getSch().getId());
  152 + }
  153 + else if(code.equals("60_002")){ //出场请求只写出场
  154 + directiveService.send60Dispatchcc(qd.getSch(), qd.getFinish(), qd.getSender(), qd.getTxtPrefix());
  155 + log.info("directive出场 60_002 nbbm: " + qd.getNbbm() + " lineCode: " + qd.getLineCode());
  156 + }
  157 + else if(code.equals("60_03")){
  158 + directiveService.send60Operation(qd.getNbbm(), qd.getState(), qd.getUpDown(), qd.getSender());
  159 + log.info("directive 60_03 nbbm: " + qd.getNbbm());
  160 + }
  161 + else if(code.equals("64")){
  162 + directiveService.lineChange(qd.getNbbm(), qd.getLineCode(), qd.getSch(), qd.getSender());
  163 + log.info("directive 64 nbbm: " + qd.getNbbm() + " lineCode: " + qd.getLineCode());
  164 + }
  165 +
  166 +
  167 + } else{
  168 + Thread.sleep(500);
  169 + if(!sleepFlag){
  170 + log.info("sleep...");
  171 + sleepFlag = true;
  172 + }
  173 + }
  174 + threadT = System.currentTimeMillis();
  175 + }
  176 + catch(InterruptedException e){
  177 + log.error("", e);
  178 + break;
  179 + }
  180 + catch (Exception e) {
  181 + log.error("", e);
  182 + }
  183 + }
  184 +
  185 + log.warn("DirectivePushQueue is break...");
  186 + }
  187 + }
  188 +}
... ...
src/main/java/com/bsth/data/schedule/auto_exec/RealScheduleAutoExecHandler.java
1   -package com.bsth.data.schedule.auto_exec;
2   -
3   -import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
4   -import com.bsth.data.schedule.DayOfSchedule;
5   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
6   -import com.bsth.websocket.handler.SendUtils;
7   -import org.apache.commons.lang3.StringUtils;
8   -import org.springframework.beans.factory.annotation.Autowired;
9   -import org.springframework.stereotype.Component;
10   -
11   -import java.util.ArrayList;
12   -import java.util.List;
13   -
14   -/**
15   - * 实际班次自动执行
16   - * Created by panzhao on 2017/10/31.
17   - */
18   -@Component
19   -public class RealScheduleAutoExecHandler {
20   -
21   - @Autowired
22   - SendUtils sendUtils;
23   - @Autowired
24   - DayOfSchedule dayOfSchedule;
25   -
26   - @Autowired
27   - GpsStatusManager gpsStatusManager;
28   -
29   - public void exec(ScheduleRealInfo sch) {
30   - boolean flag = false;
31   - long t = System.currentTimeMillis();
32   -
33   - if (StringUtils.isEmpty(sch.getFcsjActual()) && sch.getDfsjT() < t) {
34   - sch.setFcsjActualAll(sch.getDfsjT());
35   - flag = true;
36   - }
37   -
38   - if (StringUtils.isEmpty(sch.getZdsjActual()) && sch.getZdsjT() < t) {
39   - sch.setZdsjActualAll(sch.getZdsjT());
40   - flag = true;
41   -
42   - //准备执行下一个班次
43   - ScheduleRealInfo next = dayOfSchedule.next(sch);
44   - if (next != null) {
45   - dayOfSchedule.addExecPlan(next);
46   - //套跑 -下发线路切换指令
47   - if(!next.getXlBm().equals(sch.getXlBm())){
48   - gpsStatusManager.changeLine(next.getClZbh(), next.getXlBm(), "auto@系统");
49   - }
50   - }
51   -
52   - //路牌有下一个班次,线调页面显示
53   - ScheduleRealInfo nextLp = dayOfSchedule.nextByLp(sch);
54   - if (null != nextLp) {
55   - //入库
56   - dayOfSchedule.save(sch);
57   -
58   - nextLp.setQdzArrDatesj(sch.getZdsjActual());
59   - List<ScheduleRealInfo> refs = new ArrayList<>();
60   - refs.add(sch);
61   - refs.add(nextLp);
62   - sendUtils.refreshSch(refs);
63   - return;
64   - }
65   - }
66   -
67   - if (flag) {
68   - dayOfSchedule.save(sch);
69   - sendUtils.refreshSch(sch);
70   - }
71   - }
72   -}
  1 +package com.bsth.data.schedule.auto_exec;
  2 +
  3 +import com.bsth.data.gpsdata_v2.status_manager.GpsStatusManager;
  4 +import com.bsth.data.schedule.DayOfSchedule;
  5 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  6 +import com.bsth.websocket.handler.SendUtils;
  7 +import org.apache.commons.lang3.StringUtils;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.stereotype.Component;
  10 +
  11 +import java.util.ArrayList;
  12 +import java.util.List;
  13 +
  14 +/**
  15 + * 实际班次自动执行
  16 + * Created by panzhao on 2017/10/31.
  17 + */
  18 +@Component
  19 +public class RealScheduleAutoExecHandler {
  20 +
  21 + @Autowired
  22 + SendUtils sendUtils;
  23 + @Autowired
  24 + DayOfSchedule dayOfSchedule;
  25 +
  26 + @Autowired
  27 + GpsStatusManager gpsStatusManager;
  28 +
  29 + public void exec(ScheduleRealInfo sch) {
  30 + boolean flag = false;
  31 + long t = System.currentTimeMillis();
  32 +
  33 + if (StringUtils.isEmpty(sch.getFcsjActual()) && sch.getDfsjT() < t) {
  34 + sch.setFcsjActualAll(sch.getDfsjT());
  35 + flag = true;
  36 + }
  37 +
  38 + if (StringUtils.isEmpty(sch.getZdsjActual()) && sch.getZdsjT() < t) {
  39 + sch.setZdsjActualAll(sch.getZdsjT());
  40 + flag = true;
  41 +
  42 + //准备执行下一个班次
  43 + ScheduleRealInfo next = dayOfSchedule.next(sch);
  44 + if (next != null) {
  45 + dayOfSchedule.addExecPlan(next);
  46 + //套跑 -下发线路切换指令
  47 + if(!next.getXlBm().equals(sch.getXlBm())){
  48 + gpsStatusManager.changeLine(next.getClZbh(), next, "auto@系统");
  49 + }
  50 + }
  51 +
  52 + //路牌有下一个班次,线调页面显示
  53 + ScheduleRealInfo nextLp = dayOfSchedule.nextByLp(sch);
  54 + if (null != nextLp) {
  55 + //入库
  56 + dayOfSchedule.save(sch);
  57 +
  58 + nextLp.setQdzArrDatesj(sch.getZdsjActual());
  59 + List<ScheduleRealInfo> refs = new ArrayList<>();
  60 + refs.add(sch);
  61 + refs.add(nextLp);
  62 + sendUtils.refreshSch(refs);
  63 + return;
  64 + }
  65 + }
  66 +
  67 + if (flag) {
  68 + dayOfSchedule.save(sch);
  69 + sendUtils.refreshSch(sch);
  70 + }
  71 + }
  72 +}
... ...
src/main/java/com/bsth/data/schedule/thread/SchedulePstThread.java
... ... @@ -101,7 +101,7 @@ public class SchedulePstThread extends Thread {
101 101 ",qdz_name=?,real_exec_date=?,remarks=?,s_gh=?,s_name=?,schedule_date=?,schedule_date_str=?,sflj=?" +
102 102 ",sp_id=?,status=?,update_date=?,xl_bm=?,xl_dir=?,xl_name=?,zdsj=?,zdsj_actual=?,zdz_code=?,zdz_name=?" +
103 103 ",ccno=?,df_auto=?,fgs_bm=?,fgs_name=?,gs_bm=?,gs_name=?,online=?,adjust_exps=?,reissue=?,jhlc_orig=?" +
104   - ",sigin_compate=?,drift_status=?,cc_service=?,major_station_name=?,rfid_state=?,first_rfid_state=? where id=?", new BatchPreparedStatementSetter() {
  104 + ",sigin_compate=?,drift_status=?,cc_service=?,major_station_name=?,rfid_state=?,first_rfid_state=?,region_id=? where id=?", new BatchPreparedStatementSetter() {
105 105 @Override
106 106 public void setValues(PreparedStatement ps, int i) throws SQLException {
107 107 ScheduleRealInfo sch = pstList.get(i);
... ... @@ -154,7 +154,8 @@ public class SchedulePstThread extends Thread {
154 154 ps.setString(47, sch.getMajorStationName());
155 155 ps.setInt(48, sch.getRfidState());
156 156 ps.setInt(49, sch.getFirstRfidState());
157   - ps.setLong(50, sch.getId());
  157 + ps.setString(50, sch.getRegionId());
  158 + ps.setLong(51, sch.getId());
158 159 }
159 160  
160 161 @Override
... ...
src/main/java/com/bsth/entity/directive/D64.java
1 1 package com.bsth.entity.directive;
2 2  
  3 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  4 +import com.fasterxml.jackson.annotation.JsonIgnore;
  5 +
3 6 import javax.persistence.*;
4 7  
5 8 /**
... ... @@ -26,6 +29,10 @@ public class D64 extends Directive{
26 29  
27 30 private D64Data data;
28 31  
  32 + @Transient
  33 + @JsonIgnore
  34 + private ScheduleRealInfo sch;
  35 +
29 36 @Embeddable
30 37 public static class D64Data {
31 38  
... ... @@ -35,7 +42,12 @@ public class D64 extends Directive{
35 42 private String deviceId;
36 43  
37 44 private String lineId;
38   -
  45 +
  46 + /**
  47 + * 预设区间seq(子线路)
  48 + */
  49 + private Integer seq;
  50 +
39 51 private String txtContent;
40 52  
41 53 public Short getCityCode() {
... ... @@ -62,6 +74,14 @@ public class D64 extends Directive{
62 74 this.lineId = lineId;
63 75 }
64 76  
  77 + public Integer getSeq() {
  78 + return seq;
  79 + }
  80 +
  81 + public void setSeq(Integer seq) {
  82 + this.seq = seq;
  83 + }
  84 +
65 85 public String getTxtContent() {
66 86 return txtContent;
67 87 }
... ... @@ -95,6 +115,14 @@ public class D64 extends Directive{
95 115 this.respAck = respAck;
96 116 }
97 117  
  118 + public ScheduleRealInfo getSch() {
  119 + return sch;
  120 + }
  121 +
  122 + public void setSch(ScheduleRealInfo sch) {
  123 + this.sch = sch;
  124 + }
  125 +
98 126 @Override
99 127 public void setDeviceId(String deviceId) {
100 128 if(this.data != null)
... ...
src/main/java/com/bsth/entity/realcontrol/ScheduleRealInfo.java
... ... @@ -257,6 +257,22 @@ public class ScheduleRealInfo implements Cloneable{
257 257 @Transient
258 258 private String operationType;
259 259  
  260 + /**
  261 + * 区间ID+seq
  262 + */
  263 + private String regionId;
  264 +
  265 + /**
  266 + * 封锁站点(站点编号以','分割)
  267 + */
  268 + private String blockStations;
  269 +
  270 + /**
  271 + * 区间切换状态 0 未发或未成功 1 成功
  272 + */
  273 + @Transient
  274 + private int regionSwitchState;
  275 +
260 276 public Integer getLpChange() {
261 277 return lpChange;
262 278 }
... ... @@ -1055,6 +1071,30 @@ public class ScheduleRealInfo implements Cloneable{
1055 1071 this.operationType = operationType;
1056 1072 }
1057 1073  
  1074 + public String getRegionId() {
  1075 + return regionId;
  1076 + }
  1077 +
  1078 + public void setRegionId(String regionId) {
  1079 + this.regionId = regionId;
  1080 + }
  1081 +
  1082 + public String getBlockStations() {
  1083 + return blockStations;
  1084 + }
  1085 +
  1086 + public void setBlockStations(String blockStations) {
  1087 + this.blockStations = blockStations;
  1088 + }
  1089 +
  1090 + public int getRegionSwitchState() {
  1091 + return regionSwitchState;
  1092 + }
  1093 +
  1094 + public void setRegionSwitchState(int regionSwitchState) {
  1095 + this.regionSwitchState = regionSwitchState;
  1096 + }
  1097 +
1058 1098 @Override
1059 1099 public ScheduleRealInfo clone() throws CloneNotSupportedException {
1060 1100 ScheduleRealInfo cloned = (ScheduleRealInfo) super.clone();
... ...
src/main/java/com/bsth/service/directive/DirectiveService.java
1   -package com.bsth.service.directive;
2   -
3   -
4   -import com.bsth.entity.directive.D60;
5   -import com.bsth.entity.directive.D64;
6   -import com.bsth.entity.directive.D80;
7   -import com.bsth.entity.directive.DC0_A3;
8   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
9   -import com.bsth.service.BaseService;
10   -
11   -import java.util.List;
12   -import java.util.Map;
13   -
14   -public interface DirectiveService extends BaseService<D60, Integer>{
15   -
16   - /**
17   - *
18   - * @Title: send60Phrase
19   - * @Description: TODO(60短语下发)
20   - * @param @param nbbm 车辆内部编码
21   - * @param @param text 短语
22   - * @return int 返回类型
23   - * @throws
24   - */
25   - int send60Phrase(String nbbm, String text, String sender);
26   -
27   - /**
28   - *
29   - * @Title: send60Dispatch
30   - * @Description: TODO(调度指令下发)
31   - * @param @param sch 要下发的班次
32   - * @param @param finish 已完成的班次数
33   - * @throws
34   - */
35   - int send60Dispatch(ScheduleRealInfo sch, int finish, String sender,String txtPrefix);
36   -
37   -
38   - int send60Dispatchcc(ScheduleRealInfo sch, int finish, String sender,String txtPrefix);
39   -
40   - /**
41   - *
42   - * @Title: send60Dispatch
43   - * @Description: TODO(调度指令下发)
44   - * @param @param id 班次ID
45   - * @throws
46   - */
47   - int send60Dispatch(Long id, String sender);
48   -
49   - int send60DispatchZndd(Long id, String sender);
50   - //60营运指令
51   - int send60Operation(String nbbm, int state, int upDown, String sender);
52   -
53   - /**
54   - *
55   - * @Title: lineChange
56   - * @Description: TODO(线路切换)
57   - * @param @param nbbm 车辆内部编码
58   - * @param @param lineId 新线路编码
59   - * @throws
60   - */
61   - int lineChange(String nbbm, String lineId, String sender);
62   -
63   - int lineChangeByDeviceId(String deviceId, String lineCode, String sender);
64   -
65   - /**
66   - *
67   - * @Title: upDownChange
68   - * @Description: TODO(切换上下行)
69   - * @param @param nbbm 车辆内部编码
70   - * @param @param upDonw 上下行 0 上行 1 下行
71   - * @throws
72   - */
73   - //int upDownChange(String nbbm, Integer upDown, String sender);
74   -
75   - /**
76   - *
77   - * @Title: sendDirectiveState
78   - * @Description: TODO(向页面推送班次指令状态)
79   - * @throws
80   - */
81   - void sendD60ToPage(ScheduleRealInfo sch);
82   -
83   - Map<String, List<D80>> findNoCofm80(String lineCodes);
84   -
85   - Map<String, Object> reply80(int id, int reply);
86   -
87   - Map<String, Object> findDirective(String nbbm, int dType, int page, int size);
88   -
89   - Map<String, Object> findAll80(Map<String, Object> map, int page, int size);
90   -
91   - D64 save64(D64 d64);
92   -
93   - Map<String, Object> sendC0A4(String nbbm);
94   -
95   - int sendC0A3(DC0_A3 c0a4);
96   -
97   - int sendC0A5(String json);
98   -
99   - int refreshLineFile(String deviceId);
100   -
101   - //int stateChange(String nbbm, Integer upDown, Integer state, String userName);
102   -
103   - Map<String,Object> deviceCofigList(Map<String, String> map, int page, int size);
104   -}
  1 +package com.bsth.service.directive;
  2 +
  3 +
  4 +import com.bsth.entity.directive.D60;
  5 +import com.bsth.entity.directive.D64;
  6 +import com.bsth.entity.directive.D80;
  7 +import com.bsth.entity.directive.DC0_A3;
  8 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  9 +import com.bsth.service.BaseService;
  10 +
  11 +import java.util.List;
  12 +import java.util.Map;
  13 +
  14 +public interface DirectiveService extends BaseService<D60, Integer>{
  15 +
  16 + /**
  17 + *
  18 + * @Title: send60Phrase
  19 + * @Description: TODO(60短语下发)
  20 + * @param @param nbbm 车辆内部编码
  21 + * @param @param text 短语
  22 + * @return int 返回类型
  23 + * @throws
  24 + */
  25 + int send60Phrase(String nbbm, String text, String sender);
  26 +
  27 + /**
  28 + *
  29 + * @Title: send60Dispatch
  30 + * @Description: TODO(调度指令下发)
  31 + * @param @param sch 要下发的班次
  32 + * @param @param finish 已完成的班次数
  33 + * @throws
  34 + */
  35 + int send60Dispatch(ScheduleRealInfo sch, int finish, String sender,String txtPrefix);
  36 +
  37 +
  38 + int send60Dispatchcc(ScheduleRealInfo sch, int finish, String sender,String txtPrefix);
  39 +
  40 + /**
  41 + *
  42 + * @Title: send60Dispatch
  43 + * @Description: TODO(调度指令下发)
  44 + * @param @param id 班次ID
  45 + * @throws
  46 + */
  47 + int send60Dispatch(Long id, String sender);
  48 +
  49 + int send60DispatchZndd(Long id, String sender);
  50 + //60营运指令
  51 + int send60Operation(String nbbm, int state, int upDown, String sender);
  52 +
  53 + /**
  54 + *
  55 + * @Title: lineChange
  56 + * @Description: TODO(线路切换)
  57 + * @param @param nbbm 车辆内部编码
  58 + * @param @param lineId 新线路编码
  59 + * @throws
  60 + */
  61 + int lineChange(String nbbm, String lineId, String sender);
  62 +
  63 + int lineChange(String nbbm, String lineId, ScheduleRealInfo sch, String sender);
  64 +
  65 + int lineChangeByDeviceId(String deviceId, String lineCode, String sender);
  66 +
  67 + int lineChangeByDeviceId(String deviceId, String lineCode, ScheduleRealInfo sch, String sender);
  68 +
  69 + /**
  70 + *
  71 + * @Title: upDownChange
  72 + * @Description: TODO(切换上下行)
  73 + * @param @param nbbm 车辆内部编码
  74 + * @param @param upDonw 上下行 0 上行 1 下行
  75 + * @throws
  76 + */
  77 + //int upDownChange(String nbbm, Integer upDown, String sender);
  78 +
  79 + /**
  80 + *
  81 + * @Title: sendDirectiveState
  82 + * @Description: TODO(向页面推送班次指令状态)
  83 + * @throws
  84 + */
  85 + void sendD60ToPage(ScheduleRealInfo sch);
  86 +
  87 + Map<String, List<D80>> findNoCofm80(String lineCodes);
  88 +
  89 + Map<String, Object> reply80(int id, int reply);
  90 +
  91 + Map<String, Object> findDirective(String nbbm, int dType, int page, int size);
  92 +
  93 + Map<String, Object> findAll80(Map<String, Object> map, int page, int size);
  94 +
  95 + D64 save64(D64 d64);
  96 +
  97 + Map<String, Object> sendC0A4(String nbbm);
  98 +
  99 + int sendC0A3(DC0_A3 c0a4);
  100 +
  101 + int sendC0A5(String json);
  102 +
  103 + int refreshLineFile(String deviceId);
  104 +
  105 + //int stateChange(String nbbm, Integer upDown, Integer state, String userName);
  106 +
  107 + Map<String,Object> deviceCofigList(Map<String, String> map, int page, int size);
  108 +}
... ...
src/main/java/com/bsth/service/directive/DirectiveServiceImpl.java
1   -package com.bsth.service.directive;
2   -
3   -import com.alibaba.fastjson.JSON;
4   -import com.alibaba.fastjson.JSONObject;
5   -import com.bsth.common.ResponseCode;
6   -import com.bsth.data.BasicData;
7   -import com.bsth.data.directive.DayOfDirectives;
8   -import com.bsth.data.directive.DirectiveCreator;
9   -import com.bsth.data.directive.GatewayHttpUtils;
10   -import com.bsth.data.gpsdata_v2.GpsRealData;
11   -import com.bsth.data.gpsdata_v2.entity.GpsEntity;
12   -import com.bsth.data.pilot80.PilotReport;
13   -import com.bsth.data.schedule.DayOfSchedule;
14   -import com.bsth.data.utils.ListFilterUtils;
15   -import com.bsth.data.utils.ListPageQueryUtils;
16   -import com.bsth.entity.directive.*;
17   -import com.bsth.entity.realcontrol.ScheduleRealInfo;
18   -import com.bsth.entity.sys.SysUser;
19   -import com.bsth.repository.directive.D60Repository;
20   -import com.bsth.repository.directive.D64Repository;
21   -import com.bsth.repository.directive.D80Repository;
22   -import com.bsth.security.util.SecurityUtils;
23   -import com.bsth.service.directive.dto.DeviceConfigDto;
24   -import com.bsth.service.impl.BaseServiceImpl;
25   -import com.bsth.websocket.handler.RealControlSocketHandler;
26   -import com.fasterxml.jackson.core.JsonProcessingException;
27   -import com.fasterxml.jackson.databind.ObjectMapper;
28   -import com.google.common.base.Splitter;
29   -import org.apache.commons.lang3.StringEscapeUtils;
30   -import org.apache.commons.lang3.StringUtils;
31   -import org.joda.time.format.DateTimeFormat;
32   -import org.joda.time.format.DateTimeFormatter;
33   -import org.slf4j.Logger;
34   -import org.slf4j.LoggerFactory;
35   -import org.springframework.beans.factory.annotation.Autowired;
36   -import org.springframework.jdbc.core.BeanPropertyRowMapper;
37   -import org.springframework.jdbc.core.JdbcTemplate;
38   -import org.springframework.stereotype.Service;
39   -
40   -import java.util.*;
41   -
42   -@Service
43   -public class DirectiveServiceImpl extends BaseServiceImpl<D60, Integer> implements DirectiveService {
44   -
45   - Logger logger = LoggerFactory.getLogger(this.getClass());
46   -
47   - @Autowired
48   - D60Repository d60Repository;
49   -
50   - @Autowired
51   - GpsRealData gpsRealDataBuffer;
52   -
53   - @Autowired
54   - D64Repository d64Repository;
55   -
56   - @Autowired
57   - RealControlSocketHandler socketHandler;
58   -
59   - @Autowired
60   - D80Repository d80Repository;
61   -
62   - @Autowired
63   - DayOfDirectives dayOfDirectives;
64   -
65   - @Autowired
66   - PilotReport pilotReport;
67   -
68   - @Autowired
69   - DayOfSchedule dayOfSchedule;
70   -
71   - @Autowired
72   - JdbcTemplate jdbcTemplate;
73   -
74   - //static Long schDiff = 1000 * 60 * 60L;
75   -
76   - private static DateTimeFormatter fmtHHmm = DateTimeFormat.forPattern("HH:mm"), fmtHHmm_CN = DateTimeFormat.forPattern("HH点mm分");
77   -
78   - @Override
79   - public int send60Phrase(String nbbm, String text, String sender) {
80   - D60 d60 = null;
81   - try {
82   - text = StringEscapeUtils.unescapeHtml4(text);
83   - text = text.replaceAll("#", "");
84   - d60 = create60Data(nbbm, text, (short) 0x00, null);
85   - } catch (Exception e) {
86   - logger.error("发送消息短语出现异常", e);
87   - return -1;
88   - }
89   -
90   - if (null == d60)
91   - return -1;
92   -
93   - // 发送指令
94   - int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
95   - if (null != sender)
96   - d60.setSender(sender);
97   - d60.setHttpCode(code);
98   -
99   - if (code != 0)
100   - d60.setErrorText("网关通讯失败, code: " + code);
101   -
102   - dayOfDirectives.put60(d60);
103   - return code;
104   - }
105   -
106   - @Override
107   - public int send60Dispatch(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
108   - D60 d60 = null;
109   - try {
110   - if (sch.isDestroy()) {
111   - logger.warn("烂班不允许发送调度指令....");
112   - return -1;
113   - }
114   -
115   - //待发应到时间
116   - String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
117   - String text = "您已完成" + finish + "个班次,下一发车时间" + fmtHHmm_CN.print(sch.getDfsjT()) + ",由"
118   - + sch.getQdzName() + "发往" + sch.getZdzName() + ";应到 " + dfsj;
119   -
120   - if(sch.getBcType().equals("venting")){
121   - text += " (直放)";
122   - }
123   - else if(sch.getBcType().equals("major")){
124   - text += " (放站到"+sch.getMajorStationName()+"带客)";
125   - } else if ("ldks".equals(sch.getBcType())) {
126   - text += " (两点空驶)";
127   - }
128   -
129   - //下发0x02指令 调度指令(闹钟有效)
130   - long t = System.currentTimeMillis() + 1000 * 30,
131   - alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
132   -
133   - if(StringUtils.isNotEmpty(txtPrefix)){
134   - text = txtPrefix + text;
135   - }
136   - text = StringEscapeUtils.unescapeHtml4(text);
137   - int state = 0;//营运状态
138   - if(dayOfSchedule.emptyService(sch))
139   - state = 1;
140   - d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
141   - , state, new Date(alarmTime));
142   -
143   - d60.setLineCode(sch.getXlBm());
144   - } catch (Exception e) {
145   - logger.error("生成调度指令时出现异常", e);
146   - return -1;
147   - }
148   -
149   - if (null == d60)
150   - return -1;
151   -
152   - d60.setSender(sender);
153   -
154   - JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
155   -
156   - //进场或者出场班次时,附加lock 标识
157   - if (null != sch.getBcType()
158   - && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
159   -
160   - jObj.put("lock", 1);
161   - }
162   -
163   - // 发送指令
164   - int code = GatewayHttpUtils.postJson(jObj.toJSONString());
165   -
166   - d60.setDispatch(true);
167   - d60.setSch(sch);
168   - d60.setHttpCode(code);
169   -
170   - if (code == 0) {
171   - sch.setDirectiveState(60);
172   - // 通知页面
173   - sendD60ToPage(sch);
174   - } else {
175   - d60.setErrorText("网关通讯失败, code: " + code);
176   - }
177   -
178   - dayOfDirectives.put60(d60);
179   - return code;
180   - }
181   -
182   - //出场请求
183   - @Override
184   - public int send60Dispatchcc(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
185   - D60 d60 = null;
186   - try {
187   - if (sch.isDestroy()) {
188   - logger.warn("烂班不允许发送调度指令....");
189   - return -1;
190   - }
191   - //待发应到时间
192   - String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
193   - String text = "出场请求,由" + sch.getQdzName() + "发往" + sch.getZdzName() + ";请出场";
194   -
195   - //下发0x02指令 调度指令(闹钟有效)
196   - long t = System.currentTimeMillis() + 1000 * 30,
197   - alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
198   -
199   - if(StringUtils.isNotEmpty(txtPrefix)){
200   - text = txtPrefix + text;
201   - }
202   - text = StringEscapeUtils.unescapeHtml4(text);
203   - int state = 0;//营运状态
204   - if(dayOfSchedule.emptyService(sch))
205   - state = 1;
206   - d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
207   - , state, new Date(alarmTime));
208   -
209   - d60.setLineCode(sch.getXlBm());
210   - } catch (Exception e) {
211   - logger.error("生成调度指令时出现异常", e);
212   - return -1;
213   - }
214   -
215   - if (null == d60)
216   - return -1;
217   -
218   - d60.setSender(sender);
219   -
220   - JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
221   -
222   - //进场或者出场班次时,附加lock 标识
223   - if (null != sch.getBcType()
224   - && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
225   -
226   - jObj.put("lock", 1);
227   - }
228   -
229   - // 发送指令
230   - int code = GatewayHttpUtils.postJson(jObj.toJSONString());
231   -
232   - d60.setDispatch(true);
233   - d60.setSch(sch);
234   - d60.setHttpCode(code);
235   -
236   - if (code == 0) {
237   - sch.setDirectiveState(60);
238   - // 通知页面
239   - sendD60ToPage(sch);
240   - } else {
241   - d60.setErrorText("网关通讯失败, code: " + code);
242   - }
243   -
244   - dayOfDirectives.put60(d60);
245   - return code;
246   - }
247   -
248   -
249   - public int send60DispatchZndd(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
250   - D60 d60 = null;
251   - try {
252   - if (sch.isDestroy()) {
253   - logger.warn("烂班不允许发送调度指令....");
254   - return -1;
255   - }
256   -
257   - //待发应到时间
258   - String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
259   - String text = "(自动补发命令)"+sch.getFcsjActual() +"发出的,由"
260   - + sch.getQdzName() + "发往" + sch.getZdzName() + ";还未确认,请尽快确认";
261   -
262   - if(sch.getBcType().equals("venting")){
263   - text += " (直放)";
264   - }
265   - else if(sch.getBcType().equals("major")){
266   - text += " (放站到"+sch.getMajorStationName()+"带客)";
267   - } else if ("ldks".equals(sch.getBcType())) {
268   - text += " (两点空驶)";
269   - }
270   -
271   - //下发0x02指令 调度指令(闹钟有效)
272   - long t = System.currentTimeMillis() + 1000 * 30,
273   - alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
274   -
275   - if(StringUtils.isNotEmpty(txtPrefix)){
276   - text = txtPrefix + text;
277   - }
278   - text = StringEscapeUtils.unescapeHtml4(text);
279   - int state = 0;//营运状态
280   - if(dayOfSchedule.emptyService(sch))
281   - state = 1;
282   - d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
283   - , state, new Date(alarmTime));
284   -
285   - d60.setLineCode(sch.getXlBm());
286   - } catch (Exception e) {
287   - logger.error("生成调度指令时出现异常", e);
288   - return -1;
289   - }
290   -
291   - if (null == d60)
292   - return -1;
293   -
294   - d60.setSender(sender);
295   -
296   - JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
297   -
298   - //进场或者出场班次时,附加lock 标识
299   - if (null != sch.getBcType()
300   - && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
301   -
302   - jObj.put("lock", 1);
303   - }
304   -
305   - // 发送指令
306   - int code = GatewayHttpUtils.postJson(jObj.toJSONString());
307   -
308   - d60.setDispatch(true);
309   - d60.setSch(sch);
310   - d60.setHttpCode(code);
311   -
312   - if (code == 0) {
313   - sch.setDirectiveState(60);
314   - // 通知页面
315   - sendD60ToPage(sch);
316   - } else {
317   - d60.setErrorText("网关通讯失败, code: " + code);
318   - }
319   - dayOfDirectives.put60(d60);
320   - return code;
321   - }
322   -
323   - /**
324   - * @Title: sendDirectiveState @Description: TODO(向页面推送班次指令状态) @throws
325   - */
326   - @Override
327   - public void sendD60ToPage(ScheduleRealInfo sch) {
328   - Map<String, Object> map = new HashMap<>();
329   - map.put("fn", "directive");
330   - map.put("t", sch);
331   -
332   - ObjectMapper mapper = new ObjectMapper();
333   -
334   - try {
335   - socketHandler.sendMessageToLine(sch.getXlBm(), mapper.writeValueAsString(map));
336   - } catch (JsonProcessingException e) {
337   - logger.error("", e);
338   - }
339   - }
340   -
341   - @Override
342   - public int send60Dispatch(Long id, String sender) {
343   - ScheduleRealInfo sch = dayOfSchedule.get(id);
344   - // 车辆已完成班次
345   - int finish = dayOfSchedule.doneSum(sch.getClZbh());
346   - return send60Dispatch(sch, finish, sender, "");
347   - }
348   -
349   - @Override
350   - public int send60DispatchZndd(Long id, String sender) {
351   - ScheduleRealInfo sch = dayOfSchedule.get(id);
352   - // 车辆已完成班次
353   - int finish = dayOfSchedule.doneSum(sch.getClZbh());
354   - return send60DispatchZndd(sch, finish, sender, "");
355   - }
356   -
357   - @Override
358   - public int send60Operation(String nbbm, int state, int upDown, String sender) {
359   - logger.info("切换运营状态, nbbm: " + nbbm + " ,state: " + state + " ,upDown:" + upDown);
360   -
361   - String text = "切换为 " + (upDown == 0 ? "上行" : "下行") + (state == 0 ? "营运" : "未营运");
362   - D60 d60 = new DirectiveCreator().createD60(nbbm, text, (short) 0x03, upDown, state, null);
363   -
364   - if (null == d60)
365   - return -1;
366   - if (null != sender)
367   - d60.setSender(sender);
368   - else
369   - d60.setSender("系统");
370   - // 发送指令
371   - int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
372   - // 添加到缓存,等待入库
373   - d60.setHttpCode(code);
374   -
375   - GpsEntity gps = gpsRealDataBuffer.getByNbbm(nbbm);
376   - if(null != gps)
377   - d60.setLineCode(gps.getLineId());
378   -
379   - if (code != 0)
380   - d60.setErrorText("网关通讯失败, code: " + code);
381   -
382   - dayOfDirectives.put60(d60);
383   - return code;
384   - }
385   -
386   - /**
387   - * 线路切换
388   - */
389   - @Override
390   - public int lineChange(String nbbm, String lineCode, String sender) {
391   - return lineChangeByDeviceId(BasicData.deviceId2NbbmMap.inverse().get(nbbm), lineCode, sender);
392   - }
393   -
394   -
395   - @Override
396   - public int lineChangeByDeviceId(String deviceId, String lineCode, String sender) {
397   - DirectiveCreator crt = new DirectiveCreator();
398   - Long t = System.currentTimeMillis();
399   - //生成64数据包
400   - D64 d64 = crt.create64(deviceId, lineCode, t);
401   -
402   - if (null != sender)
403   - d64.setSender(sender);
404   - else
405   - d64.setSender("系统");
406   -
407   - //String deviceId = d64.getDeviceId();
408   - int code = GatewayHttpUtils.postJson(JSON.toJSONString(d64));
409   - // 入库
410   - d64.setHttpCode(code);
411   - d64.getData().setTxtContent("切换线路[" + BasicData.lineCode2NameMap.get(lineCode) + "]");
412   - // 通知设备刷新线路文件,忽略结果
413   - if (code == 0)
414   - GatewayHttpUtils.postJson(crt.createDeviceRefreshData(deviceId, lineCode));
415   - else
416   - d64.setErrorText(" 网关通讯失败, code: " + code);
417   -
418   - dayOfDirectives.put64(d64);
419   - return code;
420   - }
421   -
422   - public D60 create60Data(String nbbm, String text, Short dispatchInstruct, ScheduleRealInfo sch) {
423   -
424   - String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
425   - String lineCode = null;
426   - if (null == deviceId) {
427   - logger.error("没有设备号对照的车辆:" + nbbm);
428   - return null;
429   - }
430   - // 根据当前GPS确定 上下行和营运状态
431   - Byte upDown = null;
432   - Integer state = null;
433   - if (null == sch) {
434   - GpsEntity gpsData = gpsRealDataBuffer.get(deviceId);
435   - if (null == gpsData) {
436   - /*
437   - * 短语指令不会变更设备状态,所以在没有gps状态对照的情况下可以下发
438   - * 其他指令在不确定状态的情况下,一律不允许
439   - */
440   - if (dispatchInstruct == 0) {
441   - upDown = 0;
442   - state = 0;
443   - } else {
444   - logger.error("没有找到gps对照,无法确认营运状态和上下行:" + nbbm);
445   - return null;
446   - }
447   - } else {
448   - upDown = gpsData.getUpDown();
449   - state = gpsData.getState();
450   - lineCode = gpsData.getLineId();
451   - }
452   - } else {
453   - upDown = Byte.parseByte(sch.getXlDir());
454   - state = 0;
455   - }
456   -
457   - return new DirectiveCreator().createD60(nbbm, text, dispatchInstruct, upDown, state, lineCode);
458   - }
459   -
460   -/* @Override
461   - public int upDownChange(String nbbm, Integer upDown, String sender) {
462   - return send60Operation(nbbm, 0, upDown, sender);
463   - }*/
464   -
465   -
466   - @Override
467   - public Map<String, List<D80>> findNoCofm80(String lineCodes) {
468   - List<String> lineList = Splitter.on(",").trimResults().splitToList(lineCodes);
469   -
470   - Map<String, List<D80>> rs = new HashMap<>();
471   - for (String code : lineList)
472   - rs.put(code, pilotReport.unconfirmed80(code));
473   -
474   - return rs;
475   - }
476   -
477   - @Override
478   - public Map<String, Object> reply80(int id, int reply) {
479   - Map<String, Object> rs = new HashMap<>();
480   - D80 d80 = pilotReport.findById(id);
481   - try {
482   - if (null == d80) {
483   - rs.put("status", ResponseCode.ERROR);
484   - rs.put("msg", "服务器没有找到对应数据!");
485   - } else if (d80.isConfirm()) {
486   - rs.put("status", ResponseCode.ERROR);
487   - rs.put("msg", "该数据已经被处理了!");
488   - } else {
489   - SysUser user = SecurityUtils.getCurrentUser();
490   - d80.setC0(reply, user.getUserName());
491   - // 入库
492   - d80Repository.save(d80);
493   - //回复网关
494   - int code = GatewayHttpUtils.postJson(JSON.toJSONString(d80.getC0()));
495   -
496   - rs.put("status", ResponseCode.SUCCESS);
497   - if (code != 0)
498   - rs.put("msg", "发送C0响应指令到车载设备失败,但该操作已经被系统记录!");
499   -
500   - pilotReport.reply(d80);
501   -
502   - // 通知页面
503   - Map<String, Object> sockMap = new HashMap<>();
504   - sockMap.put("fn", "d80Confirm");
505   - sockMap.put("id", d80.getId());
506   - sockMap.put("lineId", d80.getData().getLineId());
507   - socketHandler.sendMessageToLine(d80.getData().getLineId().toString(), JSON.toJSONString(sockMap));
508   - }
509   - } catch (Exception e) {
510   - logger.error("80响应出现异常...", e);
511   - rs.put("status", ResponseCode.SUCCESS);
512   - }
513   -
514   - return rs;
515   - }
516   -
517   - @Override
518   - public Map<String, Object> findDirective(String nbbms, int dType, int page, int size) {
519   - Map<String, Object> rsMap = new HashMap<>();
520   - try{
521   -
522   - List<String> carArray = new ArrayList<>(), deviceArray=new ArrayList<>();
523   - if(StringUtils.isNotEmpty(nbbms)){
524   - carArray = Splitter.on(",").splitToList(nbbms);
525   - //转换成设备号
526   - Map<String, String> nbbm2deviceMap = BasicData.deviceId2NbbmMap.inverse();
527   - for(int i = 0, len=carArray.size(); i < len; i++){
528   - deviceArray.add(nbbm2deviceMap.get(carArray.get(i)));
529   - }
530   - }
531   -
532   - List<Directive> list = new ArrayList<>();
533   - switch (dType) {
534   - case -1:
535   - //所有指令
536   - list = new ArrayList<>(dayOfDirectives.all());
537   - break;
538   - case 0:
539   - //调度指令
540   - Collection<D60> dptArray = dayOfDirectives.all60();
541   - for (D60 d60 : dptArray) {
542   - if (d60.isDispatch())
543   - list.add(d60);
544   - }
545   - break;
546   - case 1:
547   - //运营指令
548   - Collection<D60> yyArray = dayOfDirectives.all60();
549   - for (D60 d60 : yyArray) {
550   - if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x03
551   - && !d60.isDispatch())
552   - list.add(d60);
553   - }
554   - break;
555   - case 2:
556   - //线路切换指令
557   - list.addAll(dayOfDirectives.all64());
558   - break;
559   - case 3:
560   - //消息短语
561   - Collection<D60> dyArray = dayOfDirectives.all60();
562   - for (D60 d60 : dyArray) {
563   - if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x00
564   - && !d60.isDispatch())
565   - list.add(d60);
566   - }
567   - break;
568   - }
569   -
570   - // 时间倒序
571   - Collections.sort(list, new DayOfDirectives.DComparator());
572   - if(deviceArray.size() > 0){
573   - //按设备号过滤
574   - List<Directive> subList = new ArrayList<>();
575   - for (Directive d : list) {
576   - if (deviceArray.contains(d.getDeviceId())) {
577   - subList.add(d);
578   - }
579   - }
580   - list = subList;
581   - }
582   -
583   - for(Directive d : list){
584   - d.setTimeHHmm(fmtHHmm.print(d.getTimestamp()));
585   - d.setNbbm(BasicData.deviceId2NbbmMap.get(d.getDeviceId()));
586   - }
587   -
588   - int count = list.size();
589   - // 分页
590   - int s = page * size, e = s + size;
591   -
592   - if (e > count)
593   - e = count;
594   -
595   - List<Directive> rs = list.subList(s, e);
596   - rsMap.put("list", rs);
597   - rsMap.put("totalPages", count % size == 0 ? (count / size - 1) : count / size);
598   - rsMap.put("page", page);
599   - }catch (Exception e){
600   - logger.error("", e);
601   - throw e;
602   - }
603   - return rsMap;
604   - }
605   -
606   - @Override
607   - public Map<String, Object> findAll80(Map<String, Object> map, int page, int size) {
608   -
609   - List all = ListFilterUtils.filter(pilotReport.findAll(), map, D80.class);
610   - //排序
611   - Collections.sort(all, new Comparator<D80>() {
612   - @Override
613   - public int compare(D80 o1, D80 o2) {
614   - return (int) (o2.getTimestamp() - o1.getTimestamp());
615   - }
616   - });
617   - List<D80> d80s = ListPageQueryUtils.paging(all, page, size);
618   - //时间格式化
619   - for (D80 d80 : d80s) {
620   - d80.setTimeStr(fmtHHmm.print(d80.getTimestamp()));
621   - }
622   - Map<String, Object> rsMap = new HashMap<>();
623   - rsMap.put("list", d80s);
624   - rsMap.put("totalPages", all.size() % size == 0 ? all.size() / size - 1: all.size() / size);
625   - rsMap.put("page", page);
626   - return rsMap;
627   - }
628   -
629   - @Override
630   - public D64 save64(D64 d64) {
631   - return d64Repository.save(d64);
632   - }
633   -
634   - @Override
635   - public Map<String, Object> sendC0A4(String nbbm) {
636   - Map<String, Object> rs = new HashMap<>();
637   -
638   - String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
639   -
640   - GpsEntity gps = gpsRealDataBuffer.get(deviceId);
641   - if(gps == null || gps.isOffline()){
642   - rs.put("status", ResponseCode.ERROR);
643   - rs.put("msg", "下发指令失败,设备离线");
644   - return rs;
645   - }
646   -
647   - Map<String, Object> c0a4 = new HashMap<>();
648   - c0a4.put("deviceId", deviceId);
649   - c0a4.put("timestamp", System.currentTimeMillis());
650   - c0a4.put("operCode", (short) 0xC0);
651   -
652   - Map<String, Object> data = new HashMap<>();
653   - data.put("deviceId", deviceId);
654   - data.put("operCode2", (short) 0xA4);
655   - c0a4.put("data", data);
656   -
657   - int code = GatewayHttpUtils.postJson(JSON.toJSONString(c0a4));
658   - if(code != 0){
659   - rs.put("status", ResponseCode.ERROR);
660   - rs.put("msg", "和网关通讯失败, code " + code);
661   - }
662   - else{
663   - rs.put("status", ResponseCode.SUCCESS);
664   - }
665   - return rs;
666   - }
667   -
668   - @Override
669   - public int sendC0A3(DC0_A3 c0a3) {
670   - GatewayHttpUtils.postJson(JSON.toJSONString(c0a3));
671   - return 0;
672   - }
673   -
674   - @Override
675   - public int sendC0A5(String json) {
676   - return GatewayHttpUtils.postJson(json);
677   - }
678   -
679   - @Override
680   - public int refreshLineFile(String deviceId) {
681   - GpsEntity gps = gpsRealDataBuffer.get(deviceId);
682   - if (gps == null)
683   - return -1;
684   -
685   - return GatewayHttpUtils.postJson(new DirectiveCreator().createDeviceRefreshData(deviceId, gps.getLineId()));
686   - }
687   -
688   -/* @Override
689   - public int stateChange(String nbbm, Integer upDown, Integer state, String userName) {
690   - return send60Operation(nbbm, state, upDown, userName);
691   - }*/
692   -
693   - @Override
694   - public Map<String, Object> deviceCofigList(Map<String, String> map, int page, int size) {
695   - Map<String, Object> rs = new HashMap<>();
696   - try {
697   - String conts="";
698   - if(StringUtils.isNotBlank(map.get("lineId"))){
699   - conts+=" AND xl='" + map.get("lineId")+"'";
700   - }
701   - if(StringUtils.isNotBlank(map.get("nbbm"))){
702   - conts+=" AND inside_code like '%" + map.get("nbbm")+"%'";
703   - }
704   - if(StringUtils.isNotBlank(map.get("deviceId"))){
705   - conts+=" AND equipment_code like '%" + map.get("deviceId")+"%'";
706   - }
707   -
708   - String sql = "select * from (SELECT * FROM (SELECT id AS cl_id,inside_code,equipment_code FROM bsth_c_cars ) t1 LEFT JOIN bsth_v_c0_a4 t2 ON t1.equipment_code = t2.id LEFT JOIN (SELECT xl,cl FROM bsth_c_s_ccinfo WHERE id IN (SELECT max(id) FROM bsth_c_s_ccinfo GROUP BY cl)) t3 ON t1.cl_id = t3.cl ORDER BY t2.`timestamp` DESC) t4 where 1=1 "+conts;
709   -
710   - int count = jdbcTemplate.queryForObject("select count(*) from ("+sql+") c1", Integer.class);
711   - int totalPages = count % size == 0 ? count / size : count / size + 1;
712   - int s = page * size;
713   - List<DeviceConfigDto> list = jdbcTemplate.query(sql + " LIMIT " + s + "," + size, new BeanPropertyRowMapper<>(DeviceConfigDto.class));
714   -
715   - rs.put("status", ResponseCode.SUCCESS);
716   - rs.put("list", list);
717   - rs.put("totalPages", totalPages);
718   - rs.put("page", page);
719   - } catch (Exception e) {
720   - e.printStackTrace();
721   - logger.error("", e);
722   - rs.put("status", ResponseCode.ERROR);
723   - }
724   - return rs;
725   - }
726   -}
  1 +package com.bsth.service.directive;
  2 +
  3 +import com.alibaba.fastjson.JSON;
  4 +import com.alibaba.fastjson.JSONObject;
  5 +import com.bsth.common.ResponseCode;
  6 +import com.bsth.data.BasicData;
  7 +import com.bsth.data.directive.DayOfDirectives;
  8 +import com.bsth.data.directive.DirectiveCreator;
  9 +import com.bsth.data.directive.GatewayHttpUtils;
  10 +import com.bsth.data.gpsdata_v2.GpsRealData;
  11 +import com.bsth.data.gpsdata_v2.entity.GpsEntity;
  12 +import com.bsth.data.pilot80.PilotReport;
  13 +import com.bsth.data.schedule.DayOfSchedule;
  14 +import com.bsth.data.utils.ListFilterUtils;
  15 +import com.bsth.data.utils.ListPageQueryUtils;
  16 +import com.bsth.entity.directive.*;
  17 +import com.bsth.entity.realcontrol.ScheduleRealInfo;
  18 +import com.bsth.entity.sys.SysUser;
  19 +import com.bsth.repository.directive.D60Repository;
  20 +import com.bsth.repository.directive.D64Repository;
  21 +import com.bsth.repository.directive.D80Repository;
  22 +import com.bsth.security.util.SecurityUtils;
  23 +import com.bsth.service.directive.dto.DeviceConfigDto;
  24 +import com.bsth.service.impl.BaseServiceImpl;
  25 +import com.bsth.websocket.handler.RealControlSocketHandler;
  26 +import com.fasterxml.jackson.core.JsonProcessingException;
  27 +import com.fasterxml.jackson.databind.ObjectMapper;
  28 +import com.google.common.base.Splitter;
  29 +import org.apache.commons.lang3.StringEscapeUtils;
  30 +import org.apache.commons.lang3.StringUtils;
  31 +import org.joda.time.format.DateTimeFormat;
  32 +import org.joda.time.format.DateTimeFormatter;
  33 +import org.slf4j.Logger;
  34 +import org.slf4j.LoggerFactory;
  35 +import org.springframework.beans.factory.annotation.Autowired;
  36 +import org.springframework.jdbc.core.BeanPropertyRowMapper;
  37 +import org.springframework.jdbc.core.JdbcTemplate;
  38 +import org.springframework.stereotype.Service;
  39 +
  40 +import java.util.*;
  41 +
  42 +@Service
  43 +public class DirectiveServiceImpl extends BaseServiceImpl<D60, Integer> implements DirectiveService {
  44 +
  45 + Logger logger = LoggerFactory.getLogger(this.getClass());
  46 +
  47 + @Autowired
  48 + D60Repository d60Repository;
  49 +
  50 + @Autowired
  51 + GpsRealData gpsRealDataBuffer;
  52 +
  53 + @Autowired
  54 + D64Repository d64Repository;
  55 +
  56 + @Autowired
  57 + RealControlSocketHandler socketHandler;
  58 +
  59 + @Autowired
  60 + D80Repository d80Repository;
  61 +
  62 + @Autowired
  63 + DayOfDirectives dayOfDirectives;
  64 +
  65 + @Autowired
  66 + PilotReport pilotReport;
  67 +
  68 + @Autowired
  69 + DayOfSchedule dayOfSchedule;
  70 +
  71 + @Autowired
  72 + JdbcTemplate jdbcTemplate;
  73 +
  74 + @Autowired
  75 + private ObjectMapper mapper;
  76 +
  77 + //static Long schDiff = 1000 * 60 * 60L;
  78 +
  79 + private static DateTimeFormatter fmtHHmm = DateTimeFormat.forPattern("HH:mm"), fmtHHmm_CN = DateTimeFormat.forPattern("HH点mm分");
  80 +
  81 + @Override
  82 + public int send60Phrase(String nbbm, String text, String sender) {
  83 + D60 d60 = null;
  84 + try {
  85 + text = StringEscapeUtils.unescapeHtml4(text);
  86 + text = text.replaceAll("#", "");
  87 + d60 = create60Data(nbbm, text, (short) 0x00, null);
  88 + } catch (Exception e) {
  89 + logger.error("发送消息短语出现异常", e);
  90 + return -1;
  91 + }
  92 +
  93 + if (null == d60)
  94 + return -1;
  95 +
  96 + // 发送指令
  97 + int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
  98 + if (null != sender)
  99 + d60.setSender(sender);
  100 + d60.setHttpCode(code);
  101 +
  102 + if (code != 0)
  103 + d60.setErrorText("网关通讯失败, code: " + code);
  104 +
  105 + dayOfDirectives.put60(d60);
  106 + return code;
  107 + }
  108 +
  109 + @Override
  110 + public int send60Dispatch(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
  111 + D60 d60 = null;
  112 + try {
  113 + if (sch.isDestroy()) {
  114 + logger.warn("烂班不允许发送调度指令....");
  115 + return -1;
  116 + }
  117 +
  118 + //待发应到时间
  119 + String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
  120 + String text = "您已完成" + finish + "个班次,下一发车时间" + fmtHHmm_CN.print(sch.getDfsjT()) + ",由"
  121 + + sch.getQdzName() + "发往" + sch.getZdzName() + ";应到 " + dfsj;
  122 +
  123 + if(sch.getBcType().equals("venting")){
  124 + text += " (直放)";
  125 + }
  126 + else if(sch.getBcType().equals("major")){
  127 + text += " (放站到"+sch.getMajorStationName()+"带客)";
  128 + } else if ("ldks".equals(sch.getBcType())) {
  129 + text += " (两点空驶)";
  130 + }
  131 +
  132 + //下发0x02指令 调度指令(闹钟有效)
  133 + long t = System.currentTimeMillis() + 1000 * 30,
  134 + alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
  135 +
  136 + if(StringUtils.isNotEmpty(txtPrefix)){
  137 + text = txtPrefix + text;
  138 + }
  139 + text = StringEscapeUtils.unescapeHtml4(text);
  140 + int state = 0;//营运状态
  141 + if(dayOfSchedule.emptyService(sch))
  142 + state = 1;
  143 + d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
  144 + , state, new Date(alarmTime));
  145 +
  146 + d60.setLineCode(sch.getXlBm());
  147 + } catch (Exception e) {
  148 + logger.error("生成调度指令时出现异常", e);
  149 + return -1;
  150 + }
  151 +
  152 + if (null == d60)
  153 + return -1;
  154 +
  155 + d60.setSender(sender);
  156 +
  157 + JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
  158 +
  159 + //进场或者出场班次时,附加lock 标识
  160 + if (null != sch.getBcType()
  161 + && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
  162 +
  163 + jObj.put("lock", 1);
  164 + }
  165 +
  166 + // 发送指令
  167 + int code = GatewayHttpUtils.postJson(jObj.toJSONString());
  168 +
  169 + d60.setDispatch(true);
  170 + d60.setSch(sch);
  171 + d60.setHttpCode(code);
  172 +
  173 + if (code == 0) {
  174 + sch.setDirectiveState(60);
  175 + // 通知页面
  176 + sendD60ToPage(sch);
  177 + } else {
  178 + d60.setErrorText("网关通讯失败, code: " + code);
  179 + }
  180 +
  181 + dayOfDirectives.put60(d60);
  182 + return code;
  183 + }
  184 +
  185 + //出场请求
  186 + @Override
  187 + public int send60Dispatchcc(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
  188 + D60 d60 = null;
  189 + try {
  190 + if (sch.isDestroy()) {
  191 + logger.warn("烂班不允许发送调度指令....");
  192 + return -1;
  193 + }
  194 + //待发应到时间
  195 + String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
  196 + String text = "出场请求,由" + sch.getQdzName() + "发往" + sch.getZdzName() + ";请出场";
  197 +
  198 + //下发0x02指令 调度指令(闹钟有效)
  199 + long t = System.currentTimeMillis() + 1000 * 30,
  200 + alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
  201 +
  202 + if(StringUtils.isNotEmpty(txtPrefix)){
  203 + text = txtPrefix + text;
  204 + }
  205 + text = StringEscapeUtils.unescapeHtml4(text);
  206 + int state = 0;//营运状态
  207 + if(dayOfSchedule.emptyService(sch))
  208 + state = 1;
  209 + d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
  210 + , state, new Date(alarmTime));
  211 +
  212 + d60.setLineCode(sch.getXlBm());
  213 + } catch (Exception e) {
  214 + logger.error("生成调度指令时出现异常", e);
  215 + return -1;
  216 + }
  217 +
  218 + if (null == d60)
  219 + return -1;
  220 +
  221 + d60.setSender(sender);
  222 +
  223 + JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
  224 +
  225 + //进场或者出场班次时,附加lock 标识
  226 + if (null != sch.getBcType()
  227 + && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
  228 +
  229 + jObj.put("lock", 1);
  230 + }
  231 +
  232 + // 发送指令
  233 + int code = GatewayHttpUtils.postJson(jObj.toJSONString());
  234 +
  235 + d60.setDispatch(true);
  236 + d60.setSch(sch);
  237 + d60.setHttpCode(code);
  238 +
  239 + if (code == 0) {
  240 + sch.setDirectiveState(60);
  241 + // 通知页面
  242 + sendD60ToPage(sch);
  243 + } else {
  244 + d60.setErrorText("网关通讯失败, code: " + code);
  245 + }
  246 +
  247 + dayOfDirectives.put60(d60);
  248 + return code;
  249 + }
  250 +
  251 +
  252 + public int send60DispatchZndd(ScheduleRealInfo sch, int finish, String sender, String txtPrefix) {
  253 + D60 d60 = null;
  254 + try {
  255 + if (sch.isDestroy()) {
  256 + logger.warn("烂班不允许发送调度指令....");
  257 + return -1;
  258 + }
  259 +
  260 + //待发应到时间
  261 + String dfsj = fmtHHmm.print(sch.getDfsjT() + (sch.getBcsj() * 60 * 1000));
  262 + String text = "(自动补发命令)"+sch.getFcsjActual() +"发出的,由"
  263 + + sch.getQdzName() + "发往" + sch.getZdzName() + ";还未确认,请尽快确认";
  264 +
  265 + if(sch.getBcType().equals("venting")){
  266 + text += " (直放)";
  267 + }
  268 + else if(sch.getBcType().equals("major")){
  269 + text += " (放站到"+sch.getMajorStationName()+"带客)";
  270 + } else if ("ldks".equals(sch.getBcType())) {
  271 + text += " (两点空驶)";
  272 + }
  273 +
  274 + //下发0x02指令 调度指令(闹钟有效)
  275 + long t = System.currentTimeMillis() + 1000 * 30,
  276 + alarmTime = sch.getDfsjT() < t?t:sch.getDfsjT();
  277 +
  278 + if(StringUtils.isNotEmpty(txtPrefix)){
  279 + text = txtPrefix + text;
  280 + }
  281 + text = StringEscapeUtils.unescapeHtml4(text);
  282 + int state = 0;//营运状态
  283 + if(dayOfSchedule.emptyService(sch))
  284 + state = 1;
  285 + d60 = new DirectiveCreator().createD60_02(sch.getClZbh(), text, Integer.parseInt(sch.getXlDir())
  286 + , state, new Date(alarmTime));
  287 +
  288 + d60.setLineCode(sch.getXlBm());
  289 + } catch (Exception e) {
  290 + logger.error("生成调度指令时出现异常", e);
  291 + return -1;
  292 + }
  293 +
  294 + if (null == d60)
  295 + return -1;
  296 +
  297 + d60.setSender(sender);
  298 +
  299 + JSONObject jObj = JSON.parseObject(JSON.toJSONString(d60));
  300 +
  301 + //进场或者出场班次时,附加lock 标识
  302 + if (null != sch.getBcType()
  303 + && (sch.getBcType().equals("out") || sch.getBcType().equals("in"))) {
  304 +
  305 + jObj.put("lock", 1);
  306 + }
  307 +
  308 + // 发送指令
  309 + int code = GatewayHttpUtils.postJson(jObj.toJSONString());
  310 +
  311 + d60.setDispatch(true);
  312 + d60.setSch(sch);
  313 + d60.setHttpCode(code);
  314 +
  315 + if (code == 0) {
  316 + sch.setDirectiveState(60);
  317 + // 通知页面
  318 + sendD60ToPage(sch);
  319 + } else {
  320 + d60.setErrorText("网关通讯失败, code: " + code);
  321 + }
  322 + dayOfDirectives.put60(d60);
  323 + return code;
  324 + }
  325 +
  326 + /**
  327 + * @Title: sendDirectiveState @Description: TODO(向页面推送班次指令状态) @throws
  328 + */
  329 + @Override
  330 + public void sendD60ToPage(ScheduleRealInfo sch) {
  331 + Map<String, Object> map = new HashMap<>();
  332 + map.put("fn", "directive");
  333 + map.put("t", sch);
  334 +
  335 + ObjectMapper mapper = new ObjectMapper();
  336 +
  337 + try {
  338 + socketHandler.sendMessageToLine(sch.getXlBm(), mapper.writeValueAsString(map));
  339 + } catch (JsonProcessingException e) {
  340 + logger.error("", e);
  341 + }
  342 + }
  343 +
  344 + @Override
  345 + public int send60Dispatch(Long id, String sender) {
  346 + ScheduleRealInfo sch = dayOfSchedule.get(id);
  347 + // 车辆已完成班次
  348 + int finish = dayOfSchedule.doneSum(sch.getClZbh());
  349 + return send60Dispatch(sch, finish, sender, "");
  350 + }
  351 +
  352 + @Override
  353 + public int send60DispatchZndd(Long id, String sender) {
  354 + ScheduleRealInfo sch = dayOfSchedule.get(id);
  355 + // 车辆已完成班次
  356 + int finish = dayOfSchedule.doneSum(sch.getClZbh());
  357 + return send60DispatchZndd(sch, finish, sender, "");
  358 + }
  359 +
  360 + @Override
  361 + public int send60Operation(String nbbm, int state, int upDown, String sender) {
  362 + logger.info("切换运营状态, nbbm: " + nbbm + " ,state: " + state + " ,upDown:" + upDown);
  363 +
  364 + String text = "切换为 " + (upDown == 0 ? "上行" : "下行") + (state == 0 ? "营运" : "未营运");
  365 + D60 d60 = new DirectiveCreator().createD60(nbbm, text, (short) 0x03, upDown, state, null);
  366 +
  367 + if (null == d60)
  368 + return -1;
  369 + if (null != sender)
  370 + d60.setSender(sender);
  371 + else
  372 + d60.setSender("系统");
  373 + // 发送指令
  374 + int code = GatewayHttpUtils.postJson(JSON.toJSONString(d60));
  375 + // 添加到缓存,等待入库
  376 + d60.setHttpCode(code);
  377 +
  378 + GpsEntity gps = gpsRealDataBuffer.getByNbbm(nbbm);
  379 + if(null != gps)
  380 + d60.setLineCode(gps.getLineId());
  381 +
  382 + if (code != 0)
  383 + d60.setErrorText("网关通讯失败, code: " + code);
  384 +
  385 + dayOfDirectives.put60(d60);
  386 + return code;
  387 + }
  388 +
  389 + @Override
  390 + public int lineChange(String nbbm, String lineCode, String sender) {
  391 + return lineChangeByDeviceId(BasicData.deviceId2NbbmMap.inverse().get(nbbm), lineCode, null, sender);
  392 + }
  393 +
  394 + /**
  395 + * 线路切换
  396 + */
  397 + @Override
  398 + public int lineChange(String nbbm, String lineCode, ScheduleRealInfo sch, String sender) {
  399 + return lineChangeByDeviceId(BasicData.deviceId2NbbmMap.inverse().get(nbbm), lineCode, sch, sender);
  400 + }
  401 +
  402 + @Override
  403 + public int lineChangeByDeviceId(String deviceId, String lineCode, String sender) {
  404 + return lineChangeByDeviceId(deviceId, lineCode, null, sender);
  405 + }
  406 +
  407 + @Override
  408 + public int lineChangeByDeviceId(String deviceId, String lineCode, ScheduleRealInfo sch, String sender) {
  409 + DirectiveCreator crt = new DirectiveCreator();
  410 + Long t = System.currentTimeMillis();
  411 + //生成64数据包
  412 + D64 d64 = crt.create64(deviceId, lineCode, sch, t);
  413 +
  414 + if (null != sender)
  415 + d64.setSender(sender);
  416 + else
  417 + d64.setSender("系统");
  418 +
  419 + //String deviceId = d64.getDeviceId();
  420 + int code = -1;
  421 + try {
  422 + code = GatewayHttpUtils.postJson(mapper.writeValueAsString(d64));
  423 + } catch (JsonProcessingException e) {
  424 + throw new RuntimeException(e);
  425 + }
  426 + // 入库
  427 + d64.setHttpCode(code);
  428 + d64.getData().setTxtContent("切换线路[" + BasicData.lineCode2NameMap.get(lineCode) + "]");
  429 + // 通知设备刷新线路文件,忽略结果
  430 + if (code == 0)
  431 + GatewayHttpUtils.postJson(crt.createDeviceRefreshData(deviceId, lineCode));
  432 + else
  433 + d64.setErrorText(" 网关通讯失败, code: " + code);
  434 +
  435 + dayOfDirectives.put64(d64);
  436 + return code;
  437 + }
  438 +
  439 + public D60 create60Data(String nbbm, String text, Short dispatchInstruct, ScheduleRealInfo sch) {
  440 +
  441 + String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
  442 + String lineCode = null;
  443 + if (null == deviceId) {
  444 + logger.error("没有设备号对照的车辆:" + nbbm);
  445 + return null;
  446 + }
  447 + // 根据当前GPS确定 上下行和营运状态
  448 + Byte upDown = null;
  449 + Integer state = null;
  450 + if (null == sch) {
  451 + GpsEntity gpsData = gpsRealDataBuffer.get(deviceId);
  452 + if (null == gpsData) {
  453 + /*
  454 + * 短语指令不会变更设备状态,所以在没有gps状态对照的情况下可以下发
  455 + * 其他指令在不确定状态的情况下,一律不允许
  456 + */
  457 + if (dispatchInstruct == 0) {
  458 + upDown = 0;
  459 + state = 0;
  460 + } else {
  461 + logger.error("没有找到gps对照,无法确认营运状态和上下行:" + nbbm);
  462 + return null;
  463 + }
  464 + } else {
  465 + upDown = gpsData.getUpDown();
  466 + state = gpsData.getState();
  467 + lineCode = gpsData.getLineId();
  468 + }
  469 + } else {
  470 + upDown = Byte.parseByte(sch.getXlDir());
  471 + state = 0;
  472 + }
  473 +
  474 + return new DirectiveCreator().createD60(nbbm, text, dispatchInstruct, upDown, state, lineCode);
  475 + }
  476 +
  477 +/* @Override
  478 + public int upDownChange(String nbbm, Integer upDown, String sender) {
  479 + return send60Operation(nbbm, 0, upDown, sender);
  480 + }*/
  481 +
  482 +
  483 + @Override
  484 + public Map<String, List<D80>> findNoCofm80(String lineCodes) {
  485 + List<String> lineList = Splitter.on(",").trimResults().splitToList(lineCodes);
  486 +
  487 + Map<String, List<D80>> rs = new HashMap<>();
  488 + for (String code : lineList)
  489 + rs.put(code, pilotReport.unconfirmed80(code));
  490 +
  491 + return rs;
  492 + }
  493 +
  494 + @Override
  495 + public Map<String, Object> reply80(int id, int reply) {
  496 + Map<String, Object> rs = new HashMap<>();
  497 + D80 d80 = pilotReport.findById(id);
  498 + try {
  499 + if (null == d80) {
  500 + rs.put("status", ResponseCode.ERROR);
  501 + rs.put("msg", "服务器没有找到对应数据!");
  502 + } else if (d80.isConfirm()) {
  503 + rs.put("status", ResponseCode.ERROR);
  504 + rs.put("msg", "该数据已经被处理了!");
  505 + } else {
  506 + SysUser user = SecurityUtils.getCurrentUser();
  507 + d80.setC0(reply, user.getUserName());
  508 + // 入库
  509 + d80Repository.save(d80);
  510 + //回复网关
  511 + int code = GatewayHttpUtils.postJson(JSON.toJSONString(d80.getC0()));
  512 +
  513 + rs.put("status", ResponseCode.SUCCESS);
  514 + if (code != 0)
  515 + rs.put("msg", "发送C0响应指令到车载设备失败,但该操作已经被系统记录!");
  516 +
  517 + pilotReport.reply(d80);
  518 +
  519 + // 通知页面
  520 + Map<String, Object> sockMap = new HashMap<>();
  521 + sockMap.put("fn", "d80Confirm");
  522 + sockMap.put("id", d80.getId());
  523 + sockMap.put("lineId", d80.getData().getLineId());
  524 + socketHandler.sendMessageToLine(d80.getData().getLineId().toString(), JSON.toJSONString(sockMap));
  525 + }
  526 + } catch (Exception e) {
  527 + logger.error("80响应出现异常...", e);
  528 + rs.put("status", ResponseCode.SUCCESS);
  529 + }
  530 +
  531 + return rs;
  532 + }
  533 +
  534 + @Override
  535 + public Map<String, Object> findDirective(String nbbms, int dType, int page, int size) {
  536 + Map<String, Object> rsMap = new HashMap<>();
  537 + try{
  538 +
  539 + List<String> carArray = new ArrayList<>(), deviceArray=new ArrayList<>();
  540 + if(StringUtils.isNotEmpty(nbbms)){
  541 + carArray = Splitter.on(",").splitToList(nbbms);
  542 + //转换成设备号
  543 + Map<String, String> nbbm2deviceMap = BasicData.deviceId2NbbmMap.inverse();
  544 + for(int i = 0, len=carArray.size(); i < len; i++){
  545 + deviceArray.add(nbbm2deviceMap.get(carArray.get(i)));
  546 + }
  547 + }
  548 +
  549 + List<Directive> list = new ArrayList<>();
  550 + switch (dType) {
  551 + case -1:
  552 + //所有指令
  553 + list = new ArrayList<>(dayOfDirectives.all());
  554 + break;
  555 + case 0:
  556 + //调度指令
  557 + Collection<D60> dptArray = dayOfDirectives.all60();
  558 + for (D60 d60 : dptArray) {
  559 + if (d60.isDispatch())
  560 + list.add(d60);
  561 + }
  562 + break;
  563 + case 1:
  564 + //运营指令
  565 + Collection<D60> yyArray = dayOfDirectives.all60();
  566 + for (D60 d60 : yyArray) {
  567 + if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x03
  568 + && !d60.isDispatch())
  569 + list.add(d60);
  570 + }
  571 + break;
  572 + case 2:
  573 + //线路切换指令
  574 + list.addAll(dayOfDirectives.all64());
  575 + break;
  576 + case 3:
  577 + //消息短语
  578 + Collection<D60> dyArray = dayOfDirectives.all60();
  579 + for (D60 d60 : dyArray) {
  580 + if (d60 != null && d60.getData().getDispatchInstruct() == (short) 0x00
  581 + && !d60.isDispatch())
  582 + list.add(d60);
  583 + }
  584 + break;
  585 + }
  586 +
  587 + // 时间倒序
  588 + Collections.sort(list, new DayOfDirectives.DComparator());
  589 + if(deviceArray.size() > 0){
  590 + //按设备号过滤
  591 + List<Directive> subList = new ArrayList<>();
  592 + for (Directive d : list) {
  593 + if (deviceArray.contains(d.getDeviceId())) {
  594 + subList.add(d);
  595 + }
  596 + }
  597 + list = subList;
  598 + }
  599 +
  600 + for(Directive d : list){
  601 + d.setTimeHHmm(fmtHHmm.print(d.getTimestamp()));
  602 + d.setNbbm(BasicData.deviceId2NbbmMap.get(d.getDeviceId()));
  603 + }
  604 +
  605 + int count = list.size();
  606 + // 分页
  607 + int s = page * size, e = s + size;
  608 +
  609 + if (e > count)
  610 + e = count;
  611 +
  612 + List<Directive> rs = list.subList(s, e);
  613 + rsMap.put("list", rs);
  614 + rsMap.put("totalPages", count % size == 0 ? (count / size - 1) : count / size);
  615 + rsMap.put("page", page);
  616 + }catch (Exception e){
  617 + logger.error("", e);
  618 + throw e;
  619 + }
  620 + return rsMap;
  621 + }
  622 +
  623 + @Override
  624 + public Map<String, Object> findAll80(Map<String, Object> map, int page, int size) {
  625 +
  626 + List all = ListFilterUtils.filter(pilotReport.findAll(), map, D80.class);
  627 + //排序
  628 + Collections.sort(all, new Comparator<D80>() {
  629 + @Override
  630 + public int compare(D80 o1, D80 o2) {
  631 + return (int) (o2.getTimestamp() - o1.getTimestamp());
  632 + }
  633 + });
  634 + List<D80> d80s = ListPageQueryUtils.paging(all, page, size);
  635 + //时间格式化
  636 + for (D80 d80 : d80s) {
  637 + d80.setTimeStr(fmtHHmm.print(d80.getTimestamp()));
  638 + }
  639 + Map<String, Object> rsMap = new HashMap<>();
  640 + rsMap.put("list", d80s);
  641 + rsMap.put("totalPages", all.size() % size == 0 ? all.size() / size - 1: all.size() / size);
  642 + rsMap.put("page", page);
  643 + return rsMap;
  644 + }
  645 +
  646 + @Override
  647 + public D64 save64(D64 d64) {
  648 + return d64Repository.save(d64);
  649 + }
  650 +
  651 + @Override
  652 + public Map<String, Object> sendC0A4(String nbbm) {
  653 + Map<String, Object> rs = new HashMap<>();
  654 +
  655 + String deviceId = BasicData.deviceId2NbbmMap.inverse().get(nbbm);
  656 +
  657 + GpsEntity gps = gpsRealDataBuffer.get(deviceId);
  658 + if(gps == null || gps.isOffline()){
  659 + rs.put("status", ResponseCode.ERROR);
  660 + rs.put("msg", "下发指令失败,设备离线");
  661 + return rs;
  662 + }
  663 +
  664 + Map<String, Object> c0a4 = new HashMap<>();
  665 + c0a4.put("deviceId", deviceId);
  666 + c0a4.put("timestamp", System.currentTimeMillis());
  667 + c0a4.put("operCode", (short) 0xC0);
  668 +
  669 + Map<String, Object> data = new HashMap<>();
  670 + data.put("deviceId", deviceId);
  671 + data.put("operCode2", (short) 0xA4);
  672 + c0a4.put("data", data);
  673 +
  674 + int code = GatewayHttpUtils.postJson(JSON.toJSONString(c0a4));
  675 + if(code != 0){
  676 + rs.put("status", ResponseCode.ERROR);
  677 + rs.put("msg", "和网关通讯失败, code " + code);
  678 + }
  679 + else{
  680 + rs.put("status", ResponseCode.SUCCESS);
  681 + }
  682 + return rs;
  683 + }
  684 +
  685 + @Override
  686 + public int sendC0A3(DC0_A3 c0a3) {
  687 + GatewayHttpUtils.postJson(JSON.toJSONString(c0a3));
  688 + return 0;
  689 + }
  690 +
  691 + @Override
  692 + public int sendC0A5(String json) {
  693 + return GatewayHttpUtils.postJson(json);
  694 + }
  695 +
  696 + @Override
  697 + public int refreshLineFile(String deviceId) {
  698 + GpsEntity gps = gpsRealDataBuffer.get(deviceId);
  699 + if (gps == null)
  700 + return -1;
  701 +
  702 + return GatewayHttpUtils.postJson(new DirectiveCreator().createDeviceRefreshData(deviceId, gps.getLineId()));
  703 + }
  704 +
  705 +/* @Override
  706 + public int stateChange(String nbbm, Integer upDown, Integer state, String userName) {
  707 + return send60Operation(nbbm, state, upDown, userName);
  708 + }*/
  709 +
  710 + @Override
  711 + public Map<String, Object> deviceCofigList(Map<String, String> map, int page, int size) {
  712 + Map<String, Object> rs = new HashMap<>();
  713 + try {
  714 + String conts="";
  715 + if(StringUtils.isNotBlank(map.get("lineId"))){
  716 + conts+=" AND xl='" + map.get("lineId")+"'";
  717 + }
  718 + if(StringUtils.isNotBlank(map.get("nbbm"))){
  719 + conts+=" AND inside_code like '%" + map.get("nbbm")+"%'";
  720 + }
  721 + if(StringUtils.isNotBlank(map.get("deviceId"))){
  722 + conts+=" AND equipment_code like '%" + map.get("deviceId")+"%'";
  723 + }
  724 +
  725 + String sql = "select * from (SELECT * FROM (SELECT id AS cl_id,inside_code,equipment_code FROM bsth_c_cars ) t1 LEFT JOIN bsth_v_c0_a4 t2 ON t1.equipment_code = t2.id LEFT JOIN (SELECT xl,cl FROM bsth_c_s_ccinfo WHERE id IN (SELECT max(id) FROM bsth_c_s_ccinfo GROUP BY cl)) t3 ON t1.cl_id = t3.cl ORDER BY t2.`timestamp` DESC) t4 where 1=1 "+conts;
  726 +
  727 + int count = jdbcTemplate.queryForObject("select count(*) from ("+sql+") c1", Integer.class);
  728 + int totalPages = count % size == 0 ? count / size : count / size + 1;
  729 + int s = page * size;
  730 + List<DeviceConfigDto> list = jdbcTemplate.query(sql + " LIMIT " + s + "," + size, new BeanPropertyRowMapper<>(DeviceConfigDto.class));
  731 +
  732 + rs.put("status", ResponseCode.SUCCESS);
  733 + rs.put("list", list);
  734 + rs.put("totalPages", totalPages);
  735 + rs.put("page", page);
  736 + } catch (Exception e) {
  737 + e.printStackTrace();
  738 + logger.error("", e);
  739 + rs.put("status", ResponseCode.ERROR);
  740 + }
  741 + return rs;
  742 + }
  743 +}
... ...
src/main/java/com/bsth/service/impl/StationRouteServiceImpl.java
... ... @@ -67,6 +67,11 @@ public class StationRouteServiceImpl extends BaseServiceImpl&lt;StationRoute, Integ
67 67 @Autowired
68 68 private LsSectionRouteRepository lsSectionRouteRepository;
69 69  
  70 + @Autowired
  71 + private LineRegionRepository lineRegionRepository;
  72 + @Autowired
  73 + private LineVersionsRepository lineVersionsRepository;
  74 +
70 75 @Override
71 76 public Iterable<StationRoute> list(Map<String, Object> map) {
72 77 List<Sort.Order> orders = new ArrayList<>();
... ... @@ -404,10 +409,13 @@ public class StationRouteServiceImpl extends BaseServiceImpl&lt;StationRoute, Integ
404 409 try {
405 410 // 获取线路ID
406 411 Integer lineId = map.get("lineId").equals("") ? 0 : Integer.parseInt(map.get("lineId").toString());
  412 + Integer version = lineVersionsRepository.findCurrentVersion(lineId);
  413 + Map<String, Object> param = new HashMap<>();
  414 + param.put("line_eq", lineId);
  415 + param.put("version_eq", version);
407 416 /** 查询线路信息 @param:<lineId:线路ID> */
408 417 Line line = lineRepository.findById(lineId).get();
409 418  
410   -
411 419 Business company = businessRepository.findByBusinessCode(line.getCompany()).get(0);
412 420  
413 421 Integer fileVersions = lineRepository.findfileVersions(lineId);
... ... @@ -421,6 +429,7 @@ public class StationRouteServiceImpl extends BaseServiceImpl&lt;StationRoute, Integ
421 429 // Integer fileVersions = map.get("fileVersions").equals("") ? 1 : Integer.parseInt(map.get("fileVersions").toString());// 没有输入就默认1
422 430 /** 查询线路信息下的站点路由信息 @param:<lineId:线路ID> */
423 431 List<Object[]> objects = stationRouteRepository.usingSingle(lineId);
  432 + List<LineRegion> lineRegions = lineRegionRepository.findAll(new CustomerSpecs<>(param));
424 433 if (objects.size()>0) {
425 434 /** 获取配置文件里的ftp登录参数 */
426 435 Map<String, Object> FTPParamMap = readPropertiesGetFTPParam();
... ... @@ -489,7 +498,14 @@ public class StationRouteServiceImpl extends BaseServiceImpl&lt;StationRoute, Integ
489 498 clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", textFileName, input);
490 499  
491 500 // textFile.delete();
492   -
  501 + if (lineRegions.size() > 0) {
  502 + FTPClientUtils.deleteFileByPrefix(String.format("%s-", line.getLineCode()), url, port, username, password, String.format("%s/voice/", remotePath));
  503 + for (LineRegion lineRegion : lineRegions) {
  504 + textStr = String.format("%s\r\n%s", head, subLine2Ftp(lineRegion));
  505 + input = new ByteArrayInputStream(textStr.getBytes("gbk"));
  506 + clientUtils.uploadFile(url, port, username, password, remotePath + "/voice/", String.format("%s-%d.txt", line.getLineCode(), lineRegion.getSeq()), input);
  507 + }
  508 + }
493 509  
494 510 resultMap.put("status", ResponseCode.SUCCESS);
495 511 }else {
... ... @@ -1001,4 +1017,30 @@ public class StationRouteServiceImpl extends BaseServiceImpl&lt;StationRoute, Integ
1001 1017 }
1002 1018 }
1003 1019 }
  1020 +
  1021 + private String subLine2Ftp(LineRegion lineRegion) {
  1022 + StringBuilder builder = new StringBuilder();
  1023 + int len = lineRegion.getStationRoutes().size();
  1024 + int idx = 1;
  1025 + for (int i = 0;i < len;i++) {
  1026 + LsStationRoute route = lineRegion.getStationRoutes().get(i);
  1027 + builder.append(route.getCenterPointWgs().getPosition().getCoordinate(0))
  1028 + .append("\t").append(route.getCenterPointWgs().getPosition().getCoordinate(1))
  1029 + .append("\t").append(i == len - 1 ? 2 : 1)
  1030 + .append("\t").append(idx).append("\t");
  1031 + for (int j = 0;j < 8 - route.getStationCode().length();j++) {
  1032 + builder.append("0");
  1033 + }
  1034 + builder.append(route.getStationCode())
  1035 + .append("\t").append((int) route.getDistances().doubleValue() * 1000)
  1036 + .append("\t0")
  1037 + .append("\t").append(route.getStationName())
  1038 + .append("\t").append(route.getStationNameEn())
  1039 + .append("\r\n");
  1040 +
  1041 + idx++;
  1042 + }
  1043 +
  1044 + return builder.toString();
  1045 + }
1004 1046 }
... ...