Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("ColorManagement");
- os.loadAPI("JsonSaveLoad");
- os.loadAPI("Receive");
- local ConfigurationFile = "Configuration.json";
- local SettingsFile = "Settings.json";
- local Configuration = {};
- local Settings = {};
- local ColorManager = nil;
- local function SendConfiguration(masterId)
- rednet.send(masterId,{Task = "SendingConfiguration",Data = Configuration},Settings.SendProtocol);
- end
- local function ReceiveLoop()
- Receive.ReceiveLoop(Settings.ReceiveProtocol);
- end
- local function FindMatchingConfigurations(fingerprint)
- local tmptab = {};
- for i=1,#Configuration do
- local configFingerprint = Configuration[i].ItemDetail.Fingerprint;
- if configFingerprint.id == fingerprint.id and configFingerprint.dmg == fingerprint.dmg and configFingerprint.nbt_hash == fingerprint.nbt_hash then
- tmptab[#tmptab + 1] = Configuration[i];
- end
- end
- return tmptab;
- end
- function ExecuteMasterData()
- while true do
- local recievedTasks = Receive.GetReceivedData();
- print("Processing requests");
- for i=1,#recievedTasks do
- local recievedTask = recievedTasks[i];
- local message = recievedTask.Message;
- local sender = recievedTask.Sender;
- if message.Task == "RequestConfiguration" then
- print("Sending configuration to "..sender);
- SendConfiguration(sender)
- elseif message.Task == "UpdateOutputState" then
- print("Updating output state");
- local mathcingConfigurations = FindMatchingConfigurations(message.Data.Fingerprint)
- for i=1,#mathcingConfigurations do
- local newState= message.Data.NewState;
- print("Recieved new state for color "..mathcingConfigurations[i].ColorId..": ".. tostring(newState));
- if mathcingConfigurations[i].InvertSignal then
- print("Inverting recieved signal, as requested by configuration");
- newState = not newState;
- end
- print("New state of color "..mathcingConfigurations[i].ColorId.." is ".. tostring(newState));
- if newState then
- ColorManager.AddColor(mathcingConfigurations[i].ColorId);
- else
- ColorManager.RemoveColor(mathcingConfigurations[i].ColorId);
- end
- end
- end
- end
- repeat
- local type,sender,data,protocol = os.pullEvent("rednet_message")
- until protocol == Settings.ReceiveProtocol
- end
- end
- Configuration = JsonSaveLoad.Load(ConfigurationFile);
- Settings = JsonSaveLoad.Load(SettingsFile);
- rednet.open(Settings.ModemDirection);
- rednet.broadcast({Task = "SendingConfiguration",Data = Configuration},Settings.SendProtocol);
- ColorManager = ColorManagement.InitalizeColors(Settings.BundledColorCable);
- parallel.waitForAll(ReceiveLoop,ExecuteMasterData);
Advertisement
Add Comment
Please, Sign In to add comment