Leon3226

beeBreederExperimental

May 19th, 2022 (edited)
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.99 KB | None | 0 0
  1.  
  2. local configFileName = "config.json";
  3. local json = require("json");
  4.  
  5. function loadConfig()
  6.   local file = io.open(configFileName);
  7.   if (file) then
  8.       local content = file:read("*all");
  9.       file:close();
  10.       print(content);
  11.       return json.decode(content);
  12.   end
  13. end
  14.  
  15. local configuration = loadConfig();
  16. local internet = require('internet');
  17. local handle;
  18. local component = require('component');
  19.  
  20. local reconnectIntervals = configuration.reconnectIntervalsSeconds;
  21. local hostIp = configuration.hostIp;
  22. local hostPort = configuration.hostPort;
  23. local debugging = true;
  24.  
  25. function pong()
  26.     return 'pong';
  27. end
  28.  
  29. function printMessage(string)
  30.     return print(string);
  31. end
  32.  
  33. function saveConfig()
  34.     local file = io.open(configFileName, 'w');
  35.     if (file) then
  36.         print(json.encode(configuration));
  37.         file:write(json.encode(configuration))
  38.         file:close();
  39.     end
  40. end
  41.  
  42. function connectToHost()
  43.     print('Connecting to host...');
  44.     handle = internet.open(hostIp, hostPort); -- use config
  45. end
  46.  
  47. function allTransposers()
  48.     return component.list('transposer');
  49. end
  50.  
  51. function identifier()
  52.     return configuration["apiaryIdentifier"];
  53. end
  54.  
  55. function setIdentifier(identifier)
  56.     configuration["apiaryIdentifier"] = identifier;
  57.     saveConfig();
  58. end
  59.  
  60. function split(inputstr, sep)
  61.     if sep == nil then
  62.         sep = "%s"
  63.     end
  64.     local t = {}
  65.     for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
  66.         table.insert(t, str)
  67.     end
  68.     return t
  69. end
  70.  
  71. function transposerInventories(transposerAdress)
  72.     if (transposerAdress == nil) then
  73.         return ''
  74.     end
  75.     local trnspsr = component.proxy(component.get(transposerAdress))
  76.     local inventories = {}
  77.     for side = 0, 5 do
  78.         local size = trnspsr.getInventorySize(side);
  79.         local name = trnspsr.getInventoryName(side);
  80.         inventories[tostring(side)] = {
  81.             size = size,
  82.             name = name
  83.         }
  84.     end
  85.     return inventories;
  86. end
  87.  
  88. function getItems(transposerAdress, side)
  89.     if (transposerAdress == nil or side == nil or tonumber(side) == nil or tonumber(side) < 0 or tonumber(side) > 5) then
  90.         return ''
  91.     end
  92.     local trnspsr = component.proxy(component.get(transposerAdress))
  93.     if (trnspsr == nil) then
  94.         return ''
  95.     end
  96.     local items = {}
  97.     local stacks = trnspsr.getAllStacks(tonumber(side));
  98.     if (stacks == nil) then
  99.         return ''
  100.     end
  101.  
  102.     local place = 1;
  103.     for item in stacks do
  104.         items[place] = item;
  105.         place = place + 1;
  106.     end
  107.  
  108.     return items;
  109. end
  110.  
  111. function moveItem(transposerAdress, firstSide, firstSlot, secondSide, secondSlot, count)
  112.     -- TODO: add cache... maybe
  113.     local fSide = tonumber(firstSide);
  114.     local sSide = tonumber(secondSide);
  115.     local fSlot = tonumber(firstSlot);
  116.     local sSlot = tonumber(secondSlot);
  117.     count = tonumber(count);
  118.  
  119.     if (transposerAdress == nil or fSide == nil or fSide < 0 or fSide > 5 or sSide == nil or sSide < 0 or sSide > 5 or
  120.         count == nil) then
  121.         return ''
  122.     end
  123.  
  124.     local transposer = component.proxy(component.get(transposerAdress))
  125.  
  126.     if (transposer == nil) then
  127.         return ''
  128.     end
  129.  
  130.     return transposer.transferItem(fSide, sSide, count, fSlot, sSlot);
  131.  
  132. end
  133.  
  134. function isAnalyzedBee(item)
  135.     return item.individual ~= nil and item.individual.type == 'bee' and item.individual.isAnalyzed;
  136. end
  137.  
  138. function debugPrint(debugString)
  139.     if (debugging) then
  140.         print(debugString);
  141.     end
  142. end
  143.  
  144. function debugWrite(debugString)
  145.     if (debugging) then
  146.         io.write(debugString);
  147.     end
  148. end
  149.  
  150. local choiseTable = {
  151.     ['transposers'] = allTransposers,
  152.     ['inventories'] = transposerInventories,
  153.     ['items'] = getItems,
  154.     ['move'] = moveItem,
  155.     ['identifier'] = identifier,
  156.     ['ping'] = pong,
  157.     ['setIdentifier'] = setIdentifier,
  158.     ['print'] = printMessage
  159. }
  160.  
  161. saveConfig();
  162. connectToHost();
  163.  
  164. while (1) do
  165.     local data = handle:read();
  166.     if (data == nil) then
  167.         debugPrint('A connection error occured. Trying to reconnect in ' .. reconnectIntervals .. ' seconds')
  168.         os.sleep(reconnectIntervals);
  169.         connectToHost();
  170.     else
  171.         print(data);
  172.         if (data == 'exit') then
  173.             handle:close()
  174.             break
  175.         end
  176.  
  177.         local separated = split(data);
  178.         local comand = separated[1];
  179.         local args = separated;
  180.         table.remove(args, 1);
  181.  
  182.         local response = 'No function found';
  183.         local func = choiseTable[comand];
  184.         if (func ~= nil) then
  185.             print('   selected ' .. comand);
  186.             local passed, result = pcall(func, table.unpack(args));
  187.             if (not passed) then
  188.                 response = 'error: ' .. result;
  189.                 print('An error occured!');
  190.                 print(result)
  191.             else
  192.                 response = json.encode(result)
  193.             end
  194.         end
  195.         handle:write(response);
  196.     end
  197. end
  198.  
Add Comment
Please, Sign In to add comment