Lius
2024-06-18 5c2bfd01241c2790c860ef17c127a788556384cc
webservice推送设备状态和设备利用率
已添加7个文件
425 ■■■■■ 文件已修改
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/WebServiceStatusJob.java 94 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/WebServiceUtilizationRateJob.java 93 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java 110 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentStatus.java 37 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentStatusList.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentUtilizationRate.java 46 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentUtilizationRateList.java 22 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/WebServiceStatusJob.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,94 @@
package org.jeecg.modules.mdc.job;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.mdc.service.IEquipmentService;
import org.jeecg.modules.mdc.util.CxfClientUtil;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.mdc.util.XmlUtil;
import org.jeecg.modules.mdc.vo.WsEquipmentStatus;
import org.jeecg.modules.mdc.vo.WsEquipmentStatusList;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.entity.SysQuartzLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ISysQuartzLogService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author Lius
 * @date 2024/6/11 11:27
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class WebServiceStatusJob implements Job {
    /**
     * url
     */
    @Value("${webservice.url}")
    private String url;
    /**
     * namespace
     */
    @Value("${webservice.namespace}")
    private String namespace;
    /**
     * method
     */
    @Value("${webservice.statusMethod}")
    private String method;
    @Resource
    private IQuartzJobService quartzJobService;
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
    @Resource
    private IEquipmentService equipmentService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        log.info("WebService上报设备状态到二级综合管控平台任务 WebServiceStatusJob start!  æ—¶é—´:" + DateUtils.now());
        long startTime = System.currentTimeMillis();
        try {
            List<WsEquipmentStatus> equipmentStatusList = equipmentService.selectEquipmentStatus();
            WsEquipmentStatusList wsEquipmentStatusList = new WsEquipmentStatusList();
            List<WsEquipmentStatus> list = new ArrayList<>();
            if (equipmentStatusList != null && !equipmentStatusList.isEmpty()) {
                for (WsEquipmentStatus wsEquipmentStatus : equipmentStatusList) {
                    list.add(wsEquipmentStatus);
                    wsEquipmentStatusList.setList(list);
                    String s = XmlUtil.convertToXml(wsEquipmentStatusList);
                    list.clear();
                    log.info("上报状态数据 ===== " + s);
                    String result = CxfClientUtil.invokeService(url, s, namespace, method);
                    log.info("上报状态结果 ===== " + result);
                }
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/job/WebServiceUtilizationRateJob.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,93 @@
package org.jeecg.modules.mdc.job;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.mdc.service.IEquipmentService;
import org.jeecg.modules.mdc.util.CxfClientUtil;
import org.jeecg.modules.mdc.util.ThrowableUtil;
import org.jeecg.modules.mdc.util.XmlUtil;
import org.jeecg.modules.mdc.vo.WsEquipmentUtilizationRate;
import org.jeecg.modules.mdc.vo.WsEquipmentUtilizationRateList;
import org.jeecg.modules.quartz.entity.QuartzJob;
import org.jeecg.modules.quartz.entity.SysQuartzLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ISysQuartzLogService;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @author Lius
 * @date 2024/6/11 16:17
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
public class WebServiceUtilizationRateJob implements Job {
    /**
     * åœ°å€
     */
    @Value("${webservice.url}")
    private String url;
    /**
     * namespace
     */
    @Value("${webservice.namespace}")
    private String namespace;
    /**
     * æ–¹æ³•
     */
    @Value("${webservice.rateMethod}")
    private String method;
    @Resource
    private IQuartzJobService quartzJobService;
    @Resource
    private ISysQuartzLogService sysQuartzLogService;
    @Resource
    private IEquipmentService equipmentService;
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        SysQuartzLog quartzLog = new SysQuartzLog();
        quartzLog.setCreateTime(new Date());
        List<QuartzJob> byJobClassName = this.quartzJobService.findByJobClassName(this.getClass().getName());
        if (byJobClassName != null && !byJobClassName.isEmpty()) {
            quartzLog.setJobId(byJobClassName.get(0).getId());
        }
        log.info("WebService上报设备利用率到二级综合管控平台任务 WebServiceUtilizationRateJob start!  æ—¶é—´:" + DateUtils.now());
        long startTime = System.currentTimeMillis();
        try {
            List<WsEquipmentUtilizationRate> equipmentRateList = equipmentService.selectEquipmentRate();
            WsEquipmentUtilizationRateList wsEquipmentUtilizationRateList = new WsEquipmentUtilizationRateList();
            List<WsEquipmentUtilizationRate> list = new ArrayList<>();
            if (equipmentRateList != null && !equipmentRateList.isEmpty()) {
                for (WsEquipmentUtilizationRate wsEquipmentUtilizationRate : equipmentRateList) {
                    list.add(wsEquipmentUtilizationRate);
                    wsEquipmentUtilizationRateList.setList(list);
                    String s = XmlUtil.convertToXml(wsEquipmentUtilizationRateList);
                    list.clear();
                    log.info("上报利用率数据 ===== " + s);
                    String result = CxfClientUtil.invokeService(url, s, namespace, method);
                    log.info("上报利用率结果 ===== " + result);
                }
            }
            quartzLog.setIsSuccess(0);
        } catch (Exception e) {
            quartzLog.setIsSuccess(-1);
            quartzLog.setExceptionDetail(ThrowableUtil.getStackTrace(e));
        }
        long endTime = System.currentTimeMillis();
        quartzLog.setExecutionTime(Integer.parseInt(String.valueOf(endTime - startTime)));
        sysQuartzLogService.save(quartzLog);
    }
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/util/XmlUtil.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,110 @@
package org.jeecg.modules.mdc.util;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import java.io.*;
/**
 * @author Lius
 * @date 2024/6/12 10:31
 */
public class XmlUtil {
    /**
     * å°†å¯¹è±¡ç›´æŽ¥è½¬æ¢æˆString类型的 XML输出
     *
     * @param obj
     * @return
     */
    public static String convertToXml(Object obj) {
        // åˆ›å»ºè¾“出流
        StringWriter sw = new StringWriter();
        try {
            // åˆ©ç”¨jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            // æ ¼å¼åŒ–xml输出的格式
//            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            // åŽ»æŽ‰ç”Ÿæˆxml时的默认报文头
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
            // å°†å¯¹è±¡è½¬æ¢æˆè¾“出流形式的xml
            marshaller.marshal(obj, sw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return sw.toString();
    }
    /**
     * å°†å¯¹è±¡æ ¹æ®è·¯å¾„转换成xml文件
     *
     * @param obj
     * @param path
     * @return
     */
    public static void convertToXml(Object obj, String path) {
        try {
            // åˆ©ç”¨jdk中自带的转换类实现
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();
            // æ ¼å¼åŒ–xml输出的格式
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                    Boolean.TRUE);
            // å°†å¯¹è±¡è½¬æ¢æˆè¾“出流形式的xml
            // åˆ›å»ºè¾“出流
            FileWriter fw = null;
            try {
                fw = new FileWriter(path);
            } catch (IOException e) {
                e.printStackTrace();
            }
            marshaller.marshal(obj, fw);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
    @SuppressWarnings("unchecked")
    /**
     * å°†String类型的xml转换成对象
     */
    public static Object convertXmlStrToObject(Class clazz, String xmlStr) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            // è¿›è¡Œå°†Xml转成对象的核心接口
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader sr = new StringReader(xmlStr);
            xmlObject = unmarshaller.unmarshal(sr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
    @SuppressWarnings("unchecked")
    /**
     * å°†file类型的xml转换成对象
     */
    public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
        Object xmlObject = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            FileReader fr = null;
            try {
                fr = new FileReader(xmlPath);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            xmlObject = unmarshaller.unmarshal(fr);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return xmlObject;
    }
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentStatus.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,37 @@
package org.jeecg.modules.mdc.vo;
import lombok.Data;
import org.jeecg.modules.mdc.util.DateAdapter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
/**
 * @author Lius
 * @date 2024/6/11 10:44
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "equipment")
public class WsEquipmentStatus {
    @XmlElement
    private String equipmentCode;
    @XmlElement
    private String equipmentName;
    @XmlElement
    private String equipmentStatus;
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date creationDate;
    @XmlElement
    private String mainDeptCode;
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentStatusList.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,23 @@
package org.jeecg.modules.mdc.vo;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/**
 * @author Lius
 * @date 2024/6/13 10:00
 */
@Data
@XmlRootElement(name = "list")
@XmlAccessorType(XmlAccessType.FIELD)
public class WsEquipmentStatusList {
    @XmlElement(name = "equipment")
    private List<WsEquipmentStatus> list;
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentUtilizationRate.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,46 @@
package org.jeecg.modules.mdc.vo;
import lombok.Data;
import org.jeecg.modules.mdc.util.DateAdapter;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import java.util.Date;
/**
 * @author Lius
 * @date 2024/6/11 16:31
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "equipment")
public class WsEquipmentUtilizationRate {
    @XmlElement
    private String equipmentCode;
    @XmlElement
    private String equipmentName;
    @XmlElement
    private String mainDeptCode;
    @XmlElement
    private String equipmentRate;
    @XmlElement
    private String equipmentRunTime;
    @XmlElement
    private String equipmentCalendar;
    @XmlElement
    private String statisType;
    @XmlElement
    @XmlJavaTypeAdapter(DateAdapter.class)
    private Date statisDate;
}
lxzn-module-mdc/src/main/java/org/jeecg/modules/mdc/vo/WsEquipmentUtilizationRateList.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,22 @@
package org.jeecg.modules.mdc.vo;
import lombok.Data;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
/**
 * @author Lius
 * @date 2024/6/12 14:37
 */
@Data
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "list")
public class WsEquipmentUtilizationRateList {
    @XmlElement(name = "equipment")
    List<WsEquipmentUtilizationRate> list;
}