Commit 1bc9b7bf authored by chenfm's avatar chenfm

通过mqtt连接状态修改设备在线状态

parent 857dff0a
package com.esv.datacenter.iot.feign;
import com.alibaba.fastjson.JSONObject;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @description:
* @project: datacenter-iot-service
* @name: com.esv.datacenter.iot.feign.EMQFeignService
* @author: chenfm
* @email: chenfengman@esvtek.com
* @createTime: 2020/8/12 15:25
* @version: 1.0
*/
@FeignClient(name = "emq-api", url = "${emq.api.url}")
public interface EMQFeignService {
/**
* description
* param []
* return java.util.Map<java.lang.String,java.lang.Object>
* author chenfm
* createTime 2020/8/12 16:19
**/
@GetMapping(value = "api/v3/connections", headers = {"Authorization=${emq.api.auth}"})
JSONObject connections();
}
package com.esv.datacenter.iot.module.devicemodel.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* 设备实例表
......@@ -53,6 +53,19 @@ public class DeviceInstanceEntity implements Serializable {
* 设备型号
*/
private String deviceModel;
/**
* 通信ID
**/
private String communicationId;
/**
* 在线状态
**/
private Integer onlineState;
/**
* 在线状态更新时间
**/
private Date onlineUpdateTime;
/**
* 是否删除:0-未删除、1-已删除
*/
......
......@@ -89,5 +89,14 @@ public interface DeviceInstanceService extends IService<DeviceInstanceEntity> {
**/
List<DeviceInstanceVO> getInstanceListByTypeId(Long deviceTypeId);
/**
* description 根据通信id更新设备在线状态
* param [communicationIdList]
* return void
* author chenfm
* createTime 2020/8/12 17:37
**/
void changeDeviceOnlineState(List<String> communicationIdList);
}
package com.esv.datacenter.iot.module.devicemodel.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
......@@ -13,12 +14,12 @@ import com.esv.datacenter.iot.module.devicemodel.entity.DeviceInstanceEntity;
import com.esv.datacenter.iot.module.devicemodel.form.DeviceInstanceForm;
import com.esv.datacenter.iot.module.devicemodel.service.DeviceInstanceService;
import com.esv.datacenter.iot.module.devicemodel.vo.DeviceInstanceVO;
import com.esv.datacenter.iot.module.devicemodel.vo.DeviceTypeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
......@@ -118,4 +119,20 @@ public class DeviceInstanceServiceImpl extends ServiceImpl<DeviceInstanceDao, De
return voList;
}
@Override
public void changeDeviceOnlineState(List<String> communicationIdList) {
UpdateWrapper<DeviceInstanceEntity> updateWrapper = new UpdateWrapper<>();
updateWrapper.in("communication_id", communicationIdList);
DeviceInstanceEntity entity = new DeviceInstanceEntity();
entity.setOnlineUpdateTime(new Date());
entity.setOnlineState(1);
baseMapper.update(entity, updateWrapper);
updateWrapper = new UpdateWrapper<>();
updateWrapper.notIn("communication_id", communicationIdList);
entity.setOnlineState(0);
baseMapper.update(entity, updateWrapper);
}
}
\ No newline at end of file
package com.esv.datacenter.iot.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.esv.datacenter.iot.feign.EMQFeignService;
import com.esv.datacenter.iot.module.devicemodel.service.DeviceInstanceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
* @description:
* @project: datacenter-iot-service
* @name: com.esv.datacenter.iot.task.DeviceStateScheduleTask
* @author: chenfm
* @email: chenfengman@esvtek.com
* @createTime: 2020/8/12 15:08
* @version: 1.0
*/
@Slf4j
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class DeviceStateScheduleTask {
private EMQFeignService emqFeignService;
private DeviceInstanceService deviceInstanceService;
public DeviceStateScheduleTask(EMQFeignService emqFeignService, DeviceInstanceService deviceInstanceService) {
this.emqFeignService = emqFeignService;
this.deviceInstanceService = deviceInstanceService;
}
// 直接指定时间间隔: 1分钟
@Scheduled(fixedRate=60000)
private void refreshDeviceState() {
log.info("定时刷新设备在线状态开始, 执行时间: {}", LocalDateTime.now());
JSONObject jsonObject = emqFeignService.connections();
log.info("获取emq客户端列表结果: {}", jsonObject.toJSONString());
if (!jsonObject.containsKey("code") || jsonObject.getInteger("code") != 0) {
log.error("获取emq客户端列表失败");
return;
}
List<String> clientIdList = new ArrayList<>();
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (Object clientObject : jsonArray) {
JSONObject client = (JSONObject) JSON.toJSON(clientObject);
clientIdList.add(client.getString("client_id"));
}
log.info("clientId list: {}", JSON.toJSONString(clientIdList));
deviceInstanceService.changeDeviceOnlineState(clientIdList);
}
}
......@@ -86,3 +86,7 @@ timescale:
table-field:
map: string-text,number-numeric,integer-int8,boolean-bit(1),date-date,time-int4,datetime-timestamptz
table-prefix: iot_data_model_
emq:
api:
url: 192.168.31.248:18083
auth: Basic YWRtaW46cHVibGlj
\ No newline at end of file
......@@ -86,3 +86,7 @@ timescale:
table-field:
map: string-text,number-numeric,boolean-bit(1)
table-prefix: iot_model_
emq:
api:
url: 192.168.0.122:18083
auth: Basic YWRtaW46cHVibGlj
\ No newline at end of file
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