-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClewareUSBClient.py
More file actions
288 lines (217 loc) · 8.43 KB
/
Copy pathClewareUSBClient.py
File metadata and controls
288 lines (217 loc) · 8.43 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import socket
import time
import os
import queue
import tkinter as tk
import configparser
import threading
import re
from ClewareUSBLib import (
cwUSB_setup,
cwUSB_cleanup,
cwUSB_list_Devices,
cwUSB_get_StateFromNum,
cwUSB_set_StateToNum,
cwUSB_set_NametoNum,
cwUSB_get_NameFromNum,
#cwiNoOfDevices,
)
RECONNECT_BASE_DELAY = 2
RECONNECT_MAX_DELAY = 30
USB_CMD_TIMEOUT = 10
isRunning = False
# ===================== USB WORKER =====================
USB_QUEUE = queue.Queue()
USB_LOCK = threading.Lock()
USB_READY = False
class USBCommand:
def __init__(self, cmd, args=None):
self.cmd = cmd
self.args = args or []
self.result = None
self.event = threading.Event()
def extract_name(line):
m = re.search(r"Name=(.*)$", line)
return m.group(1).strip() if m else "unknown"
def extract_serial(line):
import re
m = re.search(r"serial number=\s*(\d+)", line)
return int(m.group(1)) if m else None
def usb_worker():
global USB_READY
while True:
job = USB_QUEUE.get()
try:
with USB_LOCK:
if job.cmd == "state_all":
entries = []
txt = cwUSB_list_Devices()
if not txt:
job.result = ""
return
for i, line in enumerate(txt.splitlines()):
try:
# Extract state
state = "1" if "ON" in line else "0"
# Extract name
name = extract_name(line)
# Use index as devID (matches Cleware numbering)
dev = extract_serial(line)
entries.append(f"{dev}:{state}:{name}")
except:
pass
job.result = ",".join(entries)
elif job.cmd == "state":
dev = int(job.args[0])
job.result = str(cwUSB_get_StateFromNum(dev))
elif job.cmd == "on":
dev, val = int(job.args[0]), 1
cwUSB_set_StateToNum(dev, val)
job.result = "OK"
elif job.cmd == "off":
dev, val = int(job.args[0]), 0
cwUSB_set_StateToNum(dev, val)
job.result = "OK"
elif job.cmd == "toggle":
dev, val = int(job.args[0]), 1 if cwUSB_get_StateFromNum(int(job.args[0])) == 0 else 0
cwUSB_set_StateToNum(dev, val)
job.result = "OK"
elif job.cmd == "rename":
dev = int(job.args[0])
name = " ".join(job.args[1:])
cwUSB_set_NametoNum(dev, name)
job.result = "OK"
elif job.cmd == "list":
job.result = cwUSB_list_Devices()
else:
job.result = "ERROR: UNKNOWN_CMD"
except Exception as e:
# USB may be temporarily unavailable — try to recover
try:
cwUSB_cleanup()
time.sleep(1)
cwUSB_setup()
except Exception:
pass
job.result = f"ERROR:{e}"
print(f"[CLIENT] RESP {job.cmd!r} -> {job.result!r}")
job.event.set()
threading.Thread(target=usb_worker, daemon=True).start()
def usb_execute(cmd, args=None):
job = USBCommand(cmd, args)
USB_QUEUE.put(job)
ok = job.event.wait(timeout=USB_CMD_TIMEOUT)
if not ok:
return "ERROR: USB_TIMEOUT"
return job.result
# ===================== SOCKET CLIENT =====================
def get_config():
config = configparser.ConfigParser(allow_unnamed_section=True)
config.read('ClewareClientConfig.ini')
host = '0.0.0.0' # Default host
port = 0 # Default port
dll = r"USBaccessX64.dll"
try:
NetConfig = config[configparser.UNNAMED_SECTION]
host = NetConfig.get('host', host)
port = NetConfig.getint('port', port)
dll = NetConfig.get('dll', dll)
except Exception:
pass
return [host, port, dll]
def run_agent():
global isRunning
host, port, _ = get_config()
node = socket.gethostname().lower()
print(f"[CLIENT] Starting Cleware agent as {node}")
# Initialize USB once
cwUSB_setup()
delay = RECONNECT_BASE_DELAY
while True:
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
s.connect((host, port))
s.sendall(f"HELLO {node}\n".encode())
isRunning = True
print("[CLIENT] Connected to server")
delay = RECONNECT_BASE_DELAY
while True:
data = s.recv(4096)
if not data:
raise ConnectionError("Server disconnected")
line = data.decode(errors="ignore").strip()
if not line:
continue
parts = line.split()
cmd, args = parts[0], parts[1:]
resp = usb_execute(cmd, args)
# debug: log what client will send back
#print(f"[CLIENT] RESP {cmd!r} -> {resp!r}")
s.sendall((resp + "\n").encode("utf-8"))
except Exception as e:
print(f"[CLIENT] Disconnected: {e}")
isRunning = False
time.sleep(delay)
delay = min(delay * 2, RECONNECT_MAX_DELAY)
break # Exit after one failed attempt for better UX in this demo
def save_config():
config = configparser.ConfigParser(allow_unnamed_section=True)
config.read('ClewareClientConfig.ini')
host = ServerAddress.get().strip()
port = Port.get().strip()
if not host or not port:
set_status("Please provide host and port", "lightcoral")
return
try:
int(port)
except ValueError:
set_status("Port must be an integer", "lightcoral")
return
with open("ClewareClientConfig.ini", "w") as f:
f.write(f"host = {host}\nport = {port}\ndll = USBaccessX64.dll")
set_status("Connection info saved", "lightgreen")
def connect():
global isRunning
isRunning = False # Reset before trying to connect
def agent_thread():
run_agent()
set_status("Connecting...", "khaki")
threading.Thread(target=agent_thread, daemon=True).start()
# Wait for connection attempt to finish (success or failure)
for _ in range(30): # Wait up to ~3 seconds (30 x 0.1s)
if isRunning:
set_status("Connected", "lightgreen")
print("Agent is running")
break
time.sleep(0.1)
else:
set_status("Connection failed", "lightcoral")
def set_status(text, bg):
text_widget.config(state="normal")
text_widget.delete("1.0", "end")
text_widget.insert("1.0", text)
text_widget.config(state="disabled")
messageVar.config(text=text, bg=bg)
#======================GUI==========================
root = tk.Tk()
root.title("Client")
tk.Label(root, text=f"Please insert server details (e.g. Address: 0.0.0.0, Port: 54757)").grid(row=0, column=0, columnspan=2, pady=(8,10))
#tk.Label(root, text="").grid(row=1, column=0) # Spacer
tk.Label(root, text=f"Current Address: {get_config()[0]}, Port: {get_config()[1]} (If ok click Connect)").grid(row=2, column=0, columnspan=2, pady=(8,10))
tk.Label(root, text="Server Address").grid(row=3, column=0, sticky="e", padx=4, pady=2)
tk.Label(root, text="Port").grid(row=4, column=0, sticky="e", padx=4, pady=2)
ServerAddress = tk.Entry(root)
Port = tk.Entry(root)
ServerAddress.grid(row=3, column=1, padx=4, pady=2)
Port.grid(row=4, column=1, padx=4, pady=2)
button = tk.Button(root, text="Save", width=20, command=save_config)
button.grid(row=5, column=0, columnspan=1, pady=5)
button2 = tk.Button(root, text="Connect", width=20, command=connect)
button2.grid(row=5, column=1, columnspan=1, pady=5)
text_widget = tk.Text(root, height=1, width=35, state="disabled")
text_widget.grid(row=6, column=0, columnspan=2, pady=(0,8))
messageVar = tk.Label(root, text="", width=40)
messageVar.grid(row=7, column=0, columnspan=2, pady=(0,8))
if __name__ == "__main__":
root.mainloop()