Commit 79797cac authored by huangcb's avatar huangcb

Merge branch 'master' into huangchb

# Conflicts:
#	src/main/java/com/esv/datacenter/iot/module/devicemodel/entity/DeviceInstanceEntity.java
#	src/main/java/com/esv/datacenter/iot/module/devicemodel/service/DeviceInstanceService.java
#	src/main/java/com/esv/datacenter/iot/module/devicemodel/service/impl/DeviceInstanceServiceImpl.java
parents b6750ef7 5539a4db
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();
}
...@@ -55,8 +55,17 @@ public class DeviceInstanceEntity implements Serializable { ...@@ -55,8 +55,17 @@ public class DeviceInstanceEntity implements Serializable {
private String deviceModel; private String deviceModel;
/** /**
* 通信ID * 通信ID
*/ **/
private String communicationId; private String communicationId;
/**
* 在线状态
**/
private Integer onlineState;
/**
* 在线状态更新时间
**/
private Date onlineUpdateTime;
/** /**
* 是否删除:0-未删除、1-已删除 * 是否删除:0-未删除、1-已删除
*/ */
......
...@@ -2,7 +2,6 @@ package com.esv.datacenter.iot.module.devicemodel.service; ...@@ -2,7 +2,6 @@ package com.esv.datacenter.iot.module.devicemodel.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.esv.datacenter.iot.common.vo.PageResultVO; import com.esv.datacenter.iot.common.vo.PageResultVO;
import com.esv.datacenter.iot.module.devicemodel.dto.DeviceInstanceDto;
import com.esv.datacenter.iot.module.devicemodel.entity.DeviceInstanceEntity; 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.form.DeviceInstanceForm;
import com.esv.datacenter.iot.module.devicemodel.vo.DeviceInstanceVO; import com.esv.datacenter.iot.module.devicemodel.vo.DeviceInstanceVO;
...@@ -117,5 +116,14 @@ public interface DeviceInstanceService extends IService<DeviceInstanceEntity> { ...@@ -117,5 +116,14 @@ public interface DeviceInstanceService extends IService<DeviceInstanceEntity> {
**/ **/
List<DeviceInstanceDto> get4List(); List<DeviceInstanceDto> get4List();
/**
* description 根据通信id更新设备在线状态
* param [communicationIdList]
* return void
* author chenfm
* createTime 2020/8/12 17:37
**/
void changeDeviceOnlineState(List<String> communicationIdList);
} }
...@@ -18,6 +18,10 @@ import org.springframework.beans.BeanUtils; ...@@ -18,6 +18,10 @@ import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.*; import java.util.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@Service("deviceInstanceService") @Service("deviceInstanceService")
...@@ -143,4 +147,20 @@ public class DeviceInstanceServiceImpl extends ServiceImpl<DeviceInstanceDao, De ...@@ -143,4 +147,20 @@ public class DeviceInstanceServiceImpl extends ServiceImpl<DeviceInstanceDao, De
public List<DeviceInstanceDto> get4List() { public List<DeviceInstanceDto> get4List() {
return this.getBaseMapper().select4List(); return this.getBaseMapper().select4List();
} }
@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);
}
}
...@@ -85,4 +85,8 @@ timescale: ...@@ -85,4 +85,8 @@ timescale:
max-lifetime: 0 max-lifetime: 0
table-field: table-field:
map: string-text,number-numeric,integer-int8,boolean-bit(1),date-date,time-int4,datetime-timestamptz map: string-text,number-numeric,integer-int8,boolean-bit(1),date-date,time-int4,datetime-timestamptz
table-prefix: iot_data_model_ table-prefix: iot_data_model_
\ No newline at end of file emq:
api:
url: 192.168.31.248:18083
auth: Basic YWRtaW46cHVibGlj
\ No newline at end of file
...@@ -85,4 +85,8 @@ timescale: ...@@ -85,4 +85,8 @@ timescale:
max-lifetime: 0 max-lifetime: 0
table-field: table-field:
map: string-text,number-numeric,boolean-bit(1) map: string-text,number-numeric,boolean-bit(1)
table-prefix: iot_model_ table-prefix: iot_model_
\ No newline at end of file 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