Commit 79e275e8 by 黄强

解析json 模板比你且複製,還差變量替換

parent f8fa2926
......@@ -140,13 +140,13 @@ public class Base64decrypt {
/**
* 机器人测试
*/
public Map<String, Object> jqrSendString( Map<String,Map<String,Object>> map,Map<String, Object> objectMap,
public Map<String, Object> jqrSendString( Map<String,Map<String,Object>> map,Map<String, Object> objectMap,long millis,
Map<String, Object> reportDto) {
Map<String, Object> mongoMap = findMongo(map);
mongoMap.putAll(objectMap);
mongoMap.remove("_id");
mongoMap.remove("time");
long millis = Calendar.getInstance().getTimeInMillis();
//处理实体类在每个字段加上时间
List<Map<String, Object>> allDto = new ArrayList<>();
for (Map.Entry<String,Object> entry : mongoMap.entrySet()) {
......@@ -168,7 +168,7 @@ public class Base64decrypt {
}
}
reportDto.put("reported", allDto);
reportDto.put("edgeTime", millis);
JSONObject jsonObj=new JSONObject(reportDto);
String s = jsonObj.toString();
System.out.println("輸出Json:"+s);
......
......@@ -48,17 +48,17 @@ public class MqttStartClient {
* @param skFileUrl
* @return
*/
public Map<String, Object> resolveSk(String skFileUrl) {
Map<String, Object> skReportDto = new HashMap<>();
public Integer resolveSk(String skFileUrl) {
// Map<String, Object> skReportDto = new HashMap<>();
String skFileValue = readSkFileToList(skFileUrl);
if (skFileValue.isEmpty()) {
/* if (skFileValue.isEmpty()) {
return skReportDto;
}
}*/
JSONObject jsonObject = JSONObject.parseObject(skFileValue);
//skReportDto.put("cmdId", "10");
skReportDto.put("version", jsonObject.getInteger("version"));
//skReportDto.put("version", jsonObject.getInteger("version"));
//skReportDto.put("sequenceId", 1);
return skReportDto;
return jsonObject.getInteger("version");
}
......@@ -158,17 +158,22 @@ public class MqttStartClient {
*/
public void jqr( JsonDataDto jsonDataDto, XBMqttDataUtil xbMqttDataUtil) {
try {
long millis = Calendar.getInstance().getTimeInMillis();
//解析sk证书信息
Map<String, Object> reportDto = resolveSk(jsonDataDto.getSkurl());
Integer version= resolveSk(jsonDataDto.getSkurl());
//reportDto.put("productKey", "PaintRobot");
//解析设备txt文本获取静态属性值
Map<String ,Object> reportDto = new LinkedHashMap<>();
Map<String, Object> fileSitesMap = readTxtFileToMap(jsonDataDto.getTxtFileUrl());
Map<String, Object> sites = new HashMap<>();
if (!fileSitesMap.isEmpty()) {
reportDto.put("clientUuid", jsonDataDto.getClientUuid()); //TODO 上报到客户那边数据中必要的参数值
reportDto.put("cmdId", jsonDataDto.getCmdId());//指 令 标 识
reportDto.put("productKey", jsonDataDto.getProductKey()); //必填,表示设备类型,设备类型在IoT平台上维护
reportDto.put("version", version);//版本
reportDto.put("sequenceId", jsonDataDto.getSequenceId());//必填,值为1
reportDto.put("edgeTime", millis);
reportDto.put("productKey", jsonDataDto.getProductKey()); //必填,表示设备类型,设备类型在IoT平台上维护
reportDto.put("clientUuid", jsonDataDto.getClientUuid()); //TODO 上报到客户那边数据中必要的参数值
sites.put("DeviceID",fileSitesMap.get("设备ID"));
sites.put("CompanyNo",fileSitesMap.get("公司编码"));
sites.put("factoryNo",fileSitesMap.get("工厂编码"));
......@@ -180,17 +185,17 @@ public class MqttStartClient {
//1 redis和mqtt分开
if (separate == 1) {
//TODO 同时MQTT上报和推送前端数据
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites, reportDto);
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites, millis ,reportDto);
StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap);
String json = new Gson().toJson(objectMap);
xbMqttDataUtil.publish( json);
} else if (separate == 2) {
// TODO 只推送前端数据
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,reportDto);
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,millis,reportDto);
StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap);
} else if (separate == 3) {
//TODO 只发送mqtt
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,reportDto);
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,millis,reportDto);
String json = new Gson().toJson(objectMap);
xbMqttDataUtil.publish(json);
}
......
......@@ -46,6 +46,10 @@ public class MqttClientController {
public Result start(@RequestParam MultipartFile file) throws IOException {
InitHander initHander = new InitHander();
List<JsonDataDto> jsonDataDtoList = initHander.analysisJsonParm(file);
if(jsonDataDtoList.isEmpty()){
String initJson = initJson();
return new Result().toFailure("标准格式:"+initJson);
}
sendMqttServer.sendMqtt(jsonDataDtoList);
/* List<BldzCclmVo> xpjVos = scadaMapper.find("校平机");
......@@ -473,4 +477,78 @@ public class MqttClientController {
return new Result().toSuccess(list);
}
private String initJson(){
String jsonStr="[\n" +
" {\n" +
" \"skurl\":\"\\\\cacert\\\\test.sk\",\n" +
" \"txtFileUrl\":\"\\\\cacert\\\\cjdqzdtqsb1.txt\",\n" +
" \"mongdbTableb\":\"chejiashebeibianliang\",\n" +
" \"clientUuid\":\"5003820063\",\n" +
" \"cmdId\":\"10\",\n" +
" \"productKey\":\"PaintRobot\",\n" +
" \"sequenceId\":1,\n" +
" \"reported\":{\n" +
" \"singleField\":[\n" +
" {\n" +
" \"subTable\":\"chejiashebeibianliang03\",\n" +
" \"feilds\":[\n" +
" {\n" +
" \"dbField\":\"d141\",\n" +
" \"fieldDataType\":\"FLOAT\",\n" +
" \"iotField\":\"GroupDistr\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"subTable\":\"chejiashebeibianliang04\",\n" +
" \"feilds\":[\n" +
" {\n" +
" \"dbField\":\"d142\",\n" +
" \"fieldDataType\":\"FLOAT\",\n" +
" \"iotField\":\"SolidFlow\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d143\",\n" +
" \"fieldDataType\":\"FLOAT\",\n" +
" \"iotField\":\"SolidContent\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d144\",\n" +
" \"fieldDataType\":\"INT\",\n" +
" \"iotField\":\"PowTime\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d145\",\n" +
" \"fieldDataType\":\"INT\",\n" +
" \"iotField\":\"Alarm\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d146\",\n" +
" \"fieldDataType\":\"STRING\",\n" +
" \"iotField\":\"AlarmMsg\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d147\",\n" +
" \"fieldDataType\":\"FLOAT\",\n" +
" \"iotField\":\"PaintFlow\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d148\",\n" +
" \"fieldDataType\":\"FLOAT\",\n" +
" \"iotField\":\"PaintValu\"\n" +
" },\n" +
" {\n" +
" \"dbField\":\"d149\",\n" +
" \"fieldDataType\":\"INT\",\n" +
" \"iotField\":\"Status\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
"]";
return jsonStr;
}
}
......@@ -35,40 +35,44 @@ public class InitHander implements CommandLineRunner {
String jsonStr = FlieUrlUtil.readJsonField(file);
//String jsonStr = strJson();//模拟数据
List<JsonDataDto> jsonDataDtoList = new ArrayList<>();
try{
List<JSONObject> list = JSONObject.parseObject(JSONArray.parseArray(jsonStr).toString(), List.class);
list.forEach(j->{
JsonDataDto jsonDataDto = new JsonDataDto();
List<JSONObject> mongdbTableList= JSONObject.parseObject(j.getJSONObject("reported").getJSONArray("singleField").toString(), List.class);
jsonDataDto.setCmdId(j.get("cmdId").toString());
jsonDataDto.setMongdbTableb(j.get("mongdbTableb").toString());
jsonDataDto.setSequenceId(j.get("sequenceId").toString());
jsonDataDto.setProductKey(j.get("productKey").toString());
jsonDataDto.setSkurl(j.get("skurl").toString());
jsonDataDto.setTxtFileUrl(j.get("txtFileUrl").toString());
jsonDataDto.setClientUuid(j.get("clientUuid").toString());
Map<String,Map<String,Object>> tableFiels = new HashMap<>();//存储对应的表和对应的字段
mongdbTableList.forEach(t->{
//处理字段
Map<String,Object> fields = new HashMap<>();//存储需要查询的表字段
String subTable = t.get("subTable").toString();//拿到表名
List<JSONObject> feildslist = JSONObject.parseObject(t.getJSONArray("feilds").toString(), List.class);
feildslist.forEach(f->{
//取出对应的字段和属性
String dbField = f.get("dbField").toString();//查询的表字段
String iotField = f.get("iotField").toString();//给iot 的字段
String fieldDataType = f.get("fieldDataType").toString(); //给iot的属性
/*Map<String,Object> objectMap = new HashMap<>();
objectMap.put(dbField,iotField);*/
fields.put(dbField,iotField);
List<JSONObject> list = JSONObject.parseObject(JSONArray.parseArray(jsonStr).toString(), List.class);
list.forEach(j->{
JsonDataDto jsonDataDto = new JsonDataDto();
List<JSONObject> mongdbTableList= JSONObject.parseObject(j.getJSONObject("reported").getJSONArray("singleField").toString(), List.class);
jsonDataDto.setCmdId(j.get("cmdId").toString());
jsonDataDto.setMongdbTableb(j.get("mongdbTableb").toString());
jsonDataDto.setSequenceId(j.get("sequenceId").toString());
jsonDataDto.setProductKey(j.get("productKey").toString());
jsonDataDto.setSkurl(j.get("skurl").toString());
jsonDataDto.setTxtFileUrl(j.get("txtFileUrl").toString());
jsonDataDto.setClientUuid(j.get("clientUuid").toString());
Map<String,Map<String,Object>> tableFiels = new HashMap<>();//存储对应的表和对应的字段
mongdbTableList.forEach(t->{
//处理字段
Map<String,Object> fields = new HashMap<>();//存储需要查询的表字段
String subTable = t.get("subTable").toString();//拿到表名
List<JSONObject> feildslist = JSONObject.parseObject(t.getJSONArray("feilds").toString(), List.class);
feildslist.forEach(f->{
//取出对应的字段和属性
String dbField = f.get("dbField").toString();//查询的表字段
String iotField = f.get("iotField").toString();//给iot 的字段
String fieldDataType = f.get("fieldDataType").toString(); //给iot的属性
/*Map<String,Object> objectMap = new HashMap<>();
objectMap.put(dbField,iotField);*/
fields.put(dbField,iotField);
});
tableFiels.put(subTable,fields);
jsonDataDto.setTableFiels(tableFiels);
});
tableFiels.put(subTable,fields);
jsonDataDto.setTableFiels(tableFiels);
jsonDataDtoList.add(jsonDataDto);
//查询数据
});
jsonDataDtoList.add(jsonDataDto);
//查询数据
});
}catch (Exception e){
return jsonDataDtoList;
}
return jsonDataDtoList;
}
......
......@@ -9,7 +9,8 @@ import java.util.Map;
@Data
public class JsonDataDto {
@JsonProperty("cmdId")
private String cmdId;
/**
* sk 路徑
*/
......@@ -29,8 +30,7 @@ public class JsonDataDto {
@JsonProperty("mongdbTableb")
private String mongdbTableb;
@JsonProperty("cmdId")
private String cmdId;
@JsonProperty("productKey")
private String productKey;
......
package com.jln.system.web.action.mqtt.pojo.vo;
public class SendJsonDataVo {
}
设备ID=5003820063
公司编码=2R00
工厂编码=2R01
设备名称=平台车架底漆自动调漆设备1
设备类型=S0608
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论