-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
76 lines (63 loc) · 2.24 KB
/
client.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
from pymodbus.client.sync import ModbusTcpClient
from factory import Factory
import subprocess
from shlex import split as shsplit
import os
class Client:
MAX_TRIES = 20
def __init__(self, sfs_path, csv_file):
os.chdir(sfs_path)
self.ud = Factory(csv_file)
self.sfs = subprocess.Popen(shsplit("java -jar sfs.jar"))
os.chdir("..")
tries = 0
success = False
while tries < self.MAX_TRIES:
try:
xdt = subprocess.run(
shsplit(
'xdotool search --onlyvisible --name "{}"'.format(
"Shop Floor Simulator"
)
),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if xdt.stdout is None:
tries += 1
else:
win = int(xdt.stdout)
# print(win)
xdt = subprocess.run(
shsplit("xdotool windowmove {} 1500 300".format(win)),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
success = True
break
except Exception:
tries += 1
# print(e)
if not success:
print("Could not reposition SFS window!")
else:
print("SFS window repositioned!")
self.mb_client = ModbusTcpClient("127.0.0.1", 5502)
if self.mb_client.connect():
print("Connected to server!")
else:
print("Could not connect to server!")
def shutdown(self):
self.mb_client.close()
print("Client shutting down!")
self.sfs.terminate()
print("SFS shutting down!")
def read_discrete_inputs(self, start_address, debug=False):
res = self.mb_client.read_discrete_inputs(start_address)
if debug:
print(
'Read discrete input: "{}" at address {}!'.format(
res.bits[0], start_address
)
)
return res.bits[0]