IThirdServiceImpl.java
12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package com.jkdata.service.impl;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jkdata.entity.HistoryPoint;
import com.jkdata.entity.SafeArea;
import com.jkdata.entity.SafeAreaNew;
import com.jkdata.tool.*;
import com.jkdata.entity.ThirdPosition;
import com.jkdata.mapper.ThirdMapper;
import com.jkdata.service.IThirdService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
@Service
public class IThirdServiceImpl implements IThirdService {
@Autowired
private ThirdMapper thirdMapper;
private static DecimalFormat df = new DecimalFormat("######0.0000");
private static SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Autowired
private RedisUtil redisUtil;
@Override
public void savePosition(JSONObject jsonObject) {
Date t = new Date();
boolean ok = addMsgLog(jsonObject,sf.format(t));//添加接收报文日志
if(ok){
ThirdPosition thirdPosition = new ThirdPosition();
thirdPosition.setNodeId(jsonObject.getString("NodeId"));
Map xyMap = jsonObject.getObject("position",Map.class);
thirdPosition.setPositionx(xyMap.get("x").toString());
thirdPosition.setPositiony(xyMap.get("y").toString());
thirdPosition.setSystemId(jsonObject.getString("SystemId"));
thirdPosition.setType(jsonObject.getString("Type"));
thirdPosition.setGroup(jsonObject.getString("group"));
thirdPosition.setGroupNumber(jsonObject.getInteger("groupNumber"));
thirdPosition.setCreateTime(t);
//判断是B1、B2
changCoordinateB1(thirdPosition);
// changCoordinateB2(thirdPosition);
}
}
/**
* 收报文日志
* @param jsonObject
*/
private boolean addMsgLog(JSONObject jsonObject, String format) {
String sql = "INSERT INTO sys_msg_log ( mag_log,create_by,create_time ) VALUES " +
"( '"+jsonObject.toJSONString()+"','"+CommonConstant.CMCC+"','"+format+"' )";
System.out.println("**********"+sql);
boolean ok = addDataToDataBase(sql);
return ok;
}
//SQL插入数据库
private boolean addDataToDataBase(String sql) {
boolean ok = thirdMapper.addDataToDataBase(sql);
return ok;
}
public static void main(String[] args) {
String x1 = "22.99456";
String y1 = "330.3669";
Map<String,Double> map = new HashMap<>();//初始化
map = CoordinateUtil.changCoordinate("B2",x1,y1);
System.out.println("新坐标为:X:"+map.get("x")+",Y:"+map.get("y"));
// double sin = Math.sin(CommonConstant.XZJD);
// double cos = Math.cos(CommonConstant.XZJD);
// System.out.println("sin:"+sin+",cos:"+cos);
// //平移到原点
// double x0 = Double.valueOf(x1) - CommonConstant.LT_B1_X;
// double y0 = Double.valueOf(y1) - CommonConstant.LT_B1_Y;
// System.out.println("平移后的坐标:x0:"+x0+",y0:"+y0);
// //旋转坐标
// double x = x0*sin + y0*cos;
// double y = y0*sin - x0*cos;
// System.out.println("旋转后坐标为:X:"+x+",Y:"+y);
// //平移后的坐标
// double py_x = x + CommonConstant.MQ_B1_X/CommonConstant.BLC_X;
// double py_y = y + CommonConstant.MQ_B1_Y/CommonConstant.BLC_Y;
// System.out.println("平移后的坐标为:X:"+py_x+",Y:"+py_y);
// //放大后的坐标
// double fd_x = CommonConstant.BLC_X*py_x;
// double fd_y = CommonConstant.BLC_Y*py_y;
// System.out.println("放大后的坐标为:X:"+fd_x+",Y:"+fd_y);
}
//B1定位坐标
private void changCoordinateB1(ThirdPosition thirdPosition) {
Map<String,Double> map = new HashMap<>();//初始化
String x1 = thirdPosition.getPositionx();
String y1 = thirdPosition.getPositiony();
map = CoordinateUtil.changCoordinate("B1",x1,y1);
thirdPosition.setPositionx(String.valueOf(map.get("x")));
thirdPosition.setPositiony(String.valueOf(map.get("y")));
addPositionInfo(thirdPosition);//添加转换后坐标信息
}
//B2定位坐标
private void changCoordinateB2(ThirdPosition thirdPosition) {
Map<String,Double> map = new HashMap<>();//初始化
String x1 = thirdPosition.getPositionx();
String y1 = thirdPosition.getPositiony();
map = CoordinateUtil.changCoordinate("B2",x1,y1);
thirdPosition.setPositionx(String.valueOf(map.get("x")));
thirdPosition.setPositiony(String.valueOf(map.get("y")));
addPositionInfo(thirdPosition);//添加转换后坐标信息
}
private boolean addPositionInfo(ThirdPosition thirdPosition) {
String uuid = UUIDGenerator.creatUUID();
String sql = "INSERT INTO sys_cmcc_position ( `position_id`,`node_id`,`system_id`,`type`,`group`," +
"`group_number`,`position_x`,`position_y`,`create_by`,`create_time` ) VALUES " +
" ( '"+uuid+"','"+thirdPosition.getNodeId()+"','"+thirdPosition.getSystemId()+"','"+thirdPosition.getType()+"','"+thirdPosition.getGroup()+"'," +
""+thirdPosition.getGroupNumber()+",'"+thirdPosition.getPositionx()+"','"+thirdPosition.getPositiony()+"','"+CommonConstant.CMCC+"','"+sf.format(thirdPosition.getCreateTime())+"' )";
System.out.println("**********"+sql);
boolean ok = addDataToDataBase(sql);
return ok;
}
@Override
public String getBuildingAllInfo() {
HashMap<String,Object> map = new HashMap<>();
String tmsp = String.valueOf(System.currentTimeMillis()/1000);
map.put("bldId",CommonConstant.BLD_ID);
map.put("ts",tmsp);
String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,tmsp,CommonConstant.CMCC_KEY}),",")
.replace(",","");
System.out.println("sign参数:"+str);
String strMd5 = MD5Util.getMD5LowerCase(str);
System.out.println("加密后参数:"+str);
map.put("sgn",strMd5);
String post = HttpUtil.post(CommonConstant.BUILD_URL,map);
System.out.println("请求结果:"+post);
return post;
}
@Override
public List<HistoryPoint> getPositionByDeviceId(String deviceId,String startTime,String endTime) {
HashMap<String,Object> map = new HashMap<>();
map.put("buildingId",CommonConstant.BLD_ID);
map.put("deviceId",deviceId);
map.put("page","0");
map.put("count","200");
map.put("startTime",startTime);
map.put("endTime",endTime);
String post = HttpUtil.post(CommonConstant.HISTORY_URL,map);
JSONObject json = JSONObject.parseObject(post);
List<HistoryPoint> list = JSONObject.parseArray(json.get("data").toString(), HistoryPoint.class);
for (HistoryPoint historyPoint : list) {
String x1 = String.valueOf(historyPoint.getX());
String y1 = String.valueOf(historyPoint.getY());
Map<String,Double> pointMap = new HashMap<>();//初始化
pointMap = CoordinateUtil.changCoordinate("B1",x1,y1);
historyPoint.setX(pointMap.get("x"));
historyPoint.setY(pointMap.get("y"));
}
return list;
}
@Override
public String getAlarmListByCount() {
HashMap<String,Object> map = new HashMap<>();
String tmsp = String.valueOf(System.currentTimeMillis()/1000);
map.put("bldId",CommonConstant.BLD_ID);
map.put("ts",tmsp);
map.put("count","10");
String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,"10",tmsp,CommonConstant.CMCC_KEY}),",")
.replace(",","");
String strMd5 = MD5Util.getMD5LowerCase(str);
map.put("sgn",strMd5);
String post = HttpUtil.post(CommonConstant.ALARM_URL,map);
return post;
}
@Override
public String getWeekAlarmState() {
HashMap<String,Object> map = new HashMap<>();
String tmsp = String.valueOf(System.currentTimeMillis()/1000);
map.put("bldId",CommonConstant.BLD_ID);
map.put("ts",tmsp);
String str = StringUtils.join(Arrays.asList(new String[]{CommonConstant.BLD_ID,tmsp,CommonConstant.CMCC_KEY}),",")
.replace(",","");
String strMd5 = MD5Util.getMD5LowerCase(str);
map.put("sgn",strMd5);
String post = HttpUtil.post(CommonConstant.WEEK_ALARM_URL,map);
return post;
}
@Override
public ThirdPosition getRealTimeLocation(String deviceId) {
ThirdPosition thirdPosition = new ThirdPosition();
thirdPosition = thirdMapper.getRealTimeLocation(deviceId);
return thirdPosition;
}
@Override
public List<SafeArea> getSafeArea() {
String get = HttpUtil.get(CommonConstant.SAFE_AREA_URL+"?buildingId="+CommonConstant.BLD_ID);
JSONObject json = JSONObject.parseObject(get);
List<SafeArea> list = JSONObject.parseArray(json.get("data").toString(), SafeArea.class);
for (SafeArea safeArea : list) {
String areas = safeArea.getAreas();//66&363,100&348,108&367,74&381
List<String> arealist = Arrays.asList(areas.split(","));
StringBuffer data = new StringBuffer();
for (String area:arealist) {
String x1 = area.split("&")[0];
String y1 = area.split("&")[1];
Map<String,Double> map = new HashMap<>();//初始化
map = CoordinateUtil.changCoordinate("B1",x1,y1);
data.append(String.valueOf(map.get("x"))+"&"+String.valueOf(map.get("y"))).append(",");
}
safeArea.setAreas(data.toString().substring(0,data.toString().length()-1));
}
return list;
}
@Override
public List<SafeArea> getSafeAreaNew() {
List<SafeArea> list = new ArrayList<>();
String get = getBuildingAllInfo();
JSONObject json = JSONObject.parseObject(get);
Map jsonToMap = JSONObject.parseObject(json.toJSONString());
JSONObject dataJson = JSONObject.parseObject(jsonToMap.get("data").toString());
Map safeMap = JSONObject.parseObject(dataJson.toJSONString());
List<SafeAreaNew> dataList = JSONObject.parseArray(safeMap.get("monitoringArea").toString(), SafeAreaNew.class);
for (SafeAreaNew safeAreaNew : dataList) {
SafeArea safeArea = new SafeArea();
String areas = safeAreaNew.getAreas();//66&363,100&348,108&367,74&381
List<String> arealist = Arrays.asList(areas.split(","));
StringBuffer data = new StringBuffer();
for (String area:arealist) {
String x1 = area.split("&")[0];
String y1 = area.split("&")[1];
Map<String,Double> map = new HashMap<>();//初始化
if(safeAreaNew.getFloorId()==26){
map = CoordinateUtil.changCoordinate("B2",x1,y1);
}else{
map = CoordinateUtil.changCoordinate("B1",x1,y1);
}
data.append(String.valueOf(map.get("x"))+"&"+String.valueOf(map.get("y"))).append(",");
}
safeArea.setAreas(data.toString().substring(0,data.toString().length()-1));
safeArea.setBuildingId(safeAreaNew.getBuildingId());
safeArea.setId(safeAreaNew.getId());
safeArea.setName(safeAreaNew.getMName());
safeArea.setMTypeDefine(safeAreaNew.getMTypeDefine());
safeArea.setBgColor(safeAreaNew.getBgColor());
safeArea.setFloorId(safeAreaNew.getFloorId());
safeArea.setIsShow(safeAreaNew.getIsShow());
safeArea.setMType(safeAreaNew.getMType());
list.add(safeArea);
}
return list;
}
@Override
public List<HistoryPoint> getPositionByDeviceIdTest(String deviceId) {
List<HistoryPoint> list = new ArrayList<>();
String historyPointStr = "1500.12,1500.96;1510.01,1560.56;1580.01,1580.56;1800.01,1810.56;1910.01,2010.56;" +
"2510.21,2557.56;3510.01,3510.56;5510.01,5515.56;7512.01,7510.56;8514.01,8610.56;";
String [] pointHistoryArr = historyPointStr.split(";");
for (int i = 0; i < pointHistoryArr.length; i++) {
System.out.println(pointHistoryArr[i]);
String [] pointArr = pointHistoryArr[i].split(",");
HistoryPoint historyPoint = new HistoryPoint();
historyPoint.setId(i+1);
historyPoint.setBuildingId("10");
historyPoint.setDeviceId(deviceId);
historyPoint.setX(Double.valueOf(pointArr[0]));
historyPoint.setY(Double.valueOf(pointArr[1]));
list.add(historyPoint);
}
return list;
}
}