From a0d0ad8de7ac244aa29d1ca84d13b90e74729e33 Mon Sep 17 00:00:00 2001 From: kaiyu <404897439@qq.com> Date: Mon, 19 Oct 2020 13:16:04 +0800 Subject: [PATCH] 增加获取六参五分钟平均值接口 --- src/main/resources/mapper/HistoryFiveMinutelyMapper.xml | 12 + /dev/null | 173 ------------------- src/main/java/com/moral/controller/WebController.java | 125 +++++++++++++ src/main/java/com/moral/service/HistoryMinutelyService.java | 2 src/main/java/com/moral/mapper/HistoryFiveMinutelyMapper.java | 8 src/main/java/com/moral/util/DateUtil.java | 179 +++++++++++++++++++ src/main/java/com/moral/common/bean/ResultBean.java | 1 src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java | 26 +- src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java | 13 + 9 files changed, 347 insertions(+), 192 deletions(-) diff --git a/src/main/java/com/moral/common/bean/ResultBean.java b/src/main/java/com/moral/common/bean/ResultBean.java index d4391f4..1889300 100644 --- a/src/main/java/com/moral/common/bean/ResultBean.java +++ b/src/main/java/com/moral/common/bean/ResultBean.java @@ -12,6 +12,7 @@ public static final int FAIL = 0; public static final int SUCCESS = 1; public static final int NO_PERMISSION = 2; + private String message; private Integer code; private Integer errno; diff --git a/src/main/java/com/moral/controller/WebController.java b/src/main/java/com/moral/controller/WebController.java index 43a14aa..7b66415 100644 --- a/src/main/java/com/moral/controller/WebController.java +++ b/src/main/java/com/moral/controller/WebController.java @@ -10,20 +10,20 @@ import com.moral.entity.MonitorPoint; import com.moral.entity.Organization; import com.moral.service.*; +import com.moral.util.DateUtil; import com.moral.util.LatLngTransformation; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import jdk.nashorn.internal.runtime.logging.Logger; +import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.text.SimpleDateFormat; +import java.util.*; import java.util.stream.Collectors; import static com.moral.common.util.WebUtils.getParametersStartingWith; @@ -56,6 +56,13 @@ return "test success!"; } + /** + * @Description: ������������ + * @Param: [parameters] + * @return: java.util.Map<java.lang.String,java.lang.Object> + * @Author: ������������ + * @Date: 2020/10/19 + */ @PostMapping("login") public Map<String, Object> login(@RequestBody Map<String, Object> parameters) { Map<String, Object> resultMap = new HashMap<String, Object>(); @@ -73,6 +80,13 @@ return resultMap; } + /** + * @Description: ������������ + * @Param: [request] + * @return: java.util.Map<java.lang.String,java.lang.Object> + * @Author: ������������ + * @Date: 2020/10/19 + */ @UserLoginToken @PostMapping("logout") public Map<String, Object> logout(HttpServletRequest request) { @@ -84,6 +98,13 @@ return resultMap; } + /** + * @Description: + * @Param: [request] + * @return: java.util.Map<java.lang.String,java.lang.Object> + * @Author: ������������ + * @Date: 2020/10/19 + */ @UserLoginToken @GetMapping("getAccountInfo") public Map<String, Object> getAccountInfo(HttpServletRequest request) { @@ -141,6 +162,13 @@ } + /** + * @Description: ������������������������ + * @Param: [request] + * @return: com.moral.common.bean.ResultBean<java.util.List<java.util.Map<java.lang.String,java.lang.Object>>> + * @Author: ������������ + * @Date: 2020/10/19 + */ @UserLoginToken @GetMapping("report_avg_datas") public ResultBean<List<Map<String, Object>>> getMonitorPointOrDeviceAvgData(HttpServletRequest request) @@ -163,6 +191,13 @@ return new ResultBean<List<Map<String, Object>>>(list); } + /** + * @Description: ������������������������������ + * @Param: [request] + * @return: com.moral.common.bean.ResultBean<java.util.List<com.moral.entity.MonitorPoint>> + * @Author: ������������ + * @Date: 2020/10/19 + */ @UserLoginToken @GetMapping("monitorpoints-devices") public ResultBean<List<MonitorPoint>> getMonitorPointsAndDevicesByRegion(HttpServletRequest request) @@ -172,6 +207,13 @@ return new ResultBean<List<MonitorPoint>>(monitorPoints); } + /** + * @Description: ��������������������������������������� + * @Param: [request] + * @return: com.moral.common.bean.ResultBean<java.util.List<com.moral.entity.Device>> + * @Author: ������������ + * @Date: 2020/10/19 + */ /* @UserLoginToken*/ @GetMapping("coordinates") public ResultBean<List<Device>> getDeviceCoordinatesAndState(HttpServletRequest request){ @@ -179,4 +221,79 @@ ParameterUtils.getRegionType4RegionCode(parameters); return new ResultBean<List<Device>>(deviceService.queryDevice(parameters)); } + + /** + * @Description: ������monitorId��������������������������������������������������������������������� + * @Param: [] + * @return: com.moral.common.bean.ResultBean<java.util.Map<java.lang.String,java.lang.Object>> + * @Author: ������������ + * @Date: 2020/10/19 + */ + + @GetMapping("fiveMinuteAvgData") + public ResultBean<List<Map<String, Object>>> getSensorFiveMinuteAvgData(HttpServletRequest request){ + //���������������������������monitorpointId + Map<String, Object> parameters = WebUtils.getParametersStartingWith(request, null); + if((!parameters.containsKey("sensorKey"))||(!parameters.containsKey("monitorPointId"))) + return ResultBean.fail("���������null"); + String sensorKey = (String) parameters.get("sensorKey"); + Integer monitorPointId = Integer.parseInt((String) parameters.get("monitorPointId")); + + //������monitorpointId������������������������������mac������ + List<Device> devices = deviceService.getDevicesByMonitorPointId(monitorPointId); + if(ObjectUtils.isEmpty(devices)) + return ResultBean.fail("���������������������monitorPointId������"); + List<String> macs = new ArrayList<>(); + devices.forEach(p->{ + macs.add(p.getMac()); + }); + + //���������������mac��������������������������������������������������������� + Map<String, Object> timeAndYearMonth = getTimeAndYearMonthForFiveMinuteData(); + String time= (String) timeAndYearMonth.get("time"); + String yearAndMonth = (String) timeAndYearMonth.get("yearAndMonth"); + parameters.put("time",time); + parameters.put("yearAndMonth",yearAndMonth); + parameters.put("macs",macs); + List<Map<String, Object>> datas = historyMinutelyService.getFiveMinutesDataByMacsAndTime(parameters); + return new ResultBean<List<Map<String, Object>>>(datas); + + } + + /** + * @Description: ������������������������������������������������������������������������������ + * @Param: [] + * @return: java.lang.String + * @Author: ������������ + * @Date: 2020/10/19 + */ + private Map<String, Object> getTimeAndYearMonthForFiveMinuteData() { + Map<String, Object> map = new HashMap<>(); + SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + Date date = new Date(); + String minute = DateUtil.getMinute(date); + String year = ""; + String month = ""; + String yearAndMonth = ""; + String startTime = ""; + Integer endMinute = Integer.parseInt(String.valueOf(minute.charAt(minute.length() - 1))); + if (endMinute >= 5) { + StringBuilder time = new StringBuilder(sdf.format(date)); + startTime = time.replace(15, 19, "0:00").toString(); + } else { + date = DateUtil.rollMinute(date, -5); + StringBuilder time = new StringBuilder(sdf.format(date)); + startTime = time.replace(15, 19, "5:00").toString(); + } + + year = DateUtil.getYear(date); + month = DateUtil.getMonth(date); + yearAndMonth = year + month; + map.put("time", startTime); + map.put("yearAndMonth", yearAndMonth); + return map; + } + + + } diff --git a/src/main/java/com/moral/mapper/HistoryFiveMinutelyMapper.java b/src/main/java/com/moral/mapper/HistoryFiveMinutelyMapper.java new file mode 100644 index 0000000..91a720a --- /dev/null +++ b/src/main/java/com/moral/mapper/HistoryFiveMinutelyMapper.java @@ -0,0 +1,8 @@ +package com.moral.mapper; + +import java.util.List; +import java.util.Map; + +public interface HistoryFiveMinutelyMapper { + List<Map<String,Object>> getFiveMinutesDataByMacsAndTime(Map<String,Object> parameters); +} diff --git a/src/main/java/com/moral/service/HistoryMinutelyService.java b/src/main/java/com/moral/service/HistoryMinutelyService.java index b4ccde5..1904618 100644 --- a/src/main/java/com/moral/service/HistoryMinutelyService.java +++ b/src/main/java/com/moral/service/HistoryMinutelyService.java @@ -38,4 +38,6 @@ List<Map<String, Object>> getMultiDeviceSensorData(Map<String, Object> parameters) throws Exception; + List<Map<String,Object>> getFiveMinutesDataByMacsAndTime(Map<String,Object> parameters); + } diff --git a/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java b/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java index e734433..79ef541 100644 --- a/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java +++ b/src/main/java/com/moral/service/impl/HistoryMinutelyServiceImpl.java @@ -1,5 +1,6 @@ package com.moral.service.impl; +import com.moral.mapper.*; import io.swagger.models.auth.In; import java.math.BigDecimal; @@ -103,6 +104,14 @@ @Resource private MonitorPointMapper monitorPointMapper; + @Resource + HistoryFiveMinutelyMapper historyFiveMinutelyMapper; + + @Override + public List<Map<String, Object>> getFiveMinutesDataByMacsAndTime(Map<String, Object> parameters) { + ValidateUtil.notNull(parameters,"���������������������������������"); + return historyFiveMinutelyMapper.getFiveMinutesDataByMacsAndTime(parameters); + } @Override public Map<String, Object> getDayAQIByDevice(Map<String, Object> parameters) { @@ -911,11 +920,11 @@ }*/ values.add(value); - - } System.out.println(data); } return null; } + + } diff --git a/src/main/java/com/moral/util/DateUtil.java b/src/main/java/com/moral/util/DateUtil.java new file mode 100644 index 0000000..4ed0b22 --- /dev/null +++ b/src/main/java/com/moral/util/DateUtil.java @@ -0,0 +1,179 @@ +package com.moral.util; + +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +public class DateUtil { + /** + * ������������������������������2010-12-01 + */ + public static String FORMAT_SHORT = "yyyy-MM-dd"; + /** + * ������������ ������2010-12-01 23:15:06 + */ + public static String FORMAT_LONG = "yyyy-MM-dd HH:mm:ss"; + /** + * ������������������������������ ������yyyy-MM-dd HH:mm:ss.S + */ + public static String FORMAT_FULL = "yyyy-MM-dd HH:mm:ss.S"; + /** + * ������������ ������2010���12���01��� + */ + public static String FORMAT_SHORT_CN = "yyyy���MM���dd"; + /** + * ������������ ������2010���12���01��� 23���15���06��� + */ + public static String FORMAT_LONG_CN = "yyyy���MM���dd��� HH���mm���ss���"; + /** + * ������������������������������������ + */ + public static String FORMAT_FULL_CN = "yyyy���MM���dd��� HH���mm���ss���SSS������"; + + + public static void main(String[] args) { + Date date = rollMinute(new Date(), -2); + System.out.println(date); + + } + + + + /** + * ������������������ + */ + public static String getTimeString() { + SimpleDateFormat df = new SimpleDateFormat(FORMAT_SHORT_CN); + Calendar calendar = Calendar.getInstance(); + return df.format(calendar.getTime()); + } + + /** + * ������������������ + * @param date ������ + * @return + */ + public static String getYear(Date date) { + SimpleDateFormat df = new SimpleDateFormat(FORMAT_FULL); + return df.format(date).substring(0, 4); + } + + /** + * ������������������������ + * + * @param date + * Date ������ + * @return ������������ + */ + public static String getMonth(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int Month = calendar.get(Calendar.MONTH); + Month++; + String str = String.valueOf(Month); + return str = str.length()==1?"0".concat(str):str; + } + + /** + * ��������������������������� + * + * @param date + * Date ������ + * @return ������������ + */ + public static int getDay(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar.get(Calendar.DAY_OF_MONTH); + } + + /** + * ��������������������������� + * + * @param date + * ������ + * @return ������������ + */ + public static int getHour(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar.get(Calendar.HOUR_OF_DAY); + } + + /** + * ������������������������ + * + * @param date + * ������ + * @return ������������ + */ + public static String getMinute(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + int minute = calendar.get(Calendar.MINUTE); + String str = String.valueOf(minute); + return str = str.length()==1?"0".concat(str):str; + } + + /** + * ������������ + * + * @param date + * Date ������ + * @return ������������ + */ + public static int getSecond(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar.get(Calendar.SECOND); + } + + /** + * ������������������������ + * + * @param date + * ������ + * @return ��������� + */ + public static long getMillis(Date date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(date); + return calendar.getTimeInMillis(); + } + + /** + * ��������������������������������������������� + * + * @param + * + * @return ������������������ + */ + public static Date rollMinute(Date d, int minute) { + return new Date(d.getTime() + minute * 60 * 1000); + } + + + /** + * ���/���?��� + * + * @param d + * @param mon + * @return + */ + public static Date rollMon(Date d, int mon) { + Calendar cal = Calendar.getInstance(); + cal.setTime(d); + cal.add(Calendar.MONTH, mon); + return cal.getTime(); + } + + //������������i��������������� + public static String getOldTime(int i){ + Long time=System.currentTimeMillis(); + Long time1=time-(1000 * 60 * 60 * 24 * i); + SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + String sd = sdf.format(new Date(Long.parseLong(String.valueOf(time1)))); + String s=sd.substring(0,10); + return s; + } +} diff --git a/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java b/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java index 9b42ef0..12e9cb8 100644 --- a/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java +++ b/src/main/java/com/moral/webSocketServer/BSTestWebsocketServer.java @@ -24,15 +24,15 @@ import java.util.concurrent.ConcurrentHashMap; @Slf4j -@ServerEndpoint("/web/testWebSocket/{param}") +@ServerEndpoint("/web/WebSocket/{param}") @Component -/** +/** * @Description: ���������������websocket - * @Param: - * @return: + * @Param: + * @return: * @Author: ������������ * @Date: 2020/9/30 - */ + */ public class BSTestWebsocketServer { public static DeviceService deviceService; @@ -116,17 +116,17 @@ channel.basicConsume(queue, false, new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { - //������MQ������������������������������������������������������������ + //������MQ������������������������������������������������������������ Map message = (Map) JSON.parse((String) JSON.parse(new String(body))); sendWindInfo(message); - + //���������������������������������������mac���������������������������������mac������������ if(mac!=null&&(!mac.equals(0))) sendDeviceInfo(message,deviceMap); - + //������������ channel.basicAck(envelope.getDeliveryTag(), true); - + //������socket������������������ if (!webSocketMap.containsKey(accountId)) { RabbitMQUtils.closeConnectionChannel(connection, channel); @@ -171,13 +171,13 @@ } } - /** + /** * @Description: ���������������������socket��������������������������������� * @Param: [param] * @return: void * @Author: ��������� * @Date: 2020/9/30 - */ + */ private void sendWindInfo(Map<String, Object> param) { try { Map<String, Object> map = new HashMap<>(); @@ -191,13 +191,13 @@ } } - /** + /** * @Description: ������������mac������accountid���������������������mac������������������ * @Param: [param] * @return: void * @Author: ��������� * @Date: 2020/9/30 - */ + */ private void sendDeviceInfo(Map<String, Object> param,Map<String,Device> deviceMap) { String deviceMac = (String) param.get("mac"); if (mac.equals(deviceMac)) { diff --git a/src/main/java/com/moral/webSocketServer/BSWebsocketServer.java b/src/main/java/com/moral/webSocketServer/BSWebsocketServer.java deleted file mode 100644 index f4901f2..0000000 --- a/src/main/java/com/moral/webSocketServer/BSWebsocketServer.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.moral.webSocketServer; - -import com.alibaba.fastjson.JSON; -import com.moral.common.util.ParameterUtils; -import com.moral.entity.Device; -import com.moral.entity.Sensor; -import com.moral.service.DeviceService; -import com.moral.service.SensorService; -import com.rabbitmq.client.*; -import com.moral.util.RabbitMQUtils; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.util.ObjectUtils; - -import javax.annotation.PostConstruct; -import javax.websocket.*; -import javax.websocket.server.PathParam; -import javax.websocket.server.ServerEndpoint; -import java.io.IOException; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -@Slf4j -@ServerEndpoint("/web/webSocket/{param}") -@Component -public class BSWebsocketServer { - - public static DeviceService deviceService; - - public static SensorService sensorService; - - /** - * concurrent������������������Set���������������������������������������MyWebSocket��������� - */ - private static ConcurrentHashMap<String, BSWebsocketServer> webSocketMap = new ConcurrentHashMap<>(); - /** - * ������������������������������������������������������������������������������ - */ - private Session session; - - private String orgId; - - private String accountId; - - private String regionCode; - - private final String exchange = "screens_data"; - - private static Map<String, Sensor> sensors; - - - @PostConstruct - public void init() { - sensors = new HashMap<>(); - List<Sensor> allSensors = sensorService.getAllSensors(); - for (Sensor sensor : allSensors) { - sensors.put(sensor.getSensorKey(), sensor); - } - } - - /** - * ��������������������������������� - */ - @OnOpen - public void onOpen(Session session, @PathParam("param") String param) { - this.session = session; - String[] params = param.split("&"); - this.accountId = params[0]; - this.orgId = params[1]; - this.regionCode = params[2]; - - if (webSocketMap.containsKey(accountId)) { - webSocketMap.remove(accountId); - webSocketMap.put(accountId, this); - } else { - webSocketMap.put(accountId, this); - } - - Map<String, Object> paramMap = new HashMap<String, Object>(); - paramMap.put("orgId", orgId); - paramMap.put("regionCode", regionCode); - ParameterUtils.getRegionType4RegionCode(paramMap); - List<Device> deviceList = deviceService.queryDevice(paramMap); - - try { - Connection connection = RabbitMQUtils.getConnection(); - final Channel channel = connection.createChannel(); - //������������������ - String queue = channel.queueDeclare().getQueue(); - - //������������������������routingKey������������ - String routingKey = ""; - for (Device d : deviceList) { - routingKey = orgId + "." + d.getMac(); - channel.queueBind(queue, exchange, routingKey); - } - - //������������,��������������������� - channel.basicQos(1);//��������������������������� - channel.basicConsume(queue, false, new DefaultConsumer(channel) { - @Override - public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { - Map message = (Map) JSON.parse((String) JSON.parse(new String(body))); - message = messageFormat(message,deviceList); - sendMessage(JSON.toJSONString(message)); - channel.basicAck(envelope.getDeliveryTag(), false); - //������socket������������������ - if (!webSocketMap.containsKey(accountId)) { - RabbitMQUtils.closeConnectionChannel(connection,channel); - } - } - }); - } catch (IOException e) { - e.printStackTrace(); - } - - } - - @OnClose - public void onClose() { - if (webSocketMap.containsKey(accountId)) { - webSocketMap.remove(accountId); - } - } - - //������������������������������ - @OnMessage - public void onMessage(String message, Session session) { - - } - - @OnError - public void onError(Session session, Throwable error) { - log.error(error.getMessage()); - } - - public void sendMessage(String message) throws IOException { - try { - if (session.isOpen()) { - this.session.getBasicRemote().sendText(message); - } - } catch (IOException e) { - - //log.error(e.getMessage()); - } - } - - //���MQ��������������������������������������������������������������� - private Map<String,Object> messageFormat( Map<String,Object> param,List<Device> deviceList) { - Map<String, Object> map = new HashMap<>(); - param.forEach((key, value) -> { - Sensor sensor = sensors.get(key); - if (!ObjectUtils.isEmpty(sensor)) { - String unit = ObjectUtils.isEmpty(sensor.getUnit())?"":(String)sensor.getUnit(); - map.put(sensor.getName(),value+unit); - } - }); - String mac = (String) param.get("mac"); - for (Device device : deviceList) { - if(mac.equals(device.getMac())){ - map.put("������",device.getLatitude()); - map.put("������",device.getLongitude()); - map.put("������",device.getState()); - break; - } - } - map.put("mac",mac); - return map; - } - -} diff --git a/src/main/resources/mapper/HistoryFiveMinutelyMapper.xml b/src/main/resources/mapper/HistoryFiveMinutelyMapper.xml new file mode 100644 index 0000000..89eee37 --- /dev/null +++ b/src/main/resources/mapper/HistoryFiveMinutelyMapper.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> +<mapper namespace="com.moral.mapper.HistoryFiveMinutelyMapper"> + <select id="getFiveMinutesDataByMacsAndTime" resultType="map"> + SELECT h.mac,json->'$.${sensorKey}' as #{sensorKey} + from history_five_minutely_${yearAndMonth} h + where time = #{time} and mac IN + <foreach collection="macs" separator="," open="(" close=")" item="mac"> + #{mac} + </foreach> + </select> +</mapper> \ No newline at end of file -- Gitblit v1.8.0