From 5ba03341349215c5c11c322104afc82bad5acf3b Mon Sep 17 00:00:00 2001 From: Jan Date: Fri, 18 Jun 2021 15:57:45 +0000 Subject: [PATCH] Upload files to 'flow-gate-monitor' --- flow-gate-monitor/config.json | 4 ++ flow-gate-monitor/startup.lua | 36 ++++++++++++++++++ flow-gate-monitor/sus.lua | 72 +++++++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 flow-gate-monitor/config.json create mode 100644 flow-gate-monitor/startup.lua create mode 100644 flow-gate-monitor/sus.lua diff --git a/flow-gate-monitor/config.json b/flow-gate-monitor/config.json new file mode 100644 index 0000000..63d9ddc --- /dev/null +++ b/flow-gate-monitor/config.json @@ -0,0 +1,4 @@ +{ + "PC_ID": "example_pc", + "WH_URL": "https://discord.com/api/aaaaaaaaaa" +} diff --git a/flow-gate-monitor/startup.lua b/flow-gate-monitor/startup.lua new file mode 100644 index 0000000..684638f --- /dev/null +++ b/flow-gate-monitor/startup.lua @@ -0,0 +1,36 @@ +local file = fs.open("config.json", "r") +local config = textutils.unserializeJSON(file.readAll()) +file.close() + +local PC_ID = config["PC_ID"] +local WH_URL = config["WH_URL"] + +print("@============@") +print("| No touchy! |") +print("@============@") +print("") +print("ID: " .. PC_ID) + +function alert() +local success = false + while not success do + local res = http.post( + WH_URL .. '?wait=true', + '{"embeds": [ { "title": "Program exited", "author": { "name": "' .. PC_ID .. '" }, "color": 16711680 } ] }', + { ['Content-Type'] = 'application/json' }, + false + ) + if res ~= nil then + success = true + else + sleep(1) + end + end +end + +while true do + shell.run("sus") + print("Restarting.") + alert() + sleep(5) +end \ No newline at end of file diff --git a/flow-gate-monitor/sus.lua b/flow-gate-monitor/sus.lua new file mode 100644 index 0000000..af94aec --- /dev/null +++ b/flow-gate-monitor/sus.lua @@ -0,0 +1,72 @@ +local gate = peripheral.wrap("top") +local meth = peripheral.getMethods("top") + +-- Load config.json +local file = fs.open("config.json", "r") +local config = textutils.unserializeJSON(file.readAll()) +file.close() + +local PC_ID = config["PC_ID"] +local WH_URL = config["WH_URL"] + + +while true do + local low = gate.getSignalLowFlow() + local high = gate.getSignalHighFlow() + + if not fs.exists("data.json") then + local data = textutils.unserializeJSON('{ "low": '.. low ..', "high": '.. high ..' }') + local file = fs.open("data.json", "w") + file.write(textutils.serialiseJSON(data)) + file.close() + end + + local file = fs.open("data.json", "r") + local json = textutils.unserializeJSON(file.readAll()) + file.close() + + if json.low ~= low then + local success = false + while not success do + local res = http.post( + WH_URL .. '?wait=true', + '{"embeds": [ { "title": "Flow limit changed (low)", "author": { "name": "' .. PC_ID .. '" }, "description": "Flow rate (low): ' .. json.low .. ' RF/t -> ' .. low ..' RF/t", "color": 16747013 } ] }', + { ['Content-Type'] = 'application/json' }, + false + ) + if res ~= nil then + success = true + else + print("POST request failed; retrying") + sleep(1) + end + end + json.low = low + end + + if json.high ~= high then + local success = false + while not success do + local res = http.post( + WH_URL .. '?wait=true', + '{"embeds": [ { "title": "Flow limit changed (high)", "author": { "name": "' .. PC_ID .. '" }, "description": "Flow rate (high): ' .. json.high .. ' RF/t -> ' .. high ..' RF/t", "color": 16747013 } ] }', + { ['Content-Type'] = 'application/json' }, + false + ) + if res ~= nil then + success = true + else + print("POST request failed; retrying") + sleep(1) + end + end + json.high = high + end + + local file = fs.open("data.json", "w") + file.write(textutils.serialiseJSON(json)) + file.close() + + sleep(1) +end +