工业级运维app手机api
xufenglei
2017-10-27 3e6d73b90033d94fb8cb59e3a0958853177410e7
设备单项本月平均值、上一天AQI指标,单项标准值接口
设备状态接口
7 files modified
178 ■■■■ changed files
src/main/java/com/moral/monitor/controller/ScreenController.java 19 ●●●●● patch | view | raw | blame | history
src/main/java/com/moral/monitor/dao/HistoryEntityMapper.java 7 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/monitor/service/ScreenService.java 4 ●●● patch | view | raw | blame | history
src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java 108 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/monitor/util/ResourceUtil.java 11 ●●●●● patch | view | raw | blame | history
src/main/resources/mapper/HistoryEntityMapper.xml 20 ●●●●● patch | view | raw | blame | history
src/main/resources/sysConfig.properties 9 ●●●● patch | view | raw | blame | history
src/main/java/com/moral/monitor/controller/ScreenController.java
@@ -66,17 +66,24 @@
        return resultMap;
    }
    
    @RequestMapping(value = "/month-average", method = RequestMethod.GET)
    public Map<String, Object> getDataByEquipmentMac(HttpServletRequest request) {
    @RequestMapping(value = "/month", method = RequestMethod.GET)
    public Map<String, Object> getMonthDataByEquipment(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
        Map<String, Object> resu =  screenService.getDataByEquipmentMac(parameters);
        return resu;
        Map<String, Object> result =  screenService.getMonthDataByEquipment(parameters);
        return result;
    }
    @RequestMapping(value = "/all-average", method = RequestMethod.GET)
    public Map<String, Object> getAverageByAll(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
        Map<String, Object> resu =  screenService.getAverageByAll(parameters);
        return resu;
        Map<String, Object> result =  screenService.getAverageByAll(parameters);
        return result;
    }
    @RequestMapping(value = "/equipment-state", method = RequestMethod.GET)
    public Map<String, Object> getEquipmentStates(HttpServletRequest request) {
        Map<String, Object> parameters = WebUtils.getParametersStartingWith(request,null);
        Map<String, Object> result =  screenService.getEquipmentStates(parameters);
        return result;
    }
}
src/main/java/com/moral/monitor/dao/HistoryEntityMapper.java
@@ -7,7 +7,10 @@
import com.moral.monitor.entity.HistoryEntityExample;
public interface HistoryEntityMapper extends BaseDao<HistoryEntity, HistoryEntityExample, Integer>{
    Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters);
    List<Map<String, Object>> getAverageByAll(Map<String, Object> parameters);
    Map<String, Object> getMonthAverageBySensor(Map<String, Object> parameters);
    List<Map<String, Object>> getAverageByAll(Map<String, Object> parameters);
    List<Map<String, Object>> getEquipmentStates(Map<String, Object> parameters);
}
src/main/java/com/moral/monitor/service/ScreenService.java
@@ -9,8 +9,10 @@
    List<AccountEntity> getAccountLists(String account, String password);
    Map<String, Object> getDataByEquipmentMac(Map<String, Object> parameters);
    Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters);
    Map<String, Object> getAverageByAll(Map<String, Object> parameters);
    Map<String, Object> getEquipmentStates(Map<String, Object> parameters);
}
src/main/java/com/moral/monitor/service/impl/ScreenServiceImpl.java
@@ -1,8 +1,11 @@
package com.moral.monitor.service.impl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -18,6 +21,7 @@
import com.moral.monitor.entity.AccountEntityExample;
import com.moral.monitor.service.OrganizationService;
import com.moral.monitor.service.ScreenService;
import com.moral.monitor.util.ResourceUtil;
@Service
public class ScreenServiceImpl implements ScreenService {
@@ -38,7 +42,7 @@
    }
    @SuppressWarnings("deprecation")
    public Map<String, Object> getDataByEquipmentMac(Map<String, Object> parameters) {
    public Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Date date = new Date();
@@ -65,25 +69,63 @@
        parameters.put("start", DateUtils.truncate(date, Calendar.MONTH));
        parameters.put("mac", "898602b8191630065884");
        parameters.put("macKey", "e1");
        Map<String, Object> monthAverageBySensor = historyMapper.getMonthAverageBySensor(parameters);
        if (MapUtils.isNotEmpty(monthAverageBySensor)) {
            resultMap.putAll(monthAverageBySensor);
        Map<String, Object> average = historyMapper.getMonthAverageBySensor(parameters);
        if (MapUtils.isNotEmpty(average)) {
            resultMap.putAll(average);
        } else {
            resultMap.put("monthAverage", 0);
            resultMap.put("average", 0);
        }
        //
        String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
        List<Double> IAQIs = new ArrayList<Double>();
        for (String macKey : macKeys) {
            double avg = 0, maxMacKey = 0, minMacKey = 0;
            parameters.put("macKey", macKey);
            parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
            average = historyMapper.getMonthAverageBySensor(parameters);
            if (MapUtils.isNotEmpty(average)) {
                avg = (Double) average.get("average");
            }
            String[] macKeyValues = ResourceUtil.getArrValue(macKey);
            int index = -1;
            for (int i = 0; i < macKeyValues.length; i++) {
                Double macKeyValue = Double.valueOf(macKeyValues[i]);
                if (avg <= macKeyValue) {
                    if (i == 0) {
                        index = 0;
                        minMacKey = macKeyValue;
                        maxMacKey = Double.valueOf(macKeyValues[i + 1]);
                    } else {
                        index = i - 1;
                        minMacKey = Double.valueOf(macKeyValues[i - 1]);
                        maxMacKey = macKeyValue;
                    }
                    continue;
                }
            }
            if (index == -1) {
                resultMap.put("AQI", ">500");
                break;
            }
            String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
            double minIAQI = Double.valueOf(IAQIValues[index]);
            double maxIAQI = Double.valueOf(IAQIValues[index + 1]);
            IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
        }
        resultMap.put("standard", 35);
        if (!resultMap.containsKey("AQI")) {
            resultMap.put("AQI", Collections.max(IAQIs));
        }
        return resultMap;
    }
    @SuppressWarnings("deprecation")
    public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
        AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf((String) parameters.get("accountId"))));
        String organization = account.getOrganization();
        //不是摩瑞尔账号的需要根据组织来获取数据权限
        if (!("-1".equals(organization) || "5212b9dfb55448e699889e01fa0fa6a2".equals(organization))) {
            Set<String> orgIds = organizationService.getChildOrganizationIds(account.getOrganization());
            parameters.put("orgIds", orgIds);
        }
        setOrgIdsByAccount(parameters);
        Date date = new Date();
        
        /** 临时代码 以保证查询有数据 **/
