parent
e9faa6d8ff
commit
a618f769ae
@ -0,0 +1,161 @@ |
||||
package com.psdc.mqtt.savedata; |
||||
|
||||
import com.alibaba.fastjson2.JSONObject; |
||||
import com.psdc.entity.*; |
||||
import com.psdc.entity.vo.DeviceStatusVo; |
||||
import com.psdc.mapper.*; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.Resource; |
||||
|
||||
/** |
||||
* @Author:Stone |
||||
* @Project:psdc |
||||
* @Filename:DataAndPowerSave |
||||
* @Slogan 致敬大师,致敬未来的你 |
||||
* @Date:2023/6/26 16:15 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
public class DataAndPowerSave { |
||||
|
||||
@Resource |
||||
PsdcDeviceMapper deviceMapper; |
||||
@Resource |
||||
PsdcElectricRtdataMapper electricRtDataMapper; |
||||
@Resource |
||||
PsdcElectricHtdataMapper electricHtDataMapper; |
||||
@Resource |
||||
PsdcDevicePowerHdata96Mapper devicePowerHtData96Mapper; |
||||
@Resource |
||||
PsdcThermometerRtdataMapper thermometerRtDataMapper; |
||||
@Resource |
||||
PsdcThermometerHtdataMapper thermometerHtDataMapper; |
||||
@Resource |
||||
PsdcStatisticsDayMapper statisticsDayMapper; |
||||
@Resource |
||||
PsdcStatisticsMonthMapper statisticsMonthMapper; |
||||
@Resource |
||||
PsdcStatisticsYearMapper statisticsYearMapper; |
||||
|
||||
public Boolean saveDevRtData(JSONObject jsonObject){ |
||||
Integer deviceId = jsonObject.getInteger("deviceId"); |
||||
String timestamp = jsonObject.getString("timestamp"); |
||||
JSONObject params = jsonObject.getJSONObject("params"); |
||||
|
||||
// 时间处理
|
||||
String date1 = timestamp.split("T")[0]; |
||||
String samDate = date1.split("-")[0] + date1.split("-")[1] + date1.split("-")[2]; |
||||
String time1 = timestamp.split("T")[1]; |
||||
int hour = Integer.parseInt(time1.substring(0, 8).split(":")[0]); |
||||
int min = Integer.parseInt(time1.substring(0, 8).split(":")[1]); |
||||
String upDateTime = date1 + " " + time1.substring(0, 8); |
||||
|
||||
// 根据设备id获取数据,查看设备库中是否有该设备,若没有该设备,则不对该设备的数据进行实时数据库入库操作
|
||||
DeviceStatusVo deviceStatusVo = deviceMapper.queryDeviceStatusById(deviceId); |
||||
|
||||
// 计算点位
|
||||
double cardNo = (hour * 60 + min) / 15.0; |
||||
double ceil = Math.ceil(cardNo); |
||||
double aa = cardNo - (ceil - 1); |
||||
if (aa < 0.75) { |
||||
ceil = Math.floor(cardNo); |
||||
} |
||||
int countNo = (int) ceil; |
||||
log.info("修改点位: " + countNo); |
||||
String valNo = "val" + countNo; |
||||
|
||||
if (deviceStatusVo != null) { |
||||
if (null != params.getFloat("P")){ |
||||
double P = params.getFloat("P"); |
||||
double TotWh = params.getFloat("TotWh"); |
||||
PsdcElectricRtdata pert = new PsdcElectricRtdata(); |
||||
pert.setDeviceId(deviceId); |
||||
long count1 = electricRtDataMapper.count(pert); |
||||
if (count1 > 0){ |
||||
electricRtDataMapper.updateByDevId(deviceId, upDateTime, P, TotWh); |
||||
} else { |
||||
pert.setUpdateTime(upDateTime); |
||||
pert.setTotp(P); |
||||
pert.setTotwh(TotWh); |
||||
electricRtDataMapper.insert(pert); |
||||
} |
||||
PsdcElectricHtdata peht = new PsdcElectricHtdata(); |
||||
peht.setDeviceId(deviceId); |
||||
long count2 = electricHtDataMapper.count(peht); |
||||
Double useEnergy; |
||||
if (count2 > 0){ |
||||
PsdcElectricHtdata htdata = electricHtDataMapper.selThisHtDataGroupByTime(deviceId); |
||||
useEnergy = TotWh - htdata.getTotwh(); |
||||
} else { |
||||
useEnergy = TotWh; |
||||
} |
||||
peht.setUpdateTime(upDateTime); |
||||
peht.setTotp(P); |
||||
peht.setTotwh(TotWh); |
||||
peht.setUseDeiffen(useEnergy); |
||||
electricHtDataMapper.insert(peht); |
||||
|
||||
// 根据设备id查询设备功率表中该设备的条数
|
||||
int count3 = devicePowerHtData96Mapper.todayPowerCount(deviceId, samDate); |
||||
// 设备有功功率入库
|
||||
if (count3 < 1) { |
||||
PsdcDevicePowerHdata96 powerHData96 = new PsdcDevicePowerHdata96(); |
||||
powerHData96.setUserId(Math.toIntExact(deviceStatusVo.getUserId())); |
||||
powerHData96.setDeviceId(deviceId); |
||||
powerHData96.setSamDate(samDate); |
||||
devicePowerHtData96Mapper.insertTodayDevPower(powerHData96); |
||||
} |
||||
devicePowerHtData96Mapper.updateTodayDevPower(valNo, P, deviceId, samDate); |
||||
} |
||||
|
||||
if (null != params.getFloat("NowTemp")){ |
||||
double InTemp = params.getFloat("InTemp"); |
||||
double OutTemp = params.getFloat("OutTemp"); |
||||
double NowTemp = params.getFloat("NowTemp"); |
||||
|
||||
PsdcThermometerRtdata ptrt = new PsdcThermometerRtdata(); |
||||
ptrt.setDeviceId(deviceId); |
||||
long count4 = thermometerRtDataMapper.count(ptrt); |
||||
if (count4 > 0){ |
||||
thermometerRtDataMapper.updateByDevId(deviceId, upDateTime, NowTemp, InTemp, OutTemp); |
||||
} else { |
||||
ptrt.setUpdateTime(upDateTime); |
||||
ptrt.setThermometerValue(NowTemp); |
||||
ptrt.setThermometerValueIn(InTemp); |
||||
ptrt.setThermometerValueOut(OutTemp); |
||||
thermometerRtDataMapper.insert(ptrt); |
||||
} |
||||
PsdcThermometerHtdata ptht = new PsdcThermometerHtdata(); |
||||
ptht.setDeviceId(deviceId); |
||||
ptht.setUpdateTime(upDateTime); |
||||
ptht.setThermometerValue(NowTemp); |
||||
ptht.setThermometerValueIn(InTemp); |
||||
ptht.setThermometerValueOut(OutTemp); |
||||
thermometerHtDataMapper.insert(ptht); |
||||
} |
||||
int devStatus = params.getInteger("Sta"); |
||||
|
||||
deviceMapper.upDevStatus(devStatus(devStatus), deviceId); |
||||
return true; |
||||
} else { |
||||
log.info("不存在该设备id"); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
|
||||
private Integer devStatus(int i){ |
||||
switch (i){ |
||||
case 0: |
||||
return 4; |
||||
case 1: |
||||
return 3; |
||||
default: |
||||
return i; |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue