computercraft-scripts/flow-gate-monitor/sus.lua
2021-06-18 15:57:45 +00:00

73 lines
2 KiB
Lua

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