Last active
December 22, 2019 20:45
-
-
Save iComputerfreak/077d9c28e0c99f00453d0dc1a6fc8f07 to your computer and use it in GitHub Desktop.
A lua script for the OpenComputers mod that checks the charge of a GregTech Battery Buffer and switches a Redstone signal on/off when the buffer reaches certain charge percentages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- This script requires the BatteryBufferDriver mod to work: https://github.com/iComputerfreak/BatteryBufferDriver | |
--[[ | |
AN UPDATED VERSION OF THIS SCRIPT IS AVAILABLE HERE: https://github.com/iComputerfreak/OpenComputers-Programs/tree/master/GeneratorControl | |
]] | |
-- To display the charge values sent by this script, use the chargeDisplay.lua script | |
-- The script can be found here: https://gist.github.com/iComputerfreak/2821836ed3b29a3b1ed2c76c2570b16e | |
local sides = require("sides") | |
local component = require("component") | |
local event = require("event") | |
local m = component.modem | |
local inet = require("internet") | |
local filesystem = require("filesystem") | |
-- Read the Telegram Bot Token from the file "BOT_TOKEN" | |
local stream = io.open("/home/BOT_TOKEN") | |
local TOKEN = stream:read("*l") -- read the whole line | |
io.close(stream) | |
local TELEGRAM_URL = "https://api.telegram.org/bot" .. TOKEN .. "/sendMessage" | |
-- Which side the battery buffer if connected to (currently only top works) | |
local SIDE = sides.top | |
-- The strength of the modem signal (0 to disable, increase if the computer with the chargeDisplay.lua script doesn't retrieve the information) | |
local STRENGTH = 20 | |
-- Whether the script should send a pulse to turn on/off the generator instead of setting the signal directly to on/off | |
local USE_PULSE = false | |
-- If the percentage values should be checked per battery instead of per buffer | |
local CHECK_PER_BATTERY = false | |
-- The value at which the generator should turn on | |
local MIN_CHARGE_PERCENT = 10 | |
-- The value at which the generator should turn off | |
local MAX_CHARGE_PERCENT = 90 | |
local running = false | |
function timestamp() | |
return "[Time]" | |
end | |
function sendMessage(message, silent) | |
silent = silent or false | |
inet.request(TELEGRAM_URL, {chat_id = "<THE CHAT ID>", text = message, disable_notification = silent}) | |
end | |
function getCharge() | |
return component.battery_buffer.getCharge() | |
end | |
function getPercent(charge, capacity) | |
local percent = charge * 100 | |
percent = percent / capacity | |
return percent | |
end | |
function getChargeOfBattery(i) | |
return component.battery_buffer.getSingleCharge(i) | |
end | |
function getCapacity() | |
return component.battery_buffer.getCapacity() | |
end | |
function getCapacityOfBattery(i) | |
return component.battery_buffer.getSingleCapacity(i) | |
end | |
-- The number of batteries actually in the buffer | |
function getBatteryCount() | |
return component.battery_buffer.getBatteryCount() | |
end | |
function shouldTurnOn() | |
if running then | |
-- If the generator is already on, don't turn it on again | |
return false | |
end | |
if CHECK_PER_BATTERY then | |
-- Check if any battery is under the minimum | |
for i = 0,(getBatteryCount() - 1) do | |
local percent = getPercent(getChargeOfBattery(i), getCapacityOfBattery(i)) | |
if percent <= MIN_CHARGE_PERCENT then | |
-- If one battery is too low, return true | |
return true | |
end | |
end | |
else | |
local percent = getPercent(getCharge(), getCapacity()) | |
return percent <= MIN_CHARGE_PERCENT | |
end | |
return false | |
end | |
function shouldTurnOff() | |
if running == false then | |
-- If the generator is already off, don't turn it off again | |
return false | |
end | |
if CHECK_PER_BATTERY then | |
-- Check if each battery is already over the maximum | |
for i = 0,(getBatteryCount() - 1) do | |
local percent = getPercent(getChargeOfBattery(i), getCapacityOfBattery(i)) | |
if percent < MAX_CHARGE_PERCENT then | |
-- If one battery is too low, return false | |
return false | |
end | |
end | |
else | |
local percent = getPercent(getCharge(), getCapacity()) | |
return percent >= MAX_CHARGE_PERCENT | |
end | |
return true | |
end | |
function isRunningDry() | |
return getPercent(getCharge(), getCapacity()) < 2 | |
end | |
function sendCharge() | |
m.broadcast(123, getCharge(), getCapacity(), running, false) | |
end | |
function turnOn() | |
component.redstone.setOutput(sides.top, 15) | |
if USE_PULSE then | |
component.redstone.setOutput(sides.top, 0) | |
end | |
running = true | |
end | |
function turnOff() | |
if USE_PULSE then | |
component.redstone.setOutput(sides.top, 15) | |
end | |
component.redstone.setOutput(sides.top, 0) | |
running = false | |
end | |
require("term").clear() | |
print("Starting Charge Control v1.0...") | |
-- In case the generator is already running when the PC boots, turn it off, so everything is synced | |
turnOff() | |
m.setStrength(STRENGTH) | |
while true do | |
if isRunningDry() then | |
turnOff() | |
break | |
end | |
if shouldTurnOn() then | |
turnOn() | |
print(timestamp() .. "Turning generator on...") | |
sendMessage("Generator turned on", true) | |
end | |
if shouldTurnOff() then | |
turnOff() | |
print(timestamp() .. "Turning generator off...") | |
sendMessage("Generator turned off", true) | |
end | |
sendCharge() | |
end | |
print("Battery Buffer has run dry!") | |
sendMessage("The buffer has run dry! Generator turned off.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment