yingsl
3 years ago
14 changed files with 394 additions and 0 deletions
-
14src/main/java/com/example/common/Constant.java
-
69src/main/java/com/example/modular/api/controller/DeviceDataPushController.java
-
40src/main/java/com/example/modular/api/service/DeviceDataPushService.java
-
204src/main/java/com/example/modular/api/service/impl/DeviceDataPushServiceImpl.java
-
1src/main/java/com/example/modular/device/mapper/mapping/BaseDeviceDeliveryMapper.xml
-
5src/main/java/com/example/modular/device/po/BaseDeviceDelivery.java
-
10src/main/java/com/example/modular/energyconsumption/mapper/ToiletElectricityConsumptionMapper.java
-
9src/main/java/com/example/modular/energyconsumption/mapper/ToiletWaterConsumptionMapper.java
-
6src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletElectricityConsumptionMapper.xml
-
7src/main/java/com/example/modular/energyconsumption/mapper/mapping/ToiletWaterConsumptionMapper.xml
-
8src/main/java/com/example/modular/energyconsumption/service/IToiletElectricityConsumptionService.java
-
9src/main/java/com/example/modular/energyconsumption/service/IToiletWaterConsumptionService.java
-
6src/main/java/com/example/modular/energyconsumption/service/impl/ToiletElectricityConsumptionServiceImpl.java
-
6src/main/java/com/example/modular/energyconsumption/service/impl/ToiletWaterConsumptionServiceImpl.java
@ -0,0 +1,69 @@ |
|||
package com.example.modular.api.controller; |
|||
|
|||
import com.example.modular.api.service.DeviceDataPushService; |
|||
import com.example.util.Result; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.web.bind.annotation.*; |
|||
|
|||
/** |
|||
* 水电表数据推送 controller |
|||
* @author ysl |
|||
* @since 2022-01-06 |
|||
*/ |
|||
@RestController |
|||
@RequestMapping("/device-data") |
|||
public class DeviceDataPushController { |
|||
|
|||
@Autowired |
|||
private DeviceDataPushService deviceDataPushService; |
|||
|
|||
/** |
|||
* 从设备编号获取用水量数据 |
|||
* @param base64 照片base64 |
|||
* @param imgUrl 照片下载地址 |
|||
* @param devId 设备ID |
|||
* @param devVol 设备电压 |
|||
* @param isPointerMeter 是否是指针表 |
|||
* @param createTime 上传时间 yyyy-MM-dd HH:mm:ss |
|||
* @param result 识别结果JSON字符串 |
|||
* @return |
|||
* @author ysl |
|||
*/ |
|||
@PostMapping("/water") |
|||
@CrossOrigin |
|||
public String deviceWaterSave (@RequestParam(value = "base64",required = false)String base64, |
|||
@RequestParam(value = "imgUrl",required = false)String imgUrl, |
|||
@RequestParam(value = "devId",required = false)String devId, |
|||
@RequestParam(value = "devVol",required = false)Integer devVol, |
|||
@RequestParam(value = "isPointerMeter",required = false,defaultValue = "false")boolean isPointerMeter, |
|||
@RequestParam(value = "createTime",required = false)String createTime, |
|||
@RequestParam(value = "result",required = false)String result) { |
|||
String resultStr = deviceDataPushService.deviceWaterSave(base64,imgUrl,devId,devVol,isPointerMeter,createTime,result); |
|||
return resultStr; |
|||
} |
|||
|
|||
/** |
|||
* 从设备编号获取用电量数据 |
|||
* @param base64 照片base64 |
|||
* @param imgUrl 照片下载地址 |
|||
* @param devId 设备ID |
|||
* @param devVol 设备电压 |
|||
* @param isPointerMeter 是否是指针表 |
|||
* @param createTime 上传时间 yyyy-MM-dd HH:mm:ss |
|||
* @param result 识别结果JSON字符串 |
|||
* @return |
|||
* @author ysl |
|||
*/ |
|||
@PostMapping("/electricity") |
|||
@CrossOrigin |
|||
public String deviceElectricitySave (@RequestParam(value = "base64",required = false)String base64, |
|||
@RequestParam(value = "imgUrl",required = false)String imgUrl, |
|||
@RequestParam(value = "devId",required = false)String devId, |
|||
@RequestParam(value = "devVol",required = false)Integer devVol, |
|||
@RequestParam(value = "isPointerMeter",required = false,defaultValue = "false")boolean isPointerMeter, |
|||
@RequestParam(value = "createTime",required = false)String createTime, |
|||
@RequestParam(value = "result",required = false)String result) { |
|||
String resultStr = deviceDataPushService.deviceElectricitySave(base64,imgUrl,devId,devVol,isPointerMeter,createTime,result); |
|||
return resultStr; |
|||
} |
|||
} |
@ -0,0 +1,40 @@ |
|||
package com.example.modular.api.service; |
|||
|
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
|
|||
/** |
|||
* 水电表数据推送 service |
|||
* @author ysl |
|||
* @since 2022-01-06 |
|||
*/ |
|||
public interface DeviceDataPushService { |
|||
|
|||
/** |
|||
* 从设备编号获取用水量数据 |
|||
* @param base64 照片base64 |
|||
* @param imgUrl 照片下载地址 |
|||
* @param devId 设备ID |
|||
* @param devVol 设备电压 |
|||
* @param isPointerMeter 是否是指针表 |
|||
* @param createTime 上传时间 yyyy-MM-dd HH:mm:ss |
|||
* @param result 识别结果JSON字符串 |
|||
* @return |
|||
* @author ysl |
|||
*/ |
|||
String deviceWaterSave(String base64,String imgUrl,String devId,Integer devVol,boolean isPointerMeter,String createTime,String result); |
|||
|
|||
|
|||
/** |
|||
* 从设备编号获取用电量数据 |
|||
* @param base64 照片base64 |
|||
* @param imgUrl 照片下载地址 |
|||
* @param devId 设备ID |
|||
* @param devVol 设备电压 |
|||
* @param isPointerMeter 是否是指针表 |
|||
* @param createTime 上传时间 yyyy-MM-dd HH:mm:ss |
|||
* @param result 识别结果JSON字符串 |
|||
* @return |
|||
* @author ysl |
|||
*/ |
|||
String deviceElectricitySave(String base64,String imgUrl,String devId,Integer devVol,boolean isPointerMeter,String createTime,String result); |
|||
} |
@ -0,0 +1,204 @@ |
|||
package com.example.modular.api.service.impl; |
|||
|
|||
import com.alibaba.fastjson.JSON; |
|||
import com.alibaba.fastjson.JSONObject; |
|||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|||
import com.example.common.Constant; |
|||
import com.example.modular.api.service.DeviceDataPushService; |
|||
import com.example.modular.device.po.BaseDeviceDelivery; |
|||
import com.example.modular.device.service.IBaseDeviceDeliveryService; |
|||
import com.example.modular.energyconsumption.po.ToiletElectricityConsumption; |
|||
import com.example.modular.energyconsumption.po.ToiletWaterConsumption; |
|||
import com.example.modular.energyconsumption.service.IToiletElectricityConsumptionService; |
|||
import com.example.modular.energyconsumption.service.IToiletWaterConsumptionService; |
|||
import com.example.modular.toilet.po.Toilet; |
|||
import com.example.modular.toilet.service.IToiletService; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.stereotype.Service; |
|||
import org.springframework.util.CollectionUtils; |
|||
|
|||
import java.text.ParseException; |
|||
import java.text.SimpleDateFormat; |
|||
import java.util.Date; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 水电表数据推送 实现类 |
|||
* @author ysl |
|||
* @since 2022-01-06 |
|||
*/ |
|||
@Service |
|||
public class DeviceDataPushServiceImpl implements DeviceDataPushService { |
|||
|
|||
@Autowired |
|||
private IBaseDeviceDeliveryService baseDeviceDeliveryService; |
|||
@Autowired |
|||
private IToiletService toiletService; |
|||
@Autowired |
|||
private IToiletWaterConsumptionService toiletWaterConsumptionService; |
|||
@Autowired |
|||
private IToiletElectricityConsumptionService toiletElectricityConsumptionService; |
|||
|
|||
@Override |
|||
public String deviceWaterSave(String base64, String imgUrl, String devId, Integer devVol, boolean isPointerMeter, String createTime, String result) { |
|||
//校验参数是否正确 |
|||
if (StringUtils.isBlank(devId) || StringUtils.isBlank(createTime) || StringUtils.isBlank(result)) { |
|||
return "PARAMS ERROR"; |
|||
} |
|||
//校验设备ID是否存在 |
|||
QueryWrapper<BaseDeviceDelivery> baseDeviceDeliveryQueryWrapper = new QueryWrapper<>(); |
|||
baseDeviceDeliveryQueryWrapper.eq("device_id",devId); |
|||
baseDeviceDeliveryQueryWrapper.eq("delete_status", Constant.TOILET_DEL_STATUS_NO); |
|||
baseDeviceDeliveryQueryWrapper.eq("device_type", Constant.DEVICE_WATER_TYPE); |
|||
List<BaseDeviceDelivery> baseDeviceDeliveryList = baseDeviceDeliveryService.list(baseDeviceDeliveryQueryWrapper); |
|||
if (CollectionUtils.isEmpty(baseDeviceDeliveryList)) { |
|||
return "DEVICE NOT EXIST"; |
|||
} |
|||
//获取公厕ID |
|||
Long toiletId = baseDeviceDeliveryList.get(0).getToiletId(); |
|||
//校验公厕是否存在 |
|||
Toilet toilet = toiletService.getById(toiletId); |
|||
if (toilet == null) { |
|||
return "TOILET NOT EXIST"; |
|||
} |
|||
//resule JSON解析 |
|||
JSONObject parseResult = JSON.parseObject(result); |
|||
Integer outputState = parseResult.getInteger("outputState"); |
|||
if (!outputState.equals(0)) { |
|||
return "outputState:" + outputState; |
|||
} |
|||
Double resultValue; |
|||
//获取数据值 |
|||
if (!isPointerMeter) { |
|||
//非指针表 |
|||
String outputStrDec = parseResult.getString("outputStrDec"); |
|||
String outputStrInt = parseResult.getString("outputStrInt"); |
|||
resultValue = Double.parseDouble(outputStrInt + "." + outputStrDec); |
|||
} else { |
|||
resultValue = parseResult.getDoubleValue("outputReadingValue"); |
|||
} |
|||
//获取上传时间当天的开始和结束时间 以及 前一天的开始时间和结束时间 |
|||
String todayStartTime = createTime.split(" ")[0] + " 00:00:00"; |
|||
String todayEndTime = createTime.split(" ")[0] + " 23:59:59"; |
|||
//获取用水量创建时间当天的数据和当天之前的用水量总和 |
|||
Double useWaterBeforeDate = toiletWaterConsumptionService.sumWaterBeforeDate(toiletId,todayStartTime); |
|||
if (resultValue < useWaterBeforeDate) { |
|||
return "USE WATER LESS THAN BEFORE"; |
|||
} |
|||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|||
Date createDateTime; |
|||
try { |
|||
createDateTime = dateFormat.parse(createTime); |
|||
} catch (ParseException e) { |
|||
return "CREATETIME FORMAT ERROR"; |
|||
} |
|||
QueryWrapper<ToiletWaterConsumption> todayToiletWaterConsumptionQueryWrapper = new QueryWrapper<>(); |
|||
todayToiletWaterConsumptionQueryWrapper.eq("toilet_id",toiletId); |
|||
todayToiletWaterConsumptionQueryWrapper.ge("create_time",todayStartTime); |
|||
todayToiletWaterConsumptionQueryWrapper.le("create_time",todayEndTime); |
|||
List<ToiletWaterConsumption> todayToiletWaterList = toiletWaterConsumptionService.list(todayToiletWaterConsumptionQueryWrapper); |
|||
Double dateUseWater = resultValue - useWaterBeforeDate; |
|||
if (CollectionUtils.isEmpty(todayToiletWaterList)) { |
|||
ToiletWaterConsumption toiletWaterConsumption = new ToiletWaterConsumption(); |
|||
toiletWaterConsumption.setToiletId(toiletId); |
|||
toiletWaterConsumption.setUseWater(dateUseWater); |
|||
toiletWaterConsumption.setCreateTime(createDateTime); |
|||
toiletWaterConsumption.setUpdateTime(createDateTime); |
|||
toiletWaterConsumptionService.save(toiletWaterConsumption); |
|||
} else { |
|||
ToiletWaterConsumption toiletWaterConsumption = todayToiletWaterList.get(0); |
|||
toiletWaterConsumption.setUseWater(dateUseWater); |
|||
toiletWaterConsumption.setUpdateTime(createDateTime); |
|||
toiletWaterConsumptionService.updateById(toiletWaterConsumption); |
|||
} |
|||
return "SUCCESS"; |
|||
} |
|||
|
|||
@Override |
|||
public String deviceElectricitySave(String base64, String imgUrl, String devId, Integer devVol, boolean isPointerMeter, String createTime, String result) { |
|||
//校验参数是否正确 |
|||
if (StringUtils.isBlank(devId) || StringUtils.isBlank(createTime) || StringUtils.isBlank(result)) { |
|||
return "PARAMS ERROR"; |
|||
} |
|||
//校验设备ID是否存在 |
|||
QueryWrapper<BaseDeviceDelivery> baseDeviceDeliveryQueryWrapper = new QueryWrapper<>(); |
|||
baseDeviceDeliveryQueryWrapper.eq("device_id",devId); |
|||
baseDeviceDeliveryQueryWrapper.eq("delete_status", Constant.TOILET_DEL_STATUS_NO); |
|||
baseDeviceDeliveryQueryWrapper.eq("device_type", Constant.DEVICE_ELECTRICITY_TYPE); |
|||
List<BaseDeviceDelivery> baseDeviceDeliveryList = baseDeviceDeliveryService.list(baseDeviceDeliveryQueryWrapper); |
|||
if (CollectionUtils.isEmpty(baseDeviceDeliveryList)) { |
|||
return "DEVICE NOT EXIST"; |
|||
} |
|||
//获取公厕ID |
|||
Long toiletId = baseDeviceDeliveryList.get(0).getToiletId(); |
|||
//校验公厕是否存在 |
|||
Toilet toilet = toiletService.getById(toiletId); |
|||
if (toilet == null) { |
|||
return "TOILET NOT EXIST"; |
|||
} |
|||
//resule JSON解析 |
|||
JSONObject parseResult = JSON.parseObject(result); |
|||
Integer outputState = parseResult.getInteger("outputState"); |
|||
if (!outputState.equals(0)) { |
|||
return "outputState:" + outputState; |
|||
} |
|||
Double resultValue; |
|||
//获取数据值 |
|||
if (!isPointerMeter) { |
|||
//非指针表 |
|||
String outputStrDec = parseResult.getString("outputStrDec"); |
|||
String outputStrInt = parseResult.getString("outputStrInt"); |
|||
resultValue = Double.parseDouble(outputStrInt + "." + outputStrDec); |
|||
} else { |
|||
resultValue = parseResult.getDoubleValue("outputReadingValue"); |
|||
} |
|||
//获取上传时间当天的开始和结束时间 以及 前一天的开始时间和结束时间 |
|||
String todayStartTime = createTime.split(" ")[0] + " 00:00:00"; |
|||
String todayEndTime = createTime.split(" ")[0] + " 23:59:59"; |
|||
//获取用电量创建时间当天的数据和当天之前的用水量总和 |
|||
Double useElectricityBeforeDate = toiletElectricityConsumptionService.sumElectricityBeforeDate(toiletId,todayStartTime); |
|||
if (resultValue < useElectricityBeforeDate) { |
|||
return "USE WATER LESS THAN BEFORE"; |
|||
} |
|||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
|||
Date createDateTime; |
|||
try { |
|||
createDateTime = dateFormat.parse(createTime); |
|||
} catch (ParseException e) { |
|||
return "CREATETIME FORMAT ERROR"; |
|||
} |
|||
QueryWrapper<ToiletElectricityConsumption> toiletElectricityConsumptionQueryWrapper = new QueryWrapper<>(); |
|||
toiletElectricityConsumptionQueryWrapper.eq("toilet_id",toiletId); |
|||
toiletElectricityConsumptionQueryWrapper.ge("create_time",todayStartTime); |
|||
toiletElectricityConsumptionQueryWrapper.le("create_time",todayEndTime); |
|||
List<ToiletElectricityConsumption> toiletElectricityConsumptionList = toiletElectricityConsumptionService.list(toiletElectricityConsumptionQueryWrapper); |
|||
Double dateUseElectricity = resultValue - useElectricityBeforeDate; |
|||
if (CollectionUtils.isEmpty(toiletElectricityConsumptionList)) { |
|||
ToiletElectricityConsumption toiletElectricityConsumption = new ToiletElectricityConsumption(); |
|||
toiletElectricityConsumption.setToiletId(toiletId); |
|||
toiletElectricityConsumption.setUseElectricity(dateUseElectricity); |
|||
toiletElectricityConsumption.setCreateTime(createDateTime); |
|||
toiletElectricityConsumption.setUpdateTime(createDateTime); |
|||
toiletElectricityConsumptionService.save(toiletElectricityConsumption); |
|||
} else { |
|||
ToiletElectricityConsumption toiletElectricityConsumption = toiletElectricityConsumptionList.get(0); |
|||
toiletElectricityConsumption.setUseElectricity(dateUseElectricity); |
|||
toiletElectricityConsumption.setUpdateTime(createDateTime); |
|||
toiletElectricityConsumptionService.updateById(toiletElectricityConsumption); |
|||
} |
|||
return "SUCCESS"; |
|||
} |
|||
|
|||
public static void main (String[] args) { |
|||
Map<String,Object> map = new HashMap<>(); |
|||
map.put("outputState",0); |
|||
map.put("outputReadingValue",15); |
|||
map.put("outputStrDec",12); |
|||
map.put("outputStrInt",13); |
|||
String s = JSON.toJSONString(map); |
|||
System.out.println(s); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue