This commit is contained in:
邱贞招 2025-02-15 16:24:00 +08:00
parent 45f09ac20a
commit b76441fe3a
8 changed files with 140 additions and 41 deletions

View File

@ -23,6 +23,7 @@ import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@ -50,6 +51,9 @@ public class AsProfileController extends BaseController
@Resource
private IAsDeviceService asDeviceService;
@Value(value = "${watering.defaultName}")
private String defaultName;
/**
* 个人信息
*/
@ -59,8 +63,7 @@ public class AsProfileController extends BaseController
LoginUser loginUser = getLoginUser();
AsUser user = loginUser.getAsUser();
AsUser asUser = asUserService.selectUserById(user.getUserId());
AjaxResult ajax = AjaxResult.success(asUser);
return ajax;
return AjaxResult.success(asUser);
}
/**
@ -167,7 +170,7 @@ public class AsProfileController extends BaseController
}
asDevice.setModel(asModel.getModel());
asDevice.setModelName(asModel.getModelName());
asDevice.setDeviceName("丁丁浇花器");
asDevice.setDeviceName(defaultName);
asDevice.setCreateTime(DateUtils.getNowDate());
return toAjax(asDeviceService.bandSn(asDevice));
}

View File

@ -33,6 +33,9 @@ watering:
token: tVpNdGKrAFHfKZNgpIWQfZukrcYHNfFM
# 创建设备地址
deviceUrl: https://iot-api.heclouds.com/device
# 默认名称
defaultName: 丁丁浇花器
# 百度植物识别
baidu:
tokenUrl: https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials
@ -197,7 +200,7 @@ qiniu:
# 七牛云命名空间
bucket: autosprout
# 过期时间(秒)
expireSeconds: 600
expireSeconds: 86400
# 七牛云GET请求域名
domain: https://lxnapi.ccttiot.com
xinzhi:

View File

@ -0,0 +1,37 @@
package com.ruoyi.common.utils;
import com.ruoyi.common.exception.ServiceException;
/**
* @author
* 2024/3/4
*/
public class ServiceUtil {
/**
* 判断是否满足条件满足则抛出异常
* @param flag 条件
* @param msg 异常说明
*/
public static void assertion(boolean flag, String msg) {
assertion(flag, msg, 500);
}
/**
* 判断是否满足条件满足则抛出异常
* @param flag 条件
* @param msg 异常说明
* @param code 业务代码
*/
public static void assertion(boolean flag, String msg, int code) {
if (flag) {
throw new ServiceException(msg, code);
}
}
public static void assertion(boolean flag, String format, Object ...args) {
if (flag) {
throw new ServiceException(String.format(format, args), 500);
}
}
}

View File

@ -15,6 +15,7 @@ import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.common.utils.onenet.Token;
import com.ruoyi.device.domain.*;
import com.ruoyi.device.domain.vo.IdentifyRes;
import com.ruoyi.device.domain.vo.IsBandVO;
import com.ruoyi.device.service.*;
import com.ruoyi.system.service.ISysConfigService;
import lombok.SneakyThrows;
@ -210,6 +211,15 @@ public class AppController extends BaseController
{
return success(asDeviceService.bandDevice(asDevice));
}
//
// /**
// * 根据mac判断设备是否绑定
// */
// @GetMapping("/isBand")
// public AjaxResult isBand(List<String> macList)
// {
// return success(asDeviceService.isBand(macList));
// }
/**
* 解绑设备
@ -420,13 +430,15 @@ public class AppController extends BaseController
Boolean collection = asUserCollectionService.isCollection(plantId, userId);
return AjaxResult.success(collection);
}
/**
* 根据mac号查询设备是否被绑定
*/
@GetMapping("/device/isBand/{mac}")
public AjaxResult isBand(@PathVariable String mac)
public AjaxResult isBand(@PathVariable List<String> mac)
{
Boolean isBand = asDeviceService.isBand(mac);
logger.info("根据mac号查询设备是否被绑定传参-----{}", JSON.toJSONString(mac));
List<IsBandVO> isBand = asDeviceService.isBand(mac);
return AjaxResult.success(isBand);
}

View File

@ -0,0 +1,13 @@
package com.ruoyi.device.domain.vo;
import lombok.Data;
@Data
public class IsBandVO {
/** 设备mac */
private String mac;
/** 是否绑定 */
private Boolean isBand;
}

View File

