短信告警通知

This commit is contained in:
wyw
2024-08-16 00:58:12 +08:00
parent 52f9a1c382
commit 9acc3940d8
4 changed files with 76 additions and 11 deletions

View File

@ -1,158 +0,0 @@
package com.fastbee.iot.anfang.controller;
import com.fastbee.common.annotation.Log;
import com.fastbee.common.config.RuoYiConfig;
import com.fastbee.common.core.controller.BaseController;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.core.page.TableDataInfo;
import com.fastbee.common.enums.BusinessType;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.file.FileUploadUtils;
import com.fastbee.common.utils.poi.ExcelUtil;
import com.fastbee.iot.anfang.service.IUploadedPhotosService;
import com.fastbee.iot.model.anfang.UploadedPhotos;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import static com.fastbee.common.utils.StringUtils.isEmpty;
/**
* 设备告警上传Controller
*
* @author Dunxi Zang
* @date 2024-06-19
*/
@RestController
@RequestMapping("/iot/photos")
@Api(tags = "安防小板")
public class UploadedPhotosController extends BaseController
{
@Autowired
private IUploadedPhotosService uploadedPhotosService;
/**
* 查询设备告警上传列表
*/
@PreAuthorize("@ss.hasPermi('iot:photos:list')")
@GetMapping("/list")
public TableDataInfo list(UploadedPhotos uploadedPhotos)
{
startPage();
List<UploadedPhotos> list = uploadedPhotosService.selectUploadedPhotosList(uploadedPhotos);
return getDataTable(list);
}
/**
* 导出设备告警上传列表
*/
@PreAuthorize("@ss.hasPermi('iot:photos:export')")
@Log(title = "设备告警上传", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, UploadedPhotos uploadedPhotos)
{
List<UploadedPhotos> list = uploadedPhotosService.selectUploadedPhotosList(uploadedPhotos);
ExcelUtil<UploadedPhotos> util = new ExcelUtil<UploadedPhotos>(UploadedPhotos.class);
util.exportExcel(response, list, "设备告警上传数据");
}
/**
* 获取设备告警上传详细信息
*/
@PreAuthorize("@ss.hasPermi('iot:photos:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id)
{
return success(uploadedPhotosService.selectUploadedPhotosById(id));
}
/**
* 新增设备告警上传
*/
@PostMapping
public AjaxResult uploadPhoto(
@RequestParam("fileToUpload") MultipartFile photo,
@RequestParam("imei") String imei,
@RequestParam("sn") String sn,
@RequestParam("lat") String lat,
@RequestParam("lng") String lng,
@RequestParam("temp") String temp,
@RequestParam("doorState") String doorState,
@RequestParam("shakeState") String shakeState,
@RequestParam("time") String time) {
if (photo.isEmpty()) {
throw new ServiceException("照片为空,上传失败");
}
try {
// 上传文件路径
String filePath = RuoYiConfig.getUploadPath();
// 上传并返回新文件名称
String fileName = FileUploadUtils.upload(filePath, photo);
// 处理可能为空的字段
Double latitude = isEmpty(lat) ? 0.0 : Double.valueOf(lat);
Double longitude = isEmpty(lng) ? 0.0 : Double.valueOf(lng);
Double temperature = isEmpty(temp) ? 0.0 : convertAndRoundTemperature(temp);
if(doorState.equals("0")){
shakeState = "1";
}
// 处理时间戳
long timestamp = Long.parseLong(time + "000");
Date date = new Date(timestamp);
//抓拍监控,并返回路径
String monitorPath = uploadedPhotosService.captureMonitorPhoto(sn);
//推送告警短信通知
uploadedPhotosService.sendAlarmMessage(sn, doorState, shakeState);
UploadedPhotos uploadedPhotos = new UploadedPhotos(
null, fileName, monitorPath,imei, sn, latitude, longitude, temperature, doorState, shakeState, date
);
return toAjax(uploadedPhotosService.insertUploadedPhotos(uploadedPhotos));
} catch (IOException e) {
throw new ServiceException("设备告警信息上传失败");
}
}
/**
* 修改设备告警上传
*/
@PreAuthorize("@ss.hasPermi('iot:photos:edit')")
@Log(title = "设备告警上传", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult edit(@RequestBody UploadedPhotos uploadedPhotos)
{
return toAjax(uploadedPhotosService.updateUploadedPhotos(uploadedPhotos));
}
/**
* 删除设备告警上传
*/
@PreAuthorize("@ss.hasPermi('iot:photos:remove')")
@Log(title = "设备告警上传", businessType = BusinessType.DELETE)
@DeleteMapping("/{ids}")
public AjaxResult remove(@PathVariable Long[] ids)
{
return toAjax(uploadedPhotosService.deleteUploadedPhotosByIds(ids));
}
public static Double convertAndRoundTemperature(String tempStr) {
// 将字符串转换为double
double temp = Double.parseDouble(tempStr);
// 使用BigDecimal进行四舍五入保留两位小数
BigDecimal bd = BigDecimal.valueOf(temp);
bd = bd.setScale(2, RoundingMode.HALF_UP);
// 返回转换后的double值
return bd.doubleValue();
}
}

View File

@ -1,77 +0,0 @@
package com.fastbee.iot.anfang.service;
import com.fastbee.iot.model.anfang.UploadedPhotos;
import java.util.List;
/**
* 存储上传的照片信息Service接口
*
* @author kerwincui
* @date 2024-06-20
*/
public interface IUploadedPhotosService
{
/**
* 查询存储上传的照片信息
*
* @param id 存储上传的照片信息主键
* @return 存储上传的照片信息
*/
public UploadedPhotos selectUploadedPhotosById(Long id);
/**
* 查询存储上传的照片信息列表
*
* @param uploadedPhotos 存储上传的照片信息
* @return 存储上传的照片信息集合
*/
public List<UploadedPhotos> selectUploadedPhotosList(UploadedPhotos uploadedPhotos);
/**
* 新增存储上传的照片信息
*
* @param uploadedPhotos 存储上传的照片信息
* @return 结果
*/
public int insertUploadedPhotos(UploadedPhotos uploadedPhotos);
/**
* 修改存储上传的照片信息
*
* @param uploadedPhotos 存储上传的照片信息
* @return 结果
*/
public int updateUploadedPhotos(UploadedPhotos uploadedPhotos);
/**
* 批量删除存储上传的照片信息
*
* @param ids 需要删除的存储上传的照片信息主键集合
* @return 结果
*/
public int deleteUploadedPhotosByIds(Long[] ids);
/**
* 删除存储上传的照片信息信息
*
* @param id 存储上传的照片信息主键
* @return 结果
*/
public int deleteUploadedPhotosById(Long id);
/**
* 抓拍监控照片
* @param sn
* @return
*/
String captureMonitorPhoto(String sn);
/**
* 发送短信通知
* @param sn
* @param doorState
* @param shakeState
*/
void sendAlarmMessage(String sn, String doorState, String shakeState);
}

View File

@ -1,208 +0,0 @@
package com.fastbee.iot.anfang.service.impl;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import com.fastbee.common.config.RuoYiConfig;
import com.fastbee.common.utils.DevParamsUtils;
import com.fastbee.common.utils.file.FileUploadUtils;
import com.fastbee.iot.anfang.service.IUploadedPhotosService;
import com.fastbee.iot.domain.Device;
import com.fastbee.iot.haikang.HaikangYingshiApi;
import com.fastbee.iot.mapper.UploadedPhotosMapper;
import com.fastbee.iot.model.anfang.UploadedPhotos;
import com.fastbee.iot.service.IDeviceService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Handler;
/**
* 存储上传的照片信息Service业务层处理
*
* @author kerwincui
* @date 2024-06-20
*/
@Service
public class UploadedPhotosServiceImpl implements IUploadedPhotosService
{
@Resource
private UploadedPhotosMapper uploadedPhotosMapper;
@Autowired
private IDeviceService deviceService;
/**
* 查询存储上传的照片信息
*
* @param id 存储上传的照片信息主键
* @return 存储上传的照片信息
*/
@Override
public UploadedPhotos selectUploadedPhotosById(Long id)
{
return uploadedPhotosMapper.selectUploadedPhotosById(id);
}
/**
* 查询存储上传的照片信息列表
*
* @param uploadedPhotos 存储上传的照片信息
* @return 存储上传的照片信息
*/
@Override
public List<UploadedPhotos> selectUploadedPhotosList(UploadedPhotos uploadedPhotos)
{
return uploadedPhotosMapper.selectUploadedPhotosList(uploadedPhotos);
}
/**
* 新增存储上传的照片信息
*
* @param uploadedPhotos 存储上传的照片信息
* @return 结果
*/
@Override
public int insertUploadedPhotos(UploadedPhotos uploadedPhotos)
{
return uploadedPhotosMapper.insertUploadedPhotos(uploadedPhotos);
}
/**
* 修改存储上传的照片信息
*
* @param uploadedPhotos 存储上传的照片信息
* @return 结果
*/
@Override
public int updateUploadedPhotos(UploadedPhotos uploadedPhotos)
{
return uploadedPhotosMapper.updateUploadedPhotos(uploadedPhotos);
}
/**
* 批量删除存储上传的照片信息
*
* @param ids 需要删除的存储上传的照片信息主键
* @return 结果
*/
@Override
public int deleteUploadedPhotosByIds(Long[] ids)
{
return uploadedPhotosMapper.deleteUploadedPhotosByIds(ids);
}
/**
* 删除存储上传的照片信息信息
*
* @param id 存储上传的照片信息主键
* @return 结果
*/
@Override
public int deleteUploadedPhotosById(Long id)
{
return uploadedPhotosMapper.deleteUploadedPhotosById(id);
}
@Override
public String captureMonitorPhoto(String sn) {
Device device = deviceService.selectDeviceBySerialNumber(sn);
if (device == null) {
return "";
}
Map<String, Object> devParams = DevParamsUtils.getDevParams(device.getDevParams());
if(!devParams.containsKey("jiankongIds")){
return "";
}
String jiankongIds = devParams.get("jiankongIds").toString();
Device jiankongDevice = deviceService.selectDeviceByDeviceId(Long.parseLong(jiankongIds));
if(jiankongDevice == null){
return "";
}
Map devData = DevParamsUtils.getDevParams(jiankongDevice.getDevParams());
if (devData.get("appKey") == null) {
return "";
}
if (devData.get("appSecret") == null) {
return "";
}
if (devData.get("channelNo") == null) {
return "";
}
Map<String, Object> map = new HashMap<>();//存放参数
map.put("appKey", devData.get("appKey"));
map.put("appSecret", devData.get("appSecret"));
cn.hutool.json.JSONObject token = HaikangYingshiApi.getToken(map);
if (token != null && token.get("code").equals("200")) {
Map<String, Object> params = new HashMap<>();
token = (cn.hutool.json.JSONObject) token.get("data");
params.put("accessToken", token.get("accessToken"));
params.put("deviceSerial", jiankongDevice.getSerialNumber());
params.put("channelNo", devData.get("channelNo"));
cn.hutool.json.JSONObject token1 = HaikangYingshiApi.capture(params);
if (token1 != null && token1.get("code").equals("200")) {
String dirs = DateUtil.thisYear() + "/" + (DateUtil.thisMonth() + 1) + "/" + DateUtil.thisDayOfMonth();
File dirfile = new File(RuoYiConfig.getUploadPath() + "/" + dirs);
if (!dirfile.exists()) {
// 目录不存在,创建目录
boolean created = dirfile.mkdirs();
if (created) {
} else {
}
} else {
}
Map data = (JSONObject) token1.get("data");
Map<String, Object> reMap = new HashMap<>();
reMap.put("picUrl", data.get("picUrl"));
if (data.get("picUrl") != null) {
try {
String picUrl = data.get("picUrl").toString();
URL url = new URL(picUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
int responseCode = httpConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConnection.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
String[] split1 = picUrl.split("\\.");
String filename =
DateUtil.date().toDateStr() + "-" + DateUtil.thisHour(true)
+ DateUtil.thisMinute() +
"-anfangmonitor-" + device.getDeviceId() + "."
+ split1[split1.length - 1].split("\\?")[0];
String pathUrl = RuoYiConfig.getUploadPath() + "/" + dirs + "/" + filename;
OutputStream outputStream = new FileOutputStream(pathUrl);
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
String httpimg = FileUploadUtils.getPathFileName(RuoYiConfig.getUploadPath(), dirs +
"/" + filename);
return httpimg;
} else {
System.out.println("读取http图片失败!");
return "";
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return "";
} else {
return "";
}
}
return "";
}
@Override
public void sendAlarmMessage(String sn, String doorState, String shakeState) {
}
}