Advertisement
Hiranus

NetColorMaster

Jun 17th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.76 KB | None | 0 0
  1. os.loadAPI("JsonSaveLoad");
  2. os.loadAPI("Receive");
  3. os.loadAPI("Limits");
  4. os.loadAPI("AEIntegration");
  5.  
  6. local SettingsFile = "Settings.json";
  7. local Settings = {};
  8. local SlaveConfiguration = {};
  9. local AEnetwork = nil;
  10.  
  11.  
  12.  
  13.  
  14. local function ReceiveLoop()
  15.  
  16.   Receive.ReceiveLoop(Settings.ReceiveProtocol);
  17. end
  18.  
  19.  
  20.  
  21. local function SendToSlave(slaveId,conf,limit,networkItemCount)
  22.   local signalToSend = nil;
  23.   if (limit.Mode == "GreaterThan" and networkItemCount > limit.Count) or (limit.Mode == "LesserThan" and networkItemCount < limit.Count) then
  24.     signalToSend = limit.Signal;
  25.   else
  26.     signalToSend = not limit.Signal;
  27.   end
  28.   if conf.CurrentState ~= signalToSend then
  29.     conf.CurrentState = signalToSend;
  30.     print("Sending signal " .. tostring(conf.CurrentState) .. " to "..slaveId)
  31.     rednet.send(slaveId,{Task="UpdateOutputState",Data= {NewState= signalToSend,Fingerprint = conf.ItemDetail.Fingerprint}},Settings.SendProtocol);
  32.   end
  33. end
  34.  
  35.  
  36.  
  37. local function ProcessCommunication()
  38.   local recievedTasks = Receive.GetReceivedData();
  39.   for i=1,#recievedTasks do
  40.     local recievedTask = recievedTasks[i];
  41.     local message = recievedTask.Message;
  42.     local sender = recievedTask.Sender;
  43.     if message.Task == "SendingConfiguration" then
  44.       SlaveConfiguration[sender] = message.Data;
  45.       print("Recieved broadcasted configuration from "..sender);
  46.     end
  47.   end
  48. end
  49.  
  50. local function ExecuteMasterData()
  51.   Limits.RefreshLimits()
  52.   print("Scanning for updates")
  53.   for slaveId,confs in pairs(SlaveConfiguration) do
  54.     for i=1,#confs do
  55.       local conf = confs[i];
  56.       local conffingerprint = conf.ItemDetail.Fingerprint;
  57.       local limit = Limits.GetLimitForItem(conffingerprint);
  58.       if limit then
  59.         local networkItemCount = 0;
  60.         local details = AEnetwork.GetItemDetails(conffingerprint);
  61.         if details then
  62.           networkItemCount = details.qty;
  63.         end
  64.         SendToSlave(slaveId,conf,limit,networkItemCount);
  65.       end
  66.     end
  67.   end
  68. end
  69.  
  70.  
  71. local function MainLoop()
  72.   while true do
  73.     ProcessCommunication()
  74.     ExecuteMasterData()
  75.     local timeout = 20+math.random(0,20);
  76.     local timerId = os.startTimer(timeout);
  77.     repeat
  78.       local type,sender,data,protocol = os.pullEvent()
  79.     until type == "timer" or (type == "rednet_message" and protocol == Settings.ReceiveProtocol)
  80.     os.cancelTimer(timerId);
  81.   end
  82. end
  83.  
  84. --Configuration = JsonSaveLoad.Load(ConfigurationFile);
  85. print("Loading settings file: "..SettingsFile);
  86. Settings = JsonSaveLoad.Load(SettingsFile);
  87. AEnetwork = AEIntegration.InitalizeAEIntegration(Settings.AEPeripheral);
  88. rednet.open(Settings.ModemDirection);
  89. rednet.broadcast({Task = "RequestConfiguration"},Settings.SendProtocol);
  90. parallel.waitForAll(ReceiveLoop,MainLoop);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement