Compare commits

...

9 Commits

Author SHA1 Message Date
1d8485fe84 reformatting 2023-11-05 17:05:43 +01:00
40eb131fce debugging 2023-11-04 16:39:57 +01:00
457a67af44 reply only to registered clients 2023-11-04 16:30:36 +01:00
ac918ad47e reply accordingly for unregistered client 2023-11-04 16:29:33 +01:00
b66b0c751a reset timer on success status 2023-11-04 16:24:18 +01:00
f3feb3e336 pong server response 2023-11-04 16:18:12 +01:00
b280a31dfd bugfixes 2023-11-04 16:14:06 +01:00
b1f92ea8b2 debugging 2023-11-04 16:11:34 +01:00
c8eb38671e client timeout ping 2023-11-04 16:05:54 +01:00
2 changed files with 63 additions and 19 deletions

View File

@ -55,7 +55,7 @@ function format_percent(options, number, mult)
percent = number * 100
end
return string.format('%.2f ', percent) .. '% ' .. string.rep(' ', #(options.unit or ''))
return string.format('%.2f ', percent) .. '% ' .. string.rep(' ', #(options.unit or ' '))
end
-- format temperature as number with two decimals and padded unit
@ -67,7 +67,7 @@ function format_temp(options, number)
else temperature = string.format('%.2f ', temperature) end
-- build string
return temperature .. 'K ' .. string.rep(' ', #(options.unit or ''))
return temperature .. 'K ' .. string.rep(' ', #(options.unit or ' '))
end
-- format liquids as compact number and padded unit
@ -260,14 +260,49 @@ function respond_print(options, name)
if #buffer > 0 then transmit_messages(options, buffer) end
end
function event_listener(options)
while true do
local _, _, senderChannel, _, message, _ = os.pullEvent('modem_message')
function reset_timer(id, time)
os.cancelTimer(id)
return os.startTimer(time)
end
function ping_status(options)
system.modem.transmit(system.channel, system.channel, 'ping')
print('Sent ping...')
local timer_id = os.startTimer(options.timeout/2)
while not system.timeout do
local event, _, senderChannel, _, _, _ = os.pullEvent()
-- respond to corresponding message only
if senderChannel == system.channel then
if event == 'modem_message' and senderChannel == system.channel then
print('Received response!')
os.cancelTimer(timer_id)
return
elseif event == 'timer' then
print('Reconnecting...')
system.timeout = true
return
end
end
end
function event_listener(options)
local timer_id = os.startTimer(options.timeout/2)
while not system.timeout do
local event, _, senderChannel, _, message, _ = os.pullEvent()
-- respond to corresponding message only
if event == 'modem_message' and senderChannel == system.channel then
-- send main display message
timer_id = reset_timer(timer_id, options.timeout/2)
respond_print(options, message)
elseif event == 'timer' then
-- send ping for server status
ping_status(options)
-- reset timer if response
if not system.timeout then
timer_id = reset_timer(timer_id, options.timeout/2)
end
end
end
end

View File

@ -67,18 +67,24 @@ function wait_for_components()
local component = ''
local success = false
-- reply to channel 1 (initializer) only
if event == 'modem_message' and senderChannel == 1 then
if event == 'modem_message' then
print(message)
-- component is trying to connect
if channel_used(replyChannel) then
system.modem.transmit(replyChannel, replyChannel, 'used')
else
-- save component
table.insert(system.channels, replyChannel)
table.insert(system.components, message)
if senderChannel == 1 then
if channel_used(replyChannel) then
system.modem.transmit(replyChannel, replyChannel, 'used')
else
-- save component
table.insert(system.channels, replyChannel)
table.insert(system.components, message)
component = message
count = count + 1
success = true
component = message
count = count + 1
success = true
end
-- component asking for status
elseif message == 'ping' and channel_used(senderChannel) then
system.modem.transmit(replyChannel, replyChannel, 'pong')
end
elseif event == 'monitor_touch' and count > 0 then break end
@ -130,15 +136,18 @@ function get_messages(buffer)
local touchExit = false
while true do
-- get next pull event
local event, _, senderChannel, _, message, _ = os.pullEvent()
local event, _, senderChannel, replyChannel, message, _ = os.pullEvent()
if event == 'modem_message' then
-- save message in buffer
if senderChannel == channel then
table.insert(buffer, message)
system.modem.transmit(channel, channel, 'ok')
break
-- dont accept messages from wrong channel
else system.modem.transmit(senderChannel, senderChannel, 'busy') end
-- invalid channel
elseif not channel_used(senderChannel) then
system.modem.transmit(replyChannel, replyChannel, 'unregistered')
-- keep busy otherwise
else system.modem.transmit(replyChannel, replyChannel, 'busy') end
elseif event == 'timer' then
system.timeout = true
return