Upload files to 'flow-gate-monitor'

This commit is contained in:
Jan 2021-06-18 15:57:45 +00:00
parent ffbaeb3a71
commit 5ba0334134
3 changed files with 112 additions and 0 deletions

View file

@ -0,0 +1,4 @@
{
"PC_ID": "example_pc",
"WH_URL": "https://discord.com/api/aaaaaaaaaa"
}

View file

@ -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

72
flow-gate-monitor/sus.lua Normal file
View file

@ -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