@ -2,12 +2,19 @@ package com.psdc.mqtt.savedata;
import com.alibaba.fastjson2.JSONObject ;
import com.psdc.entity.* ;
import com.psdc.entity.res.AppInfoRes ;
import com.psdc.entity.res.PsdcSceneRes ;
import com.psdc.entity.res.PsdcThermometerDataRes ;
import com.psdc.entity.vo.DeviceStatusVo ;
import com.psdc.mapper.* ;
import com.psdc.utils.SecurityUtils ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Component ;
import javax.annotation.Resource ;
import java.text.SimpleDateFormat ;
import java.util.Date ;
import java.util.List ;
/ * *
* @Author : Stone
@ -30,6 +37,10 @@ public class DataAndPowerSave {
@Resource
PsdcDevicePowerHdata96Mapper devicePowerHtData96Mapper ;
@Resource
PsdcStrategyLogMapper psdcStrategyLogMapper ;
@Resource
PsdcSceneMapper psdcSceneMapper ;
@Resource
PsdcThermometerRtdataMapper thermometerRtDataMapper ;
@Resource
PsdcThermometerHtdataMapper thermometerHtDataMapper ;
@ -40,10 +51,16 @@ public class DataAndPowerSave {
@Resource
PsdcStatisticsYearMapper statisticsYearMapper ;
public Boolean saveDevRtData ( JSONObject jsonObject ) {
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy-MM-dd HH" ) ;
public Boolean saveDevRtData ( JSONObject jsonObject ) {
Integer deviceId = jsonObject . getInteger ( "deviceId" ) ;
String timestamp = jsonObject . getString ( "timestamp" ) ;
JSONObject params = jsonObject . getJSONObject ( "params" ) ;
Integer devStatus = 1 ;
Double InTemp = 0 . 0 ;
Double OutTemp = 0 . 0 ;
Double NowTemp = 0 . 0 ;
// 时间处理
String date1 = timestamp . split ( "T" ) [ 0 ] ;
@ -55,6 +72,10 @@ public class DataAndPowerSave {
// 根据设备id获取数据,查看设备库中是否有该设备,若没有该设备,则不对该设备的数据进行实时数据库入库操作
DeviceStatusVo deviceStatusVo = deviceMapper . queryDeviceStatusById ( deviceId ) ;
if ( null ! = params . getInteger ( "Sta" ) ) {
devStatus = params . getInteger ( "Sta" ) ;
}
// 计算点位
double cardNo = ( hour * 60 + min ) / 15 . 0 ;
@ -68,13 +89,13 @@ public class DataAndPowerSave {
String valNo = "val" + countNo ;
if ( deviceStatusVo ! = null ) {
if ( null ! = params . getFloat ( "P" ) ) {
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 ) {
if ( count1 > 0 ) {
electricRtDataMapper . updateByDevId ( deviceId , upDateTime , P , TotWh ) ;
} else {
pert . setUpdateTime ( upDateTime ) ;
@ -86,8 +107,8 @@ public class DataAndPowerSave {
peht . setDeviceId ( deviceId ) ;
long count2 = electricHtDataMapper . count ( peht ) ;
Double useEnergy ;
if ( count2 > 0 ) {
PsdcElectricHtdata htdata = electricHtDataMapper . selThisHtDataGroupByTime ( deviceId ) ;
if ( count2 > 0 ) {
PsdcElectricHtdata htdata = electricHtDataMapper . selThisHtDataGroupByTime ( deviceId ) . get ( 0 ) ;
useEnergy = TotWh - htdata . getTotwh ( ) ;
} else {
useEnergy = TotWh ;
@ -111,15 +132,15 @@ public class DataAndPowerSave {
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" ) ;
if ( null ! = params . getFloat ( "NowTemp" ) ) {
InTemp = Double . valueOf ( params . getFloat ( "InTemp" ) ) ;
OutTemp = Double . valueOf ( params . getFloat ( "OutTemp" ) ) ;
NowTemp = Double . valueOf ( params . getFloat ( "NowTemp" ) ) ;
PsdcThermometerRtdata ptrt = new PsdcThermometerRtdata ( ) ;
ptrt . setDeviceId ( deviceId ) ;
long count4 = thermometerRtDataMapper . count ( ptrt ) ;
if ( count4 > 0 ) {
if ( count4 > 0 ) {
thermometerRtDataMapper . updateByDevId ( deviceId , upDateTime , NowTemp , InTemp , OutTemp ) ;
} else {
ptrt . setUpdateTime ( upDateTime ) ;
@ -137,10 +158,66 @@ public class DataAndPowerSave {
thermometerHtDataMapper . insert ( ptht ) ;
}
if ( null ! = params . getInteger ( "Sta" ) ) {
int devStatus = params . getInteger ( "Sta" ) ;
deviceMapper . upDevStatus ( devStatus ( devStatus ) , deviceId ) ;
deviceMapper . upDevStatus ( devStatus ( devStatus ) , deviceId ) ;
// 查看是否有正在执行的策略
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 ( ) ) ) ) {
if ( split2 . length = = split . length ) {
for ( PsdcScene psc : list ) {
b1 = isOk ( psc . getSceneKey ( ) , psc . getSceneValue ( ) , devStatus , NowTemp , InTemp , OutTemp ) ;
if ( ! b1 ) {
break ;
}
}
if ( b1 ) {
String runSta = strategyLog . getStrategyRunRes ( ) + "1" ;
Double [ ] s = getInTemp ( date1 + " " + hour , "温度传感器" , deviceId ) ;
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 ) {
b1 = isOk ( psc . getSceneKey ( ) , psc . getSceneValue ( ) , devStatus , NowTemp , InTemp , OutTemp ) ;
if ( ! b1 ) {
break ;
}
}
for ( PsdcSceneRes psc : list2 ) {
b2 = isOk2 ( psc . getIsJudge ( ) , psc . getJudgeDevice ( ) , psc . getJudgeElement ( ) , psc . getJudgeData ( ) , date1 ) ;
if ( ! b2 ) {
break ;
}
}
if ( b1 & & b2 ) {
String runSta = strategyLog . getStrategyRunRes ( ) + "1" + "," ;
if ( runSta . indexOf ( "null" ) > = 0 ) {
runSta = runSta . substring ( 4 ) ;
}
psdcStrategyLogMapper . updateRunRes ( strategyLog . getId ( ) , null , null , null , runSta , null , null ) ;
synchronized ( AppInfoRes . class ) {
AppInfoRes . class . notify ( ) ;
}
}
}
}
}
return true ;
} else {
log . info ( "不存在该设备Id" ) ;
@ -149,8 +226,8 @@ public class DataAndPowerSave {
}
private Integer devStatus ( int i ) {
switch ( i ) {
private Integer devStatus ( int i ) {
switch ( i ) {
case 0 :
return 4 ;
case 1 :
@ -160,4 +237,82 @@ public class DataAndPowerSave {
}
}
private Boolean isOk ( String key , String value , Integer devStatus , Double temp , Double inTemp , Double outTemp ) {
String s = String . valueOf ( devStatus ) ;
switch ( key ) {
case "open" :
if ( value . equals ( s ) ) {
return true ;
}
case "close" :
if ( value . equals ( s ) ) {
return true ;
}
case "temp" :
if ( temp > = Double . parseDouble ( value ) ) {
return true ;
}
case "intemp" :
if ( inTemp > = Double . parseDouble ( value ) ) {
return true ;
}
case "outtemp" :
if ( outTemp > = Double . parseDouble ( value ) ) {
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 ) {
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 ;
}
}