-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathHelloWorldWebsockets.py
75 lines (55 loc) · 1.9 KB
/
HelloWorldWebsockets.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
import asyncio
import websockets
import time
class LineUs:
"""An example class to show how to use the Line-us WebSockets API"""
def __init__(self, line_us_name):
self.uri = f'ws://{line_us_name}'
self.__line_us = None
self.__hello_message = None
self.__loop = asyncio.get_event_loop()
def __del__(self):
self.__loop.close()
async def async_connect(self):
self.__line_us = await websockets.connect(self.uri)
self.__hello_message = await self.__line_us.recv()
def connect(self):
self.__loop.run_until_complete(self.async_connect())
def get_hello_string(self):
return self.__hello_message
async def async_disconnect(self):
await self.__line_us.close()
def disconnect(self):
self.__loop.run_until_complete(self.async_disconnect())
async def async_send(self, message):
await self.__line_us.send(message)
reply = await self.__line_us.recv()
return reply
def send(self, message):
reply = self.__loop.run_until_complete(self.async_send(message))
return reply
def g01(self, x, y, z):
command = f'G01 X{x} Y{y} Z{z}'
return self.send(command)
if __name__ == '__main__':
my_line_us = LineUs('line-us.local')
my_line_us.connect()
print(my_line_us.get_hello_string())
time.sleep(1)
my_line_us.g01(900, 300, 0)
my_line_us.g01(900, -300, 0)
my_line_us.g01(900, -300, 1000)
my_line_us.g01(1200, 300, 0)
my_line_us.g01(1200, -300, 0)
my_line_us.g01(1200, -300, 1000)
my_line_us.g01(900, 0, 0)
my_line_us.g01(1200, 0, 0)
my_line_us.g01(1200, 0, 1000)
my_line_us.g01(1500, 150, 0)
my_line_us.g01(1500, -300, 0)
my_line_us.g01(1500, -300, 1000)
my_line_us.g01(1500, 250, 0)
my_line_us.g01(1500, 300, 0)
my_line_us.g01(1500, 300, 1000)
time.sleep(1)
my_line_us.disconnect()