Commit 391570b2 by YG8999

设备网关状态监控

parent 491c2809
......@@ -2,6 +2,7 @@ package share.quartz.task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import share.system.service.DeviceGatewayService;
import share.system.service.DeviceStatusLogService;
/**
......@@ -15,12 +16,26 @@ public class DeviceTask {
@Autowired
private DeviceStatusLogService deviceStatusLogService;
@Autowired
private DeviceGatewayService deviceGatewayService;
/**
* 设备异常状态短信提醒任务
*/
public void sendSmsByYc() {
// 设备状态异常变更发送短信
deviceStatusLogService.sendSmsByYc();
}
/**
* 设备网关状态监控
*/
public void deviceGatewayStatusMonitor() {
// 更新网关状态
boolean b = deviceGatewayService.deviceGatewayStatusMonitor();
if (b) {
// 离线网关,发送短信提醒
}
}
}
......@@ -6,6 +6,9 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import share.common.annotation.Excel;
import share.common.core.domain.BaseEntity;
import lombok.Data;
import java.util.Date;
/**
* 设备网关信息对象 s_device_gateway
*
......@@ -48,10 +51,12 @@ public class DeviceGateway extends BaseEntity
/** 网络分组 */
@Excel(name = "网络分组")
@TableField(value = "`group`")
private String group;
/** 设备状态 */
@Excel(name = "设备状态")
@TableField(value = "`status`")
private String status;
/** mqtt服务器IP/域名 */
......@@ -74,6 +79,11 @@ public class DeviceGateway extends BaseEntity
@Excel(name = "设备位置")
private String devPosition;
/** 网关最后上报时间 */
private Date lastReportDate;
/** 网关最后上报内容 */
private String lastReportContent;
@Override
public String toString() {
......
......@@ -59,4 +59,11 @@ public interface DeviceGatewayService extends IService<DeviceGateway>
* @return 结果
*/
public int deleteDeviceGatewayById(Long id);
/**
* 网关状态监控
* @return
*/
boolean deviceGatewayStatusMonitor();
}
package share.system.service.impl;
import java.util.Date;
import java.util.List;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import share.common.enums.DeviceStatusEnum;
import share.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
......@@ -94,4 +99,24 @@ public class DeviceGatewayServiceImpl extends ServiceImpl<DeviceGatewayMapper, D
{
return deviceGatewayMapper.deleteDeviceGatewayById(id);
}
/**
* 网关状态监控
* @return
*/
@Override
public boolean deviceGatewayStatusMonitor() {
List<DeviceGateway> list = deviceGatewayMapper.selectDeviceGatewayList(new DeviceGateway());
list.stream().forEach(gateway -> {
Date lastReportDate = gateway.getLastReportDate();
// 网关上报(最近10分钟内没有上报数据,网关设备离线)
Date startDate = DateUtil.offsetMinute(DateUtil.date(), -10);
if (lastReportDate != null && lastReportDate.compareTo(startDate) > 0) {
gateway.setStatus(DeviceStatusEnum.DEVICE_ONLINE.getCode());
} else {
gateway.setStatus(DeviceStatusEnum.DEVICE_OFFLINE.getCode());
}
});
return this.updateBatchById(list);
}
}
......@@ -530,6 +530,18 @@ public class MqttxServiceImpl implements MqttxService {
private boolean updateDevice(String topic, String payload) {
JSONObject json = JSONUtil.parseObj(payload);
if (json.size() > 0) {
// 网关devId
String gatewayDevId = json.getStr("devId");
// 网关信息查询
DeviceGateway deviceGateway = deviceGatewayMapper.selectDeviceGatewayByDevId(gatewayDevId);
if (deviceGateway != null) {
deviceGateway.setLastReportDate(DateUtil.date());
deviceGateway.setLastReportContent(payload);
deviceGateway.setUpdateTime(DateUtil.date());
// 更新网关信息
deviceGatewayMapper.updateDeviceGateway(deviceGateway);
}
// 设备上报信息
JSONArray array = json.getJSONArray("params");
if (array != null && array.size() > 0) {
List<String> devIds = new ArrayList<>();
......
......@@ -24,10 +24,15 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="updateTime" column="update_time" />
<result property="remark" column="remark" />
<result property="devPosition" column="dev_position" />
<result property="lastReportDate" column="last_report_date" />
<result property="lastReportContent" column="last_report_content" />
</resultMap>
<sql id="selectDeviceGatewayVo">
select id, dev_name, dev_mac, dev_id, dev_psw, dev_ver, dev_type, `group`, status, mqtt_iP, mqtt_port, mqtt_user, mqtt_paswd, create_by, create_time, update_by, update_time, remark, dev_position from s_device_gateway
select id, dev_name, dev_mac, dev_id, dev_psw, dev_ver, dev_type, `group`, status, mqtt_iP, mqtt_port,
mqtt_user, mqtt_paswd, create_by, create_time, update_by, update_time, remark,
dev_position, last_report_date, last_report_content
from s_device_gateway
</sql>
<select id="selectDeviceGatewayList" parameterType="DeviceGateway" resultMap="DeviceGatewayResult">
......@@ -79,6 +84,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">update_time,</if>
<if test="remark != null">remark,</if>
<if test="devPosition != null">dev_position,</if>
<if test="lastReportDate != null">last_report_date,</if>
<if test="lastReportContent != null">last_report_content,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="devName != null">#{devName},</if>
......@@ -99,6 +106,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">#{updateTime},</if>
<if test="remark != null">#{remark},</if>
<if test="devPosition != null">#{devPosition},</if>
<if test="lastReportDate != null">#{lastReportDate},</if>
<if test="lastReportContent != null">#{lastReportContent},</if>
</trim>
</insert>
......@@ -123,6 +132,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="updateTime != null">update_time = #{updateTime},</if>
<if test="remark != null">remark = #{remark},</if>
<if test="devPosition != null">dev_position = #{devPosition},</if>
<if test="lastReportDate != null">last_report_date = #{lastReportDate},</if>
<if test="lastReportContent != null">last_report_content = #{lastReportContent},</if>
</trim>
where id = #{id}
</update>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment