mirror of
https://github.com/kerwincui/FastBee.git
synced 2025-10-09 10:01:51 +08:00
添加扫码添加设备接口
This commit is contained in:
@@ -8,6 +8,7 @@ import com.ruoyi.common.enums.BusinessType;
|
|||||||
import com.ruoyi.common.utils.poi.ExcelUtil;
|
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.DeviceRelateUserInput;
|
||||||
import com.ruoyi.iot.model.DeviceShortOutput;
|
import com.ruoyi.iot.model.DeviceShortOutput;
|
||||||
import com.ruoyi.iot.mqtt.EmqxService;
|
import com.ruoyi.iot.mqtt.EmqxService;
|
||||||
import com.ruoyi.iot.service.IDeviceService;
|
import com.ruoyi.iot.service.IDeviceService;
|
||||||
@@ -173,7 +174,7 @@ public class DeviceController extends BaseController
|
|||||||
* 新增设备
|
* 新增设备
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('iot:device:add')")
|
@PreAuthorize("@ss.hasPermi('iot:device:add')")
|
||||||
@Log(title = "设备", businessType = BusinessType.INSERT)
|
@Log(title = "添加设备", businessType = BusinessType.INSERT)
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@ApiOperation("添加设备")
|
@ApiOperation("添加设备")
|
||||||
public AjaxResult add(@RequestBody Device device)
|
public AjaxResult add(@RequestBody Device device)
|
||||||
@@ -181,11 +182,29 @@ public class DeviceController extends BaseController
|
|||||||
return AjaxResult.success(deviceService.insertDevice(device));
|
return AjaxResult.success(deviceService.insertDevice(device));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备关联用户
|
||||||
|
*/
|
||||||
|
@PreAuthorize("@ss.hasPermi('iot:device:add')")
|
||||||
|
@Log(title = "设备关联用户", businessType = BusinessType.UPDATE)
|
||||||
|
@PostMapping("/relateUser")
|
||||||
|
@ApiOperation("设备关联用户")
|
||||||
|
public AjaxResult relateUser(@RequestBody DeviceRelateUserInput deviceRelateUserInput)
|
||||||
|
{
|
||||||
|
if(deviceRelateUserInput.getUserId()==0 || deviceRelateUserInput.getUserId()==null){
|
||||||
|
return AjaxResult.error("用户ID不能为空");
|
||||||
|
}
|
||||||
|
if(deviceRelateUserInput.getDeviceNumberAndProductIds()==null || deviceRelateUserInput.getDeviceNumberAndProductIds().size()==0){
|
||||||
|
return AjaxResult.error("设备编号和产品ID不能为空");
|
||||||
|
}
|
||||||
|
return deviceService.deviceRelateUser(deviceRelateUserInput);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改设备
|
* 修改设备
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
||||||
@Log(title = "设备", businessType = BusinessType.UPDATE)
|
@Log(title = "修改设备", businessType = BusinessType.UPDATE)
|
||||||
@PutMapping
|
@PutMapping
|
||||||
@ApiOperation("修改设备")
|
@ApiOperation("修改设备")
|
||||||
public AjaxResult edit(@RequestBody Device device)
|
public AjaxResult edit(@RequestBody Device device)
|
||||||
@@ -197,9 +216,9 @@ public class DeviceController extends BaseController
|
|||||||
* 重置设备状态
|
* 重置设备状态
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
||||||
@Log(title = "设备", businessType = BusinessType.UPDATE)
|
@Log(title = "重置设备状态", businessType = BusinessType.UPDATE)
|
||||||
@PutMapping("/reset/{serialNumber}")
|
@PutMapping("/reset/{serialNumber}")
|
||||||
@ApiOperation("重置设备设备")
|
@ApiOperation("重置设备状态")
|
||||||
public AjaxResult resetDeviceStatus(@PathVariable String serialNumber)
|
public AjaxResult resetDeviceStatus(@PathVariable String serialNumber)
|
||||||
{
|
{
|
||||||
Device device=new Device();
|
Device device=new Device();
|
||||||
@@ -211,13 +230,16 @@ public class DeviceController extends BaseController
|
|||||||
* 删除设备
|
* 删除设备
|
||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('iot:device:remove')")
|
@PreAuthorize("@ss.hasPermi('iot:device:remove')")
|
||||||
@Log(title = "设备", businessType = BusinessType.DELETE)
|
@Log(title = "删除设备", businessType = BusinessType.DELETE)
|
||||||
@DeleteMapping("/{deviceIds}")
|
@DeleteMapping("/{deviceIds}")
|
||||||
@ApiOperation("批量删除设备")
|
@ApiOperation("批量删除设备")
|
||||||
public AjaxResult remove(@PathVariable Long[] deviceIds) throws SchedulerException {
|
public AjaxResult remove(@PathVariable Long[] deviceIds) throws SchedulerException {
|
||||||
return toAjax(deviceService.deleteDeviceByDeviceId(deviceIds[0]));
|
return toAjax(deviceService.deleteDeviceByDeviceId(deviceIds[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成设备编号
|
||||||
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
@PreAuthorize("@ss.hasPermi('iot:device:edit')")
|
||||||
@GetMapping("/generator")
|
@GetMapping("/generator")
|
||||||
@ApiOperation("生成设备编号")
|
@ApiOperation("生成设备编号")
|
||||||
|
@@ -40,6 +40,15 @@ public interface DeviceMapper
|
|||||||
*/
|
*/
|
||||||
public Device selectDeviceBySerialNumber(String serialNumber);
|
public Device selectDeviceBySerialNumber(String serialNumber);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据设备编号查询设备数量
|
||||||
|
*
|
||||||
|
* @param serialNumber 设备主键
|
||||||
|
* @return 设备
|
||||||
|
*/
|
||||||
|
public int selectDeviceCountBySerialNumber(String serialNumber);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据设备编号查询简介设备
|
* 根据设备编号查询简介设备
|
||||||
*
|
*
|
||||||
|
@@ -50,10 +50,10 @@ public interface DeviceUserMapper
|
|||||||
/**
|
/**
|
||||||
* 删除设备用户
|
* 删除设备用户
|
||||||
*
|
*
|
||||||
* @param UserIdAndDeviceIdModel 用户ID和设备ID
|
* @param UserIdDeviceIdModel 用户ID和设备ID
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int deleteDeviceUserByDeviceId(UserIdDeviceIdModel userDeviceGroupIdModel);
|
public int deleteDeviceUserByDeviceId(UserIdDeviceIdModel UserIdDeviceIdModel);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 批量删除设备用户
|
* 批量删除设备用户
|
||||||
@@ -78,5 +78,9 @@ public interface DeviceUserMapper
|
|||||||
*/
|
*/
|
||||||
public DeviceUser selectDeviceUserByDeviceIdAndUserId(@Param("deviceId") Long deviceId, @Param("userId") Long userId);
|
public DeviceUser selectDeviceUserByDeviceIdAndUserId(@Param("deviceId") Long deviceId, @Param("userId") Long userId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据deviceId 和 userId 删除设备用户,不包含设备所有者
|
||||||
|
*/
|
||||||
public int deleteDeviceUser(DeviceUser deviceUser);
|
public int deleteDeviceUser(DeviceUser deviceUser);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,38 @@
|
|||||||
|
package com.ruoyi.iot.model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author kerwincui
|
||||||
|
* @date 2021-12-16
|
||||||
|
*/
|
||||||
|
public class DeviceNumberAndProductId
|
||||||
|
{
|
||||||
|
/** 产品ID,用于自动添加设备 */
|
||||||
|
private Long productId;
|
||||||
|
|
||||||
|
/** 设备编号集合 */
|
||||||
|
private String deviceNumber;
|
||||||
|
|
||||||
|
public DeviceNumberAndProductId(){}
|
||||||
|
|
||||||
|
public DeviceNumberAndProductId(Long productId, String deviceNumber){
|
||||||
|
this.productId=productId;
|
||||||
|
this.deviceNumber=deviceNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getProductId() {
|
||||||
|
return productId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProductId(Long productId) {
|
||||||
|
this.productId = productId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeviceNumber() {
|
||||||
|
return deviceNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceNumber(String deviceNumber) {
|
||||||
|
this.deviceNumber = deviceNumber;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,41 @@
|
|||||||
|
package com.ruoyi.iot.model;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author kerwincui
|
||||||
|
* @date 2021-12-16
|
||||||
|
*/
|
||||||
|
public class DeviceRelateUserInput
|
||||||
|
{
|
||||||
|
|
||||||
|
/** 用户Id */
|
||||||
|
private Long userId;
|
||||||
|
|
||||||
|
/** 设备编号和产品ID集合 */
|
||||||
|
private List<DeviceNumberAndProductId> deviceNumberAndProductIds;
|
||||||
|
|
||||||
|
public DeviceRelateUserInput(){}
|
||||||
|
|
||||||
|
public DeviceRelateUserInput(Long userId,List<DeviceNumberAndProductId> deviceNumberAndProductIds){
|
||||||
|
this.userId=userId;
|
||||||
|
this.deviceNumberAndProductIds=deviceNumberAndProductIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getUserId() {
|
||||||
|
return userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUserId(Long userId) {
|
||||||
|
this.userId = userId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<DeviceNumberAndProductId> getDeviceNumberAndProductIds() {
|
||||||
|
return deviceNumberAndProductIds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeviceNumberAndProductIds(List<DeviceNumberAndProductId> deviceNumberAndProductIds) {
|
||||||
|
this.deviceNumberAndProductIds = deviceNumberAndProductIds;
|
||||||
|
}
|
||||||
|
}
|
@@ -118,6 +118,14 @@ public interface IDeviceService
|
|||||||
*/
|
*/
|
||||||
public Device insertDevice(Device device);
|
public Device insertDevice(Device device);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备关联用户
|
||||||
|
*
|
||||||
|
* @param deviceRelateUserInput 设备
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public AjaxResult deviceRelateUser(DeviceRelateUserInput deviceRelateUserInput);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备认证后自动添加设备
|
* 设备认证后自动添加设备
|
||||||
*
|
*
|
||||||
|
@@ -103,27 +103,27 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public DeviceStatistic selectDeviceStatistic() {
|
public DeviceStatistic selectDeviceStatistic() {
|
||||||
Device device=new Device();
|
Device device = new Device();
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 获取设备、产品和告警数量
|
// 获取设备、产品和告警数量
|
||||||
DeviceStatistic statistic=deviceMapper.selectDeviceProductAlertCount(device);
|
DeviceStatistic statistic = deviceMapper.selectDeviceProductAlertCount(device);
|
||||||
if(statistic==null){
|
if (statistic == null) {
|
||||||
statistic=new DeviceStatistic();
|
statistic = new DeviceStatistic();
|
||||||
return statistic;
|
return statistic;
|
||||||
}
|
}
|
||||||
// 获取属性、功能和事件
|
// 获取属性、功能和事件
|
||||||
DeviceStatistic thingsCount=logService.selectCategoryLogCount(device);
|
DeviceStatistic thingsCount = logService.selectCategoryLogCount(device);
|
||||||
if(thingsCount==null){
|
if (thingsCount == null) {
|
||||||
return statistic;
|
return statistic;
|
||||||
}
|
}
|
||||||
// 组合属性、功能、事件和监测数据
|
// 组合属性、功能、事件和监测数据
|
||||||
@@ -196,15 +196,15 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int reportDeviceThingsModelValue(ThingsModelValuesInput input, int type,boolean isShadow) {
|
public int reportDeviceThingsModelValue(ThingsModelValuesInput input, int type, boolean isShadow) {
|
||||||
// 查询物模型
|
// 查询物模型
|
||||||
String thingsModels = thingsModelService.getCacheThingsModelByProductId(input.getProductId());
|
String thingsModels = thingsModelService.getCacheThingsModelByProductId(input.getProductId());
|
||||||
JSONObject thingsModelObject = JSONObject.parseObject(thingsModels);
|
JSONObject thingsModelObject = JSONObject.parseObject(thingsModels);
|
||||||
List<ThingsModelValueItemDto> valueList =null;
|
List<ThingsModelValueItemDto> valueList = null;
|
||||||
if(type==1){
|
if (type == 1) {
|
||||||
JSONArray properties = thingsModelObject.getJSONArray("properties");
|
JSONArray properties = thingsModelObject.getJSONArray("properties");
|
||||||
valueList = properties.toJavaList(ThingsModelValueItemDto.class);
|
valueList = properties.toJavaList(ThingsModelValueItemDto.class);
|
||||||
}else if(type==2){
|
} else if (type == 2) {
|
||||||
JSONArray functions = thingsModelObject.getJSONArray("functions");
|
JSONArray functions = thingsModelObject.getJSONArray("functions");
|
||||||
valueList = functions.toJavaList(ThingsModelValueItemDto.class);
|
valueList = functions.toJavaList(ThingsModelValueItemDto.class);
|
||||||
}
|
}
|
||||||
@@ -213,12 +213,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
ThingsModelValuesOutput deviceThings = deviceMapper.selectDeviceThingsModelValueBySerialNumber(input.getDeviceNumber());
|
ThingsModelValuesOutput deviceThings = deviceMapper.selectDeviceThingsModelValueBySerialNumber(input.getDeviceNumber());
|
||||||
List<ThingsModelValueItem> thingsModelValues = JSONObject.parseArray(deviceThings.getThingsModelValue(), ThingsModelValueItem.class);
|
List<ThingsModelValueItem> thingsModelValues = JSONObject.parseArray(deviceThings.getThingsModelValue(), ThingsModelValueItem.class);
|
||||||
|
|
||||||
for(int i=0;i<input.getThingsModelValueRemarkItem().size();i++){
|
for (int i = 0; i < input.getThingsModelValueRemarkItem().size(); i++) {
|
||||||
// 赋值
|
// 赋值
|
||||||
for(int j=0;j<thingsModelValues.size();j++){
|
for (int j = 0; j < thingsModelValues.size(); j++) {
|
||||||
if (input.getThingsModelValueRemarkItem().get(i).getId().equals(thingsModelValues.get(j).getId())) {
|
if (input.getThingsModelValueRemarkItem().get(i).getId().equals(thingsModelValues.get(j).getId())) {
|
||||||
// 影子模式只更新影子值
|
// 影子模式只更新影子值
|
||||||
if(!isShadow){
|
if (!isShadow) {
|
||||||
thingsModelValues.get(j).setValue(String.valueOf(input.getThingsModelValueRemarkItem().get(i).getValue()));
|
thingsModelValues.get(j).setValue(String.valueOf(input.getThingsModelValueRemarkItem().get(i).getValue()));
|
||||||
}
|
}
|
||||||
thingsModelValues.get(j).setShadow(String.valueOf(input.getThingsModelValueRemarkItem().get(i).getValue()));
|
thingsModelValues.get(j).setShadow(String.valueOf(input.getThingsModelValueRemarkItem().get(i).getValue()));
|
||||||
@@ -227,7 +227,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//日志
|
//日志
|
||||||
for(int k=0;k<valueList.size();k++){
|
for (int k = 0; k < valueList.size(); k++) {
|
||||||
if (valueList.get(k).getId().equals(input.getThingsModelValueRemarkItem().get(i).getId())) {
|
if (valueList.get(k).getId().equals(input.getThingsModelValueRemarkItem().get(i).getId())) {
|
||||||
valueList.get(k).setValue(input.getThingsModelValueRemarkItem().get(i).getValue());
|
valueList.get(k).setValue(input.getThingsModelValueRemarkItem().get(i).getValue());
|
||||||
// TODO 场景联动、告警规则匹配处理
|
// TODO 场景联动、告警规则匹配处理
|
||||||
@@ -240,7 +240,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
deviceLog.setLogValue(input.getThingsModelValueRemarkItem().get(i).getValue());
|
deviceLog.setLogValue(input.getThingsModelValueRemarkItem().get(i).getValue());
|
||||||
deviceLog.setRemark(input.getThingsModelValueRemarkItem().get(i).getRemark());
|
deviceLog.setRemark(input.getThingsModelValueRemarkItem().get(i).getRemark());
|
||||||
deviceLog.setIdentity(input.getThingsModelValueRemarkItem().get(i).getId());
|
deviceLog.setIdentity(input.getThingsModelValueRemarkItem().get(i).getId());
|
||||||
deviceLog.setIsMonitor(valueList.get(k).getIsMonitor()==null ? 0:valueList.get(k).getIsMonitor());
|
deviceLog.setIsMonitor(valueList.get(k).getIsMonitor() == null ? 0 : valueList.get(k).getIsMonitor());
|
||||||
deviceLog.setLogType(type);
|
deviceLog.setLogType(type);
|
||||||
deviceLog.setUserId(deviceThings.getUserId());
|
deviceLog.setUserId(deviceThings.getUserId());
|
||||||
deviceLog.setUserName(deviceThings.getUserName());
|
deviceLog.setUserName(deviceThings.getUserName());
|
||||||
@@ -248,7 +248,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
deviceLog.setTenantName(deviceThings.getTenantName());
|
deviceLog.setTenantName(deviceThings.getTenantName());
|
||||||
deviceLog.setCreateTime(DateUtils.getNowDate());
|
deviceLog.setCreateTime(DateUtils.getNowDate());
|
||||||
// 1=影子模式,2=在线模式,3=其他
|
// 1=影子模式,2=在线模式,3=其他
|
||||||
deviceLog.setMode(isShadow?1:2);
|
deviceLog.setMode(isShadow ? 1 : 2);
|
||||||
logService.saveDeviceLog(deviceLog);
|
logService.saveDeviceLog(deviceLog);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -268,12 +268,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
@Override
|
@Override
|
||||||
public List<Device> selectDeviceList(Device device) {
|
public List<Device> selectDeviceList(Device device) {
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
}
|
}
|
||||||
@@ -290,12 +290,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
@Override
|
@Override
|
||||||
public List<Device> selectUnAuthDeviceList(Device device) {
|
public List<Device> selectUnAuthDeviceList(Device device) {
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
}
|
}
|
||||||
@@ -312,12 +312,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
@Override
|
@Override
|
||||||
public List<Device> selectDeviceListByGroup(Device device) {
|
public List<Device> selectDeviceListByGroup(Device device) {
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
}
|
}
|
||||||
@@ -332,15 +332,15 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public List<DeviceAllShortOutput> selectAllDeviceShortList() {
|
public List<DeviceAllShortOutput> selectAllDeviceShortList() {
|
||||||
Device device=new Device();
|
Device device = new Device();
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
break;
|
break;
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
break;
|
break;
|
||||||
@@ -358,13 +358,13 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
@Override
|
@Override
|
||||||
public List<DeviceShortOutput> selectDeviceShortList(Device device) {
|
public List<DeviceShortOutput> selectDeviceShortList(Device device) {
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if(roles.get(i).getRoleKey().equals("tenant")){
|
if (roles.get(i).getRoleKey().equals("tenant")) {
|
||||||
// 租户查看产品下所有设备
|
// 租户查看产品下所有设备
|
||||||
device.setTenantId(user.getUserId());
|
device.setTenantId(user.getUserId());
|
||||||
break;
|
break;
|
||||||
}else if (roles.get(i).getRoleKey().equals("general")){
|
} else if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
// 用户查看自己设备
|
// 用户查看自己设备
|
||||||
device.setUserId(user.getUserId());
|
device.setUserId(user.getUserId());
|
||||||
break;
|
break;
|
||||||
@@ -415,14 +415,14 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
// 获取value
|
// 获取value
|
||||||
for (int j = 0; j < thingsValueArray.size(); j++) {
|
for (int j = 0; j < thingsValueArray.size(); j++) {
|
||||||
if (thingsValueArray.getJSONObject(j).getString("id").equals(thingsModel.getId())) {
|
if (thingsValueArray.getJSONObject(j).getString("id").equals(thingsModel.getId())) {
|
||||||
String value=thingsValueArray.getJSONObject(j).getString("value");
|
String value = thingsValueArray.getJSONObject(j).getString("value");
|
||||||
String shadow=thingsValueArray.getJSONObject(j).getString("shadow");
|
String shadow = thingsValueArray.getJSONObject(j).getString("shadow");
|
||||||
thingsModel.setValue(value);
|
thingsModel.setValue(value);
|
||||||
thingsModel.setShadow(shadow);
|
thingsModel.setShadow(shadow);
|
||||||
// bool 类型默认值为0,解决移动端报错问题
|
// bool 类型默认值为0,解决移动端报错问题
|
||||||
if(thingsModel.getType().equals("bool")){
|
if (thingsModel.getType().equals("bool")) {
|
||||||
thingsModel.setValue(value.equals("")?"0":value);
|
thingsModel.setValue(value.equals("") ? "0" : value);
|
||||||
thingsModel.setShadow(shadow.equals("")?"0":shadow);
|
thingsModel.setShadow(shadow.equals("") ? "0" : shadow);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -518,9 +518,9 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public Device insertDevice(Device device) {
|
public Device insertDevice(Device device) {
|
||||||
// 设备编号唯一检查
|
// 设备编号唯一检查
|
||||||
Device existDevice=deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
|
Device existDevice = deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
|
||||||
if(existDevice!=null){
|
if (existDevice != null) {
|
||||||
log.error("设备编号:"+device.getSerialNumber()+"已经存在了,新增设备失败");
|
log.error("设备编号:" + device.getSerialNumber() + "已经存在了,新增设备失败");
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
SysUser sysUser = getLoginUser().getUser();
|
SysUser sysUser = getLoginUser().getUser();
|
||||||
@@ -531,14 +531,14 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
device.setUserName(sysUser.getUserName());
|
device.setUserName(sysUser.getUserName());
|
||||||
device.setRssi(0);
|
device.setRssi(0);
|
||||||
// 设置租户
|
// 设置租户
|
||||||
Product product=productService.selectProductByProductId(device.getProductId());
|
Product product = productService.selectProductByProductId(device.getProductId());
|
||||||
device.setTenantId(product.getTenantId());
|
device.setTenantId(product.getTenantId());
|
||||||
device.setTenantName(product.getTenantName());
|
device.setTenantName(product.getTenantName());
|
||||||
device.setImgUrl(product.getImgUrl());
|
device.setImgUrl(product.getImgUrl());
|
||||||
// 随机经纬度和地址
|
// 随机经纬度和地址
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
device.setNetworkIp(user.getLoginIp());
|
device.setNetworkIp(user.getLoginIp());
|
||||||
setLocation(user.getLoginIp(),device);
|
setLocation(user.getLoginIp(), device);
|
||||||
|
|
||||||
deviceMapper.insertDevice(device);
|
deviceMapper.insertDevice(device);
|
||||||
// 添加设备用户
|
// 添加设备用户
|
||||||
@@ -551,10 +551,60 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
deviceUser.setTenantId(product.getTenantId());
|
deviceUser.setTenantId(product.getTenantId());
|
||||||
deviceUser.setTenantName(product.getTenantName());
|
deviceUser.setTenantName(product.getTenantName());
|
||||||
deviceUser.setIsOwner(1);
|
deviceUser.setIsOwner(1);
|
||||||
|
deviceUser.setCreateTime(DateUtils.getNowDate());
|
||||||
deviceUserMapper.insertDeviceUser(deviceUser);
|
deviceUserMapper.insertDeviceUser(deviceUser);
|
||||||
return device;
|
return device;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备关联用户
|
||||||
|
*
|
||||||
|
* @param deviceRelateUserInput
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Transactional(rollbackFor = Exception.class)
|
||||||
|
public AjaxResult deviceRelateUser(DeviceRelateUserInput deviceRelateUserInput) {
|
||||||
|
// 查询用户信息
|
||||||
|
SysUser sysUser = userService.selectUserById(deviceRelateUserInput.getUserId());
|
||||||
|
for (int i = 0; i < deviceRelateUserInput.getDeviceNumberAndProductIds().size(); i++) {
|
||||||
|
Device existDevice = deviceMapper.selectDeviceBySerialNumber(deviceRelateUserInput.getDeviceNumberAndProductIds().get(i).getDeviceNumber());
|
||||||
|
if (existDevice != null) {
|
||||||
|
if (existDevice.getUserId().longValue() == deviceRelateUserInput.getUserId().longValue()) {
|
||||||
|
return AjaxResult.error("用户已经拥有设备:" + existDevice.getDeviceName() + ", 设备编号:" + existDevice.getSerialNumber());
|
||||||
|
}
|
||||||
|
// 先删除设备的所有用户
|
||||||
|
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(null,existDevice.getDeviceId()));
|
||||||
|
// 添加新的设备用户
|
||||||
|
DeviceUser deviceUser = new DeviceUser();
|
||||||
|
deviceUser.setUserId(sysUser.getUserId());
|
||||||
|
deviceUser.setUserName(sysUser.getUserName());
|
||||||
|
deviceUser.setPhonenumber(sysUser.getPhonenumber());
|
||||||
|
deviceUser.setDeviceId(existDevice.getDeviceId());
|
||||||
|
deviceUser.setDeviceName(existDevice.getDeviceName());
|
||||||
|
deviceUser.setTenantId(existDevice.getTenantId());
|
||||||
|
deviceUser.setTenantName(existDevice.getTenantName());
|
||||||
|
deviceUser.setIsOwner(1);
|
||||||
|
deviceUser.setCreateTime(DateUtils.getNowDate());
|
||||||
|
deviceUserMapper.insertDeviceUser(deviceUser);
|
||||||
|
// 更新设备用户信息
|
||||||
|
existDevice.setUserId(deviceRelateUserInput.getUserId());
|
||||||
|
existDevice.setUserName(sysUser.getUserName());
|
||||||
|
deviceMapper.updateDevice(existDevice);
|
||||||
|
} else {
|
||||||
|
// 自动添加设备
|
||||||
|
int result = insertDeviceAuto(
|
||||||
|
deviceRelateUserInput.getDeviceNumberAndProductIds().get(i).getDeviceNumber(),
|
||||||
|
deviceRelateUserInput.getUserId(),
|
||||||
|
deviceRelateUserInput.getDeviceNumberAndProductIds().get(i).getProductId());
|
||||||
|
if (result == 0) {
|
||||||
|
return AjaxResult.error("设备不存在,自动添加设备时失败,请检查产品编号是否正确");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return AjaxResult.success("添加设备成功");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设备认证后自动添加设备
|
* 设备认证后自动添加设备
|
||||||
*
|
*
|
||||||
@@ -562,18 +612,18 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int insertDeviceAuto(String serialNumber,Long userId,Long productId) {
|
public int insertDeviceAuto(String serialNumber, Long userId, Long productId) {
|
||||||
// 设备编号唯一检查
|
// 设备编号唯一检查
|
||||||
Device existDevice=deviceMapper.selectDeviceBySerialNumber(serialNumber);
|
int count = deviceMapper.selectDeviceCountBySerialNumber(serialNumber);
|
||||||
if(existDevice!=null){
|
if (count != 0) {
|
||||||
log.error("设备编号:"+serialNumber+"已经存在了,新增设备失败");
|
log.error("设备编号:" + serialNumber + "已经存在了,新增设备失败");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Device device = new Device();
|
Device device = new Device();
|
||||||
int random = (int) (Math.random() * (9000)) + 1000;
|
int random = (int) (Math.random() * (9000)) + 1000;
|
||||||
device.setDeviceName("设备" + random);
|
device.setDeviceName("设备" + random);
|
||||||
device.setSerialNumber(serialNumber);
|
device.setSerialNumber(serialNumber);
|
||||||
SysUser user=userService.selectUserById(userId);
|
SysUser user = userService.selectUserById(userId);
|
||||||
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));
|
||||||
@@ -587,12 +637,16 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
device.setCreateTime(DateUtils.getNowDate());
|
device.setCreateTime(DateUtils.getNowDate());
|
||||||
device.setThingsModelValue(JSONObject.toJSONString(getThingsModelDefaultValue(productId)));
|
device.setThingsModelValue(JSONObject.toJSONString(getThingsModelDefaultValue(productId)));
|
||||||
// 随机位置
|
// 随机位置
|
||||||
device.setLongitude(BigDecimal.valueOf(116.23-(Math.random()*15)));
|
device.setLongitude(BigDecimal.valueOf(116.23 - (Math.random() * 15)));
|
||||||
device.setLatitude(BigDecimal.valueOf(39.54-(Math.random()*15)));
|
device.setLatitude(BigDecimal.valueOf(39.54 - (Math.random() * 15)));
|
||||||
device.setNetworkAddress("中国");
|
device.setNetworkAddress("中国");
|
||||||
device.setNetworkIp("127.0.0.1");
|
device.setNetworkIp("127.0.0.1");
|
||||||
// 设置租户
|
// 设置租户
|
||||||
Product product=productService.selectProductByProductId(productId);
|
Product product = productService.selectProductByProductId(productId);
|
||||||
|
if (product == null) {
|
||||||
|
log.error("自动添加设备时,根据产品ID查找不到对应产品");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
device.setTenantId(product.getTenantId());
|
device.setTenantId(product.getTenantId());
|
||||||
device.setTenantName(product.getTenantName());
|
device.setTenantName(product.getTenantName());
|
||||||
device.setImgUrl(product.getImgUrl());
|
device.setImgUrl(product.getImgUrl());
|
||||||
@@ -615,6 +669,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取物模型值
|
* 获取物模型值
|
||||||
|
*
|
||||||
* @param productId
|
* @param productId
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -635,6 +690,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取设备设置的影子
|
* 获取设备设置的影子
|
||||||
|
*
|
||||||
* @param device
|
* @param device
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
@@ -654,17 +710,17 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
for (int i = 0; i < thingsModelValueItems.size(); i++) {
|
for (int i = 0; i < thingsModelValueItems.size(); i++) {
|
||||||
if (!thingsModelValueItems.get(i).getValue().equals(thingsModelValueItems.get(i).getShadow())) {
|
if (!thingsModelValueItems.get(i).getValue().equals(thingsModelValueItems.get(i).getShadow())) {
|
||||||
shadowList.add(thingsModelValueItems.get(i));
|
shadowList.add(thingsModelValueItems.get(i));
|
||||||
System.out.println("添加影子:"+thingsModelValueItems.get(i).getId());
|
System.out.println("添加影子:" + thingsModelValueItems.get(i).getId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ThingsModelShadow shadow=new ThingsModelShadow();
|
ThingsModelShadow shadow = new ThingsModelShadow();
|
||||||
for (int i = 0; i < shadowList.size(); i++) {
|
for (int i = 0; i < shadowList.size(); i++) {
|
||||||
boolean isGetValue = false;
|
boolean isGetValue = false;
|
||||||
for (int j = 0; j < properties.size(); j++) {
|
for (int j = 0; j < properties.size(); j++) {
|
||||||
if (properties.getJSONObject(j).getInteger("isMonitor")==0 && properties.getJSONObject(j).getString("id").equals(shadowList.get(i).getId())) {
|
if (properties.getJSONObject(j).getInteger("isMonitor") == 0 && properties.getJSONObject(j).getString("id").equals(shadowList.get(i).getId())) {
|
||||||
IdentityAndName item = new IdentityAndName(shadowList.get(i).getId(), shadowList.get(i).getShadow());
|
IdentityAndName item = new IdentityAndName(shadowList.get(i).getId(), shadowList.get(i).getShadow());
|
||||||
shadow.getProperties().add(item);
|
shadow.getProperties().add(item);
|
||||||
System.out.println("添加影子属性:"+item.getId());
|
System.out.println("添加影子属性:" + item.getId());
|
||||||
isGetValue = true;
|
isGetValue = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -674,7 +730,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
if (functions.getJSONObject(k).getString("id").equals(shadowList.get(i).getId())) {
|
if (functions.getJSONObject(k).getString("id").equals(shadowList.get(i).getId())) {
|
||||||
IdentityAndName item = new IdentityAndName(shadowList.get(i).getId(), shadowList.get(i).getShadow());
|
IdentityAndName item = new IdentityAndName(shadowList.get(i).getId(), shadowList.get(i).getShadow());
|
||||||
shadow.getFunctions().add(item);
|
shadow.getFunctions().add(item);
|
||||||
System.out.println("添加影子功能:"+item.getId());
|
System.out.println("添加影子功能:" + item.getId());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -686,18 +742,19 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改设备
|
* 修改设备
|
||||||
|
*
|
||||||
* @param device 设备
|
* @param device 设备
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public AjaxResult updateDevice(Device device) {
|
public AjaxResult updateDevice(Device device) {
|
||||||
// 设备编号唯一检查
|
// 设备编号唯一检查
|
||||||
Device oldDevice=deviceMapper.selectDeviceByDeviceId(device.getDeviceId());
|
Device oldDevice = deviceMapper.selectDeviceByDeviceId(device.getDeviceId());
|
||||||
if(!oldDevice.getSerialNumber().equals(device.getSerialNumber())){
|
if (!oldDevice.getSerialNumber().equals(device.getSerialNumber())) {
|
||||||
Device existDevice=deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
|
Device existDevice = deviceMapper.selectDeviceBySerialNumber(device.getSerialNumber());
|
||||||
if(existDevice!=null){
|
if (existDevice != null) {
|
||||||
log.error("设备编号:"+device.getSerialNumber()+" 已经存在,新增设备失败");
|
log.error("设备编号:" + device.getSerialNumber() + " 已经存在,新增设备失败");
|
||||||
return AjaxResult.success("设备编号:"+device.getSerialNumber()+" 已经存在,修改失败",0);
|
return AjaxResult.success("设备编号:" + device.getSerialNumber() + " 已经存在,修改失败", 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
device.setUpdateTime(DateUtils.getNowDate());
|
device.setUpdateTime(DateUtils.getNowDate());
|
||||||
@@ -710,26 +767,27 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
}
|
}
|
||||||
deviceMapper.updateDevice(device);
|
deviceMapper.updateDevice(device);
|
||||||
// 设备取消禁用
|
// 设备取消禁用
|
||||||
if(oldDevice.getStatus()==2 && device.getStatus()==4){
|
if (oldDevice.getStatus() == 2 && device.getStatus() == 4) {
|
||||||
// 发布设备信息
|
// 发布设备信息
|
||||||
emqxService.publishInfo(oldDevice.getProductId(),oldDevice.getSerialNumber());
|
emqxService.publishInfo(oldDevice.getProductId(), oldDevice.getSerialNumber());
|
||||||
}
|
}
|
||||||
return AjaxResult.success("修改成功",1);
|
return AjaxResult.success("修改成功", 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成设备唯一编号
|
* 生成设备唯一编号
|
||||||
|
*
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String generationDeviceNum() {
|
public String generationDeviceNum() {
|
||||||
// 设备编号:D + userId + 15位随机字母和数字
|
// 设备编号:D + userId + 15位随机字母和数字
|
||||||
SysUser user = getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
String number= "D"+user.getUserId().toString()+toolService.getStringRandom(10);
|
String number = "D" + user.getUserId().toString() + toolService.getStringRandom(10);
|
||||||
int count= deviceMapper.getDeviceNumCount(number);
|
int count = deviceMapper.getDeviceNumCount(number);
|
||||||
if(count==0) {
|
if (count == 0) {
|
||||||
return number;
|
return number;
|
||||||
}else{
|
} else {
|
||||||
generationDeviceNum();
|
generationDeviceNum();
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
@@ -737,16 +795,15 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param device 设备状态和定位更新
|
* @param device 设备状态和定位更新
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int updateDeviceStatusAndLocation(Device device,String ipAddress) {
|
public int updateDeviceStatusAndLocation(Device device, String ipAddress) {
|
||||||
// 设置自动定位和状态
|
// 设置自动定位和状态
|
||||||
if(ipAddress!="") {
|
if (ipAddress != "") {
|
||||||
if(device.getActiveTime()==null){
|
if (device.getActiveTime() == null) {
|
||||||
device.setActiveTime(DateUtils.getNowDate());
|
device.setActiveTime(DateUtils.getNowDate());
|
||||||
}
|
}
|
||||||
// 定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
// 定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
||||||
@@ -755,7 +812,7 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
setLocation(ipAddress, device);
|
setLocation(ipAddress, device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int result=deviceMapper.updateDeviceStatus(device);
|
int result = deviceMapper.updateDeviceStatus(device);
|
||||||
|
|
||||||
// 添加到设备日志
|
// 添加到设备日志
|
||||||
DeviceLog deviceLog = new DeviceLog();
|
DeviceLog deviceLog = new DeviceLog();
|
||||||
@@ -770,12 +827,12 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
deviceLog.setCreateTime(DateUtils.getNowDate());
|
deviceLog.setCreateTime(DateUtils.getNowDate());
|
||||||
// 日志模式 1=影子模式,2=在线模式,3=其他
|
// 日志模式 1=影子模式,2=在线模式,3=其他
|
||||||
deviceLog.setMode(3);
|
deviceLog.setMode(3);
|
||||||
if(device.getStatus()==3){
|
if (device.getStatus() == 3) {
|
||||||
deviceLog.setLogValue("1");
|
deviceLog.setLogValue("1");
|
||||||
deviceLog.setRemark("设备上线");
|
deviceLog.setRemark("设备上线");
|
||||||
deviceLog.setIdentity("online");
|
deviceLog.setIdentity("online");
|
||||||
deviceLog.setLogType(5);
|
deviceLog.setLogType(5);
|
||||||
}else if(device.getStatus()==4){
|
} else if (device.getStatus() == 4) {
|
||||||
deviceLog.setLogValue("0");
|
deviceLog.setLogValue("0");
|
||||||
deviceLog.setRemark("设备离线");
|
deviceLog.setRemark("设备离线");
|
||||||
deviceLog.setIdentity("offline");
|
deviceLog.setIdentity("offline");
|
||||||
@@ -786,7 +843,6 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @param device 设备状态
|
* @param device 设备状态
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@@ -797,114 +853,115 @@ public class DeviceServiceImpl implements IDeviceService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据IP获取地址
|
* 根据IP获取地址
|
||||||
|
*
|
||||||
* @param ip
|
* @param ip
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private void setLocation(String ip,Device device){
|
private void setLocation(String ip, Device device) {
|
||||||
String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
|
String IP_URL = "http://whois.pconline.com.cn/ipJson.jsp";
|
||||||
String address = "未知地址";
|
String address = "未知地址";
|
||||||
// 内网不查询
|
// 内网不查询
|
||||||
if (IpUtils.internalIp(ip))
|
if (IpUtils.internalIp(ip)) {
|
||||||
{
|
device.setNetworkAddress("内网IP");
|
||||||
device.setNetworkAddress( "内网IP");
|
|
||||||
}
|
}
|
||||||
try
|
try {
|
||||||
{
|
|
||||||
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
|
String rspStr = HttpUtils.sendGet(IP_URL, "ip=" + ip + "&json=true", Constants.GBK);
|
||||||
if (!StringUtils.isEmpty(rspStr))
|
if (!StringUtils.isEmpty(rspStr)) {
|
||||||
{
|
|
||||||
JSONObject obj = JSONObject.parseObject(rspStr);
|
JSONObject obj = JSONObject.parseObject(rspStr);
|
||||||
device.setNetworkAddress(obj.getString("addr"));
|
device.setNetworkAddress(obj.getString("addr"));
|
||||||
System.out.println(device.getSerialNumber()+"- 设置地址:"+obj.getString("addr"));
|
System.out.println(device.getSerialNumber() + "- 设置地址:" + obj.getString("addr"));
|
||||||
// 查询经纬度
|
// 查询经纬度
|
||||||
setLatitudeAndLongitude(obj.getString("city"),device);
|
setLatitudeAndLongitude(obj.getString("city"), device);
|
||||||
}
|
}
|
||||||
}
|
} catch (Exception e) {
|
||||||
catch (Exception e){
|
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置经纬度
|
* 设置经纬度
|
||||||
|
*
|
||||||
* @param city
|
* @param city
|
||||||
*/
|
*/
|
||||||
private void setLatitudeAndLongitude(String city,Device device){
|
private void setLatitudeAndLongitude(String city, Device device) {
|
||||||
String BAIDU_URL="https://api.map.baidu.com/geocoder";
|
String BAIDU_URL = "https://api.map.baidu.com/geocoder";
|
||||||
String baiduResponse = HttpUtils.sendGet(BAIDU_URL,"address="+city + "&output=json", Constants.GBK);
|
String baiduResponse = HttpUtils.sendGet(BAIDU_URL, "address=" + city + "&output=json", Constants.GBK);
|
||||||
if(!StringUtils.isEmpty(baiduResponse)){
|
if (!StringUtils.isEmpty(baiduResponse)) {
|
||||||
JSONObject baiduObject = JSONObject.parseObject(baiduResponse);
|
JSONObject baiduObject = JSONObject.parseObject(baiduResponse);
|
||||||
JSONObject location=baiduObject.getJSONObject("result").getJSONObject("location");
|
JSONObject location = baiduObject.getJSONObject("result").getJSONObject("location");
|
||||||
device.setLongitude(location.getBigDecimal("lng"));
|
device.setLongitude(location.getBigDecimal("lng"));
|
||||||
device.setLatitude(location.getBigDecimal("lat"));
|
device.setLatitude(location.getBigDecimal("lat"));
|
||||||
System.out.println(device.getSerialNumber()+"- 设置经度:"+location.getBigDecimal("lng")+",设置纬度:"+location.getBigDecimal("lat"));
|
System.out.println(device.getSerialNumber() + "- 设置经度:" + location.getBigDecimal("lng") + ",设置纬度:" + location.getBigDecimal("lat"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上报设备信息
|
* 上报设备信息
|
||||||
|
*
|
||||||
* @param device 设备
|
* @param device 设备
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int reportDevice(Device device,Device deviceEntity) {
|
public int reportDevice(Device device, Device deviceEntity) {
|
||||||
// 未采用设备定位则清空定位,定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
// 未采用设备定位则清空定位,定位方式(1=ip自动定位,2=设备定位,3=自定义)
|
||||||
if(deviceEntity.getLocationWay()!=2){
|
if (deviceEntity.getLocationWay() != 2) {
|
||||||
device.setLatitude(null);
|
device.setLatitude(null);
|
||||||
device.setLongitude(null);
|
device.setLongitude(null);
|
||||||
}
|
}
|
||||||
int result=0;
|
int result = 0;
|
||||||
if(deviceEntity!=null){
|
if (deviceEntity != null) {
|
||||||
// 更新设备信息
|
// 更新设备信息
|
||||||
device.setUpdateTime(DateUtils.getNowDate());
|
device.setUpdateTime(DateUtils.getNowDate());
|
||||||
if(deviceEntity.getActiveTime()==null || deviceEntity.getActiveTime().equals("")) {
|
if (deviceEntity.getActiveTime() == null || deviceEntity.getActiveTime().equals("")) {
|
||||||
device.setActiveTime(DateUtils.getNowDate());
|
device.setActiveTime(DateUtils.getNowDate());
|
||||||
}
|
}
|
||||||
device.setThingsModelValue(null);
|
device.setThingsModelValue(null);
|
||||||
result= deviceMapper.updateDeviceBySerialNumber(device);
|
result = deviceMapper.updateDeviceBySerialNumber(device);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 重置设备状态
|
* 重置设备状态
|
||||||
|
*
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public int resetDeviceStatus(String deviceNum) {
|
public int resetDeviceStatus(String deviceNum) {
|
||||||
int result=deviceMapper.resetDeviceStatus(deviceNum);
|
int result = deviceMapper.resetDeviceStatus(deviceNum);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除设备
|
* 删除设备
|
||||||
|
*
|
||||||
* @param deviceId 需要删除的设备主键
|
* @param deviceId 需要删除的设备主键
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@Transactional(rollbackFor = Exception.class)
|
@Transactional(rollbackFor = Exception.class)
|
||||||
public int deleteDeviceByDeviceId(Long deviceId) throws SchedulerException {
|
public int deleteDeviceByDeviceId(Long deviceId) throws SchedulerException {
|
||||||
SysUser user=getLoginUser().getUser();
|
SysUser user = getLoginUser().getUser();
|
||||||
// 是否为普通用户,普通用户如果不是设备所有者,只能删除设备用户和用户自己的设备关联分组信息
|
// 是否为普通用户,普通用户如果不是设备所有者,只能删除设备用户和用户自己的设备关联分组信息
|
||||||
boolean isGeneralUser=false;
|
boolean isGeneralUser = false;
|
||||||
List<SysRole> roles=user.getRoles();
|
List<SysRole> roles = user.getRoles();
|
||||||
for(int i=0;i<roles.size();i++){
|
for (int i = 0; i < roles.size(); i++) {
|
||||||
if (roles.get(i).getRoleKey().equals("general")){
|
if (roles.get(i).getRoleKey().equals("general")) {
|
||||||
isGeneralUser=true;
|
isGeneralUser = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Device device=deviceMapper.selectDeviceByDeviceId(deviceId);
|
Device device = deviceMapper.selectDeviceByDeviceId(deviceId);
|
||||||
if(isGeneralUser && device.getUserId().longValue()!=user.getUserId()){
|
if (isGeneralUser && device.getUserId().longValue() != user.getUserId()) {
|
||||||
// 删除用户分组中的设备 普通用户,且不是设备所有者。
|
// 删除用户分组中的设备 普通用户,且不是设备所有者。
|
||||||
deviceMapper.deleteDeviceGroupByDeviceId(new UserIdDeviceIdModel(user.getUserId(),deviceId));
|
deviceMapper.deleteDeviceGroupByDeviceId(new UserIdDeviceIdModel(user.getUserId(), deviceId));
|
||||||
// 删除用户的设备用户信息。
|
// 删除用户的设备用户信息。
|
||||||
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(user.getUserId(),deviceId));
|
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(user.getUserId(), deviceId));
|
||||||
}else{
|
} else {
|
||||||
// 删除设备分组。 租户、管理员和设备所有者
|
// 删除设备分组。 租户、管理员和设备所有者
|
||||||
deviceMapper.deleteDeviceGroupByDeviceId(new UserIdDeviceIdModel(null,deviceId));
|
deviceMapper.deleteDeviceGroupByDeviceId(new UserIdDeviceIdModel(null, deviceId));
|
||||||
// 删除设备用户。
|
// 删除设备用户。
|
||||||
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(null,deviceId));
|
deviceUserMapper.deleteDeviceUserByDeviceId(new UserIdDeviceIdModel(null, deviceId));
|
||||||
// 删除定时任务
|
// 删除定时任务
|
||||||
deviceJobService.deleteJobByDeviceIds(new Long[]{deviceId});
|
deviceJobService.deleteJobByDeviceIds(new Long[]{deviceId});
|
||||||
// 批量删除设备日志
|
// 批量删除设备日志
|
||||||
|
@@ -229,6 +229,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where serial_number = #{serialNumber}
|
where serial_number = #{serialNumber}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
<select id="selectDeviceCountBySerialNumber" parameterType="String" resultType="int">
|
||||||
|
select count(device_id) from iot_device
|
||||||
|
where serial_number = #{serialNumber}
|
||||||
|
</select>
|
||||||
|
|
||||||
<select id="selectDeviceProductAlertCount" parameterType="com.ruoyi.iot.domain.Device" resultType="com.ruoyi.iot.model.DeviceStatistic">
|
<select id="selectDeviceProductAlertCount" parameterType="com.ruoyi.iot.domain.Device" resultType="com.ruoyi.iot.model.DeviceStatistic">
|
||||||
select
|
select
|
||||||
<!--设备数量-->
|
<!--设备数量-->
|
||||||
|
Reference in New Issue
Block a user