完成数据传输功能的实现

This commit is contained in:
MuJ 2024-02-20 09:20:41 +08:00
parent 854e065c6d
commit 5e2de1d226
6 changed files with 142 additions and 4 deletions

View File

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

Binary file not shown.

View File

@ -12,7 +12,7 @@ class VoltageReceiver():
def receive_data(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind(('localhost', 9996))
self.sock.bind(('127.0.0.1', 9996))
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.sock.listen(1)
@ -55,10 +55,38 @@ class VoltageReceiver():
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)

View File

@ -1,4 +1,6 @@
import socket
import random
import time
# 创建一个socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -7,7 +9,8 @@ s.connect(('localhost', 9996))
while True:
try:
# 输入需要发送的数据
data = input("Enter the voltage data you want to send (or 'exit' to quit): ")
data = str(random.randint(1, 10))
time.sleep(0.1)
if data.lower() == 'exit':
break
# 发送数据:

1
test.txt Normal file
View File

@ -0,0 +1 @@
#