Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GPS/Relay Hybrid Computer Routines
- -- Edit the below values to reflect the world coordinates
- -- of the GPS computer. Format: x, y, z
- local deviceLoc = {-203, 134, 136}
- local centralPCChannel = 1
- -- edit msgsRetained value to adjust the number of messages kept for
- -- each registered device to compare for duplicates
- local msgsRetained = 10
- local eventTable = {}
- local cID = os.getComputerID ()
- local cLabel = os.getComputerID ()
- local gpsChan = gps.CHANNEL_GPS
- local commTable = { {1, {"000-00.000" }}, {2, {"000-00.000"}}, {gpsChan, {"0000-0000"}} }
- local msgContainer = {}
- local m = nil
- function initDevice ()
- local periLoc = redstone.getSides()
- local periStr, wModem = "", ""
- term.clear ()
- term.setCursorPos (1, 1)
- for i = 1, 6 do
- periStr = peripheral.getType (periLoc[i])
- if periStr == "modem" then
- if m == nil then
- m = peripheral.wrap (periLoc[i])
- wModem = periLoc[i]
- print ('Wireless modem connected on '..periLoc[i]..' side.')
- else
- wModem = periLoc[i]
- print ('Rednet modem connected on '..periLoc[i]..' side.')
- end
- end
- end
- local tempStr = "GPS_Relay_"..tostring(cID)
- if (cLabel == "") or (cLabel ~= tempStr) then
- os.setComputerLabel (tempStr)
- cLabel = tempStr
- end
- print ('Device label : '..cLabel..' on channel '..tostring(cID))
- print ('Pausing for network startup.')
- sleep (1)
- msgContainer = {0, 3, cLabel, cID, cID, reqMessageID () }
- m.transmit (centralPCChannel, cID, textutils.serialize (msgContainer))
- end
- function reqMessageID ()
- local IDStr = tostring( os.day() )..'-'..tostring( os.time () )
- return IDStr
- end
- function isDupe (checkMsg)
- -- To DO:
- -- review and correct table format for stored channels
- -- review and correct list format for msg IDs
- -- NOTE: should be no duplicate checking on GPS requests
- local chChan = checkMsg[4]
- for i = 1, #commTable do
- if commTable[i][1] == chChan then
- if #commTable[i][2] == 1 then
- commTable[i][2] = { checkMsg[6] }
- return false
- else
- local chkInTab = false
- for ii = 1, #commTable[i][2] do
- if commTable[i][2][ii] == checkMsg[6] then
- chkInTab = true
- end
- end
- if not chkInTab then
- table.insert (commTable[i], checkMsg[6])
- if #commTable[i] > msgsRetained then
- table.remove (commTable[i], 1)
- return false
- end
- else
- return true
- end
- end
- end
- end
- end
- function openChannels ()
- m.closeAll ()
- if not commTable == {} then
- for i = 1, #commTable do
- m.open (commTable[i][1])
- end
- end
- end
- function main ()
- initDevice ()
- openChannels ()
- while true do
- print ('Listening for message...')
- eventTable = { os.pullEvent () }
- if eventTable[1] == "rednet_message" then
- print ('rednet_message received.')
- elseif eventTable[1] == "modem_message" then
- if (eventTable[3] == 65534) and
- (eventTable[5] == 'PING') then
- print ('GPS request received from channel '..tostring(eventTable[4]))
- m.transmit (eventTable[4], 65534, textutils.serialize (deviceLoc))
- else
- -- check message table contents for type 4 (registered channel data)
- -- check against comm log for duplicates
- -- relay message
- msgContainer = textutils.unserialize (eventTable[5])
- print ('Received message ID '..msgContainer[6].." from channel "..tostring (msgContainer[5])..'.')
- if msgContainer[5] == cID then
- print ('Rejected bounced origin message.')
- elseif msgContainer[1] == 4 then
- print ('Received network channel list update.')
- local inTab = false
- for i = 1, #msgContainer[7] do
- if not (msgContainer[7][i] == cID) then
- for ii = 1, #commTable do
- if commTable[ii][1] == msgContainer[7][i] then
- inTab = true
- end
- end
- if not inTab then
- table.insert (commTable, { msgContainer[7][i], {"0000-0000"} } )
- end
- inTab = false
- end
- end
- elseif isDupe(msgContainer) then
- print ('Rejected duplicate message.')
- else
- m.transmit (eventTable[3], cID, eventTable[5])
- print ('Relayed message.')
- end
- end
- else write ('.')
- end
- sleep (0.5)
- end
- end
- main ()
Advertisement
Add Comment
Please, Sign In to add comment