Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- modified to work with draconic rf storage (trying to determine actual 'gain' from reactor)
- --Rev 20210701 wtp
- --Moved extraneous notes and documentation into https://pastebin.com/FcZRymGb
- --author: wparish@wparish.com
- -- rev 1.2 import borrowed functions
- -- ============================== --
- -- functions (borrowed from knPtJCjb)
- -- logging functions
- local function getTime()
- contentSite = http.get("http://worldtimeapi.org/api/timezone/GMT").readAll()
- if string.find(contentSite, "datetime") ~= nil then -- and string.find(contentSite, "localDate") == 3 then
- content = contentSite
- else
- content = "00:00:00.00000','utcDate"
- end
- timePos = string.find(content,"datetime")+22
- time = string.sub(content, timePos, timePos+13)
- -- if string.sub(content, timePos, timePos+1) == "PM" then
- -- timePM = tostring(tonumber(string.sub(time,1,2))+12)..string.sub(time,3)
- -- return timePM
- -- else
- return time
- -- I don't care if the time is 24H
- -- end
- end
- -- numeric functions
- local function round(num, idp)
- local mult = 10^(idp or 0)
- return math.floor(num * mult + 0.5) / multi
- end
- -- file functions
- -- write table to file 'path'
- local function fileWrite(path, text)
- local file = io.open(path, "w")
- file:write(text)
- file:close()
- end
- -- get table from 'path'
- local function fileGetTable(path)
- if fs.exists(path) then
- local file = io.open(path, "r")
- local lines = {}
- local i = 1
- local line = file:read("*l")
- while line ~= nil do
- lines[i] = line
- line = file:read("*l")
- i = i +1
- end
- file:close()
- return lines
- end
- return {}
- end
- -- update line in file 'path' (n is line number)
- local function fileReplaceLine(path, n, text)
- local lines = fileGetTable(path)
- lines[n] = text
- fileWriteFromTable(path, lines)
- end
- -- append line of text to path
- local function fileAppend(path, text)
- local file = io.open(path, "a")
- file:write(text.."\n")
- file:close()
- end
- -- get file length for path
- local function fileGetLength(path)
- local file = io.open(path, "r")
- local i = 0
- while file:read("*l") ~= nil do
- i = i +1
- end
- file:close()
- return I
- end
- -- return array of lines
- local function fileGetLines(path, startN, endN)
- local lines = fileGetTable(path)
- local linesOut = {}
- local x = 1
- for i = startN, endN, 1 do
- linesOut[x] = lines[i]
- x = x + 1
- end
- return linesOut
- end
- local function getLogs(path, xPos, yPos)
- local Logs = fileGetLines(path, fileGetLength(path)-5, fileGetLength(path))
- for i = 1, 6, 1 do
- mon.setCursorPos(xPos+2,yPos+1+i)
- mon.write(Logs[i])
- end
- end
- local function addLog(path, time, text)
- fileAppend(path, "["..time.."]")
- fileAppend(path, text)
- end
- -- end borrowed functions--
- -- ===================== --
- -- Each instance will have unique values for the flux gates. I'm going to store those as:
- -- Mym.li (values.cfg)
- -- local values = {
- -- "fluxgate".."flux_gate_308",
- -- "fluxgate_out".."flux_gate_309",
- -- slack API codes (to decrease odds I'll post those to pastebin again)
- -- }
- -- meh whatever just put the values line 1 = fluxgate, line 2 = fluxgate_out (values only), line 3 = slack API codes
- -- Dirtcraft fluxgate flux_gate_67 and fluxgate_out flux_gate_68
- -- local values = {}
- local storage = peripheral.find("draconic_rf_storage")
- local monitor = peripheral.wrap("bottom")
- local core = peripheral.wrap("right")
- --local fluxgate = peripheral.wrap("flux_gate_308") -- dump energy
- --local fluxgate_out = peripheral.wrap("flux_gate_309") -- from reactor
- local powerstate = 0
- local x=0
- local y=0
- local x1=0
- local msg="empty"
- local fieldStrengthPercent=0
- local lastFieldStrengthPercent
- local energySaturationPercent=0
- local lastEnergySaturationPercent=0 -- last energy saturation
- local ecrft=0 -- energy core rf per tick
- local adjtimeout=0
- local coretemp=0
- local history="50 field saturation gain/loss @ 2 sec intervals"
- -- initialize log
- if fs.exists("logs.txt") then
- else
- file = io.open("logs.txt", "w")
- file:write("")
- file:close()
- end
- local values=fileGetTable("values.cfg")
- addLog("logs.txt",getTime(),"Values")
- for i,value in pairs(values) do
- addLog("logs.txt",getTime(),i..": "..value)
- end
- -- print(textutils.serialix(values))
- local fluxgate = peripheral.wrap(values[1]) -- dump energy
- local fluxgate_out = peripheral.wrap(values[2]) -- from reactor
- local slack_api = peripheral.wrap(values[3]) -- authentication URL code
- term.redirect(monitor)
- term.setBackgroundColor(colors.gray)
- fluxgate.setOverrideEnabled(true) -- required for direct computer control of flux gate flow rate
- while true do
- local stab = core.getReactorInfo()
- y=x
- x=storage.getEnergyStored()
- powerstate=stab.status
- monitor.clear()
- monitor.setTextColor(colors.yellow)
- monitor.setCursorPos(32,1)
- print(getTime().." GMT")
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(15,1)
- print("status:")
- if powerstate=="online" then
- monitor.setTextColor(colors.green)
- else
- monitor.setTextColor(colors.red)
- end
- monitor.setCursorPos(23,1)
- print(powerstate)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(7,2)
- print("generationRate: "..stab.generationRate.." RF/t")
- monitor.setCursorPos(10,3)
- coretemp=stab.temperature
- monitor.setTextColor(colors.green)
- if (coretemp>7000) then
- monitor.setTextColor(colors.orange)
- end
- if (coretemp>8000) then
- monitor.setTextColor(colors.purple)
- end
- print("temperature: "..coretemp)
- monitor.setTextColor(colors.white)
- monitor.setCursorPos(8,4)
- print("fieldStrength: "..stab.fieldStrength)
- -- these two max values are static and don't really tell us anything
- --monitor.setCursorPos(5,5)
- --print("maxFieldStrength: "..stab.maxFieldStrength)
- monitor.setCursorPos(7,6)
- print("fieldDrainRate: "..stab.fieldDrainRate)
- monitor.setCursorPos(1,7)
- print("Fuel Conversion Rate: "..stab.fuelConversionRate.." nb/t")
- --monitor.setCursorPos(4,8)
- --print("maxFuelConversion: "..stab.maxFuelConversion)
- local fuelConversionPercent=stab.fuelConversion*100/stab.maxFuelConversion
- monitor.setCursorPos(7,9)
- print("fuelConversion: "..stab.fuelConversion.."("..math.floor(fuelConversionPercent).."%)")
- lastEnergySaturationPercent=energySaturationPercent
- energySaturationPercent=stab.energySaturation*100/stab.maxEnergySaturation
- monitor.setCursorPos(5,10)
- print("energySaturation: "..stab.energySaturation.."("..math.floor(energySaturationPercent).."%) "..tonumber(string.format("%.3f",energySaturationPercent-lastEnergySaturationPercent)))
- --if energySaturationPercent==50 then
- -- stab.setGenerationRate=stab.generationRate+10000
- -- sleep(5)
- -- end
- --monitor.setCursorPos(2,11)
- --print("maxEnergySaturation: "..stab.maxEnergySaturation)
- monitor.setCursorPos(8,12)
- print("fluxgate flow: "..fluxgate.getFlow())
- lastFieldStrengthPercent=fieldStrengthPercent
- fieldStrengthPercent=stab.fieldStrength*100/stab.maxFieldStrength
- monitor.setCursorPos(2,13)
- print("Cont. fieldStrength: "..tonumber(string.format("%.3f",(fieldStrengthPercent))).."%".." ("..fieldStrengthPercent-lastFieldStrengthPercent..")")
- monitor.setCursorPos(2,15)
- print("Energy Core Storage: "..tonumber(string.format("%.3f",x)))
- ecrft=((((x-y)/1000/1000)/2)/20) -- there are 20 ticks in a second so you /20 not multiple dufus
- monitor.setCursorPos(5,16)
- x1=ecrft
- if (x1>999999999) then
- -- leave x1 alone for millions
- msg="MRF/t"
- end
- if (x1<10000000000) then
- x1=x1*1000
- msg="KRF/t"
- end
- if (x1<1000000) then
- x1=x1*1000
- msg="RF/t"
- end
- print("Energy Core RF/t: "..x1..msg..", "..math.floor(x*100/2140000000000).."%")
- if (fieldStrengthPercent>51 and energySaturationPercent>51) then
- -- monitor.setCursorPos(15,14)
- redstone.setOutput("top",false)
- end
- if (adjtimeout>0) then
- adjtimeout=adjtimeout-1
- monitor.setCursorPos(18,14)
- monitor.setTextColor(colors.cyan)
- print("Adjtimeout="..adjtimeout)
- end
- if (coretemp<7000 and adjtimeout==0 and energySaturationPercent>51) then
- -- coretemp temp set to 'save' 7k
- monitor.setCursorPos(18,14)
- monitor.setTextColor(colors.pink)
- print("experimental: adding~10k")
- addLog("logs.txt",getTime(),"adding 10k to "..fluxgate_out.getSignalLowFlow())
- fluxgate_out.setSignalLowFlow(fluxgate_out.getSignalLowFlow()+10000)
- adjtimeout=120 -- no faster than 1 adj. per 2 min
- end
- --end
- if (fieldStrengthPercent<51 or energySaturationPercent<51) then
- monitor.setCursorPos(1,14)
- if (lastEnergySaturationPercent<energySaturationPercent) then
- monitor.setTextColor(colors.green)
- print("++++ Correcting")
- history=history.."+"
- elseif (lastEnergySaturationPercent>energySaturationPercent) then
- monitor.setTextColor(colors.yellow)
- print("!!!! Degrading")
- history=history.."-"
- end
- if string.len(history)>50 then
- history=string.sub(history,2,50)
- end
- monitor.setCursorPos(1,18)
- monitor.setTextColor(colors.lightBlue)
- print(history)
- redstone.setOutput("top", not redstone.getOutput("top"))
- redstone.setOutput("back",false)
- -- or temp over?? 8800?
- if (fieldStrengthPercent<45 or energySaturationPercent<45) then
- redstone.setOutput("top",true)
- core.stopReactor()
- monitor.setCursorPos(20,14)
- monitor.setTextColor(colors.red)
- print("*** REACTOR SCRAM ***")
- addLog("logs.txt",getTime(),"SCRAM: fs-"..fieldStrengthPercent..", es-"..energySaturationPercent)
- term.setBackgroundColor(colors.yellow)
- end
- end
- monitor.setTextColor(colors.white)
- if (fieldStrengthPercent>51 and energySaturationPercent>51) then
- redstone.setOutput("top",false)
- redstone.setOutput("back",true)
- -- redstone.setOutput("back",true) -- redstone.getOutput("top"))
- end
- -- Energy dump, if core<50% then
- -- set fluxgate low flow to core net gain (presuming you're generating at least 500k)
- if (x*100/2141200000000)>50 then -- per above now I'm only setting the output when over 70%
- -- seems to work more predictably just allowing the generation rate, using the change kept allow massive drops
- -- and recover time was poor (even with the 70% safety margin) an empty T7 is really a RF vacuum.
- -- fluxgate.setFlowOverride(ecrft*4000)
- fluxgate.setFlowOverride((stab.generationRate-(stab.fieldDrainRate*2))-20500) -- *2 to dump extra above generation range
- -- end
- else
- -- Want to let a little through since I rely on this energy to keep my AE2 system operational
- fluxgate.setFlowOverride(500000)
- end
- sleep(2)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement