diff --git a/README.md b/README.md index ad8d83e..db17197 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ -# Liu +# 主要目标:实现对数据的预测和攻击的预警,完成图像化显示 +# 分目标: +# 1.数据传输(完成) +# 2.同步进行数据传输和数据预测 +# 3.数据预测结果显示 +# 4.预测结果和实际结果比对 +# 5.抗攻击模型和原始模型多重预测比对 + diff --git a/__pycache__/listen.cpython-310.pyc b/__pycache__/listen.cpython-310.pyc new file mode 100644 index 0000000..416cdc8 Binary files /dev/null and b/__pycache__/listen.cpython-310.pyc differ diff --git a/listen.py b/listen.py index 7f91efd..386deed 100644 --- a/listen.py +++ b/listen.py @@ -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: # 当数据准备好时,打印数据 - print(receiver.get_data()) + 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() # 停止监听 diff --git a/main.py b/main.py new file mode 100644 index 0000000..0952e20 --- /dev/null +++ b/main.py @@ -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) diff --git a/post.py b/post.py index e952314..842443e 100644 --- a/post.py +++ b/post.py @@ -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 # 发送数据: diff --git a/test.txt b/test.txt new file mode 100644 index 0000000..4287ca8 --- /dev/null +++ b/test.txt @@ -0,0 +1 @@ +# \ No newline at end of file