Commit f9a37633 by dxh

["暂无"]:iot json参数,参数为空赋值为空字符

parent d55bfcb2
...@@ -2,15 +2,7 @@ ...@@ -2,15 +2,7 @@
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager"> <component name="FacetManager">
<facet type="Spring" name="Spring"> <facet type="Spring" name="Spring">
<configuration> <configuration />
<fileset id="fileset" name="Spring Application Context" removed="false">
<file>file://$MODULE_DIR$/src/main/java/com/jln/common/config/CommonConfig.java</file>
<file>file://$MODULE_DIR$/src/main/java/com/jln/common/RedisUtil/RedisConfig.java</file>
<file>file://$MODULE_DIR$/src/main/java/com/jln/common/config/GlobalConfig.java</file>
<file>file://$MODULE_DIR$/src/main/java/com/jln/common/config/WebDataConvertConfig.java</file>
<file>file://$MODULE_DIR$/src/main/java/com/jln/common/config/DateConfig.java</file>
</fileset>
</configuration>
</facet> </facet>
<facet type="web" name="Web"> <facet type="web" name="Web">
<configuration> <configuration>
......
package com.jln.system.web.action.mqtt; package com.jln.system.web.action.mqtt;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.JsonObject;
import com.jln.common.utils.EmptyUtil; import com.jln.common.utils.EmptyUtil;
import com.jln.system.web.action.mqtt.pojo.dto.JsonDataDto;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
...@@ -27,62 +33,38 @@ public class Base64decrypt { ...@@ -27,62 +33,38 @@ public class Base64decrypt {
@Value("${sleepTime}") @Value("${sleepTime}")
private Long sleepTime; private Long sleepTime;
private Map<String,Object> findMongo( Map<String,Set<Map<String,Object>>> tableFiels ) {
private Map<String,Object> findMongo( Map<String,Map<String,Object>> tableFiels ) {
DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyyMM"); DateTimeFormatter fmDate = DateTimeFormatter.ofPattern("yyyyMM");
String yearMonth = LocalDate.now().format(fmDate); String yearMonth = LocalDate.now().format(fmDate);
Map<String,Object> map = null; Map<String,Object> map = new HashMap<>();
for (Map.Entry<String, Set<Map<String, Object>>> stringSetEntry : tableFiels.entrySet()) {
for (Map.Entry<String,Map<String,Object>> entry : tableFiels.entrySet()) {
//System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
//新建一个query对象
Query query = new Query(); Query query = new Query();
for (Map.Entry<String,Object> fields : entry.getValue().entrySet()){ Map<String,String> mapper = new HashMap<>();
query.fields().include(fields.getKey()); for (Map<String, Object> stringStringMap : stringSetEntry.getValue()) {
for (String s : stringStringMap.keySet()) {
query.fields().include(s);
mapper.put(s,stringStringMap.get(s).toString());
}
} }
query.fields().include("time"); query.fields().include("time");
query.limit(1); query.limit(1);
query.with(Sort.by( query.with(Sort.by(
Sort.Order.desc("time") Sort.Order.desc("time")
)); ));
List<JSONObject> objectList = mongoTemplate.find(query, JSONObject.class, entry.getKey()+yearMonth);
List<JSONObject> objectList = mongoTemplate.find(query, JSONObject.class, stringSetEntry.getKey()+yearMonth);
//mongdb最新数据时间 //mongdb最新数据时间
Map<String ,Object> map1 = objectList.get(0).toJavaObject(Map.class); Map<String ,Object> map1 = objectList.get(0).toJavaObject(Map.class);
for (Map.Entry<String, Object> stringObjectEntry : map1.entrySet()) {
Set<Map.Entry<String, Object>> entries = entry.getValue().entrySet();//key 是 数据库字段,value 是iot map.put(mapper.get(stringObjectEntry.getKey()),stringObjectEntry.getValue());
//处理上报的字段
Long time = Long.valueOf(map1.get("time").toString());
//当前时间
long millis = Calendar.getInstance().getTimeInMillis();
//如果当前时间-最新上报数据的时间<设置关机范围,那么直接给
if ((millis - time) < jugeTime && !ObjectUtils.isEmpty(objectList)) {
JSONObject jsonObject = objectList.get(0);
if(!EmptyUtil.isEmpty(map)){
jsonObject.toJavaObject(Map.class);
map.putAll(jsonObject.toJavaObject(Map.class));
}else{
map = jsonObject.toJavaObject(Map.class);
}
}
for (Map.Entry<String,Object> fields : entry.getValue().entrySet()){//
boolean contains = map.containsKey(fields.getKey());
if(contains){
Object valeDb = map.get(fields.getKey());
map.remove(fields.getKey()) ;
map.put(fields.getValue().toString(),valeDb);
}
} }
} }
map.remove(null);
return map; return map;
} }
private Map findMongo1(String deviceId,int start,int end) { /* private Map findMongo1(String deviceId,int start,int end) {
int i = (int)Math.ceil((double) start/50); int i = (int)Math.ceil((double) start/50);
int j =(int) Math.ceil((double) end/50); int j =(int) Math.ceil((double) end/50);
...@@ -133,43 +115,65 @@ public class Base64decrypt { ...@@ -133,43 +115,65 @@ public class Base64decrypt {
} }
} }
return map; return map;
} }*/
/** /**
* 机器人测试 * 机器人测试
*/ */
public Map<String, Object> jqrSendString( Map<String,Map<String,Object>> map,Map<String, Object> objectMap,long millis, public Map<String, Object> jqrSendString(JsonDataDto jsonDataDto, Map<String, Object> objectMap, long millis, Map<String, Object> reportDto) {
Map<String, Object> reportDto) { Map<String, Object> mongoMap = findMongo(jsonDataDto.getTableFiels());
Map<String, Object> mongoMap = findMongo(map);
mongoMap.putAll(objectMap); mongoMap.putAll(objectMap);
mongoMap.remove("_id");
mongoMap.remove("time");
millis = Calendar.getInstance().getTimeInMillis(); millis = Calendar.getInstance().getTimeInMillis();
//处理实体类在每个字段加上时间 //处理实体类在每个字段加上时间
List<Map<String, Object>> allDto = new ArrayList<>(); List<Map<String, Object>> allDto = new ArrayList<>();
for (Map.Entry<String,Object> entry : mongoMap.entrySet()) { Map<String,Object> sites = new HashMap<>();
Map<String, Object> sites = new HashMap<>(); for (Map.Entry<String, Object> stringObjectEntry : objectMap.entrySet()) {
Map<String,Object> properites = new HashMap<>();
try { properites.put(stringObjectEntry.getKey(),stringObjectEntry.getValue());
sites.put(entry.getKey(), entry.getValue()); properites.put("ts",millis);
sites.put("ts", millis); allDto.add(properites);
allDto.add(sites); }
} catch (Exception e) { try{
System.out.println(e); Map<String,Object> treeMap = new HashMap<>();
Map<String, Object> site = new HashMap<>(); for (Map<String, Object> stringObjectMap : jsonDataDto.getTree()) {
site.put("Status", "2"); //循环key
site.put("ts", millis); Set<Map.Entry<String, Object>> entrySet = stringObjectMap.entrySet();
allDto.add(site); for (Map.Entry<String, Object> stringObjectEntry : entrySet) {
reportDto.put("reported", allDto); String value = JSON.toJSONString(stringObjectEntry.getValue());
reportDto.put("edgeTime", millis); try{
return reportDto; JSONObject jsonObject = JSONObject.parseObject(value);
Map<String,Object> keyValue = new HashMap<>();
for (Map.Entry<String, Object> objectEntry : jsonObject.entrySet()) {
keyValue.put(String.valueOf(objectEntry.getValue()),mongoMap.get(objectEntry.getValue()));
}
treeMap.put(stringObjectEntry.getKey(),keyValue);
}catch (Exception e){
Map<String,Object> keyValue = new HashMap<>();
keyValue.put(String.valueOf(stringObjectEntry.getValue()),mongoMap.get(stringObjectEntry.getValue()));
keyValue.put("ts",millis);
allDto.add(keyValue);
}
}
} }
allDto.add(treeMap);
}catch (Exception e){
System.out.println(e);
Map<String, Object> site = new HashMap<>();
site.put("Status", "2");
site.put("ts", millis);
allDto.add(site);
reportDto.put("reported", allDto);
reportDto.put("edgeTime", millis);
return reportDto;
} }
reportDto.put("reported", allDto); reportDto.put("reported", allDto);
JSONObject jsonObj=new JSONObject(reportDto); JSONObject jsonObj=new JSONObject(reportDto);
String s = jsonObj.toString(); String s =jsonObj.toString();
System.out.println("輸出Json:"+s); String str = JSONObject.toJSONString(jsonObj, SerializerFeature.WriteMapNullValue);
System.out.println("輸出Json:"+str);
return reportDto; return reportDto;
} }
} }
package com.jln.system.web.action.mqtt; package com.jln.system.web.action.mqtt;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.jln.system.web.action.mqtt.pojo.dto.*; import com.jln.system.web.action.mqtt.pojo.dto.*;
import com.jln.system.web.action.mqtt.pojo.vo.StaticData; import com.jln.system.web.action.mqtt.pojo.vo.StaticData;
...@@ -186,19 +187,19 @@ public class MqttStartClient { ...@@ -186,19 +187,19 @@ public class MqttStartClient {
//1 redis和mqtt分开 //1 redis和mqtt分开
if (separate == 1) { if (separate == 1) {
//TODO 同时MQTT上报和推送前端数据 //TODO 同时MQTT上报和推送前端数据
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites, millis ,reportDto); Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto,sites, millis ,reportDto);
StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap); StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap);
String json = new Gson().toJson(objectMap); String str = JSONObject.toJSONString(objectMap, SerializerFeature.WriteMapNullValue);
xbMqttDataUtil.publish( json); xbMqttDataUtil.publish( str);
} else if (separate == 2) { } else if (separate == 2) {
// TODO 只推送前端数据 // TODO 只推送前端数据
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,millis,reportDto); Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto,sites,millis,reportDto);
StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap); StaticData.data.put(jsonDataDto.getMongdbTableb(), objectMap);
} else if (separate == 3) { } else if (separate == 3) {
//TODO 只发送mqtt //TODO 只发送mqtt
Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto.getTableFiels(),sites,millis,reportDto); Map<String, Object> objectMap = base64decrypt.jqrSendString( jsonDataDto,sites,millis,reportDto);
String json = new Gson().toJson(objectMap); String str = JSONObject.toJSONString(objectMap, SerializerFeature.WriteMapNullValue);
xbMqttDataUtil.publish(json); xbMqttDataUtil.publish(str);
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -13,10 +13,7 @@ import org.springframework.stereotype.Component; ...@@ -13,10 +13,7 @@ import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@Component //@Component
public class InitHander implements CommandLineRunner { public class InitHander implements CommandLineRunner {
...@@ -36,8 +33,7 @@ public class InitHander implements CommandLineRunner { ...@@ -36,8 +33,7 @@ public class InitHander implements CommandLineRunner {
String jsonStr = FlieUrlUtil.readJsonField(file); String jsonStr = FlieUrlUtil.readJsonField(file);
//String jsonStr = strJson();//模拟数据 //String jsonStr = strJson();//模拟数据
List<JsonDataDto> jsonDataDtoList = new ArrayList<>(); List<JsonDataDto> jsonDataDtoList = new ArrayList<>();
try{ List<Map<String,Object>> tree = new ArrayList<>();
List<JSONObject> list = JSONObject.parseObject(JSONArray.parseArray(jsonStr).toString(), List.class); List<JSONObject> list = JSONObject.parseObject(JSONArray.parseArray(jsonStr).toString(), List.class);
list.forEach(j->{ list.forEach(j->{
JsonDataDto jsonDataDto = new JsonDataDto(); JsonDataDto jsonDataDto = new JsonDataDto();
...@@ -49,32 +45,68 @@ public class InitHander implements CommandLineRunner { ...@@ -49,32 +45,68 @@ public class InitHander implements CommandLineRunner {
jsonDataDto.setSkurl(j.get("skurl").toString()); jsonDataDto.setSkurl(j.get("skurl").toString());
jsonDataDto.setTxtFileUrl(j.get("txtFileUrl").toString()); jsonDataDto.setTxtFileUrl(j.get("txtFileUrl").toString());
jsonDataDto.setClientUuid(j.get("clientUuid").toString()); jsonDataDto.setClientUuid(j.get("clientUuid").toString());
Map<String,Map<String,Object>> tableFiels = new HashMap<>();//存储对应的表和对应的字段
Map<String,Set<Map<String,Object>>> tableMap = new HashMap<>();//存储表名、对象
mongdbTableList.forEach(t->{ mongdbTableList.forEach(t->{
//处理字段 //处理字段
Map<String,Object> fields = new HashMap<>();//存储需要查询的表字段 List<JSONObject> feildslist = JSONObject.parseObject(String.valueOf(t.getJSONArray("feilds")), List.class);
String subTable = t.get("subTable").toString();//拿到表名 if(!EmptyUtil.isEmpty(feildslist)){
List<JSONObject> feildslist = JSONObject.parseObject(t.getJSONArray("feilds").toString(), List.class); for (JSONObject jsonObject : feildslist) {
feildslist.forEach(f->{ Map<String,Object> objectMap = new HashMap<>();
//取出对应的字段和属性 for (String key : jsonObject.keySet()) {
String dbField = f.get("dbField").toString();//查询的表字段
String iotField = f.get("iotField").toString();//给iot 的字段 Map<String,Object> fildsMap = new HashMap<>();//存储mongo字段、iot字段
String fieldDataType = f.get("fieldDataType").toString(); //给iot的属性 List<JSONObject> objectList = JSONObject.parseObject(jsonObject.get(key).toString(), List.class);
/*Map<String,Object> objectMap = new HashMap<>(); for (JSONObject object : objectList) {
objectMap.put(dbField,iotField);*/ String dbField = object.get("dbField").toString();//查询的表字段
fields.put(dbField, EmptyUtil.isEmpty(iotField) ? "" : iotField); String iotField = object.get("iotField").toString();//给iot 的字段
}); String tableName = object.get("subTable").toString();//表名
tableFiels.put(subTable,fields);
jsonDataDto.setTableFiels(tableFiels); fildsMap.put(dbField,iotField);
Map<String,Object> mapping = new HashMap<>();
mapping.put(dbField,iotField);
//根据表名判断对应的set集合是否存在不存在则新增set集合 set集合存放mongo字段和iot字段
if(EmptyUtil.isEmpty(tableMap.get(tableName))){
Set<Map<String,Object>> set = new LinkedHashSet<>();
set.add(mapping);
tableMap.put(tableName,set);
}else{
tableMap.get(tableName).add(mapping);
}
}
objectMap.put(key,fildsMap);
}
tree.add(objectMap);
}
}else{
String dbField = t.get("dbField").toString();//查询的表字段
String iotField = t.get("iotField").toString();//给iot 的字段
String tableName = t.get("subTable").toString();//表名
Map<String,Object> mapping = new HashMap<>();
mapping.put(dbField,iotField);
//根据表名判断对应的set集合是否存在不存在则新增set集合 set集合存放mongo字段和iot字段
if(EmptyUtil.isEmpty(tableMap.get(tableName))){
Set<Map<String,Object>> set = new LinkedHashSet<>();
set.add(mapping);
tableMap.put(tableName,set);
}else{
tableMap.get(tableName).add(mapping);
}
tree.add(mapping);
}
jsonDataDto.setTableFiels(tableMap);
jsonDataDto.setTree(tree);
}); });
jsonDataDtoList.add(jsonDataDto); jsonDataDtoList.add(jsonDataDto);
//查询数据 //查询数据
}); });
}catch (Exception e){
return jsonDataDtoList;
}
return jsonDataDtoList; return jsonDataDtoList;
} }
......
...@@ -3,9 +3,7 @@ package com.jln.system.web.action.mqtt.pojo.dto; ...@@ -3,9 +3,7 @@ package com.jln.system.web.action.mqtt.pojo.dto;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data; import lombok.Data;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
@Data @Data
public class JsonDataDto { public class JsonDataDto {
...@@ -47,5 +45,11 @@ public class JsonDataDto { ...@@ -47,5 +45,11 @@ public class JsonDataDto {
* 存放查询的表和字段 * 存放查询的表和字段
*/ */
@JsonProperty("tableFiels") @JsonProperty("tableFiels")
private Map<String, Map<String,Object>> tableFiels = new HashMap<>(); private Map<String, Set<Map<String,Object>>> tableFiels = new HashMap<>();
/**
* 存放查询的表和字段
*/
@JsonProperty("tree")
private List<Map<String,Object>> tree = new ArrayList<>();
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论