Advertisement
Gazer29

#01 EngineSingle

Apr 11th, 2021 (edited)
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require("component")
  2. local internet = require("internet")
  3. local term = require ("term")
  4. local json = require("json")
  5. local serial = require("serialization")
  6. local fs = require("filesystem")
  7. local AllEntities = component.world_link.getLoadedEntities()
  8. local entity = component.entity_link.getAPI("immersiverailroading:locomotivediesel")
  9. local CONFIG_FILE = "/home/EngineCMDs.dat"
  10. local DEFAULT_CONFIG = {}
  11. local TcpIP = "84.65.32.30"
  12. local TcpSendPort = 6667
  13. local TcpRecvPort = 6667
  14. local wait = 0.1
  15.  
  16. local modem = component.modem
  17. local stocktypes = {
  18.     "immersiverailroading:locomotivediesel",
  19.     "immersiverailroading:locomotivesteam"
  20.     }
  21.  
  22. function overArray()
  23.     local b = {}
  24.     count = 0
  25.     if entity ~= nil then
  26.         rollingstock = entity
  27.         local posit_array = {}
  28.         local a = {}
  29.         uuid = rollingstock.getUUID()
  30.         position = rollingstock.getLocation()
  31.         xpos = position.getX()
  32.         ypos = position.getY()
  33.         zpos = position.getZ()
  34.         speed = rollingstock.getCurrentSpeed()
  35.         if locotable[uuid] == nil then
  36.             setspeed = 0
  37.             method = "Manual"
  38.         else
  39.             setspeed = locotable[uuid].setspeed
  40.             method = locotable[uuid].method
  41.         end
  42.         tag = "" -- add later
  43.         c = {["x"] = xpos, ["y"] = ypos, ["z"] = zpos}
  44.         a = {}
  45.         a["uuid"] = uuid
  46.         a["speed"] = speed
  47.         a["setspeed"] = setspeed
  48.         a["method"] = method
  49.         a["posit_array"] = c
  50.         a["name"] = rollingstock.getName()
  51.         a["tag"] = "" --rollingstock.getTag()
  52.         table.insert(b,1,a)
  53.         end
  54.     data = json:encode(b)
  55.     if data ~= "" then
  56.         con:write("#01,"..data.."\r\n")
  57.         con:flush()
  58.         end
  59. end
  60.  
  61. function recvtcp()
  62.     data = nil
  63.     local decoded = nil
  64.     local b = {}
  65.     if (con) then
  66.         index = con:read(10)
  67.         length = tonumber(index)
  68.         data = con:read(length)
  69.         if data ~= nil then
  70.             decoded = json:decode(data)
  71.             a = {}
  72.             for i, v in pairs(decoded) do
  73.                 uuid = v.uuid
  74.                 v.uuid = nil
  75.                 a[uuid] = v
  76.                 end
  77.             end
  78.         end
  79.     if a == {} then
  80.         a = nil
  81.         end
  82.     return a
  83. end
  84.  
  85. function parseResponse(data)
  86.     for uuid, d in pairs(data) do
  87.         locotable[uuid] = d
  88.         end
  89. end
  90.  
  91. function changeloco(data)
  92.     for i, loco in pairs(data) do
  93.         tag = loco.tag
  94.         method = loco.method
  95.         horn = loco.horn
  96.         setspeed = loco.setspeed
  97.         uuid = loco.uuid
  98.         a = AllEntities.whereProperty("uuid", uuid).asList()
  99.         b = a[1]
  100.         c = b.getAPI("immersiverailroading:locomotive")
  101.         c.setTag(loco.tag)
  102.         print(uuid, tag, method, horn, setspeed)
  103.         end
  104.     end
  105.  
  106. function saveConfig(file, cfg)
  107.   local f = io.open(file, "w")
  108.   if f == nil then error("Couldn't open " .. file .. " to write config.") end
  109.   f:write(serial.serialize(cfg, 100000))
  110.   f:close()
  111.   --print("Saved to file")
  112. end
  113.  
  114. function loadConfig()
  115.   local f = io.open(CONFIG_FILE, "r")
  116.   if f == nil then
  117.     print("Could not open " .. CONFIG_FILE .. ".")
  118.     print("Loading default config")
  119.     return DEFAULT_CONFIG
  120.     else
  121.     local config = serial.unserialize(f:read("*a"))
  122.     f:close()
  123.     return config
  124.     end
  125. end
  126.  
  127. local function broadcast(msg)
  128.     print("broadcasting table")
  129.   modem.broadcast(54421, serial.serialize({t = "STATUS",v = msg}))
  130. end
  131.  
  132. -- Main loop --
  133.  
  134. con = internet.open(TcpIP,TcpSendPort)
  135. modem.open(54421)  
  136. while true do
  137.     if (con) then
  138.         locotable = loadConfig()
  139.         overArray()
  140.         response = recvtcp()
  141.         end
  142.     if response ~= nil then
  143.         parseResponse(response)
  144.         saveConfig(CONFIG_FILE, locotable)
  145.         end
  146.     broadcast(locotable)
  147.     os.sleep(wait)
  148. end
  149. modem.close(54421)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement