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

379 lines
12 KiB
Java
Raw Normal View History

package com.ruoyi.device.app;
import com.alibaba.fastjson2.JSON;
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-04-03 17:38:01 +08:00
import com.ruoyi.device.domain.AsUserCollection;
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.StringUtils;
import com.ruoyi.common.utils.baidu.GetToken;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.device.domain.*;
import com.ruoyi.device.domain.vo.IdentifyRes;
import com.ruoyi.device.service.*;
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-04-03 17:38:01 +08:00
import java.math.BigDecimal;
import java.net.URLEncoder;
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;
@Value(value = "${baidu.identifyUrl}")
private String identifyUrl;
/**
* 查询文章列表
*/
@GetMapping("/article/list")
public TableDataInfo list(AsArticle asArticle)
{
startPage();
List<AsArticle> list = asArticleService.selectAsArticleList(asArticle);
return getDataTable(list);
}
2023-12-15 15:39:32 +08:00
/**
* 根据登录用户获取绑定设备
*/
@GetMapping("/getDeviceInfoByUser")
public AjaxResult list(Long userId,String deviceId)
{
List<AsDevice> device;
if(StringUtils.isEmpty(deviceId) || "null".equals(deviceId)){
device = asUserService.selectDeviceInfoByUser(userId,null);
}else{
device = asUserService.selectDeviceInfoByUser(userId,Long.parseLong(deviceId));
}
return AjaxResult.success(device);
}
/**
* 滑动浇水
*/
@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")
public TableDataInfo wateringRecord(String deviceId)
{
startPage();
List<AsWateringRecord> asWateringRecords = asWateringRecordService.selectAsWateringRecordList(AsWateringRecord.builder().deviceId(Long.parseLong(deviceId)).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));
}
/**
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)
@PostMapping("/network/{deviceId}")
public AjaxResult settingNetwork(@PathVariable Long deviceId)
{
return toAjax(asDeviceService.settingNetwork(deviceId));
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);
}
/**
* 根据mac号查询设备是否被绑定
*/
@GetMapping("/device/isBand/{mac}")
public AjaxResult isBand(@PathVariable String mac)
{
Boolean isBand = asDeviceService.isBand(mac);
return AjaxResult.success(isBand);
}
2024-04-03 17:38:01 +08:00
}