| | |
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import com.moral.common.util.ReportTimeFormat;
|
| | | import com.moral.entity.charts.DataCondition;
|
| | | import com.moral.entity.charts.LineChartCriteria;
|
| | | import com.moral.entity.charts.PairData;
|
| | | import com.moral.entity.charts.TimePeriod;
|
| | | import org.apache.commons.lang3.time.DateUtils;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.util.ObjectUtils;
|
| | |
| | | private SensorMapper sensorMapper;
|
| | |
|
| | | // volatile
|
| | | private Set<String> sensorKeys = new HashSet<String>();
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) {
|
| | |
| | | Map<String, List<Object>> resultMap = new HashMap<String, List<Object>>();
|
| | | List<Map<String, Object>> list = JSON.parseObject((String)parameters.get("items"), new TypeReference<List<Map<String, Object>>>() {});
|
| | | parameters.remove("items");
|
| | | Map<String, Object> timeType = JSON.parseObject((String)parameters.get("timeTypes"), new TypeReference<Map<String, Object>>() {});
|
| | | parameters.remove("timeTypes");
|
| | | parameters.putAll(timeType);
|
| | | parameters.put("type", parameters.get("value"));
|
| | | parameters.remove("value");
|
| | | |
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | | CompletionService<Map<String, List<Object>>> cs = new ExecutorCompletionService<Map<String, List<Object>>>(threadPool);
|
| | |
|
| | |
| | | dataList.add(cs.take().get());
|
| | | }
|
| | | Map[] maps = new HashMap[list.size()];
|
| | | Set<String> sensors = new TreeSet<String>(new Comparator<String>() {
|
| | | @Override
|
| | | public int compare(String o1, String o2) {
|
| | | return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
|
| | | }
|
| | | });
|
| | | for (Object object : dataList) {
|
| | | Map<String, Object> map = (Map<String, Object>)object;
|
| | | for (String key : map.keySet()) {
|
| | | int index = Integer.valueOf(key.replace("data", ""));
|
| | | maps[index] = map;
|
| | | if (key.startsWith("data")) {
|
| | | int index = Integer.valueOf(key.replace("data", ""));
|
| | | maps[index] = map;
|
| | | }
|
| | | if (key.startsWith("sensors")) {
|
| | | sensors.addAll((List<String>) map.get(key));
|
| | | }
|
| | | }
|
| | | }
|
| | | resultMap.put("time", timeList);
|
| | | resultMap.put("data", Arrays.asList(maps));
|
| | | resultMap.put("times", timeList);
|
| | | resultMap.put("datas", Arrays.asList(maps));
|
| | | resultMap.put("sensors", new ArrayList<Object>(sensors));
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | |
| | | List<Object> timeList = (List<Object>) parameters.get("timeList");
|
| | |
|
| | | List<Object> dataList = new ArrayList<Object>();
|
| | | Set<String> sensors ;
|
| | | if (parameters.containsKey("sensorKey")) {
|
| | | String sensorKey = (String) parameters.get("sensorKey");
|
| | | Sensor sensor = new Sensor();
|
| | | sensor.setSensorKey(sensorKey);
|
| | | sensor = sensorMapper.selectOne(sensor );
|
| | | sensors = new HashSet<String>();
|
| | | sensors.add(sensorKey + "-" + sensor.getName() + "-" + sensor.getUnit());
|
| | | } else {
|
| | | sensors = new TreeSet<String>(new Comparator<String>() {
|
| | | @Override
|
| | | public int compare(String o1, String o2) {
|
| | | return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
|
| | | }
|
| | | });
|
| | | sensors.addAll((Set<String>)parameters.get("sensors"));
|
| | | }
|
| | | Set<String> sensors = new TreeSet<String>(new Comparator<String>() {
|
| | | @Override
|
| | | public int compare(String o1, String o2) {
|
| | | return Integer.compare(Integer.valueOf(o1.split("-")[0].replace("e", "")), Integer.valueOf(o2.split("-")[0].replace("e", "")));
|
| | | }
|
| | | });
|
| | | sensors.addAll((Set<String>)parameters.get("sensors"));
|
| | | Map<String, Double[]> doubleMap = new LinkedHashMap<String, Double[]>();
|
| | | for (Map<String, Object> map : resultList) {
|
| | | String time = map.get("time").toString();
|
| | |
| | | dataList.add(doubleMap);
|
| | | String part = parameters.get("part").toString();
|
| | | resultMap.put("data" + part, dataList);
|
| | | resultMap.put("sensors" + part, new ArrayList<Object>(sensors));
|
| | | return resultMap;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getMonitorPointOrDeviceAvgData(Map<String, Object> parameters) throws Exception {
|
| | | List<Map<String, Object>> resul ;
|
| | | List<Map<String, Object>> resultList ;
|
| | |
|
| | | String type = (String) parameters.get("type");
|
| | | String time = (String) parameters.get("time");
|
| | |
| | | parameters.put("end", end);
|
| | |
|
| | | if (parameters.containsKey("sensorKey")) {
|
| | | String sensorKey = (String) parameters.get("sensorKey");
|
| | | String sensorKeyColumn = "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
|
| | | List<String> sensors;
|
| | | try {
|
| | | sensors = JSON.parseObject((String)parameters.get("sensors"), new TypeReference<List<String>>() {});
|
| | | parameters.put("sensors", new HashSet<String>(sensors));
|
| | | } catch (Exception e) {
|
| | | sensors = new ArrayList<String>();
|
| | | sensors.add((String)parameters.get("sensorKey"));
|
| | | }
|
| | | String sensorKeyColumn = "";
|
| | | for (int i = 0; i < sensors.size(); i++) {
|
| | | String sensorKey = sensors.get(i).split("-")[0];
|
| | | sensorKeyColumn += "AVG(json -> '$." + sensorKey + "[0]') " + sensorKey;
|
| | | if (i != sensors.size() - 1) {
|
| | | sensorKeyColumn += " ,";
|
| | | } |
| | | |
| | | }
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | }
|
| | |
|
| | | sensorKeys.clear();
|
| | | //sensorKeys.clear();
|
| | | // 监控点平均值
|
| | | if (!parameters.containsKey("mac")) {
|
| | | resul = new ArrayList<Map<String,Object>>();
|
| | | resultList = new ArrayList<Map<String,Object>>();
|
| | | Integer monitorPointId = Integer.valueOf(parameters.get("monitorPoint").toString());
|
| | | List<Map<String, Object>> deviceVersions = deviceMapper.getDeviceVersionIdByMonitorPoint(monitorPointId);
|
| | | ExecutorService threadPool = Executors.newCachedThreadPool();
|
| | |
| | | Integer deviceVersionId = (Integer) map.get("deviceVersionId");
|
| | | if (!parameter.containsKey("sensorKeyColumn")) {
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(deviceVersionId);
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors);
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors, parameters);
|
| | | parameter.put("sensorKeyColumn", sensorKeyColumn);
|
| | | parameters.put("sensors", sensorKeys);
|
| | | }
|
| | | List<String> macs = deviceMapper.getDeviceMacByMonitorPointAndDeviceVersion(monitorPointId, deviceVersionId);
|
| | | if (ObjectUtils.isEmpty(macs) || macs.contains("null") ) {
|
| | |
| | | for (String sensorKey : value.keySet()) {
|
| | | resultMap.put(sensorKey, value.get(sensorKey)[2]);
|
| | | }
|
| | | resul.add(resultMap);
|
| | | resultList.add(resultMap);
|
| | | }
|
| | | // 设备 平均值
|
| | | } else {
|
| | |
| | | device.setMac(mac);
|
| | | device = deviceMapper.selectOne(device);
|
| | | List<Map<String, Object>> sensors = sensorMapper.getSensorsByDeviceVersionId(device.getDeviceVersionId());
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors);
|
| | | String sensorKeyColumn = getSensorKeyColumnBySensors(sensors,parameters);
|
| | | parameters.put("sensorKeyColumn", sensorKeyColumn);
|
| | | parameters.put("sensors", sensorKeys);
|
| | |
|
| | | }
|
| | | resul = historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
|
| | | resultList = historyMinutelyMapper.getMonitorPointOrDeviceAvgData(parameters);
|
| | | }
|
| | | return resul;
|
| | | return resultList;
|
| | | }
|
| | |
|
| | | private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors) {
|
| | | private String getSensorKeyColumnBySensors(List<Map<String, Object>> sensors,Map<String, Object> parameters) {
|
| | | String sensorKeyColumn = "";
|
| | | |
| | | Set<String> sensorKeys = new HashSet<String>();
|
| | | if (parameters.containsKey("sensors")) {
|
| | | sensorKeys = (Set<String>) parameters.get("sensors");
|
| | | }
|
| | | for (int i = 0; i < sensors.size(); i++) {
|
| | | String sensorKey = (String) sensors.get(i).get("sensor_key");
|
| | | String name = (String) sensors.get(i).get("name");
|
| | |
| | | sensorKeyColumn += " ,";
|
| | | }
|
| | | sensorKeys.add(sensorKey + "-" + name + "-" + unit );
|
| | | parameters.put("sensors", sensorKeys);
|
| | | }
|
| | | return sensorKeyColumn;
|
| | | }
|
| | |
| | | return result;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据线性表单的条件规则,获取多条线性表单数据
|
| | | * @param lineChartCriteria
|
| | | * @return
|
| | | */
|
| | | @Override
|
| | | public Map<String, List<List<PairData>>> queryLineChartDateByCrieria(LineChartCriteria lineChartCriteria){
|
| | | Map<String,List<List<PairData>>> listMap = new HashMap<>();
|
| | | for(String sensorKey:lineChartCriteria.getSensorKeys()){
|
| | | TimePeriod timePeriod = lineChartCriteria.getTimePeriod();
|
| | | List<DataCondition> dataConditionList = lineChartCriteria.getDataConditions();
|
| | | List<List<PairData>> list = new ArrayList<>();
|
| | | dataConditionList.forEach(item -> {
|
| | | List<PairData> pairDataList = queryOneLineChartDateByCrieria(sensorKey,timePeriod,item);
|
| | | list.add(pairDataList);
|
| | | });
|
| | | listMap.put(sensorKey,list);
|
| | | }
|
| | |
|
| | | return listMap;
|
| | | }
|
| | |
|
| | | /**
|
| | | * 根据线性表单的条件规则,获取一条线性表单数据
|
| | | * @param sensorKey
|
| | | * @param timePeriod
|
| | | * @param dataCondition
|
| | | * @return
|
| | | */
|
| | | public List<PairData> queryOneLineChartDateByCrieria(String sensorKey, TimePeriod timePeriod, DataCondition dataCondition){
|
| | | List<String> timeList = ReportTimeFormat.makeTimeList(timePeriod);
|
| | | List<PairData> lineChartDatas = historyMinutelyMapper.selectLineChartDateByCrieria(sensorKey,timePeriod,dataCondition);
|
| | | List<PairData> lineChartDatasWithEmpty = new ArrayList<>();
|
| | | int m = 0;
|
| | | int dataLength = lineChartDatas.size()-1;
|
| | | m = dataLength>-1?0:-1;
|
| | | if(m>-1){
|
| | | for(int n =0;n<timeList.size();n++){
|
| | | String time = timeList.get(n);
|
| | | if(m>-1){
|
| | | PairData pairData = lineChartDatas.get(m);
|
| | | String keyTime = pairData.getCategory();
|
| | | if(time.equals(keyTime)){
|
| | | lineChartDatasWithEmpty.add(pairData);
|
| | | m = m<dataLength ? m+1 : -1;
|
| | | }else{
|
| | | lineChartDatasWithEmpty.add(generateEmptyData(time));
|
| | | }
|
| | | }else {
|
| | | lineChartDatasWithEmpty.add(generateEmptyData(time));
|
| | | }
|
| | |
|
| | | }
|
| | | }else{
|
| | | fillEmptyDataToList(timeList,lineChartDatasWithEmpty);
|
| | | }
|
| | | return lineChartDatasWithEmpty;
|
| | | }
|
| | | private PairData generateEmptyData(String time) {
|
| | | PairData pairData = new PairData();
|
| | | pairData.setCategory(time);
|
| | | pairData.setValue(null);
|
| | | return pairData;
|
| | | }
|
| | | private void fillEmptyDataToList(List<String> timeList,List<PairData> lineChartDatasWithEmpty){
|
| | | timeList.forEach( time ->{
|
| | | PairData pairData = generateEmptyData(time);
|
| | | lineChartDatasWithEmpty.add(pairData);
|
| | | });
|
| | | }
|
| | | }
|