autosprout/AutoSprout-watering/src/main/java/com/ruoyi/device/app/AppController.java

539 lines
17 KiB
Java
Raw Normal View History

package com.ruoyi.device.app;
import com.alibaba.fastjson2.JSON;
2024-04-25 16:46:22 +08:00
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
2023-12-15 15:39:32 +08:00
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
2024-10-18 21:59:28 +08:00
import com.ruoyi.common.core.domain.entity.AsArticleClassify;
import com.ruoyi.common.core.page.TableDataInfo;
2023-12-15 15:39:32 +08:00
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.baidu.GetToken;
import com.ruoyi.common.utils.http.HttpUtils;
2024-04-25 16:46:22 +08:00
import com.ruoyi.common.utils.ip.IpUtils;
2024-11-06 16:59:37 +08:00
import com.ruoyi.common.utils.onenet.Token;
import com.ruoyi.device.domain.*;
import com.ruoyi.device.domain.vo.IdentifyRes;
2025-02-15 16:24:00 +08:00
import com.ruoyi.device.domain.vo.IsBandVO;
import com.ruoyi.device.service.*;
2024-10-18 21:59:28 +08:00
import com.ruoyi.system.service.ISysConfigService;
import lombok.SneakyThrows;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
2023-12-15 15:39:32 +08:00
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
2024-11-06 16:59:37 +08:00
import java.io.UnsupportedEncodingException;
2024-04-03 17:38:01 +08:00
import java.math.BigDecimal;
import java.net.URLEncoder;
2024-11-06 16:59:37 +08:00
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
2024-04-24 15:53:18 +08:00
import java.util.ArrayList;
import java.util.List;
/**
* 用户信息
*
* @author ruoyi
*/
@RestController
@RequestMapping("/app")
public class AppController extends BaseController
{
@Resource
private IAsUserService asUserService;
@Resource
private IAsWateringRecordService asWateringRecordService;
2023-12-15 15:39:32 +08:00
@Resource
private IAsDeviceService asDeviceService;
2023-12-15 15:39:32 +08:00
@Resource
private IAsDeviceVersionService asDeviceVersionService;
@Autowired
private IAsTimerService asTimerService;
@Autowired
private IAsPlantService asPlantService;
@Autowired
private IAsArticleService asArticleService;
@Autowired
private IAsPlantIdentifyLogService asPlantIdentifyLogService;
@Autowired
private IAsPlantAnalysisLogService asPlantAnalysisLogService;
2024-04-03 17:38:01 +08:00
@Autowired
private IAsUserCollectionService asUserCollectionService;
2024-04-24 15:53:18 +08:00
@Autowired
private IAsModelService asModelService;
2024-10-18 21:59:28 +08:00
@Autowired
private IAsArticleClassifyService asArticleClassifyService;
@Autowired
private ISysConfigService configService;
@Resource
private IAsFeedbackService asFeedbackService;
@Value(value = "${baidu.identifyUrl}")
private String identifyUrl;
/**
* 查询文章列表
*/
@GetMapping("/article/list")
2024-10-18 21:59:28 +08:00
public TableDataInfo articleList(AsArticle asArticle)
{
startPage();
List<AsArticle> list = asArticleService.selectAsArticleList(asArticle);
return getDataTable(list);
}
2023-12-15 15:39:32 +08:00
2024-10-18 21:59:28 +08:00
/**
* 获取分类列表
*/
@GetMapping("/articleClassify/list")
public AjaxResult articleClassifyList(AsArticleClassify asArticleClassify)
{
List<AsArticleClassify> asArticleClassifies = asArticleClassifyService.selectClassifyList(asArticleClassify);
return success(asArticleClassifies);
}
/**
* 获取文章详细信息
*/
@GetMapping(value = "/article/{articleId}")
public AjaxResult getArticleInfo(@PathVariable("articleId") Long articleId)
{
return success(asArticleService.selectAsArticleByArticleId(articleId));
}
/**
* 根据登录用户获取绑定设备
*/
@GetMapping("/getDeviceInfoByUser")
public AjaxResult list(Long userId,String isDefault)
{
List<AsDevice> device = asUserService.selectDeviceInfoByUser(userId,isDefault);
return AjaxResult.success(device);
}
2024-11-02 17:00:08 +08:00
/**
* 修改设备名称
*/
@PutMapping("/editDeviceName")
public AjaxResult editDeviceName(Long deviceId, String deviceName)
{
logger.info("接收到修改设备名称请求deviceId=【{}】,deviceName=【{}】",deviceId,deviceName);
int i = asDeviceService.updateAsDevice(AsDevice.builder().deviceId(deviceId).deviceName(deviceName).build());
return AjaxResult.success(i);
}
/**
* 切换默认展示设备
*/
@PutMapping("/toggleDevice")
public AjaxResult toggleDevice(Long userId,Long deviceId)
{
int i = asDeviceService.toggleDevice(userId,deviceId);
return AjaxResult.success(i);
}
/**
* 滑动浇水
*/
@GetMapping(value = "/watering/{deviceId}/{wateringSwitch}")
public AjaxResult watering(@PathVariable("deviceId") Long deviceId,@PathVariable("wateringSwitch") Integer wateringSwitch)
{
AsDevice device = asDeviceService.selectAsDeviceByDeviceId(deviceId);
return asDeviceService.watering(device,wateringSwitch);
}
/**
* 根据设备id获取更多浇水记录
*/
@GetMapping("/getWateringRecord")
2024-04-27 20:09:05 +08:00
public TableDataInfo wateringRecord(AsWateringRecord asWateringRecord)
{
startPage();
2024-04-27 20:09:05 +08:00
List<AsWateringRecord> asWateringRecords = asWateringRecordService.selectAsWateringRecordList(AsWateringRecord.builder().deviceId(asWateringRecord.getDeviceId()).build());
return getDataTable(asWateringRecords);
}
2023-12-15 15:39:32 +08:00
/**
* 查询固件版本列表
*/
@GetMapping("/version/list")
public TableDataInfo list(AsDeviceVersion asDeviceVersion)
{
startPage();
List<AsDeviceVersion> list = asDeviceVersionService.selectAsDeviceVersionList(asDeviceVersion);
return getDataTable(list);
}
/**
* 修改设备列表
*/
@Log(title = "设备列表", businessType = BusinessType.UPDATE)
@PutMapping("/device")
public AjaxResult edit(@RequestBody AsDevice asDevice)
{
2024-04-10 16:09:43 +08:00
logger.info("接收到修改设备参数请求:【{}】",JSON.toJSON(asDevice));
2023-12-15 15:39:32 +08:00
return toAjax(asDeviceService.updateAsDevice(asDevice));
}
/**
* 获取设备列表详细信息
*/
@GetMapping(value = "/getDeviceInfo/{deviceId}")
public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
{
return success(asDeviceService.selectAsDeviceByDeviceId(deviceId));
}
/**
* 绑定设备
*/
@PostMapping(value = "/bandDevice")
public AjaxResult bandDevice(@RequestBody AsDevice asDevice)
{
return success(asDeviceService.bandDevice(asDevice));
}
2025-02-15 16:24:00 +08:00
//
// /**
// * 根据mac判断设备是否绑定
// */
// @GetMapping("/isBand")
// public AjaxResult isBand(List<String> macList)
// {
// return success(asDeviceService.isBand(macList));
// }
/**
2024-04-18 13:38:51 +08:00
* 解绑设备
*/
2024-04-18 13:38:51 +08:00
@Log(title = "解绑设备", businessType = BusinessType.DELETE)
@DeleteMapping("/device/unbind/{deviceId}")
public AjaxResult unbind(@PathVariable Long deviceId)
{
AsDevice device = new AsDevice();
device.setDeviceId(deviceId);
2024-04-18 13:38:51 +08:00
device.setUserId(0L);
device.setIsNetwork("0");
return toAjax(asDeviceService.logicallyDelete(device));
}
2024-01-24 12:02:48 +08:00
/**
2024-04-03 17:38:01 +08:00
* 植物识别
2024-01-24 12:02:48 +08:00
*/
@SneakyThrows
2024-01-24 12:02:48 +08:00
@Log(title = "植物识别", businessType = BusinessType.OTHER)
@GetMapping("/plant/identify")
public AjaxResult plant(String url,String userId)
2024-01-24 12:02:48 +08:00
{
2024-04-03 17:38:01 +08:00
logger.info("接收到植物图片-----【{}】",url);
2024-01-24 12:02:48 +08:00
/** 请求百度获取token*/
if(ObjectUtils.isEmpty(url)){
return AjaxResult.error("url不能为空");
}
String token = GetToken.getAccessToken();
String param = "url="+ URLEncoder.encode(url,"UTF-8");
String post = HttpUtils.sendPost2(identifyUrl + "?access_token=" + token, param);
logger.info("百度植物识别返回-----"+ post);
/** 根据名字查询自己的植物库中是否有该植物没有则返回 没有该植物信息
* 有则返回该植物的详情 */
IdentifyRes identifyRes = JSONObject.parseObject(post, IdentifyRes.class);
2024-04-03 17:38:01 +08:00
if(ObjectUtils.isEmpty(identifyRes.getResult())){
return AjaxResult.error("网络异常,请重试");
}
AsPlantIdentifyLog asPlantIdentifyLog = new AsPlantIdentifyLog();
asPlantIdentifyLog.setUserId(Long.parseLong(userId));
asPlantIdentifyLog.setUrl(url);
asPlantIdentifyLog.setResponse(post);
int i = asPlantIdentifyLogService.insertAsPlantIdentifyLog(asPlantIdentifyLog);
if(i>0){
logger.info("植物识别记录日志成功");
}
List<IdentifyRes.PlantItem> plantList = identifyRes.getResult();
for (IdentifyRes.PlantItem plantItem : plantList) {
String name = plantItem.getName();
if(!"非植物".equals(name)){
//根据名字查询自己的植物库中是否有该植物
AsPlant asPlant = asPlantService.selectAsPlantByName(name);
if(ObjectUtils.isEmpty(asPlant)){
plantItem.setPlantId(null);
plantItem.setMsg("没有该植物信息");
2024-04-03 17:38:01 +08:00
identifyLog("non-existent",asPlantIdentifyLog.getId(), plantItem.getScore(), name);
AsPlant asPlant1 = new AsPlant();
asPlant1.setPlantName(name);
asPlantService.insertAsPlant(asPlant1);
plantItem.setPlantId(asPlant1.getPlantId());
}else{
2024-04-03 17:38:01 +08:00
identifyLog("exist",asPlantIdentifyLog.getId(), plantItem.getScore(), name);
plantItem.setPlantId(asPlant.getPlantId());
2024-04-03 17:38:01 +08:00
AsPlant asPlant1 = asPlantService.selectAsPlantByPlantId(asPlant.getPlantId());
if(ObjectUtils.isNotEmpty(asPlant1)){
plantItem.setIntroduce(asPlant1.getIntroduce());
}
plantItem.setUrl(asPlant.getPicture());
}
}
}
return AjaxResult.success(identifyRes.getResult());
}
2024-04-03 17:38:01 +08:00
/**jil*/
private void identifyLog(String type,Long logId, BigDecimal score, String name) {
AsPlantAnalysisLog asPlantAnalysisLog;
if("non-existent".equals(type)){
/** 记录植物库中没有该植物,用于后期补充植物库*/
asPlantAnalysisLog = AsPlantAnalysisLog.builder()
.plantName(name)
.isExist("0")
.identifyId(logId)
.msg("没有该植物信息")
.score(score)
.build();
}else{
asPlantAnalysisLog = AsPlantAnalysisLog.builder()
.plantName(name)
.isExist("1")
.identifyId(logId)
.msg("识别成功")
.score(score)
.build();
}
int i1 = asPlantAnalysisLogService.insertAsPlantAnalysisLog(asPlantAnalysisLog);
if(i1>0){
logger.info("植物识别记录日志成功");
}
}
/**
* 查询定时器列表
*/
@GetMapping("/timer/list")
public TableDataInfo timerList(AsTimer asTimer)
{
startPage();
List<AsTimer> list = asTimerService.selectAsTimerList(asTimer);
return getDataTable(list);
}
/**
* 新增定时器
*/
@Log(title = "定时器", businessType = BusinessType.INSERT)
@PostMapping("/timer")
public AjaxResult add(@RequestBody AsTimer asTimer)
{
logger.info("添加asTimer-----"+JSON.toJSONString(asTimer));
return toAjax(asTimerService.insertAsTimer(asTimer));
}
/**
* 修改定时器
*/
@Log(title = "定时器", businessType = BusinessType.UPDATE)
@PutMapping("/timer")
public AjaxResult edit(@RequestBody AsTimer asTimer)
{
return toAjax(asTimerService.updateAsTimer(asTimer));
}
/**
* 开关定时器
*/
@Log(title = "开关定时器", businessType = BusinessType.UPDATE)
@PutMapping("/timer/switch")
public AjaxResult isSwitch(@RequestBody AsTimer asTimer)
{
2024-04-09 14:52:24 +08:00
logger.info("开关定时器传参-----"+JSON.toJSONString(asTimer));
return toAjax(asTimerService.isSwitch(asTimer));
}
/**
* 删除定时器
*/
@Log(title = "定时器", businessType = BusinessType.DELETE)
2024-04-08 14:16:48 +08:00
@DeleteMapping("/timer/{deviceId}/{ids}")
public AjaxResult remove(@PathVariable Long deviceId,@PathVariable Long[] ids)
{
2024-04-08 14:16:48 +08:00
logger.info("删除定时器传参-----"+JSON.toJSONString(ids));
return toAjax(asTimerService.deleteAsTimerByIds(deviceId,ids));
}
/**
* 配网成功
*/
@Log(title = "配网成功", businessType = BusinessType.UPDATE)
2024-04-19 14:11:08 +08:00
@PostMapping("/network/{mac}")
public AjaxResult settingNetwork(@PathVariable String mac)
{
2024-04-19 14:11:08 +08:00
return toAjax(asDeviceService.settingNetwork(mac));
2024-01-24 12:02:48 +08:00
}
2024-04-03 17:38:01 +08:00
/**
* 根据用户id查询收藏列表
*/
@GetMapping("/collection/list")
public TableDataInfo list(AsUserCollection asUserCollection)
{
startPage();
List<AsUserCollection> list = asUserCollectionService.selectAsUserCollectionList(asUserCollection);
return getDataTable(list);
}
/**
* 新增收藏
*/
@Log(title = "用户收藏", businessType = BusinessType.INSERT)
@PostMapping("/collection")
public AjaxResult add(@RequestBody AsUserCollection asUserCollection)
{
2024-04-03 21:09:13 +08:00
Long aLong = asUserCollectionService.insertAsUserCollection(asUserCollection);
return success(aLong);
2024-04-03 17:38:01 +08:00
}
/**
* 取消收藏
*/
@Log(title = "用户收藏", businessType = BusinessType.DELETE)
@DeleteMapping("/collection/{collectionIds}")
public AjaxResult cancelCollection(@PathVariable Long[] collectionIds)
{
return toAjax(asUserCollectionService.deleteAsUserCollectionByCollectionIds(collectionIds));
}
/**
* 是否已收藏
*/
@Log(title = "用户收藏", businessType = BusinessType.UPDATE)
@GetMapping("/isCollection")
public AjaxResult isCollection(Long plantId,Long userId)
{
Boolean collection = asUserCollectionService.isCollection(plantId, userId);
return AjaxResult.success(collection);
}
2025-02-15 16:24:00 +08:00
/**
* 根据mac号查询设备是否被绑定
*/
@GetMapping("/device/isBand/{mac}")
2025-02-15 16:24:00 +08:00
public AjaxResult isBand(@PathVariable List<String> mac)
{
2025-02-15 16:24:00 +08:00
logger.info("根据mac号查询设备是否被绑定传参-----{}", JSON.toJSONString(mac));
List<IsBandVO> isBand = asDeviceService.isBand(mac);
return AjaxResult.success(isBand);
}
2024-04-03 17:38:01 +08:00
/**
* 获取天气信息
*/
@GetMapping("/weather")
public AjaxResult weather()
{
//获取当前请求ip,用ip获取到归属地信息
String ipAddr = IpUtils.getIpAddr();
return AjaxResult.success(asDeviceService.getWeather(ipAddr));
}
2024-04-24 15:53:18 +08:00
/**
* 根据型号返回型号名称
*/
2024-04-25 16:46:22 +08:00
@PostMapping("/getModelName")
public AjaxResult getModelName(@RequestBody String asDevicesRes)
2024-04-24 15:53:18 +08:00
{
2024-04-25 16:46:22 +08:00
logger.info("根据型号返回型号名称参数-----"+asDevicesRes);
List<AsDevice> devices = JSONArray.parseArray(asDevicesRes, AsDevice.class);
List<AsDevice> asDevices = new ArrayList<>();
for (AsDevice device:devices) {
AsDevice asDevice = asDeviceService.selectAsDeviceByMac(device);
asDevices.add(asDevice);
2024-04-24 15:53:18 +08:00
}
2024-04-25 16:46:22 +08:00
return AjaxResult.success(asDevices);
2024-04-24 15:53:18 +08:00
}
2024-11-06 16:59:37 +08:00
/**
* 所有型号列表
*/
@GetMapping("/getAllModelList")
public AjaxResult getAllModelList()
{
logger.info("【所有型号列表】参数-----");
List<AsModel> asModels = asModelService.selectAsModelList(new AsModel());
return AjaxResult.success(asModels);
}
/**
* 获取已录入设备列表
*/
@GetMapping("/getExistMac/{macs}")
public AjaxResult getExistMac(@PathVariable List<String> macs)
{
logger.info("【获取已录入设备列表】参数-----macs=【{}】",macs);
List<AsDevice> devices = asDeviceService.getExistMac(macs);
return AjaxResult.success(devices);
}
2024-10-18 21:59:28 +08:00
/**
* 客服电话
*/
@GetMapping("/getServerPhone")
public AjaxResult getServerPhone()
{
logger.info("客服电话-----");
AjaxResult success = AjaxResult.success();
String phone = configService.selectConfigByKey("rl.server.phone");
success.put(AjaxResult.DATA_TAG,phone);
return success;
}
/**
* 新增反馈意见
*/
@Log(title = "反馈意见", businessType = BusinessType.INSERT)
@PostMapping("/feedback")
public AjaxResult add(@RequestBody AsFeedback asFeedback)
{
return toAjax(asFeedbackService.insertAsFeedback(asFeedback));
}
2024-11-06 16:59:37 +08:00
/**
* 获取onenet token
*/
@GetMapping("/getToken")
public AjaxResult getToken()
{
logger.info("获取onenet token-----");
AjaxResult success = AjaxResult.success();
try {
return success(Token.getToken());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (InvalidKeyException e) {
throw new RuntimeException(e);
}
}
2024-10-18 21:59:28 +08:00
}