379 lines
12 KiB
Java
379 lines
12 KiB
Java
package com.ruoyi.device.app;
|
||
|
||
import com.alibaba.fastjson2.JSON;
|
||
import com.alibaba.fastjson2.JSONObject;
|
||
import com.ruoyi.common.annotation.Log;
|
||
import com.ruoyi.common.core.controller.BaseController;
|
||
import com.ruoyi.common.core.domain.AjaxResult;
|
||
import com.ruoyi.device.domain.AsUserCollection;
|
||
import com.ruoyi.common.core.page.TableDataInfo;
|
||
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;
|
||
import org.springframework.web.bind.annotation.*;
|
||
|
||
import javax.annotation.Resource;
|
||
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;
|
||
|
||
@Resource
|
||
private IAsDeviceService asDeviceService;
|
||
|
||
@Resource
|
||
private IAsDeviceVersionService asDeviceVersionService;
|
||
|
||
@Autowired
|
||
private IAsTimerService asTimerService;
|
||
|
||
@Autowired
|
||
private IAsPlantService asPlantService;
|
||
|
||
@Autowired
|
||
private IAsArticleService asArticleService;
|
||
|
||
@Autowired
|
||
private IAsPlantIdentifyLogService asPlantIdentifyLogService;
|
||
|
||
@Autowired
|
||
private IAsPlantAnalysisLogService asPlantAnalysisLogService;
|
||
|
||
@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);
|
||
}
|
||
|
||
|
||
/**
|
||
* 根据登录用户获取绑定设备
|
||
*/
|
||
@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);
|
||
}
|
||
|
||
/**
|
||
* 查询固件版本列表
|
||
*/
|
||
@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)
|
||
{
|
||
logger.info("接收到修改设备参数请求:【{}】",JSON.toJSON(asDevice));
|
||
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));
|
||
}
|
||
|
||
/**
|
||
* 解绑设备
|
||
*/
|
||
@Log(title = "解绑设备", businessType = BusinessType.DELETE)
|
||
@DeleteMapping("/device/unbind/{deviceId}")
|
||
public AjaxResult unbind(@PathVariable Long deviceId)
|
||
{
|
||
AsDevice device = new AsDevice();
|
||
device.setDeviceId(deviceId);
|
||
device.setUserId(0L);
|
||
device.setIsNetwork("0");
|
||
return toAjax(asDeviceService.logicallyDelete(device));
|
||
}
|
||
|
||
/**
|
||
* 植物识别
|
||
*/
|
||
@SneakyThrows
|
||
@Log(title = "植物识别", businessType = BusinessType.OTHER)
|
||
@GetMapping("/plant/identify")
|
||
public AjaxResult plant(String url,String userId)
|
||
{
|
||
logger.info("接收到植物图片-----【{}】",url);
|
||
/** 请求百度获取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);
|
||
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("没有该植物信息");
|
||
identifyLog("non-existent",asPlantIdentifyLog.getId(), plantItem.getScore(), name);
|
||
AsPlant asPlant1 = new AsPlant();
|
||
asPlant1.setPlantName(name);
|
||
asPlantService.insertAsPlant(asPlant1);
|
||
plantItem.setPlantId(asPlant1.getPlantId());
|
||
}else{
|
||
identifyLog("exist",asPlantIdentifyLog.getId(), plantItem.getScore(), name);
|
||
plantItem.setPlantId(asPlant.getPlantId());
|
||
AsPlant asPlant1 = asPlantService.selectAsPlantByPlantId(asPlant.getPlantId());
|
||
if(ObjectUtils.isNotEmpty(asPlant1)){
|
||
plantItem.setIntroduce(asPlant1.getIntroduce());
|
||
}
|
||
plantItem.setUrl(asPlant.getPicture());
|
||
}
|
||
}
|
||
}
|
||
return AjaxResult.success(identifyRes.getResult());
|
||
}
|
||
|
||
/**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)
|
||
{
|
||
logger.info("开关定时器传参-----"+JSON.toJSONString(asTimer));
|
||
return toAjax(asTimerService.isSwitch(asTimer));
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 删除定时器
|
||
*/
|
||
@Log(title = "定时器", businessType = BusinessType.DELETE)
|
||
@DeleteMapping("/timer/{deviceId}/{ids}")
|
||
public AjaxResult remove(@PathVariable Long deviceId,@PathVariable Long[] ids)
|
||
{
|
||
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));
|
||
}
|
||
|
||
/**
|
||
* 根据用户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)
|
||
{
|
||
Long aLong = asUserCollectionService.insertAsUserCollection(asUserCollection);
|
||
return success(aLong);
|
||
}
|
||
|
||
/**
|
||
* 取消收藏
|
||
*/
|
||
@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);
|
||
}
|
||
|
||
}
|