|
|
|
@ -1,41 +1,36 @@ |
|
|
|
package com.qniao.iot.device.power; |
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil; |
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.qniao.iot.device.power.config.ApolloConfig; |
|
|
|
import com.qniao.iot.device.power.constant.ConfigConstant; |
|
|
|
import com.qniao.iot.device.power.event.IotDevicePowerOnAndOffDataEvent; |
|
|
|
import com.qniao.iot.device.power.utils.SnowFlake; |
|
|
|
import com.qniao.iot.machine.event.MachineIotDataReceivedEvent; |
|
|
|
import com.qniao.iot.machine.event.MachineIotDataReceivedEventKafkaDeserializationSchema; |
|
|
|
import com.qniao.iot.machine.command.MachineOutputCommand; |
|
|
|
import com.qniao.iot.machine.schema.MachineOutputCommandDeserializationSchema; |
|
|
|
import com.qniao.iot.rc.constant.MachinePwrStatusEnum; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.flink.api.common.eventtime.WatermarkStrategy; |
|
|
|
import org.apache.flink.api.common.functions.FilterFunction; |
|
|
|
import org.apache.flink.api.common.state.StateTtlConfig; |
|
|
|
import org.apache.flink.api.common.state.ValueState; |
|
|
|
import org.apache.flink.api.common.state.ValueStateDescriptor; |
|
|
|
import org.apache.flink.api.common.time.Time; |
|
|
|
import org.apache.flink.api.common.typeinfo.TypeInformation; |
|
|
|
import org.apache.flink.configuration.Configuration; |
|
|
|
import org.apache.flink.connector.kafka.source.KafkaSource; |
|
|
|
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer; |
|
|
|
import org.apache.flink.streaming.api.CheckpointingMode; |
|
|
|
import org.apache.flink.streaming.api.datastream.DataStreamSource; |
|
|
|
import org.apache.flink.streaming.api.datastream.DataStream; |
|
|
|
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; |
|
|
|
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; |
|
|
|
import org.apache.flink.streaming.api.functions.KeyedProcessFunction; |
|
|
|
import org.apache.flink.streaming.connectors.elasticsearch.ElasticsearchSinkFunction; |
|
|
|
import org.apache.flink.streaming.connectors.elasticsearch7.ElasticsearchSink; |
|
|
|
import org.apache.flink.streaming.connectors.rabbitmq.RMQSource; |
|
|
|
import org.apache.flink.streaming.connectors.rabbitmq.common.RMQConnectionConfig; |
|
|
|
import org.apache.flink.util.Collector; |
|
|
|
import org.apache.http.HttpHost; |
|
|
|
import org.apache.http.auth.AuthScope; |
|
|
|
import org.apache.http.auth.UsernamePasswordCredentials; |
|
|
|
import org.apache.http.client.CredentialsProvider; |
|
|
|
import org.apache.http.impl.client.BasicCredentialsProvider; |
|
|
|
import org.apache.kafka.clients.consumer.ConsumerConfig; |
|
|
|
import org.apache.kafka.clients.consumer.OffsetResetStrategy; |
|
|
|
import org.elasticsearch.action.admin.indices.alias.Alias; |
|
|
|
import org.elasticsearch.action.index.IndexRequest; |
|
|
|
import org.elasticsearch.action.search.SearchRequest; |
|
|
|
@ -48,7 +43,6 @@ import org.elasticsearch.client.indices.CreateIndexRequest; |
|
|
|
import org.elasticsearch.client.indices.CreateIndexResponse; |
|
|
|
import org.elasticsearch.client.indices.GetIndexRequest; |
|
|
|
import org.elasticsearch.common.xcontent.XContentType; |
|
|
|
import org.elasticsearch.index.query.BoolQueryBuilder; |
|
|
|
import org.elasticsearch.index.query.QueryBuilders; |
|
|
|
import org.elasticsearch.rest.RestStatus; |
|
|
|
import org.elasticsearch.search.SearchHit; |
|
|
|
@ -64,6 +58,7 @@ import java.time.ZoneOffset; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
import java.util.concurrent.locks.ReentrantLock; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public class IotDevicePowerOnAndOffDataJob { |
|
|
|
@ -99,53 +94,35 @@ public class IotDevicePowerOnAndOffDataJob { |
|
|
|
final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); |
|
|
|
env.enableCheckpointing(60000L, CheckpointingMode.EXACTLY_ONCE); |
|
|
|
// 获取设备数据源 |
|
|
|
KafkaSource<MachineIotDataReceivedEvent> source = KafkaSource.<MachineIotDataReceivedEvent>builder() |
|
|
|
.setBootstrapServers(ApolloConfig.getStr(ConfigConstant.SOURCE_KAFKA_BOOTSTRAP_SERVERS)) |
|
|
|
.setTopics(ApolloConfig.getStr(ConfigConstant.SOURCE_KAFKA_TOPICS)) |
|
|
|
.setGroupId(ApolloConfig.getStr(ConfigConstant.SOURCE_KAFKA_GROUP_ID)) |
|
|
|
.setStartingOffsets(OffsetsInitializer.committedOffsets(OffsetResetStrategy.LATEST)) |
|
|
|
.setProperty(ConsumerConfig.SESSION_TIMEOUT_MS_CONFIG, "8000") |
|
|
|
.setValueOnlyDeserializer(new MachineIotDataReceivedEventKafkaDeserializationSchema()) |
|
|
|
RMQConnectionConfig connectionConfig = new RMQConnectionConfig.Builder() |
|
|
|
.setHost(ApolloConfig.getStr(ConfigConstant.SOURCE_RABBITMQ_HOST)) |
|
|
|
.setPort(ApolloConfig.getInt(ConfigConstant.SOURCE_RABBITMQ_PORT)) |
|
|
|
.setUserName(ApolloConfig.getStr(ConfigConstant.SOURCE_RABBITMQ_USERNAME)) |
|
|
|
.setPassword(ApolloConfig.getStr(ConfigConstant.SOURCE_RABBITMQ_PASSWORD)) |
|
|
|
.setVirtualHost(ApolloConfig.getStr(ConfigConstant.SOURCE_RABBITMQ_VIRTUALHOST)) |
|
|
|
.build(); |
|
|
|
|
|
|
|
// 设备数据源转换 |
|
|
|
DataStreamSource<MachineIotDataReceivedEvent> dataStreamSource = env |
|
|
|
.fromSource(source, WatermarkStrategy.noWatermarks(), "machineIotDataReceivedEvent Kafka Source"); |
|
|
|
final DataStream<MachineOutputCommand> dataStreamSource = env |
|
|
|
.addSource(new RMQSource<>(connectionConfig, ApolloConfig.getStr(ConfigConstant.SOURCE_RABBITMQ_QUEUE), |
|
|
|
false, new MachineOutputCommandDeserializationSchema())).setParallelism(1); |
|
|
|
|
|
|
|
// 数据过滤 |
|
|
|
SingleOutputStreamOperator<MachineIotDataReceivedEvent> streamOperator = dataStreamSource |
|
|
|
.filter((FilterFunction<MachineIotDataReceivedEvent>) value -> { |
|
|
|
|
|
|
|
Long reportTime = value.getReportTime(); |
|
|
|
if (reportTime != null |
|
|
|
&& value.getDataSource() != null && value.getMachinePwrStat() != null) { |
|
|
|
String reportTimeStr = StrUtil.toString(reportTime); |
|
|
|
if (reportTimeStr.length() == 10) { |
|
|
|
// 机智云那边的设备可能是秒或毫秒 |
|
|
|
reportTime = reportTime * 1000; |
|
|
|
} |
|
|
|
long nowTime = LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli(); |
|
|
|
// 晚30分钟的数据就不要了 |
|
|
|
return nowTime - reportTime <= (30 * 60 * 1000); |
|
|
|
} |
|
|
|
return false; |
|
|
|
}); |
|
|
|
|
|
|
|
SingleOutputStreamOperator<IotDevicePowerOnAndOffDataEvent> outputStreamOperator = streamOperator |
|
|
|
.keyBy(MachineIotDataReceivedEvent::getMachineIotMac) |
|
|
|
.process(new KeyedProcessFunction<Long, MachineIotDataReceivedEvent, IotDevicePowerOnAndOffDataEvent>() { |
|
|
|
SingleOutputStreamOperator<IotDevicePowerOnAndOffDataEvent> outputStreamOperator = dataStreamSource |
|
|
|
.keyBy(MachineOutputCommand::getMac) |
|
|
|
.process(new KeyedProcessFunction<Long, MachineOutputCommand, IotDevicePowerOnAndOffDataEvent>() { |
|
|
|
|
|
|
|
private ValueState<IotDevicePowerOnAndOffDataEvent> powerOnAndOffDataEventValueState; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void open(Configuration parameters) { |
|
|
|
|
|
|
|
StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(Time.hours(1)) |
|
|
|
StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(Time.minutes(10)) |
|
|
|
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite) |
|
|
|
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired) |
|
|
|
.build(); |
|
|
|
|
|
|
|
ValueStateDescriptor<IotDevicePowerOnAndOffDataEvent> powerOnAndOffDataEventValue = new ValueStateDescriptor<>("powerOnAndOffDataEventValue", |
|
|
|
ValueStateDescriptor<IotDevicePowerOnAndOffDataEvent> powerOnAndOffDataEventValue |
|
|
|
= new ValueStateDescriptor<>("powerOnAndOffDataEventValue", |
|
|
|
TypeInformation.of(IotDevicePowerOnAndOffDataEvent.class)); |
|
|
|
// 设置状态值的过期时间,为了解决手动修改数据没有同步的问题 |
|
|
|
powerOnAndOffDataEventValue.enableTimeToLive(ttlConfig); |
|
|
|
@ -156,64 +133,51 @@ public class IotDevicePowerOnAndOffDataJob { |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void processElement(MachineIotDataReceivedEvent event, |
|
|
|
KeyedProcessFunction<Long, MachineIotDataReceivedEvent, IotDevicePowerOnAndOffDataEvent>.Context ctx, |
|
|
|
public void processElement(MachineOutputCommand command, |
|
|
|
KeyedProcessFunction<Long, MachineOutputCommand, IotDevicePowerOnAndOffDataEvent>.Context ctx, |
|
|
|
Collector<IotDevicePowerOnAndOffDataEvent> out) throws Exception { |
|
|
|
|
|
|
|
IotDevicePowerOnAndOffDataEvent lastPowerOnAndOffDataEvent = getLastPowerOnAndOffDataEvent(event); |
|
|
|
IotDevicePowerOnAndOffDataEvent lastPowerOnAndOffDataEvent = getLastPowerOnAndOffDataEvent(command); |
|
|
|
Long lastReportTime = lastPowerOnAndOffDataEvent.getReportTime(); |
|
|
|
Long reportTime = event.getReportTime(); |
|
|
|
if (reportTime > lastReportTime) { |
|
|
|
Long reportTime = command.getTimestamp(); |
|
|
|
if (reportTime >= lastReportTime) { |
|
|
|
Integer lastMachinePwrStat = lastPowerOnAndOffDataEvent.getMachinePwrStat(); |
|
|
|
Integer machinePwrStat = event.getMachinePwrStat(); |
|
|
|
Integer machinePwrStat = command.getMachinePwrStat(); |
|
|
|
Integer lastMachineWorkingStat = lastPowerOnAndOffDataEvent.getMachineWorkingStat(); |
|
|
|
Integer machineWorkingStat = event.getMachineWorkingStat(); |
|
|
|
Integer machineWorkingStat = command.getMachineWorkingStat(); |
|
|
|
if (!((lastMachineWorkingStat == 0 && machineWorkingStat == 0) |
|
|
|
|| (lastMachineWorkingStat == 2 && machineWorkingStat == 2))) { |
|
|
|
Long lastAccJobCount = lastPowerOnAndOffDataEvent.getAccJobCount(); |
|
|
|
Long accJobCount = event.getAccJobCount(); |
|
|
|
Long currJobCount = event.getCurrJobCount(); |
|
|
|
Integer dataSource = event.getDataSource(); |
|
|
|
Long currJobCount = command.getCurrJobCount(); |
|
|
|
Long currJobDuration = command.getCurrJobDuration(); |
|
|
|
IotDevicePowerOnAndOffDataEvent powerOnAndOffDataEvent = new IotDevicePowerOnAndOffDataEvent(); |
|
|
|
powerOnAndOffDataEvent.setId(snowflake.nextId()); |
|
|
|
powerOnAndOffDataEvent.setDataSource(event.getDataSource()); |
|
|
|
powerOnAndOffDataEvent.setMachineIotMac(event.getMachineIotMac()); |
|
|
|
powerOnAndOffDataEvent.setMachinePwrStat(event.getMachinePwrStat()); |
|
|
|
powerOnAndOffDataEvent.setMachineWorkingStat(event.getMachineWorkingStat()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(event.getCurrJobCount()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(event.getCurrJobDuration()); |
|
|
|
powerOnAndOffDataEvent.setAccJobCount(event.getAccJobCount()); |
|
|
|
powerOnAndOffDataEvent.setReportTime(event.getReportTime()); |
|
|
|
powerOnAndOffDataEvent.setDataSource(command.getDataSource()); |
|
|
|
powerOnAndOffDataEvent.setMachineIotMac(command.getMac()); |
|
|
|
powerOnAndOffDataEvent.setMachinePwrStat(command.getMachinePwrStat()); |
|
|
|
powerOnAndOffDataEvent.setMachineWorkingStat(command.getMachineWorkingStat()); |
|
|
|
powerOnAndOffDataEvent.setAccJobCount(command.getCurrTotalOutput()); |
|
|
|
powerOnAndOffDataEvent.setReportTime(command.getTimestamp()); |
|
|
|
powerOnAndOffDataEvent.setReceivedTime(LocalDateTime |
|
|
|
.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
|
|
|
if (MachinePwrStatusEnum.MACHINE_OFF.getValue().equals(lastMachinePwrStat)) { |
|
|
|
if (MachinePwrStatusEnum.MACHINE_ON.getValue().equals(machinePwrStat)) { |
|
|
|
// 上次是关机,但是这次是开机,说明周期产能从新开始 |
|
|
|
if (dataSource == 1) { |
|
|
|
// 根云 |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(accJobCount - lastAccJobCount); |
|
|
|
} else { |
|
|
|
// 机智云 |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(currJobCount); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(currJobCount); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(currJobDuration); |
|
|
|
} |
|
|
|
} else { |
|
|
|
Long lastCurrJobCount = lastPowerOnAndOffDataEvent.getCurrJobCount(); |
|
|
|
Long lastCurrJobDuration = lastPowerOnAndOffDataEvent.getCurrJobDuration(); |
|
|
|
// 直接累加 |
|
|
|
if (dataSource == 1) { |
|
|
|
// 根云 |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(lastCurrJobCount + (accJobCount - lastAccJobCount)); |
|
|
|
} else { |
|
|
|
// 机智云 |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(lastCurrJobCount + currJobCount); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(lastCurrJobCount + currJobCount); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(lastCurrJobDuration + currJobDuration); |
|
|
|
} |
|
|
|
// 上次的状态只有两种,要么只有开机时间不为空,要么是开机和关机时间都不为空,否则不处理 |
|
|
|
if (lastPowerOnAndOffDataEvent.getMachinePowerOnTime() != null) { |
|
|
|
if (lastPowerOnAndOffDataEvent.getMachinePowerOffTime() == null) { |
|
|
|
// 只有开机时间不为空 |
|
|
|
if (MachinePwrStatusEnum.MACHINE_OFF.getValue().equals(machinePwrStat)) { |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOffTime(event.getReportTime()); |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOffTime(command.getTimestamp()); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(lastPowerOnAndOffDataEvent.getMachinePowerOnTime()); |
|
|
|
powerOnAndOffDataEventValueState.update(powerOnAndOffDataEvent); |
|
|
|
@ -222,7 +186,7 @@ public class IotDevicePowerOnAndOffDataJob { |
|
|
|
// // 开机和关机时间都不为空,说明上一个生产周期已经过了,那么当前设备的电源状态必须要是开机状态 |
|
|
|
if (MachinePwrStatusEnum.MACHINE_ON.getValue().equals(machinePwrStat)) { |
|
|
|
// 开机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(event.getReportTime()); |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(command.getTimestamp()); |
|
|
|
powerOnAndOffDataEventValueState.update(powerOnAndOffDataEvent); |
|
|
|
out.collect(powerOnAndOffDataEvent); |
|
|
|
} |
|
|
|
@ -232,20 +196,20 @@ public class IotDevicePowerOnAndOffDataJob { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private IotDevicePowerOnAndOffDataEvent getLastPowerOnAndOffDataEvent(MachineIotDataReceivedEvent event) throws IOException { |
|
|
|
private IotDevicePowerOnAndOffDataEvent getLastPowerOnAndOffDataEvent(MachineOutputCommand command) throws IOException { |
|
|
|
|
|
|
|
IotDevicePowerOnAndOffDataEvent iotDevicePowerOnAndOffDataEvent = powerOnAndOffDataEventValueState.value(); |
|
|
|
if (iotDevicePowerOnAndOffDataEvent == null) { |
|
|
|
iotDevicePowerOnAndOffDataEvent = getByEs(event); |
|
|
|
iotDevicePowerOnAndOffDataEvent = getByEs(command); |
|
|
|
} |
|
|
|
return iotDevicePowerOnAndOffDataEvent; |
|
|
|
} |
|
|
|
|
|
|
|
private IotDevicePowerOnAndOffDataEvent getByEs(MachineIotDataReceivedEvent event) throws IOException { |
|
|
|
private IotDevicePowerOnAndOffDataEvent getByEs(MachineOutputCommand command) throws IOException { |
|
|
|
|
|
|
|
// 构建查询条件(注意:termQuery 支持多种格式查询,如 boolean、int、double、string 等,这里使用的是 string 的查询) |
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
searchSourceBuilder.query(QueryBuilders.termQuery("machineIotMac", event.getMachineIotMac())); |
|
|
|
searchSourceBuilder.query(QueryBuilders.termQuery("machineIotMac", command.getMac())); |
|
|
|
searchSourceBuilder.sort("receivedTime", SortOrder.DESC); |
|
|
|
searchSourceBuilder.size(1); |
|
|
|
// 创建查询请求对象,将查询对象配置到其中 |
|
|
|
@ -265,76 +229,28 @@ public class IotDevicePowerOnAndOffDataJob { |
|
|
|
return JSONUtil.toBean(reqHit.getSourceAsString(), IotDevicePowerOnAndOffDataEvent.class); |
|
|
|
} |
|
|
|
} |
|
|
|
// 如果没有就找清洗后的数据 |
|
|
|
MachineIotDataReceivedEvent deviceMonitoringData = getMachineIotDataReceivedEvent(event.getMachineIotMac()); |
|
|
|
IotDevicePowerOnAndOffDataEvent powerOnAndOffDataEvent = new IotDevicePowerOnAndOffDataEvent(); |
|
|
|
powerOnAndOffDataEvent.setId(snowflake.nextId()); |
|
|
|
if (deviceMonitoringData != null) { |
|
|
|
powerOnAndOffDataEvent.setDataSource(deviceMonitoringData.getDataSource()); |
|
|
|
powerOnAndOffDataEvent.setMachineIotMac(deviceMonitoringData.getMachineIotMac()); |
|
|
|
powerOnAndOffDataEvent.setAccJobCount(deviceMonitoringData.getAccJobCount()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(0L); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(0L); |
|
|
|
Integer machinePwrStat = deviceMonitoringData.getMachinePwrStat(); |
|
|
|
powerOnAndOffDataEvent.setMachinePwrStat(deviceMonitoringData.getMachinePwrStat()); |
|
|
|
powerOnAndOffDataEvent.setMachineWorkingStat(deviceMonitoringData.getMachineWorkingStat()); |
|
|
|
Long reportTime = deviceMonitoringData.getReportTime(); |
|
|
|
// 开机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(reportTime); |
|
|
|
if (machinePwrStat == 0) { |
|
|
|
// 关机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOffTime(reportTime); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setReportTime(reportTime); |
|
|
|
} else { |
|
|
|
powerOnAndOffDataEvent.setDataSource(event.getDataSource()); |
|
|
|
powerOnAndOffDataEvent.setMachineIotMac(event.getMachineIotMac()); |
|
|
|
powerOnAndOffDataEvent.setAccJobCount(event.getAccJobCount()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(event.getCurrJobCount()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(event.getCurrJobDuration()); |
|
|
|
Integer machinePwrStat = event.getMachinePwrStat(); |
|
|
|
powerOnAndOffDataEvent.setMachinePwrStat(event.getMachinePwrStat()); |
|
|
|
powerOnAndOffDataEvent.setMachineWorkingStat(event.getMachineWorkingStat()); |
|
|
|
Long reportTime = event.getReportTime(); |
|
|
|
// 开机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(reportTime); |
|
|
|
if (machinePwrStat == 0) { |
|
|
|
// 关机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOffTime(reportTime); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setReportTime(reportTime); |
|
|
|
powerOnAndOffDataEvent.setDataSource(command.getDataSource()); |
|
|
|
powerOnAndOffDataEvent.setMachineIotMac(command.getMac()); |
|
|
|
powerOnAndOffDataEvent.setAccJobCount(command.getCurrTotalOutput()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobCount(command.getCurrJobCount()); |
|
|
|
powerOnAndOffDataEvent.setCurrJobDuration(command.getCurrJobDuration()); |
|
|
|
Integer machinePwrStat = command.getMachinePwrStat(); |
|
|
|
powerOnAndOffDataEvent.setMachinePwrStat(machinePwrStat); |
|
|
|
powerOnAndOffDataEvent.setMachineWorkingStat(command.getMachineWorkingStat()); |
|
|
|
Long reportTime = command.getTimestamp(); |
|
|
|
// 开机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOnTime(reportTime); |
|
|
|
if (machinePwrStat == 0) { |
|
|
|
// 关机 |
|
|
|
powerOnAndOffDataEvent.setMachinePowerOffTime(reportTime); |
|
|
|
} |
|
|
|
powerOnAndOffDataEvent.setReportTime(reportTime); |
|
|
|
powerOnAndOffDataEvent.setReceivedTime(LocalDateTime |
|
|
|
.now().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()); |
|
|
|
return powerOnAndOffDataEvent; |
|
|
|
} |
|
|
|
|
|
|
|
private MachineIotDataReceivedEvent getMachineIotDataReceivedEvent(Long machineIotMac) { |
|
|
|
|
|
|
|
try { |
|
|
|
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
|
|
|
BoolQueryBuilder bool = new BoolQueryBuilder(); |
|
|
|
BoolQueryBuilder boolQueryBuilder = bool.must(QueryBuilders.termQuery("machineIotMac", machineIotMac)); |
|
|
|
searchSourceBuilder.size(1); |
|
|
|
searchSourceBuilder.sort("reportTime", SortOrder.DESC); |
|
|
|
searchSourceBuilder.query(boolQueryBuilder); |
|
|
|
SearchRequest request = new SearchRequest(ApolloConfig.getStr(ConfigConstant.DATA_ELASTICSEARCH_INDEX)); |
|
|
|
request.source(searchSourceBuilder); |
|
|
|
// 执行请求 |
|
|
|
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); |
|
|
|
if (RestStatus.OK.equals(response.status())) { |
|
|
|
SearchHit[] hits = response.getHits().getHits(); |
|
|
|
if (hits.length > 0) { |
|
|
|
SearchHit hit = hits[0]; |
|
|
|
String sourceAsString = hit.getSourceAsString(); |
|
|
|
return JSONUtil.toBean(sourceAsString, MachineIotDataReceivedEvent.class); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error("获取 machine_iot_data_received_event 索引数据异常"); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
}).name("iotDevicePowerOnAndOffDataEvent keyBy machineIotMac"); |
|
|
|
|
|
|
|
sinkEs(outputStreamOperator); |
|
|
|
|