-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_dashboard.py
115 lines (94 loc) · 2.27 KB
/
web_dashboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
Send stuff to the web dashboard.
Example:
```
import web_dashboard as wd
wd.id = "<your id>"
wd.init_grid(10)
wd.init_log()
wd.set_square(0, 0, "red")
wd.set_square(1, 1, "green")
wd.log("Hello, world!")
```
"""
import socket
import time
SERVER_ADDRESS = ("192.168.4.33", 3101)
id = ""
sock = None
def connect_web_server():
global sock
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(SERVER_ADDRESS)
# ("red" | "green" | "blue" | "yellow" | "cyan" | "magenta" | "none" | "magnet" | "grey")
def set_square(x: int, y: int, color: str):
if color not in [
"red",
"green",
"blue",
"yellow",
"cyan",
"magenta",
"none",
"magnet",
"grey",
]:
raise ValueError("Invalid color")
message = f"set/{id}/{x}/{y}/{color}||".encode()
sock.sendall(message)
def init_grid(grid_size: int):
message = f"init/grid/{id}/{grid_size}/{grid_size}||".encode()
sock.sendall(message)
def init_log():
message = f"init/logs/{id}||".encode()
sock.sendall(message)
def init_plot():
message = f"init/plot/{id}||".encode()
sock.sendall(message)
def log(msg: str):
message = f"log/{id}/{msg}||".encode()
sock.sendall(message)
def color_to_hex(color: str):
if color == "red":
return "FF0000"
elif color == "green":
return "00FF00"
elif color == "blue":
return "0000FF"
elif color == "yellow":
return "FFFF00"
elif color == "cyan":
return "00FFFF"
elif color == "magenta":
return "FF00FF"
elif color == "none":
return "FFFFFF"
elif color == "magnet":
return "FF00FF"
elif color == "grey":
return "808080"
else:
raise ValueError("Invalid color")
def plot(x: float, y: float, color: str):
if color not in [
"red",
"green",
"blue",
"yellow",
"cyan",
"magenta",
"none",
"magnet",
"grey",
]:
raise ValueError("Invalid color")
message = f"plot/{id}/{x}/{y}/{color_to_hex(color)}||".encode()
sock.sendall(message)
# # time.sleep(1)
# id = "test"
# connect_web_server()
# init_plot()
# # time.sleep(1)
# plot(0, 0, "red")
# plot(1, 1, "green")
# plot(2, 2, "blue")