Compare commits

...

3 Commits

Author SHA1 Message Date
MuJ 5e6561c5a6 完成第一次测试 2024-02-26 22:06:50 +08:00
MuJ 5e2de1d226 完成数据传输功能的实现 2024-02-20 09:20:41 +08:00
MuJ 854e065c6d 初步完成了接收和发送数据的代码编写 2024-02-04 21:36:00 +08:00
6 changed files with 268 additions and 1 deletions

View File

@ -1,2 +1,9 @@
# Liu
# 主要目标:实现对数据的预测和攻击的预警,完成图像化显示
# 分目标:
# 1.数据传输(完成)
# 2.同步进行数据传输和数据预测
# 3.数据预测结果显示
# 4.预测结果和实际结果比对
# 5.抗攻击模型和原始模型多重预测比对

Binary file not shown.

92
listen.py Normal file
View File

@ -0,0 +1,92 @@
import socket
import threading
class VoltageReceiver():
def __init__(self):
self.data = "" # 数据存储
self.active = False # 控制线程的开关
self.dataready = 0 # 数据是否准备好的标志位
self.thread = None # 线程存储
self.sock = None # 套接字存储
def receive_data(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind(('127.0.0.1', 9996))
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.listen(1)
while self.active: # 只有当 self.active 为 True 时,才开始监听
conn, addr = self.sock.accept()
while conn:
try:
data = conn.recv(1024)
except socket.error as e:
print("Remote host has disconnected.") # 打印提醒
break
if not data or not self.active: # 如果没有数据或 self.active 为 False 则跳出循环
break
data = data.decode()
if self.dataready:
# 新的数据已经准备好,而旧的数据还没被取走,发出警告
self.alert()
with threading.Lock(): # 使用锁确保线程安全
self.data = data
self.dataready = 1
conn.close()
def start(self): # 开始监听
if not self.thread or not self.thread.is_alive(): # 如果线程未创建或已结束,那么创建一个新线程
self.active = True
self.thread = threading.Thread(target=self.receive_data)
self.thread.start()
def stop(self): # 停止监听
self.active = False
if self.sock: # 关闭套接字,使得 sock.accept() 退出阻塞
self.sock.close()
def get_data(self): # 获取数据
with threading.Lock(): # 使用锁确保线程安全
self.dataready = 0
return self.data
def alert(self): # 报警函数
print("Warning: New data has arrived but the old data has not been taken away.")
def write_to_file(data, filename):
"""
This function write data to a local .txt file
:param data: str, data to be written to the file
:param filename: str, name of the file
"""
with open(filename, 'w') as file:
file.write(data)
def read_from_file(filename):
"""
This function read data from a local .txt file
:param filename: str, name of the file
:return: str, data read from the file
"""
with open(filename, 'r') as file:
data = file.read().replace('\n', '')
return data
if __name__ == "__main__": # 测试代码
receiver = VoltageReceiver() # 创建一个 VoltageReceiver 的实例
receiver.start() # 开始监听
while receiver.active:
if receiver.dataready: # 当数据准备好时,打印数据
data = read_from_file('test.txt')
if data == '#' or data == '':
print(receiver.get_data())
write_to_file(receiver.get_data(), "test.txt")
else:
pass
receiver.stop() # 停止监听

99
main.py Normal file
View File

@ -0,0 +1,99 @@
# Import packages
from dash import Dash, dcc, html, Input, Output
import feffery_antd_components as fac
import plotly.graph_objs as go
from listen import VoltageReceiver
def read_from_file(filename):
"""
This function read data from a local .txt file
:param filename: str, name of the file
:return: str, data read from the file
"""
with open(filename, 'r') as file:
data = file.read().replace('\n', '')
return data
def write_to_file(data, filename):
"""
This function write data to a local .txt file
:param data: str, data to be written to the file
:param filename: str, name of the file
"""
with open(filename, 'w') as file:
file.write(data)
def go_figure_set(X, Y, figuremode, width=600, height=400, title=''):
# 创建折线图
line_chart = go.Figure(data=go.Scatter(x=X, y=Y, mode=figuremode))
line_chart.update_layout(
autosize=False,
width=width,
height=height,
title=title
)
return line_chart
# Initialize the app
app = Dash(__name__)
X = list(range(1, 1001))
Y = [0] * 1000
# App layout
app.layout = html.Div(
[
html.Div([
dcc.Graph(
id='line-chart',
figure=go_figure_set(X, Y, 'lines'),
)], style={
'display': 'inline-block',
'position': 'absolute', # 使用绝对定位
'right': '20px', # 距离页面右边20px
'top': '20px', # 距离页面顶部20px
'width': '600px' # 设置图表容器的宽度
}), # 将折线图添加到Dash应用中
dcc.Interval(
id='interval-update',
interval=1*10, # in milliseconds
),
],
style={
'padding': '50px 100px'
},
)
# Define callback to update statistic
@app.callback(
Output('line-chart', 'figure'),
Input('interval-update', 'n_intervals')
)
def update_output(n_intervals):
data = read_from_file('test.txt')
if data == '#':
pass
else:
Y.append(float(data))
Y.pop(0)
write_to_file('#', "test.txt")
line_chart = go_figure_set(X, Y, 'lines')
# 更新图表布局设置新的X轴范围
line_chart.update_layout(xaxis=dict(range=[0, 2001]))
line_chart.add_trace(go.Scatter(x=list(range(1001, 1051)), y=list(range(1, 51)), mode='lines',line=dict(color='red')))
return line_chart
# Run the app
if __name__ == '__main__':
app.run_server(debug=True, port=8050)

68
post.py Normal file
View File

@ -0,0 +1,68 @@
import socket
import random
import time
import pandas as pd
import numpy as np
def data_clean(data, is_column=False):
"""_summary_
Args:
data (_type_): csv数据
is_column (bool, optional): 清除含有NaN数据的列. Defaults to False.即清除含有NaN数据的行
Returns:
_type_: 清洗过的数据
"""
if not is_column:
data = data.dropna(axis=0)
return data
else:
data = data.dropna(axis=1)
return data
def data_load(data_path, is_column=False):
"""
数据加载
data_path: 数据路径
is_column: 是否是列数据
return:X,Y
"""
# 读取csv文件
df = pd.read_csv(data_path)
# 进行数据清洗
data_clean(df, is_column)
df = df[df['output'] == 1]
# 去除第一列
df = df.drop(df.columns[0], axis=1)
# 初始化X,Y
X= []
# 遍历DataFrame的每一行
for index, row in df.iterrows():
# 获取前127个数据项
X.append(row.iloc[0:129])
return np.array(X)
# 输入需要发送的数据
data = data_load('data\PowerQualityDistributionDataset1.csv')
# 创建一个socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 建立连接:
s.connect(('localhost', 9996))
while True:
for i in data:
for j in i:
try:
time.sleep(0.5)
# 发送数据:
s.send((str(j)).encode())
except KeyboardInterrupt:
break
# 关闭连接
s.close()

1
test.txt Normal file
View File

@ -0,0 +1 @@
#3293.482471