Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require("component")
- local internet = require("internet")
- local term = require ("term")
- local json = require("json")
- local serial = require("serialization")
- local fs = require("filesystem")
- local AllEntities = component.world_link.getLoadedEntities()
- --local entity = component.entity_link.getAPI("immersiverailroading:locomotivediesel")
- local CONFIG_FILE = "/home/EngineCMDs.dat"
- local DEFAULT_CONFIG = {}
- local TcpIP = "84.65.32.30"
- local TcpSendPort = 6667
- local TcpRecvPort = 6667
- local wait = 0.1
- local modem = component.modem
- local stocktypes = {
- "immersiverailroading:locomotivediesel",
- "immersiverailroading:locomotivesteam"
- }
- link_table = {}
- for i,v in pairs(component.list()) do
- if v == "entity_link" then
- table.insert(link_table, i)
- end
- end
- print("Entity_link's found: ", #link_table)
- function overArray()
- local b = {}
- count = 0
- for i, link in pairs(link_table) do
- local entity = component.proxy(link).getAPI("immersiverailroading:locomotivediesel")
- if entity ~= nil then
- rollingstock = entity
- local posit_array = {}
- local a = {}
- uuid = rollingstock.getUUID()
- position = rollingstock.getLocation()
- xpos = position.getX()
- ypos = position.getY()
- zpos = position.getZ()
- speed = rollingstock.getCurrentSpeed()
- if locotable[uuid] == nil then
- setspeed = 0
- method = "Manual"
- else
- setspeed = locotable[uuid].setspeed
- method = locotable[uuid].method
- end
- tag = "" -- add later
- c = {["x"] = xpos, ["y"] = ypos, ["z"] = zpos}
- a = {}
- a["uuid"] = uuid
- a["speed"] = speed
- a["setspeed"] = setspeed
- a["method"] = method
- a["posit_array"] = c
- a["name"] = rollingstock.getName()
- a["tag"] = "" --rollingstock.getTag()
- table.insert(b,1,a)
- end
- end
- data = json:encode(b)
- if data ~= "" then
- con:write("#01,"..data.."\r\n")
- con:flush()
- end
- end
- function recvtcp()
- data = nil
- local decoded = nil
- local b = {}
- if (con) then
- index = con:read(10)
- length = tonumber(index)
- data = con:read(length)
- if data ~= nil then
- decoded = json:decode(data)
- a = {}
- for i, v in pairs(decoded) do
- uuid = v.uuid
- v.uuid = nil
- a[uuid] = v
- end
- end
- end
- if a == {} then
- a = nil
- end
- return a
- end
- function parseResponse(data)
- for uuid, d in pairs(data) do
- locotable[uuid] = d
- end
- end
- function changeloco(data)
- for i, loco in pairs(data) do
- tag = loco.tag
- method = loco.method
- horn = loco.horn
- setspeed = loco.setspeed
- uuid = loco.uuid
- a = AllEntities.whereProperty("uuid", uuid).asList()
- b = a[1]
- c = b.getAPI("immersiverailroading:locomotive")
- c.setTag(loco.tag)
- print(uuid, tag, method, horn, setspeed)
- end
- end
- function saveConfig(file, cfg)
- local f = io.open(file, "w")
- if f == nil then error("Couldn't open " .. file .. " to write config.") end
- f:write(serial.serialize(cfg, 100000))
- f:close()
- --print("Saved to file")
- end
- function loadConfig()
- local f = io.open(CONFIG_FILE, "r")
- if f == nil then
- print("Could not open " .. CONFIG_FILE .. ".")
- print("Loading default config")
- return DEFAULT_CONFIG
- else
- local config = serial.unserialize(f:read("*a"))
- f:close()
- return config
- end
- end
- local function broadcast(msg)
- print("broadcasting table")
- modem.broadcast(54421, serial.serialize({t = "STATUS",v = msg}))
- end
- -- Main loop --
- con = internet.open(TcpIP,TcpSendPort)
- modem.open(54421)
- while true do
- if (con) then
- locotable = loadConfig()
- overArray()
- response = recvtcp()
- end
- if response ~= nil then
- parseResponse(response)
- saveConfig(CONFIG_FILE, locotable)
- end
- broadcast(locotable)
- os.sleep(wait)
- end
- modem.close(54421)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement