-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_physical_plotter_test_server.rb
77 lines (67 loc) · 1.41 KB
/
_physical_plotter_test_server.rb
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
require "socket"
require "base64"
require "securerandom"
class Client
def initialize(socket)
@socket = socket
@uuid = SecureRandom.uuid
@connected = true
@authenticated = false
Thread.new { update }
end
def update
write(@uuid)
loop do
if !@authenticated
if (read == @uuid)
@authenticated = true
puts "Client-#{@uuid} Connected"
else
write("401")
end
else
parse(read)
end
sleep 0.1
end
end
def parse(string)
return if string.size == 0
puts "->#{string}"
case string.split(" ").first
when "download"
write("Received download")
when "home"
write("Moving to 0:0")
when "move"
axes = string.sub("move", "").strip.split(":")
write("moving to #{axes.first}:#{axes.last}")
when "pen_up"
write("Lifting pen")
when "pen_down"
write("Lowering pen")
when "status"
write("PEN: 1.0\nX: 0\nY: 0\nX_ENDSTOP: true\nY_ENDSTOP: false")
when "stop"
write("HALTING")
else
write("Unknown command")
end
end
def read
Base64.decode64(@socket.recv(2048))
end
def write(string)
@socket.puts(Base64.strict_encode64(string))
@socket.puts "\r\n"
end
def close
@socket.close
end
end
@server = TCPServer.new(8962)
puts "Server listening..."
loop do
Client.new(@server.accept)
end
@server.close if @server