@@ -98,11 +140,51 @@
        parameters.put("start", DateUtils.addMinutes(date, -10));
        parameters.put("end", DateUtils.addMinutes(date, -5));
        List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
        Map<String, Object> result = new HashMap<String, Object>();
        for (Map<String, Object> map : averageByAll) {
            result.put((String) map.get("mac_key"), map.get("avg"));
        }
        return result;
    }
    public void setOrgIdsByAccount(Map<String, Object> parameters) {
        AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf((String) parameters.get("accountId"))));
        String organization = account.getOrganization();
        //不是摩瑞尔账号的需要根据组织来获取数据权限
        if (!("-1".equals(organization) || "5212b9dfb55448e699889e01fa0fa6a2".equals(organization))) {
            Set<String> orgIds = organizationService.getChildOrganizationIds(account.getOrganization());
            parameters.put("orgIds", orgIds);
        }
    }
    @Override
    public Map<String, Object> getEquipmentStates(Map<String, Object> parameters) {
        Map<String, Object> result = new HashMap<String, Object>();
        setOrgIdsByAccount(parameters);
        List<Map<String, Object>> list = historyMapper.getEquipmentStates(parameters);
        Long all = 0L, normal = 0L, abnormal = 0L, stop = 0L;
        for (Map<String, Object> map : list) {
            Long count = (Long) map.get("count");
            all += count;
            switch ((Integer) map.get("state")) {
            case 0:
                normal = count;
                break;
            case 4:
                stop = count;
                break;
            default:
                abnormal += count;
            }
        }
        result.put("all", all);
        result.put("normal", normal);
        result.put("abnormal", abnormal);
        result.put("stop", stop);
        return result;
    }
    private double calculateIAQI(double maxIAQI, double minIAQI, double maxMacKey, double minMacKey,double avg) {
        return (maxIAQI - minIAQI) * (avg - minMacKey) / (maxMacKey - minMacKey) + minIAQI;
    }
}
src/main/java/com/moral/monitor/util/ResourceUtil.java
@@ -24,4 +24,15 @@
    public static String getRandCodeType() {
        return bundle.getString("randCodeType");
    }
    public static String getValue(String key){
        return bundle.getString(key);
    }
    public static String[] getArrValue(String key){
        String string = bundle.getString(key);
        return string.split(",");
    }
}
src/main/resources/mapper/HistoryEntityMapper.xml
@@ -227,7 +227,7 @@
  </update>
  <select id="getMonthAverageBySensor" resultType="map">
        SELECT
            AVG(mac_value) monthAverage
            AVG(mac_value) average
        FROM
            history
        WHERE
@@ -288,4 +288,22 @@
        GROUP BY
            mac_key
    </select>
    <select id="getEquipmentStates" resultType="map">
        SELECT
            COUNT(state) count,
            state
        FROM
            equipment
        <if test="orgIds != null and orgIds.size > 0">
            JOIN org_equ oe ON e.id = oe.equid
            AND oe.orgid IN
            <foreach collection="orgIds" item="listItem" open="(" separator="," close=")" >
                #{listItem}
            </foreach>
        </if>
        GROUP BY
            state
    </select>
</mapper>
src/main/resources/sysConfig.properties
@@ -1,2 +1,9 @@
randCodeLength=4
randCodeType=5
randCodeType=5
e1=0,35,75,115,150,250,350,500
e2=0,50,150,250,350,420,500,600
e10=0,2,4,14,24,36,48,60
e11=0,50,150,475,800,1600,2100,2620
e15=0,100,160,215,265,800,1000,1200
e16=0,40,80,180,280,565,750,940
IAQI=0,50,100,150,200,300,400,500