@ -5,6 +5,7 @@ import java.util.List;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.device.domain.AsDevice;
import com.ruoyi.device.domain.AsDeviceQuery;
import com.ruoyi.device.domain.vo.IsBandVO;
import com.ruoyi.device.domain.vo.WeatherResponse;
/**
@ -110,7 +111,8 @@ public interface IAsDeviceService
* @param mac 设备mac
* @return 结果
*/
Boolean isBand(String mac);
List<IsBandVO> isBand(List<String> mac);
/**
* 切换默认展示设备

View File

@ -10,14 +10,15 @@ import com.ruoyi.common.core.domain.entity.AsUser;
import com.ruoyi.common.core.domain.onenet.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.onenet.CreateDeviceVo;
import com.ruoyi.common.utils.onenet.IotUtil;
import com.ruoyi.common.utils.onenet.Token;
import com.ruoyi.common.utils.pinyin.PinyinUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.device.domain.*;
import com.ruoyi.device.domain.vo.IPVo;
import com.ruoyi.device.domain.vo.IsBandVO;
import com.ruoyi.device.domain.vo.WeatherResponse;
import com.ruoyi.device.mapper.*;
import com.ruoyi.device.service.IAsDeviceService;
@ -30,16 +31,18 @@ import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionTemplate;
import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -82,6 +85,9 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
@Resource
private IAsWateringRecordService asWateringRecordService;
@Autowired
private TransactionTemplate transactionTemplate;
@Value(value = "${watering.iotUrl}")
private String iotUrl;
@ -106,6 +112,9 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
@Value(value = "${watering.deviceUrl}")
private String deviceUrl;
@Value(value = "${watering.defaultName}")
private String defaultName;
/**
* 查询设备详情
@ -123,7 +132,9 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
}
/** 请求onenet更新设备参数*/
updateDeviceParam(device);
device.setPicture(modelMapper.selectAsModelByModelId(device.getModelId()).getPicture());
if(device.getModelId() != null){
device.setPicture(modelMapper.selectAsModelByModelId(device.getModelId()).getPicture());
}
return device;
}
@ -199,7 +210,7 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
List<DatapointValue.Ds> dsArray = value.getDsArray();
//清除无效定时器
List<DatapointValue.Ds> ds = asTimerService.cleanInvalidDs(dsArray);
if(sw && ds.size()>0){//如果定时器开启且定时器列表不为空
if(sw && !ds.isEmpty()){//如果定时器开启且定时器列表不为空
modeList.add("1");
}
Boolean kaiguan = tr.getKaiguan();
@ -211,7 +222,7 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
asDevice1.setCurrentSoilMoisture(value.getTuran_show());
List<AsWateringRecord> asWateringRecords = asWateringRecordService.selectAsWateringRecordList(AsWateringRecord.builder().deviceId(asDevice1.getDeviceId()).build());
// 根据asWateringRecords查询最后一次浇水时间
if(asWateringRecords.size()>0){
if(!asWateringRecords.isEmpty()){
asDevice1.setLastWatering(asWateringRecords.get(0).getWaterTime());
}
/**将value转成device对象并保存到数据库*/
@ -418,17 +429,17 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
@SneakyThrows
@Override
public AjaxResult watering(AsDevice device,Integer wateringSwitch) {
/** 1.拼接url */
/* 1.拼接url */
String deviceName = device.getMac();//mac地址就是产品名称
String param = "device_name=" + deviceName + "&product_id=" + productId +"&timeout=" + timeout;
String sendUrl = iotUrl+ IotConstants.ADDS_COMMAND + "?"+param;
logger.info("IOT请求地址:【{}】", sendUrl);
/** 2.获取token */
/* 2.获取token */
String token = Token.getToken();
logger.info("IOT获取到Authorization:【{}】",token);
/** 3.请求下发 */
/* 3.请求下发 */
String command = wateringSwitch == 1 ? IotConstants.COMMAND_OPEN : IotConstants.COMMAND_CLOSE;
String result = HttpUtils.sendPostWithToken(sendUrl, command, token);
JSONObject paramsObj = JSON.parseObject(result);
@ -463,25 +474,31 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
*/
@Override
public Boolean bandDevice(AsDevice asDevice) {
/** 根据mac号获取到设备详情*/
String mac = asDevice.getMac();
AsDevice device = asDeviceMapper.selectAsDeviceByMac(mac);
if(ObjectUtils.isEmpty(device)){
throw new ServiceException("该设备不存在");
}
if(ObjectUtils.isNotEmpty(device.getUserId()) && device.getUserId()>0){
throw new ServiceException("该设备已经绑定用户,请先解绑!");
}
device.setUserId(asDevice.getUserId());
device.setBluetoothId(asDevice.getBluetoothId());
device.setBluetoothName(asDevice.getBluetoothName());
int i = asDeviceMapper.updateAsDevice(device);
if(i>0){
toggleDevice(asDevice.getUserId(), device.getDeviceId());
return true;
}else{
return false;
}
AsDevice device = asDeviceMapper.selectAsDeviceByMac(asDevice.getMac());
/* 根据mac号获取到设备详情*/
Boolean execute = transactionTemplate.execute(e -> {
if(ObjectUtils.isEmpty(device)){
asDevice.setDeviceName(defaultName);
int i = bandSn(asDevice);
ServiceUtil.assertion(i == 0, "录入失败!");
}else{
if(ObjectUtils.isNotEmpty(device.getUserId()) && device.getUserId()>0){
throw new ServiceException("该设备已经绑定用户,请先解绑!");
}
device.setUserId(asDevice.getUserId());
device.setBluetoothId(asDevice.getBluetoothId());
device.setBluetoothName(asDevice.getBluetoothName());
int i = asDeviceMapper.updateAsDevice(device);
ServiceUtil.assertion(i == 0, "绑定失败!");
// 切换默认设备
toggleDevice(asDevice.getUserId(), device.getDeviceId());
}
logger.info("=================【绑定设备】成功==================");
return Boolean.TRUE;
});
if(Boolean.FALSE.equals(execute))throw new ServiceException("绑定失败");
return true;
}
/**
@ -500,7 +517,7 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
AsDeviceQuery asDeviceQuery = new AsDeviceQuery();
asDeviceQuery.setUserId(device2.getUserId());
List<AsDevice> asDevices = asDeviceMapper.selectAsDeviceList(asDeviceQuery);
if(asDevices.size()>0){
if(!asDevices.isEmpty()){
AsDevice device1 = asDevices.get(0);
device1.setIsDefault("1");
asDeviceMapper.updateAsDevice(device1);
@ -530,12 +547,24 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
* @param mac 设备mac
* @return 结果
*/
@Override
public Boolean isBand(String mac) {
int i = asDeviceMapper.isBandByMac(mac);
return i>0;
}
@Override
public List<IsBandVO> isBand(List<String> macList) {
List<IsBandVO> isBandVOS = new ArrayList<>();
for(String mac:macList){
IsBandVO isBandVO = new IsBandVO();
isBandVO.setMac(mac);
isBandVO.setIsBand(isBand(mac));
isBandVOS.add(isBandVO);
}
return isBandVOS;
}
/**
* 切换默认展示设备
@ -547,9 +576,9 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
asDeviceQuery.setUserId(userId);
asDeviceQuery.setIsDefault("1");
List<AsDevice> asDevices = asDeviceMapper.selectAsDeviceList(asDeviceQuery);
if(ObjectUtils.isNotEmpty(asDevices) && asDevices.size()>0){
if(ObjectUtils.isNotEmpty(asDevices) && !asDevices.isEmpty()){
for(AsDevice device:asDevices){
if(device.getDeviceId()!=deviceId){
if(!Objects.equals(device.getDeviceId(), deviceId)){
asDeviceMapper.updateAsDevice(AsDevice.builder().deviceId(device.getDeviceId()).isDefault("0").build());
}
}
@ -564,14 +593,14 @@ public class AsDeviceServiceImpl extends ServiceImpl<AsDeviceMapper, AsDevice> i
public WeatherResponse getWeather(String ipAddr) {
String location = "";
if(StringUtils.isNotEmpty(ipAddr)){
/** 获取归属地 https://opendata.baidu.com/api.php?query=36.28.139.223&co=&resource_id=6006&oe=utf8*/
/* 获取归属地 https://opendata.baidu.com/api.php?query=36.28.139.223&co=&resource_id=6006&oe=utf8*/
String url = "https://opendata.baidu.com/api.php?query="+ ipAddr +"&co=&resource_id=6006&oe=utf8";
String sendGet = HttpUtils.sendGet(url);
logger.info("获取到ip响应【{}】",sendGet);
IPVo vo = JSONObject.parseObject(sendGet,IPVo.class);
logger.info("转换成IPVo对象【{}】",JSON.toJSONString(vo));
List<IPVo.IpData> ipData = vo.getData();
if(ObjectUtils.isNotEmpty(ipData) && ipData.size()>0){
if(ObjectUtils.isNotEmpty(ipData) && !ipData.isEmpty()){
IPVo.IpData ip = ipData.get(0);
String locationStr = ip.getLocation();
//将location中去掉'电信''联通''移动'字符串后去掉空格再将字符串中的省份去掉再将字符串中的市去掉

View File

@ -85,7 +85,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="isBandByMac" resultType="java.lang.Integer">
select count(1) from as_device
where mac = #{mac} and user_id IS NOT NULL and is_network = 1 and user_id != 0
where mac = #{mac} and user_id IS NOT NULL and user_id != 0
</select>
<insert id="insertAsDevice" parameterType="AsDevice" useGeneratedKeys="true" keyProperty="deviceId">