Advertisement
Ubidibity

DraconicReactorMonitor-1.2

Jun 26th, 2021 (edited)
1,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.42 KB | Source Code | 0 0
  1. -- modified to work with draconic rf storage (trying to determine actual 'gain' from reactor)
  2. --Rev 20210701 wtp
  3. --Moved extraneous notes and documentation into https://pastebin.com/FcZRymGb
  4. -- rev 1.2 import borrowed functions
  5. -- ============================== --
  6. -- functions (borrowed from knPtJCjb)
  7.  
  8. -- logging functions
  9. local function getTime()
  10.  
  11.   contentSite = http.get("http://worldtimeapi.org/api/timezone/GMT").readAll()
  12.  
  13.   if string.find(contentSite, "datetime") ~= nil then -- and string.find(contentSite, "localDate") == 3 then
  14.       content = contentSite
  15.   else
  16.       content = "00:00:00.00000','utcDate"
  17.   end
  18.   timePos = string.find(content,"datetime")+22
  19.   time = string.sub(content, timePos, timePos+13)
  20. --    if string.sub(content, timePos, timePos+1) == "PM" then
  21. --        timePM = tostring(tonumber(string.sub(time,1,2))+12)..string.sub(time,3)
  22. --        return timePM
  23. --    else
  24.   return time
  25. -- I don't care if the time is 24H
  26. --    end
  27. end
  28.  
  29. -- numeric functions
  30. local function round(num, idp)
  31.   local mult = 10^(idp or 0)
  32.   return math.floor(num * mult + 0.5) / multi
  33. end
  34.  
  35. -- file functions
  36. -- write table to file 'path'
  37. local function fileWrite(path, text)
  38.   local file = io.open(path, "w")
  39.   file:write(text)
  40.   file:close()
  41. end
  42.  
  43. -- get table from 'path'
  44. local function fileGetTable(path)
  45.   if fs.exists(path) then
  46.     local file = io.open(path, "r")
  47.     local lines = {}
  48.     local i = 1
  49.     local line = file:read("*l")
  50.     while line ~= nil do
  51.       lines[i] = line
  52.       line = file:read("*l")
  53.        i = i +1
  54.     end
  55.     file:close()
  56.     return lines
  57.   end
  58.   return {}
  59. end
  60.  
  61. -- update line in file 'path' (n is line number)
  62. local function fileReplaceLine(path, n, text)
  63.   local lines = fileGetTable(path)
  64.   lines[n] = text
  65.   fileWriteFromTable(path, lines)
  66. end
  67.  
  68. -- append line of text to path
  69. local function fileAppend(path, text)
  70.   local file = io.open(path, "a")
  71.   file:write(text.."\n")
  72.   file:close()
  73. end
  74.  
  75. -- get file length for path
  76. local function fileGetLength(path)
  77.   local file = io.open(path, "r")
  78.   local i = 0
  79.   while file:read("*l") ~= nil do
  80.     i = i +1
  81.   end
  82.   file:close()
  83.   return I
  84. end
  85.  
  86. -- return array of lines
  87. local function fileGetLines(path, startN, endN)
  88. local lines = fileGetTable(path)
  89. local linesOut = {}
  90. local x = 1
  91.   for i = startN, endN, 1 do
  92.     linesOut[x] = lines[i]
  93.     x = x + 1
  94.   end
  95.   return linesOut
  96. end
  97.  
  98. local function getLogs(path, xPos, yPos)
  99. local Logs = fileGetLines(path, fileGetLength(path)-5, fileGetLength(path))
  100.   for i = 1, 6, 1 do
  101.     mon.setCursorPos(xPos+2,yPos+1+i)
  102.     mon.write(Logs[i])
  103.   end
  104. end
  105.  
  106. local function addLog(path, time, text)
  107.   fileAppend(path, "["..time.."]")
  108.   fileAppend(path, text)
  109. end
  110.  
  111. -- end borrowed functions--
  112. -- ===================== --
  113.  
  114. -- Each instance will have unique values for the flux gates.  I'm going to store those as:
  115. -- Mym.li (values.cfg)
  116. -- local values = {
  117. --    "fluxgate".."flux_gate_308",
  118. --    "fluxgate_out".."flux_gate_309",
  119. --  slack API codes (to decrease odds I'll post those to pastebin again)
  120. -- }
  121. -- meh whatever just put the values line 1 = fluxgate, line 2 = fluxgate_out (values only), line 3 = slack API codes
  122. -- Dirtcraft fluxgate flux_gate_67 and fluxgate_out flux_gate_68
  123. -- local values = {}
  124.  
  125. local storage = peripheral.find("draconic_rf_storage")
  126. local monitor = peripheral.wrap("bottom")
  127. local    core = peripheral.wrap("right")
  128. --local fluxgate = peripheral.wrap("flux_gate_308") -- dump energy
  129. --local fluxgate_out = peripheral.wrap("flux_gate_309") -- from reactor
  130. local powerstate = 0
  131. local x=0
  132. local y=0
  133. local x1=0
  134. local msg="empty"
  135. local fieldStrengthPercent=0
  136. local lastFieldStrengthPercent
  137. local energySaturationPercent=0
  138. local lastEnergySaturationPercent=0 -- last energy saturation
  139. local ecrft=0 -- energy core rf per tick
  140. local adjtimeout=0
  141. local coretemp=0
  142. local history="50 field saturation gain/loss @ 2 sec intervals"
  143.  
  144. -- initialize log
  145. if fs.exists("logs.txt") then
  146. else
  147.     file = io.open("logs.txt", "w")
  148.     file:write("")
  149.     file:close()
  150. end
  151.  
  152. local values=fileGetTable("values.cfg")
  153. addLog("logs.txt",getTime(),"Values")
  154.  
  155. for i,value in pairs(values) do
  156.   addLog("logs.txt",getTime(),i..": "..value)
  157. end
  158. -- print(textutils.serialix(values))
  159. local fluxgate = peripheral.wrap(values[1]) -- dump energy
  160. local fluxgate_out = peripheral.wrap(values[2]) -- from reactor
  161. local slack_api = peripheral.wrap(values[3]) -- authentication URL code
  162.  
  163. term.redirect(monitor)
  164. term.setBackgroundColor(colors.gray)
  165. fluxgate.setOverrideEnabled(true)  -- required for direct computer control of flux gate flow rate
  166.  
  167. while true do
  168.  
  169. local stab = core.getReactorInfo()
  170.  
  171. y=x
  172. x=storage.getEnergyStored()
  173. powerstate=stab.status
  174.  
  175. monitor.clear()
  176.  
  177. monitor.setTextColor(colors.yellow)
  178. monitor.setCursorPos(32,1)
  179. print(getTime().." GMT")
  180.  
  181. monitor.setTextColor(colors.white)
  182. monitor.setCursorPos(15,1)
  183. print("status:")
  184. if powerstate=="online" then
  185.   monitor.setTextColor(colors.green)
  186. else
  187.   monitor.setTextColor(colors.red)
  188. end
  189. monitor.setCursorPos(23,1)
  190. print(powerstate)
  191.  
  192. monitor.setTextColor(colors.white)
  193. monitor.setCursorPos(7,2)
  194. print("generationRate: "..stab.generationRate.." RF/t")
  195.  
  196. monitor.setCursorPos(10,3)
  197. coretemp=stab.temperature
  198. monitor.setTextColor(colors.green)
  199. if (coretemp>7000) then
  200.   monitor.setTextColor(colors.orange)
  201. end
  202. if (coretemp>8000) then
  203.   monitor.setTextColor(colors.purple)
  204. end
  205. print("temperature: "..coretemp)
  206. monitor.setTextColor(colors.white)
  207.  
  208. monitor.setCursorPos(8,4)
  209. print("fieldStrength: "..stab.fieldStrength)
  210.  
  211. -- these two max values are static and don't really tell us anything
  212. --monitor.setCursorPos(5,5)
  213. --print("maxFieldStrength: "..stab.maxFieldStrength)
  214.  
  215. monitor.setCursorPos(7,6)
  216. print("fieldDrainRate: "..stab.fieldDrainRate)
  217.  
  218. monitor.setCursorPos(1,7)
  219. print("Fuel Conversion Rate: "..stab.fuelConversionRate.." nb/t")
  220.  
  221. --monitor.setCursorPos(4,8)
  222. --print("maxFuelConversion: "..stab.maxFuelConversion)
  223.  
  224. local fuelConversionPercent=stab.fuelConversion*100/stab.maxFuelConversion
  225. monitor.setCursorPos(7,9)
  226. print("fuelConversion: "..stab.fuelConversion.."("..math.floor(fuelConversionPercent).."%)")
  227.  
  228. lastEnergySaturationPercent=energySaturationPercent
  229. energySaturationPercent=stab.energySaturation*100/stab.maxEnergySaturation
  230. monitor.setCursorPos(5,10)
  231. print("energySaturation: "..stab.energySaturation.."("..math.floor(energySaturationPercent).."%) "..tonumber(string.format("%.3f",energySaturationPercent-lastEnergySaturationPercent)))
  232. --if energySaturationPercent==50 then
  233. --  stab.setGenerationRate=stab.generationRate+10000
  234. --  sleep(5)
  235. -- end
  236.  
  237. --monitor.setCursorPos(2,11)
  238. --print("maxEnergySaturation: "..stab.maxEnergySaturation)
  239.  
  240. monitor.setCursorPos(8,12)
  241. print("fluxgate flow: "..fluxgate.getFlow())
  242.  
  243. lastFieldStrengthPercent=fieldStrengthPercent
  244. fieldStrengthPercent=stab.fieldStrength*100/stab.maxFieldStrength
  245. monitor.setCursorPos(2,13)
  246. print("Cont. fieldStrength: "..tonumber(string.format("%.3f",(fieldStrengthPercent))).."%".."  ("..fieldStrengthPercent-lastFieldStrengthPercent..")")
  247.  
  248. monitor.setCursorPos(2,15)
  249. print("Energy Core Storage: "..tonumber(string.format("%.3f",x)))
  250. ecrft=((((x-y)/1000/1000)/2)/20) -- there are 20 ticks in a second so you /20 not multiple dufus
  251.  
  252. monitor.setCursorPos(5,16)
  253.  
  254. x1=ecrft
  255. if (x1>999999999) then
  256. -- leave x1 alone for millions
  257.   msg="MRF/t"
  258. end
  259.  
  260. if (x1<10000000000) then
  261.   x1=x1*1000
  262.   msg="KRF/t"
  263. end
  264.  
  265. if (x1<1000000) then
  266.   x1=x1*1000
  267.   msg="RF/t"
  268. end
  269.  
  270. print("Energy Core RF/t: "..x1..msg..", "..math.floor(x*100/2140000000000).."%")
  271. if (fieldStrengthPercent>51 and energySaturationPercent>51) then
  272. --  monitor.setCursorPos(15,14)
  273.   redstone.setOutput("top",false)
  274. end
  275.  
  276. if (adjtimeout>0) then
  277.   adjtimeout=adjtimeout-1
  278.   monitor.setCursorPos(18,14)
  279.   monitor.setTextColor(colors.cyan)
  280.   print("Adjtimeout="..adjtimeout)
  281. end
  282. if (coretemp<7000 and adjtimeout==0 and energySaturationPercent>51) then
  283. -- coretemp temp set to 'save' 7k
  284.     monitor.setCursorPos(18,14)
  285.     monitor.setTextColor(colors.pink)
  286.     print("experimental: adding~10k")
  287.     addLog("logs.txt",getTime(),"adding 10k to "..fluxgate_out.getSignalLowFlow())
  288.     fluxgate_out.setSignalLowFlow(fluxgate_out.getSignalLowFlow()+10000)
  289.     adjtimeout=120  -- no faster than 1 adj. per 2 min
  290. end
  291.  
  292. --end
  293.  
  294. if (fieldStrengthPercent<51 or energySaturationPercent<51) then
  295.   monitor.setCursorPos(1,14)
  296.   if (lastEnergySaturationPercent<energySaturationPercent) then
  297.     monitor.setTextColor(colors.green)
  298.     print("++++ Correcting")
  299.     history=history.."+"
  300.   elseif (lastEnergySaturationPercent>energySaturationPercent) then
  301.     monitor.setTextColor(colors.yellow)
  302.     print("!!!! Degrading")
  303.     history=history.."-"
  304.   end
  305.   if string.len(history)>50 then
  306.     history=string.sub(history,2,50)
  307.   end
  308.   monitor.setCursorPos(1,18)
  309.   monitor.setTextColor(colors.lightBlue)
  310.   print(history)
  311.  
  312.   redstone.setOutput("top", not redstone.getOutput("top"))
  313.   redstone.setOutput("back",false)
  314. -- or temp over?? 8800?
  315.   if (fieldStrengthPercent<45 or energySaturationPercent<45) then
  316.     redstone.setOutput("top",true)
  317.     core.stopReactor()
  318.     monitor.setCursorPos(20,14)
  319.     monitor.setTextColor(colors.red)
  320.     print("*** REACTOR SCRAM ***")
  321.     addLog("logs.txt",getTime(),"SCRAM: fs-"..fieldStrengthPercent..", es-"..energySaturationPercent)
  322.     term.setBackgroundColor(colors.yellow)
  323.   end
  324. end
  325.  
  326. monitor.setTextColor(colors.white)
  327.  
  328. if (fieldStrengthPercent>51 and energySaturationPercent>51) then
  329.   redstone.setOutput("top",false)
  330.   redstone.setOutput("back",true)
  331. --  redstone.setOutput("back",true) -- redstone.getOutput("top"))
  332. end
  333. -- Energy dump, if core<50% then
  334. -- set fluxgate low flow to core net gain (presuming you're generating at least 500k)
  335.  
  336.   if (x*100/2141200000000)>50 then  -- per above now I'm only setting the output when over 70%
  337.  
  338. -- seems to work more predictably just allowing the generation rate, using the change kept allow massive drops
  339. -- and recover time was poor (even with the 70% safety margin) an empty T7 is really a RF vacuum.
  340. --    fluxgate.setFlowOverride(ecrft*4000)
  341.     fluxgate.setFlowOverride((stab.generationRate-(stab.fieldDrainRate*2))-20500) -- *2 to dump extra above generation range
  342. --  end
  343.   else
  344.     -- Want to let a little through since I rely on this energy to keep my AE2 system operational
  345.     fluxgate.setFlowOverride(500000)
  346.   end
  347.  
  348. sleep(2)
  349. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement