Guest User

GPS/Relay Hybrid Routines - Global Builders Project

a guest
Sep 19th, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.34 KB | None | 0 0
  1. -- GPS/Relay Hybrid Computer Routines
  2.  
  3. -- Edit the below values to reflect the world coordinates
  4. -- of the GPS computer. Format: x, y, z
  5.  
  6. local deviceLoc = {-203, 134, 136}
  7.  
  8. local centralPCChannel = 1
  9.  
  10. -- edit msgsRetained value to adjust the number of messages kept for
  11. -- each registered device to compare for duplicates
  12.  
  13. local msgsRetained = 10
  14.  
  15. local eventTable = {}
  16. local cID = os.getComputerID ()
  17. local cLabel = os.getComputerID ()
  18. local gpsChan = gps.CHANNEL_GPS
  19. local commTable = { {1, {"000-00.000" }}, {2, {"000-00.000"}}, {gpsChan, {"0000-0000"}} }
  20. local msgContainer = {}
  21. local m = nil
  22.  
  23.  
  24.  
  25. function initDevice ()
  26.  
  27.     local periLoc = redstone.getSides()
  28.     local periStr, wModem = "", ""
  29.  
  30.     term.clear ()
  31.     term.setCursorPos (1, 1)
  32.  
  33.     for i = 1, 6 do
  34.         periStr = peripheral.getType (periLoc[i])
  35.         if periStr == "modem" then
  36.             if m == nil then
  37.                 m = peripheral.wrap (periLoc[i])
  38.                 wModem = periLoc[i]
  39.                 print ('Wireless modem connected on '..periLoc[i]..' side.')
  40.             else
  41.                 wModem = periLoc[i]
  42.                 print ('Rednet modem connected on '..periLoc[i]..' side.')
  43.             end
  44.         end
  45.     end
  46.  
  47.     local tempStr = "GPS_Relay_"..tostring(cID)
  48.     if (cLabel == "") or (cLabel ~= tempStr) then
  49.         os.setComputerLabel (tempStr)
  50.         cLabel = tempStr
  51.     end
  52.  
  53.     print ('Device label : '..cLabel..' on channel '..tostring(cID))
  54.     print ('Pausing for network startup.')
  55.     sleep (1)
  56.     msgContainer = {0, 3, cLabel, cID, cID, reqMessageID () }
  57.     m.transmit (centralPCChannel, cID, textutils.serialize (msgContainer))
  58. end
  59.  
  60.  
  61. function reqMessageID ()
  62.  
  63.     local IDStr = tostring( os.day() )..'-'..tostring( os.time () )
  64.     return IDStr
  65. end
  66.  
  67.  
  68. function isDupe (checkMsg)
  69.  
  70. -- To DO:
  71. -- review and correct table format for stored channels
  72. -- review and correct list format for msg IDs
  73. -- NOTE: should be no duplicate checking on GPS requests
  74.    
  75.     local chChan = checkMsg[4]
  76.    
  77.     for i = 1, #commTable do
  78.         if commTable[i][1] == chChan then
  79.             if #commTable[i][2] == 1 then
  80.                 commTable[i][2] = { checkMsg[6] }
  81.                 return false
  82.             else
  83.                 local chkInTab = false
  84.                 for ii = 1, #commTable[i][2] do
  85.                     if commTable[i][2][ii] == checkMsg[6] then
  86.                         chkInTab = true
  87.                     end
  88.                 end
  89.                 if not chkInTab then
  90.                     table.insert (commTable[i], checkMsg[6])
  91.                     if #commTable[i] > msgsRetained then
  92.                         table.remove (commTable[i], 1)
  93.                         return false
  94.                     end
  95.                 else
  96.                     return true
  97.                 end
  98.             end
  99.              
  100.         end
  101.     end
  102. end
  103.  
  104.  
  105. function openChannels ()
  106.  
  107.     m.closeAll ()
  108.     if not commTable == {} then
  109.         for i = 1, #commTable do
  110.             m.open (commTable[i][1])
  111.         end
  112.     end
  113. end
  114.  
  115.  
  116. function main ()
  117.  
  118.     initDevice ()
  119.     openChannels ()
  120.  
  121.     while true do
  122.         print ('Listening for message...')
  123.         eventTable = { os.pullEvent () }
  124.    
  125.         if eventTable[1] == "rednet_message" then
  126.             print ('rednet_message received.')
  127.        
  128.         elseif eventTable[1] == "modem_message" then
  129.             if (eventTable[3] == 65534) and
  130.                 (eventTable[5] == 'PING') then
  131.                 print ('GPS request received from channel '..tostring(eventTable[4]))
  132.                      
  133.                 m.transmit (eventTable[4], 65534, textutils.serialize (deviceLoc))            
  134.  
  135.             else
  136.                 -- check message table contents for type 4 (registered channel data)
  137.                 -- check against comm log for duplicates
  138.                 -- relay message
  139.                 msgContainer = textutils.unserialize (eventTable[5])
  140.                 print ('Received message ID '..msgContainer[6].." from channel "..tostring (msgContainer[5])..'.')
  141.                 if msgContainer[5] == cID then
  142.                     print ('Rejected bounced origin message.')
  143.                 elseif msgContainer[1] == 4 then
  144.                     print ('Received network channel list update.')
  145.                     local inTab = false
  146.                     for i = 1, #msgContainer[7] do
  147.                         if not (msgContainer[7][i] == cID) then
  148.                             for ii = 1, #commTable do
  149.                                 if commTable[ii][1] == msgContainer[7][i] then
  150.                                     inTab = true
  151.                                 end
  152.                             end
  153.                             if not inTab then
  154.                                 table.insert (commTable, { msgContainer[7][i], {"0000-0000"} } )
  155.                             end
  156.                             inTab = false
  157.                         end
  158.                     end
  159.                 elseif isDupe(msgContainer) then
  160.                     print ('Rejected duplicate message.')
  161.                 else
  162.                     m.transmit (eventTable[3], cID, eventTable[5])        
  163.                     print ('Relayed message.')        
  164.                 end
  165.             end    
  166.         else write ('.')
  167.         end
  168.         sleep (0.5)
  169.     end
  170. end
  171.  
  172. main ()
Advertisement
Add Comment
Please, Sign In to add comment