package cn.flightfeather.supervision.timingtask
|
|
import cn.flightfeather.supervision.common.net.FumeHttpService
|
import cn.flightfeather.supervision.domain.entity.DeviceInfo
|
import cn.flightfeather.supervision.domain.entity.FumeMinuteValue
|
import cn.flightfeather.supervision.domain.enumeration.DistrictType
|
import cn.flightfeather.supervision.domain.enumeration.SceneType
|
import cn.flightfeather.supervision.domain.mapper.DeviceInfoMapper
|
import cn.flightfeather.supervision.domain.mapper.FumeMinuteValueMapper
|
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
|
import com.github.pagehelper.PageHelper
|
import org.slf4j.LoggerFactory
|
import org.springframework.beans.factory.annotation.Autowired
|
import org.springframework.stereotype.Component
|
import tk.mybatis.mapper.entity.Example
|
import java.time.LocalDateTime
|
import java.time.LocalTime
|
import java.time.ZoneId
|
import java.time.ZoneOffset
|
import java.time.format.DateTimeFormatter
|
import java.util.*
|
import javax.annotation.PostConstruct
|
import kotlin.math.round
|
|
/**
|
* 上传油烟监测数据
|
*/
|
@Component
|
class PushFume : BaseTimingTask() {
|
|
companion object {
|
// private lateinit var instance: PushFume
|
val LOGGER = LoggerFactory.getLogger(PushFume::class.java)
|
}
|
|
@Autowired
|
lateinit var deviceInfoMapper: DeviceInfoMapper
|
|
@Autowired
|
lateinit var fumeMinuteValueMapper: FumeMinuteValueMapper
|
|
// @PostConstruct
|
// fun init() {
|
// instance = this
|
// }
|
|
private val deviceCodeList = mutableListOf<String>()
|
|
override val period: Long
|
get() = 1L
|
|
override fun doTask(localtime: LocalDateTime) {
|
LOGGER.info("===========开始执行油烟数据上传任务===============")
|
// 刷新监测点编号
|
refreshDeviceCode()
|
|
//每10分钟计算一次平均值并上传
|
// FIXME: 2021/4/8 均值的计算逻辑之后应该放到其他模块
|
val min = localtime.minute
|
if (min != 0 && min != 10 && min != 20 && min != 30 && min != 40 && min != 50) return
|
|
//计算取值时间
|
val endTime = Date.from(localtime.minusMinutes(1).withSecond(59).atZone(ZoneId.systemDefault()).toInstant())
|
val startTime = Date.from(localtime.minusMinutes(10).withSecond(0).atZone(ZoneId.systemDefault()).toInstant())
|
|
//生成上传数据结构体
|
val postData = FumeHttpService.PostData()
|
val allData = mutableListOf<FumeMinuteValue>()
|
deviceCodeList.forEach {
|
//获取前10分钟的数据
|
val dataList = fumeMinuteValueMapper.selectByExample(Example(FumeMinuteValue::class.java).apply {
|
createCriteria().andEqualTo("mvStatCode", it)
|
.andBetween("mvCreateTime", startTime, endTime)
|
and(createCriteria().orIsNull("mvUpload")
|
.orEqualTo("mvUpload", false))
|
})
|
|
//计算均值
|
var count = 0
|
var total = 0.0
|
dataList.forEach {
|
total += it.mvFumeConcentration2
|
if (it.mvFumeConcentration2 != 0.0) {
|
count++
|
}
|
}
|
if (count == 0) {
|
dataUpdate(dataList)
|
return@forEach
|
}
|
val average = round(total / count * 100) / 100
|
|
//均值等于0,不上报,直接更新上传状态
|
if (average == 0.0) {
|
dataUpdate(dataList)
|
return@forEach
|
}
|
|
//生成上传数据结构体
|
dataList.last().let {
|
postData.data.add(FumeHttpService.FumeData(
|
"hengzhiyuan_${it.mvStatCode}",
|
DateUtil.DateToString(startTime, DateUtil.DateStyle.YYYY_MM_DD_HH_MM_SS),
|
0.0, average,
|
if (it.mvFanStatus) 3 else 2,
|
it.mvFanElectricity, 0,
|
if (it.mvPurifierStatus) 3 else 2,
|
it.mvPurifierElectricity, 0,
|
0, 0.0
|
))
|
}
|
|
allData.addAll(dataList)
|
}
|
|
//上传数据并更新数据状态
|
LOGGER.info("===========数据采样时间:$localtime")
|
postData.data.forEach {
|
LOGGER.info("${it.equipmentShowId} ** ${it.dataTime}")
|
}
|
LOGGER.info("=================================")
|
FumeHttpService.uploadData(postData)?.run {
|
LOGGER.info(this.toString())
|
if (this["code"].asInt == 0 && this["data"].asInt > 0) {
|
dataUpdate(allData)
|
}
|
LOGGER.info("===========油烟数据上传任务结束============")
|
}
|
}
|
|
/**
|
* 刷新监测点编号
|
*/
|
@Synchronized
|
private fun refreshDeviceCode() {
|
val now = LocalDateTime.now()
|
if (deviceCodeList.isEmpty() || (now.hour == 0 && now.minute <= period)) {
|
deviceCodeList.clear()
|
deviceInfoMapper.selectByExample(Example(DeviceInfo::class.java).apply {
|
createCriteria().andEqualTo("diProvinceCode", "31")
|
.andEqualTo("diCityCode", "3100")
|
.andEqualTo("diDistrictCode", DistrictType.XuHui.code)
|
.andEqualTo("diSceneTypeId", SceneType.Restaurant.value)
|
.andEqualTo("diDeviceTypeId", 1)
|
.andEqualTo("diSupplier", "hengzhiyuan")
|
}).forEach {
|
it?.let {
|
deviceCodeList.add(it.diCode)
|
}
|
}
|
}
|
}
|
|
/**
|
* 更新数据状态
|
*/
|
private fun dataUpdate(dataList: List<FumeMinuteValue>) {
|
dataList.forEach {
|
it.mvUpload = true
|
fumeMinuteValueMapper.updateByPrimaryKey(it)
|
}
|
}
|
}
|