View difference between Paste ID: Jqp4KJDQ and g9gfxwsb
SHOW: | | - or go back to the newest paste.
1-
local lamp = peripheral.find "colorful_lamp"
1+
local config = dofile "config.lua"
2-
lamp.setLampColor(32767)
2+
3-
local sensor = peripheral.find "plethora:sensor"
3+
4-
local label = os.getComputerLabel()
4+
5-
local switch_config = dofile "config.lua"
5+
6
	local ws
7
8
	local function send_packet(msg)
9
		ws.send(textutils.serialiseJSON(msg))
10
	end
11
12
	local function send(data)
13-
		local ok, err = pcall(ws.send, textutils.serialiseJSON(msg))
13+
14-
		if not ok then printError(err) try_connect_loop() end
14+
15
16
	local function connect()
17
		if ws then ws.close() end
18
		ws, err = http.websocket "wss://spudnet.osmarks.net/v4?enc=json"
19
		if not ws then print("websocket failure %s", err) return false end
20
		ws.url = "wss://spudnet.osmarks.net/v4?enc=json"
21
22
		send_packet { type = "identify", implementation = "ARR control unit", key = settings.get "spudnet_key" }
23
		send_packet { type = "set_channels", channels = { "comm:arr" } }
24
25
		print("websocket connected")
26
27-
		send_packet { type = "identify", implementation = "ARR switching unit", key = settings.get "spudnet_key" }
27+
28
	end
29
	
30
	local function try_connect_loop()
31
		while not connect() do
32
			sleep(0.5)
33
		end
34
	end
35
	
36
	try_connect_loop()
37
38
	local function recv()
39
		while true do
40
			local e, u, x = os.pullEvent "websocket_message"
41
			if u == ws.url then return textutils.unserialiseJSON(x) end
42
		end
43
	end
44
	
45
	local ping_timeout_timer = nil
46
47
	local function ping_timer()
48
		while true do
49
			local _, t = os.pullEvent "timer"
50
			if t == ping_timeout_timer and ping_timeout_timer then
51
				-- 15 seconds since last ping, we probably got disconnected
52
				print "SPUDNET timed out, attempting reconnect"
53
				try_connect_loop()
54
			end
55
		end
56
	end
57
	
58
	local function main()
59
		while true do
60
			local packet = recv()
61
			if packet.type == "ping" then
62
				send_packet { type = "pong", seq = packet.seq }
63
				if ping_timeout_timer then os.cancelTimer(ping_timeout_timer) end
64
				ping_timeout_timer = os.startTimer(15)
65
			elseif packet.type == "error" then
66
				print("SPUDNET error", packet["for"], packet.error, packet.detail, textutils.serialise(packet))
67
			elseif packet.type == "message" then
68
				os.queueEvent("spudnet_message", packet.data)
69
			end
70
		end
71
	end
72
73
	return send, function() parallel.waitForAll(ping_timer, main) end
74
end
75
76
local spudnet_send, spudnet_handler = spudnet()
77
78
local switches = {}
79
local sweep_timer = os.startTimer(1)
80
local function main()
81
	while true do
82
		local ev = {os.pullEvent()}
83-
local directions = {
83+
		if ev[1] == "spudnet_message" then
84-
	["+ "] = "east",
84+
			local data = ev[2]
85-
	[" +"] = "south",
85+
			if data.type == "sw_ping" then
86-
	["- "] = "west",
86+
				data.timer = timer
87-
	[" -"] = "north"
87+
				data.last_ping = os.epoch "utc"
88-
}
88+
				switches[data.id] = data
89
				print(data.id)
90-
local function direction_name(vec)
90+
				spudnet_send { type = "sw_cmd", cmd = "set", lamp = (os.clock() % 16) * 2048 }
91-
	local function symbol(v)
91+
92-
		if math.abs(v) < 0.1 then return " "
92+
		elseif ev[1] == "timer" then
93-
		elseif v < 0 then return "-"
93+
			if ev[2] == sweep_timer then
94-
		else return "+" end
94+
				local now = os.epoch "utc"
95
				for id, switch in pairs(switches) do
96-
	return directions[symbol(vec.x) .. symbol(vec.z)]
96+
					if now - switch.last_ping >= 2000 then
97
						print(switch, "antimemeticized")
98
						switches[id] = nil
99
					end
100
				end
101-
		local entities = sensor.sense()
101+
				sweep_timer = os.startTimer(1)
102-
		for _, entity in pairs(entities) do
102+
103-
			entity.position = vector.new(entity.x, entity.y, entity.z) + switch_config.offset
103+
104-
			entity.velocity = vector.new(entity.motionX, entity.motionY, entity.motionZ)
104+
105
end
106-
		table.sort(entities, function(a, b) return a.position:length() < b.position:length() end)
106+
107-
		local carts = {}
107+
parallel.waitForAll(spudnet_handler, main)