mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 00:45:55 +08:00
设备数据同步和设备的默认定位
This commit is contained in:
@@ -9,11 +9,13 @@ import com.ruoyi.common.utils.poi.ExcelUtil;
|
|||||||
import com.ruoyi.iot.domain.Device;
|
import com.ruoyi.iot.domain.Device;
|
||||||
import com.ruoyi.iot.model.DeviceAllShortOutput;
|
import com.ruoyi.iot.model.DeviceAllShortOutput;
|
||||||
import com.ruoyi.iot.model.DeviceShortOutput;
|
import com.ruoyi.iot.model.DeviceShortOutput;
|
||||||
|
import com.ruoyi.iot.mqtt.EmqxService;
|
||||||
import com.ruoyi.iot.service.IDeviceService;
|
import com.ruoyi.iot.service.IDeviceService;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import org.quartz.SchedulerException;
|
import org.quartz.SchedulerException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.context.annotation.Lazy;
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -35,6 +37,10 @@ public class DeviceController extends BaseController
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IDeviceService deviceService;
|
private IDeviceService deviceService;
|
||||||
|
|
||||||
|
@Lazy
|
||||||
|
@Autowired
|
||||||
|
private EmqxService emqxService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询设备列表
|
* 查询设备列表
|
||||||
*/
|
*/
|
||||||
@@ -119,6 +125,17 @@ public class DeviceController extends BaseController
|
|||||||
return AjaxResult.success(deviceService.selectDeviceByDeviceId(deviceId));
|
return AjaxResult.success(deviceService.selectDeviceByDeviceId(deviceId));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备数据同步
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('iot:device:query')")
|
||||||
|
@GetMapping(value = "/synchronization/{serialNumber}")
|
||||||
|
@ApiOperation("设备数据同步")
|
||||||
|
public AjaxResult deviceSynchronization(@PathVariable("serialNumber") String serialNumber)
|
||||||
|
{
|
||||||
|
return AjaxResult.success(emqxService.deviceSynchronization(serialNumber));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据设备编号详细信息
|
* 根据设备编号详细信息
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -203,8 +203,6 @@ public class ToolController extends BaseController {
|
|||||||
} else if (model.getAction().equals("client_connected")) {
|
} else if (model.getAction().equals("client_connected")) {
|
||||||
device.setStatus(3);
|
device.setStatus(3);
|
||||||
deviceService.updateDeviceStatusAndLocation(device, model.getIpaddress());
|
deviceService.updateDeviceStatusAndLocation(device, model.getIpaddress());
|
||||||
// 发布设备状态
|
|
||||||
emqxService.publishStatus(device.getProductId(), device.getSerialNumber(), 3, device.getIsShadow());
|
|
||||||
// 影子模式,发布属性和功能
|
// 影子模式,发布属性和功能
|
||||||
if (device.getIsShadow() == 1) {
|
if (device.getIsShadow() == 1) {
|
||||||
ThingsModelShadow shadow = deviceService.getDeviceShadowThingsModel(device);
|
ThingsModelShadow shadow = deviceService.getDeviceShadowThingsModel(device);
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ public class EmqxService {
|
|||||||
* 发布的主题
|
* 发布的主题
|
||||||
*/
|
*/
|
||||||
String pStatusTopic = "/status/post";
|
String pStatusTopic = "/status/post";
|
||||||
|
String pInfoTopic = "/info/get";
|
||||||
String pNtpTopic = "/ntp/get";
|
String pNtpTopic = "/ntp/get";
|
||||||
String pPropertyTopic = "/property/get";
|
String pPropertyTopic = "/property/get";
|
||||||
String pFunctionTopic = "/function/get";
|
String pFunctionTopic = "/function/get";
|
||||||
@@ -127,10 +128,15 @@ public class EmqxService {
|
|||||||
*/
|
*/
|
||||||
private void reportDevice(Long productId, String deviceNum, String message) {
|
private void reportDevice(Long productId, String deviceNum, String message) {
|
||||||
try {
|
try {
|
||||||
|
// 设备实体
|
||||||
|
Device deviceEntity=deviceService.selectDeviceBySerialNumber(deviceNum);
|
||||||
|
// 上报设备信息
|
||||||
Device device = JSON.parseObject(message, Device.class);
|
Device device = JSON.parseObject(message, Device.class);
|
||||||
device.setProductId(productId);
|
device.setProductId(productId);
|
||||||
device.setSerialNumber(deviceNum);
|
device.setSerialNumber(deviceNum);
|
||||||
deviceService.reportDevice(device);
|
deviceService.reportDevice(device,deviceEntity);
|
||||||
|
// 发布设备状态
|
||||||
|
publishStatus(productId, deviceNum, 3, deviceEntity.getIsShadow());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("接收设备信息,解析数据时异常 message={}", e.getMessage());
|
logger.error("接收设备信息,解析数据时异常 message={}", e.getMessage());
|
||||||
}
|
}
|
||||||
@@ -216,7 +222,14 @@ public class EmqxService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 2.发布时钟同步信息
|
* 2.发布设备信息
|
||||||
|
*/
|
||||||
|
public void publishInfo(Long productId, String deviceNum) {
|
||||||
|
emqxClient.publish(1, false, "/" + productId + "/" + deviceNum + pInfoTopic, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 3.发布时钟同步信息
|
||||||
*
|
*
|
||||||
* @param message
|
* @param message
|
||||||
*/
|
*/
|
||||||
@@ -228,7 +241,7 @@ public class EmqxService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 3.发布属性
|
* 4.发布属性
|
||||||
*/
|
*/
|
||||||
public void publishProperty(Long productId, String deviceNum, List<IdentityAndName> thingsList) {
|
public void publishProperty(Long productId, String deviceNum, List<IdentityAndName> thingsList) {
|
||||||
if (thingsList == null) {
|
if (thingsList == null) {
|
||||||
@@ -239,7 +252,7 @@ public class EmqxService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 4.发布功能
|
* 5.发布功能
|
||||||
*/
|
*/
|
||||||
public void publishFunction(Long productId, String deviceNum, List<IdentityAndName> thingsList) {
|
public void publishFunction(Long productId, String deviceNum, List<IdentityAndName> thingsList) {
|
||||||
if (thingsList == null) {
|
if (thingsList == null) {
|
||||||
@@ -250,5 +263,21 @@ public class EmqxService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备数据同步
|
||||||
|
*
|
||||||
|
* @param deviceNumber 设备编号
|
||||||
|
* @return 设备
|
||||||
|
*/
|
||||||
|
public Device deviceSynchronization(String deviceNumber) {
|
||||||
|
Device device=deviceService.selectDeviceBySerialNumber(deviceNumber);
|
||||||
|
// 1-未激活,2-禁用,3-在线,4-离线
|
||||||
|
if(device.getStatus()==3){
|
||||||
|
device.setStatus(4);
|
||||||
|
deviceService.updateDevice(device);
|
||||||
|
// 发布设备信息
|
||||||
|
publishInfo(device.getProductId(),device.getSerialNumber());
|
||||||
|
}
|
||||||
|
return device;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ public interface IDeviceService
|
|||||||
* @param device 设备
|
* @param device 设备
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int reportDevice(Device device);
|
public int reportDevice(Device device,Device deviceentity);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除设备
|
* 删除设备
|
||||||
|
|||||||
@@ -530,16 +530,19 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
device.setTenantName(product.getTenantName());
|
device.setTenantName(product.getTenantName());
|
||||||
device.setImgUrl(product.getImgUrl());
|
device.setImgUrl(product.getImgUrl());
|
||||||
// 随机经纬度和地址
|
// 随机经纬度和地址
|
||||||
device.setNetworkIp("127.0.0.1");
|
SysUser user = getLoginUser().getUser();
|
||||||
if(device.getLongitude()==null || device.getLongitude().equals("")){
|
device.setNetworkIp(user.getLoginIp());
|
||||||
device.setLongitude(BigDecimal.valueOf(116.23-(Math.random()*15)));
|
setLocation(user.getLoginIp(),device);
|
||||||
}
|
// if(device.getLongitude()==null || device.getLongitude().equals("")){
|
||||||
if(device.getLatitude()==null || device.getLatitude().equals("")){
|
// device.setLongitude(BigDecimal.valueOf(116.23-(Math.random()*15)));
|
||||||
device.setLatitude(BigDecimal.valueOf(39.54-(Math.random()*15)));
|
// }
|
||||||
}
|
// if(device.getLatitude()==null || device.getLatitude().equals("")){
|
||||||
if(device.getNetworkAddress()==null || device.getNetworkAddress().equals("")){
|
// device.setLatitude(BigDecimal.valueOf(39.54-(Math.random()*15)));
|
||||||
device.setNetworkAddress("中国");
|
// }
|
||||||
}
|
// if(device.getNetworkAddress()==null || device.getNetworkAddress().equals("")){
|
||||||
|
// device.setNetworkAddress("中国");
|
||||||
|
// }
|
||||||
|
|
||||||
deviceMapper.insertDevice(device);
|
deviceMapper.insertDevice(device);
|
||||||
// 添加设备用户
|
// 添加设备用户
|
||||||
DeviceUser deviceUser = new DeviceUser();
|
DeviceUser deviceUser = new DeviceUser();
|
||||||
@@ -577,11 +580,13 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
device.setUserId(userId);
|
device.setUserId(userId);
|
||||||
device.setUserName(user.getUserName());
|
device.setUserName(user.getUserName());
|
||||||
device.setFirmwareVersion(BigDecimal.valueOf(1.0));
|
device.setFirmwareVersion(BigDecimal.valueOf(1.0));
|
||||||
device.setStatus(3); // 设备状态(1-未激活,2-禁用,3-在线,4-离线)
|
// 设备状态(1-未激活,2-禁用,3-在线,4-离线)
|
||||||
|
device.setStatus(3);
|
||||||
device.setActiveTime(DateUtils.getNowDate());
|
device.setActiveTime(DateUtils.getNowDate());
|
||||||
device.setIsShadow(0);
|
device.setIsShadow(0);
|
||||||
device.setRssi(0);
|
device.setRssi(0);
|
||||||
device.setLocationWay(1); // 1-自动定位,2-设备定位,3-自定义位置
|
// 1-自动定位,2-设备定位,3-自定义位置
|
||||||
|
device.setLocationWay(1);
|
||||||
device.setCreateTime(DateUtils.getNowDate());
|
device.setCreateTime(DateUtils.getNowDate());
|
||||||
device.setThingsModelValue(JSONObject.toJSONString(getThingsModelDefaultValue(productId)));
|
device.setThingsModelValue(JSONObject.toJSONString(getThingsModelDefaultValue(productId)));
|
||||||
// 随机位置
|
// 随机位置
|
||||||
@@ -830,8 +835,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int reportDevice(Device device) {
|
public int reportDevice(Device device,Device deviceEntity) {
|
||||||
Device deviceEntity=deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
|
|
||||||
// 未采用设备定位则清空定位,定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
// 未采用设备定位则清空定位,定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
||||||
if(deviceEntity.getLocationWay()!=2){
|
if(deviceEntity.getLocationWay()!=2){
|
||||||
device.setLatitude(null);
|
device.setLatitude(null);
|
||||||
|
|||||||
Reference in New Issue
Block a user