Skip to content

Commit e0b180e

Browse files
committed
WIP
1 parent 3a8e242 commit e0b180e

File tree

6 files changed

+200
-0
lines changed

6 files changed

+200
-0
lines changed

LineUs.py

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import socket
2+
import shlex
3+
from zeroconf import ServiceBrowser, Zeroconf
4+
5+
6+
class LineUs:
7+
"""An example class to show how to use the Line-us API"""
8+
9+
def __init__(self):
10+
self.__line_us = None
11+
self.__connected = False
12+
self.__hello_message = ''
13+
self.on_found_line_us_callback = None
14+
self.zeroconf = Zeroconf()
15+
self.listener = LineUsListener()
16+
self.browser = ServiceBrowser(self.zeroconf, "_lineus._tcp.local.", self.listener)
17+
self.line_us_name = ''
18+
self.info = {}
19+
20+
def connect(self, line_us_name):
21+
self.__line_us = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
22+
try:
23+
self.__line_us.connect((line_us_name, 1337))
24+
except:
25+
return False
26+
self.__connected = True
27+
self.line_us_name = line_us_name
28+
self.__hello_message = self.__read_response()
29+
return True
30+
31+
def connected(self):
32+
return self.__connected
33+
34+
def get_name(self):
35+
return self.line_us_name
36+
37+
def get_info(self):
38+
info = {}
39+
raw_info = self.send_gcode('M122', '')
40+
fields = shlex.split(raw_info.decode('utf-8'))
41+
if fields.pop(0) != 'ok':
42+
return None
43+
else:
44+
for field in fields:
45+
if field.split(':')[0] == 'mac':
46+
info['mac'] = field[5:]
47+
else:
48+
item = field.split(':')
49+
info[item[0]] = item[1]
50+
return info
51+
52+
def get_hello_string(self):
53+
if self.__connected:
54+
return self.__hello_message.encode('ascii')
55+
else:
56+
return 'Not connected'
57+
58+
def disconnect(self):
59+
"""Close the connection to the Line-us"""
60+
self.__line_us.close()
61+
self.__connected = False
62+
63+
def g01(self, x, y, z):
64+
"""Send a G01 (interpolated move), and wait for the response before returning"""
65+
cmd = b'G01 X'
66+
cmd += str(x).encode()
67+
cmd += b' Y'
68+
cmd += str(y).encode()
69+
cmd += b' Z'
70+
cmd += str(z).encode()
71+
self.__send_command(cmd)
72+
self.__read_response()
73+
74+
def send_gcode(self, gcode, parameters):
75+
cmd = gcode.encode()
76+
cmd += b' '
77+
cmd += parameters.encode()
78+
self.__send_command(cmd)
79+
return self.__read_response()
80+
81+
def __read_response(self):
82+
"""Read from the socket one byte at a time until we get a null"""
83+
line = b''
84+
while True:
85+
char = self.__line_us.recv(1)
86+
if char != b'\x00':
87+
line += char
88+
elif char == b'\x00':
89+
break
90+
return line
91+
92+
def __send_command(self, command):
93+
"""Send the command to Line-us"""
94+
command += b'\x00'
95+
self.__line_us.send(command)
96+
97+
def on_found_line_us(self, callback):
98+
self.listener.on_found_line_us(callback)
99+
100+
101+
class LineUsListener:
102+
103+
def __init__(self):
104+
self.on_found_line_us_callback = None
105+
self.line_us_list = []
106+
107+
@staticmethod
108+
def remove_service(_self, _zeroconf, _service_type, name):
109+
print("Service %s removed" % (name,))
110+
111+
def add_service(self, zeroconf, service_type, name):
112+
info = zeroconf.get_service_info(service_type, name)
113+
line_us_name = info.server.split('.')[0]
114+
line_us = (line_us_name, info.server, socket.inet_ntoa(info.address), info.port)
115+
# print(f'Found Line-us: {line_us[0]} at: {line_us[2]} port {line_us[3]}')
116+
self.line_us_list.append(line_us)
117+
if self.on_found_line_us_callback is not None:
118+
self.on_found_line_us_callback(line_us)
119+
120+
def on_found_line_us(self, callback):
121+
self.on_found_line_us_callback = callback
122+
123+
def get_first_line_us(self):
124+
if len(self.line_us_list) > 0:
125+
return self.line_us_list[0]
126+
else:
127+
return None
128+
129+
def get_line_us(self, number):
130+
return self.line_us_list[number]
131+
132+
def get_line_us_list(self):
133+
return
134+
135+
136+
if __name__ == '__main__':
137+
def callback_func(line_us):
138+
print(f'callback: {line_us[0]}')
139+
140+
141+
my_line_us = LineUs()
142+
my_line_us.on_found_line_us(callback_func)
143+
while True:
144+
pass

LineUsPythonModule/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LineUsPythonModule

Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
zeroconf = "*"
8+
9+
[dev-packages]
10+
11+
[requires]
12+
python_version = "3.7"

Pipfile.lock

Lines changed: 42 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__init__.py

Whitespace-only changes.

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from lineusapi.LineUs import LineUsAPI

0 commit comments

Comments
 (0)