parent
6e2245538d
commit
880c395e97
@ -0,0 +1,186 @@ |
||||
package com.psdc.mqtt.savedata; |
||||
|
||||
import com.alibaba.fastjson2.JSONObject; |
||||
import com.psdc.entity.PsdcScene; |
||||
import com.psdc.entity.PsdcStrategyLog; |
||||
import com.psdc.entity.res.AppInfoRes; |
||||
import com.psdc.entity.res.PsdcSceneRes; |
||||
import com.psdc.entity.res.PsdcThermometerDataRes; |
||||
import com.psdc.mapper.*; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @Author:戴仕崑 |
||||
* @Project:psdc |
||||
* @Filename:ControlReaSave |
||||
* @Slogan 致敬大师,致敬未来的你 |
||||
* @Date:2023/7/10 9:53 |
||||
* @Version 1.0 |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
public class ControlResultSave { |
||||
|
||||
@Resource |
||||
PsdcStrategyLogMapper psdcStrategyLogMapper; |
||||
@Resource |
||||
PsdcDeviceMapper deviceMapper; |
||||
@Resource |
||||
PsdcSceneMapper psdcSceneMapper; |
||||
@Resource |
||||
PsdcThermometerRtdataMapper thermometerRtDataMapper; |
||||
|
||||
|
||||
public Boolean saveDevRtData(JSONObject jsonObject){ |
||||
|
||||
Integer deviceId = jsonObject.getInteger("deviceId"); |
||||
String timestamp = jsonObject.getString("timestamp"); |
||||
Integer sceneCode = jsonObject.getInteger("sceneCode"); |
||||
Integer statusCode = jsonObject.getInteger("statusCode"); |
||||
|
||||
|
||||
// 时间处理
|
||||
String date1 = timestamp.split("T")[0]; |
||||
String time1 = timestamp.split("T")[1]; |
||||
int hour = Integer.parseInt(time1.substring(0, 8).split(":")[0]); |
||||
String upDateTime = date1 + " " + time1.substring(0, 8); |
||||
|
||||
// 查看是否有正在执行的策略
|
||||
PsdcStrategyLog strategyLog = psdcStrategyLogMapper.selMyRunLog(2); |
||||
|
||||
if (null != strategyLog) { |
||||
String[] split = strategyLog.getRunSceneCodes().split(","); |
||||
Long runCode = Long.parseLong(split[split.length - 1]); // 正在进行步骤
|
||||
String[] split2 = strategyLog.getAllSceneCodes().split(","); |
||||
Boolean b1 = false; |
||||
Boolean b2 = false; |
||||
List<PsdcScene> list = psdcSceneMapper.queryBySceneCode(Math.toIntExact(runCode)); |
||||
if (String.valueOf(deviceId).equals(String.valueOf(list.get(0).getDeviceId()))) { |
||||
|
||||
|
||||
Double[] s = getInTemp(date1 + " " + hour, "温度传感器", deviceId); |
||||
if (split2.length == split.length) { |
||||
for (PsdcScene psc : list) { |
||||
if (String.valueOf(sceneCode).equals(String.valueOf(psc.getSceneCode()))){ |
||||
b1 = isOk(statusCode); |
||||
} |
||||
if (!b1) { |
||||
break; |
||||
} |
||||
} |
||||
String runSta = strategyLog.getStrategyRunRes() + statusCode; |
||||
psdcStrategyLogMapper.updateRunRes(strategyLog.getId(), upDateTime, 1, null, runSta, s[0], s[1]); |
||||
|
||||
synchronized (AppInfoRes.class) { |
||||
AppInfoRes.class.notify(); |
||||
} |
||||
} else { |
||||
Integer nextCode = Integer.valueOf(split2[split.length]); |
||||
List<PsdcSceneRes> list2 = psdcSceneMapper.queryScenes2(Long.valueOf(nextCode)); |
||||
for (PsdcScene psc : list) { |
||||
if (String.valueOf(sceneCode).equals(String.valueOf(psc.getSceneCode()))){ |
||||
b1 = isOk(statusCode); |
||||
} |
||||
if (!b1) { |
||||
break; |
||||
} |
||||
} |
||||
for (PsdcSceneRes psc : list2) { |
||||
b2 = isOk2(psc.getIsJudge(), psc.getJudgeDevice(), psc.getJudgeElement(), psc.getJudgeData(), date1); |
||||
if (!b2) { |
||||
break; |
||||
} |
||||
} |
||||
|
||||
String runSta = strategyLog.getStrategyRunRes() + statusCode + ","; |
||||
if (runSta.indexOf("null") >= 0) { |
||||
runSta = runSta.substring(4); |
||||
} |
||||
|
||||
if (b1) { |
||||
psdcStrategyLogMapper.updateRunRes(strategyLog.getId(), null, null, null, runSta, null, null); |
||||
if (b2){ |
||||
synchronized (AppInfoRes.class) { |
||||
AppInfoRes.class.notify(); |
||||
} |
||||
} |
||||
} else { |
||||
psdcStrategyLogMapper.updateRunRes(strategyLog.getId(), upDateTime, 1, null, runSta, s[0], s[1]); |
||||
synchronized (AppInfoRes.class) { |
||||
AppInfoRes.class.notify(); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
private Boolean isOk(Integer statusCode) { |
||||
switch (statusCode){ |
||||
case 1: |
||||
return true; |
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
private Boolean isOk2(Integer isJudge, Integer judgeDevice, String judgeElement, String judgeData, String today) { |
||||
switch (isJudge) { |
||||
case 2: |
||||
return true; |
||||
case 1: |
||||
if (null != thermometerRtDataMapper.queryById(judgeDevice, today)) { |
||||
PsdcThermometerDataRes query = thermometerRtDataMapper.queryById(judgeDevice, today); |
||||
switch (judgeElement) { |
||||
case "thermometer_value": |
||||
if (query.getThermometerValue() >= Double.parseDouble(judgeData)) ; |
||||
return true; |
||||
case "thermometer_value_in": |
||||
if (query.getThermometerValueIn() >= Double.parseDouble(judgeData)) ; |
||||
return true; |
||||
case "thermometer_value_out": |
||||
if (query.getThermometerValueOut() >= Double.parseDouble(judgeData)) ; |
||||
return true; |
||||
} |
||||
} |
||||
default: |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
public Double[] getInTemp(String dateTime, String s, Integer deviceId) { |
||||
return getDoubles(dateTime, s, deviceId, deviceMapper, thermometerRtDataMapper); |
||||
} |
||||
|
||||
static Double[] getDoubles(String dateTime, String s, Integer deviceId, PsdcDeviceMapper deviceMapper, PsdcThermometerRtdataMapper thermometerRtDataMapper) { |
||||
Integer userId = deviceMapper.queryById(deviceId).getUserId(); |
||||
List<PsdcThermometerDataRes> dataResList = thermometerRtDataMapper.selWenDu(Long.valueOf(userId), s, dateTime); |
||||
Double[] ary = new Double[2]; |
||||
Double houseInTemp = 0.0; // 实验室内平均温度
|
||||
int countIn = 0; |
||||
Double houseOutTemp = 0.0; // 实验室外平均温度
|
||||
int countOut = 0; |
||||
if (dataResList.size() > 0 && null != dataResList) { |
||||
for (PsdcThermometerDataRes dsc : dataResList) { |
||||
if (dsc.getInstallAddress().contains("室内")) { |
||||
houseInTemp = houseInTemp + dsc.getThermometerValue(); |
||||
countIn = countIn + 1; |
||||
} |
||||
if (dsc.getInstallAddress().contains("室外")) { |
||||
houseOutTemp = houseOutTemp + dsc.getThermometerValue(); |
||||
countOut = countOut + 1; |
||||
} |
||||
|
||||
} |
||||
} |
||||
ary[0] = houseInTemp / countIn; |
||||
ary[1] = houseOutTemp / countOut; |
||||
return ary; |
||||
} |
||||
} |
Loading…
Reference in new issue