沙钢智慧能源系统后端代码
liriming
2024-12-04 f0a8000dd1df19739b678fe527163a821268ae3c
shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java
@@ -33,6 +33,10 @@
    @Resource
    private RabbitTemplate rabbitTemplate;
    @Resource
    private AlarmConsumer alarmConsumer;
    @RabbitHandler
    public void process(PredictFinishMessage finishMessage) throws InterruptedException {
        System.out.println("测试消费模型预测完成RabbitMQ消息----------------------");
@@ -45,37 +49,43 @@
                OutputIdList.add(item.getOutId());
            });
            configList.forEach(item -> {
                PreDataJsonReqVO preDataJsonReqVO = new PreDataJsonReqVO();
                preDataJsonReqVO.setPredictTime(finishMessage.getPredictTime());
                preDataJsonReqVO.setOutputIdList(OutputIdList);
                Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(preDataJsonReqVO);
                PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
                reqVO.setPredictTime(finishMessage.getPredictTime());
                reqVO.setOutputIdList(OutputIdList);
                Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(reqVO);
                if (!CollectionUtils.isEmpty(preData)) {
                    List<Object[]> result = preData.get(item.getOutId());
                    int toIndex = result.size();
                    int fromIndex = result.size() - item.getCompLength();
                    List<Object[]> predictList = result.subList(fromIndex, toIndex);
                    for(Object[] data : predictList){
                        BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
                        if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) {
                            AlarmMessage alarmMessage = new AlarmMessage();
                            AlarmMessageRespDTO alarmMessageRespDTO = new AlarmMessageRespDTO();
                            alarmMessageRespDTO.setConfigId(item.getId());
                            if (dataValue.compareTo(item.getLowerLimit()) < 0) {
                                alarmMessageRespDTO.setAlarmType("1");//超下限
                            }else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
                                alarmMessageRespDTO.setAlarmType("2");//超上限
                    if(toIndex > 0){
                        int fromIndex = result.size() - item.getCompLength();
                        List<Object[]> predictList = result.subList(fromIndex, toIndex);
                        for(Object[] data : predictList){
                            BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
                            if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) {
                                AlarmMessage alarmMessage = new AlarmMessage();
                                AlarmMessageRespDTO alarmMessageRespDTO = new AlarmMessageRespDTO();
                                alarmMessageRespDTO.setConfigId(item.getId());
                                if (dataValue.compareTo(item.getLowerLimit()) < 0) {
                                    alarmMessageRespDTO.setAlarmType("1");//超下限
                                }else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
                                    alarmMessageRespDTO.setAlarmType("2");//超上限
                                }
                                alarmMessageRespDTO.setAlarmTime(DateUtils.parse(data[0].toString(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                                JSONObject content = new JSONObject();
                                content.put("alarmObj", item.getAlarmObj());
                                content.put("alarmType", alarmMessageRespDTO.getAlarmType());
                                content.put("unit", item.getUnit());
                                content.put("overLimitTime", data[0]);
                                content.put("overLimitValue", dataValue);
                                alarmMessageRespDTO.setContent(JSONObject.toJSONString(content));
                                alarmMessage.setAlarmMessageRespDTO(alarmMessageRespDTO);
                                try {
                                    alarmConsumer.process(alarmMessage);
                                } catch (InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                                break;
                            }
                            alarmMessageRespDTO.setAlarmTime(DateUtils.parse(data[0].toString(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                            JSONObject content = new JSONObject();
                            content.put("alarmObj", item.getAlarmObj());
                            content.put("alarmType", alarmMessageRespDTO.getAlarmType());
                            content.put("unit", item.getUnit());
                            content.put("overLimitTime", data[0]);
                            content.put("overLimitValue", dataValue);
                            alarmMessageRespDTO.setContent(JSONObject.toJSONString(content));
                            alarmMessage.setAlarmMessageRespDTO(alarmMessageRespDTO);
                            rabbitTemplate.convertAndSend(AlarmMessage.EXCHANGE, AlarmMessage.ALARM_ROUTING_KEY, alarmMessage);
                            break;
                        }
                    }
                }