104 lines
2.7 KiB
Python
104 lines
2.7 KiB
Python
# -*- encoding:utf-8 -*-
|
|
|
|
'''
|
|
@Author : dingjiawen
|
|
@Date : 2023/5/17 15:42
|
|
@Usage : 燃气轮机数据读取和示例
|
|
@Desc : 燃气轮机数据读取和示例
|
|
'''
|
|
import numpy as np
|
|
import pandas as pd
|
|
from datetime import datetime
|
|
import sys
|
|
from apscheduler.schedulers.blocking import BlockingScheduler
|
|
import pytz
|
|
from utils.KafkaUtils import *
|
|
|
|
data_path = r"E:\data\Historian(1)\historian_data_demo\2023_01_02.npy"
|
|
total_data = np.load(data_path)
|
|
|
|
|
|
data_sorted = total_data[total_data[:, 2].argsort()]
|
|
now_time = datetime.now()
|
|
index = 0
|
|
|
|
print(total_data)
|
|
print(data_sorted)
|
|
|
|
def sent_data():
|
|
try:
|
|
global index
|
|
single_data = total_data[index, :]
|
|
# print(single_data)
|
|
data = {
|
|
"tagName": single_data[0],
|
|
"value": single_data[1],
|
|
"realtime": single_data[2],
|
|
"time": now_time.strftime("%Y%m%d%H%M%S")
|
|
}
|
|
data_send = json.dumps(data).encode("gbk")
|
|
# 发送三条消息
|
|
producer = getKafkaProducer()
|
|
producer.send(
|
|
'ods_base_data',
|
|
value=data_send
|
|
) # 向分区1发送消息
|
|
# print("send {}".format(
|
|
# data_send))
|
|
# 监控是否发送成功
|
|
index = index + 1
|
|
except Exception as e:
|
|
try:
|
|
traceback.print_exc(file=sys.stdout)
|
|
except Exception as ex:
|
|
print(ex)
|
|
finally:
|
|
index = min(len(total_data),index)
|
|
# index = index % 5000
|
|
print(data)
|
|
print("end send data")
|
|
pass
|
|
|
|
|
|
def sent_data_sorted():
|
|
try:
|
|
global index
|
|
single_data = data_sorted[index, :]
|
|
# print(single_data)
|
|
data = {
|
|
"tagName": single_data[0],
|
|
"value": single_data[1],
|
|
"realtime": single_data[2],
|
|
"time": now_time.strftime("%Y%m%d%H%M%S")
|
|
}
|
|
data_send = json.dumps(data).encode("gbk")
|
|
# 发送三条消息
|
|
producer = getKafkaProducer()
|
|
producer.send(
|
|
'ods_base_data',
|
|
value=data_send
|
|
) # 向分区1发送消息
|
|
# print("send {}".format(
|
|
# data_send))
|
|
# 监控是否发送成功
|
|
index = index + 1
|
|
except Exception as e:
|
|
try:
|
|
traceback.print_exc(file=sys.stdout)
|
|
except Exception as ex:
|
|
print(ex)
|
|
finally:
|
|
index = min(len(total_data),index)
|
|
# index = index % 5000
|
|
print(data)
|
|
print("end send data")
|
|
pass
|
|
pass
|
|
|
|
|
|
|
|
# if __name__ == '__main__':
|
|
# timez = pytz.timezone('Asia/Shanghai')
|
|
# scheduler = BlockingScheduler(timezone=timez)
|
|
# scheduler.add_job(sent_data_sorted, 'interval', seconds=0.5, next_run_time=datetime.now(), max_instances=10) # 定时发送
|
|
# scheduler.start() |