silasbause

CC Item Flowrate Monitoring

Dec 3rd, 2021 (edited)
445
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local side = "back"
  2. local secondSide = "none"
  3. local mode = "fluid"
  4. local identifier = "UNDEF"
  5. local clearedLast = os.clock()
  6. local flow_timeout = 0
  7. local headers = {
  8. ["Content-Type"] = "text/plain; charset=utf-8",
  9. }
  10. local jobName = "UNDEF-Computer"
  11.  
  12. local logData = function(job, metric, value, unit)
  13.     local x, y = term.getCursorPos()
  14.     local status = "UNDEFINED"
  15.     local req = {
  16.     url = "http://localhost:9091/metrics/job/"..job,
  17.     body = metric.." "..value .. "\n",
  18.     method = "POST",
  19.     binary = false,
  20.     headers = headers,
  21.     redirect = false
  22.     }
  23.    
  24.     term.setCursorPos(1,y + 1)
  25.     if http.post(req) then
  26.         status = "OK"
  27.     else
  28.         status = "ERROR"
  29.     end
  30.     term.write(metric .. string.format(": %04.2f ", value) .. unit .. ":    " .. status)
  31. end
  32.  
  33. local measureFlowrate = function(valueFunction)
  34.     local currentStorage = valueFunction()["amount"]
  35.     rs.setAnalogOutput(side, 15)
  36.     os.sleep(20 / 20)
  37.     local flowrate = valueFunction()["amount"] - currentStorage
  38.     flowrate = flowrate / 20
  39.     rs.setAnalogOutput(side, 0)
  40.     return flowrate
  41. end
  42.  
  43. local measureItemflow = function(storage_in, storage_out)
  44.     local movedItems = 0
  45.     local rate = clearedLast - os.clock()
  46.     for slot, item in pairs(storage_in.list()) do
  47.         --print(("%d x %s in slot %d"):format(item.count, item.name, slot))
  48.         movedItems = movedItems + storage_in.pushItems(peripheral.getName(storage_out), slot)
  49.     end
  50.     clearedLast = os.clock()
  51.     return movedItems / rate * -1
  52. end
  53.  
  54. local storeValue = function(valueName, value)
  55.         if fs.exists("/monitoring/") then
  56.             fs.makeDir("/monitoring/")
  57.         end
  58.         local h = fs.open("/monitoring/"..valueName, "w")
  59.         h.writeLine(value)
  60.         h.flush()
  61.         h.close()
  62.         return true
  63. end
  64.  
  65. local readValue = function(valueName)
  66.     if fs.exists("/monitoring/"..valueName) then
  67.         local h = fs.open("/monitoring/"..valueName, "r")
  68.         local value = h.readAll()
  69.         h.close()
  70.         return value
  71.     else
  72.         print("Error Reading Value (" .. valueName .."): Does not exists!")
  73.         return false
  74.     end
  75. end
  76.  
  77. local calcFlowTimeout = function(flowrate)
  78.    local flowrateNum = tonumber(flowrate)
  79.    if flowrateNum > 10 then
  80.         return 5
  81.    elseif flowrateNum > 0.1 then
  82.         return 40
  83.    else
  84.         return 20 * 60
  85.    end
  86. end
  87.  
  88. local newUserSetting = function(question, valueName)
  89.     term.clear()
  90.     term.setCursorPos(1,1)
  91.     term.write(question)
  92.     term.setCursorPos(1,2)
  93.     local value = read()
  94.     if value == "\n" then
  95.         term.setCursorPos(1,3)
  96.         term.write("Please enter a value, Rebooting...")
  97.         os.sleep(2)
  98.         os.reboot()
  99.     else
  100.         if not storeValue(valueName, value) then
  101.             term.setCursorPos(1,3)
  102.             term.write("Rebooting...")
  103.             os.sleep(2)
  104.             os.reboot()
  105.         end
  106.     end
  107.        
  108. end
  109.  
  110. local initialSetup = function()
  111.     if readValue("configured") then
  112.         return true
  113.     else
  114.         newUserSetting("Which jobName should this Computer use?", "job")
  115.         newUserSetting("Which identifier should this Computer use (Use one for all PCs)?", "ident")
  116.         newUserSetting("Which mode do you want to use (item, fluid)? ", "mode")
  117.         if readValue("mode") == "fluid" then
  118.             newUserSetting("Which side is the peripheral?", "pSide")
  119.             newUserSetting("Please estimate the maximum Flowrate (mB/t)", "flowrate")
  120.         else
  121.             newUserSetting("Which side is the chest to extract?", "pSide")
  122.             newUserSetting("Which side is the chest to deposit?", "pSideTwo")
  123.         end
  124.         newUserSetting("Do you want to start monitoring?", "configured")
  125.         return false
  126.     end
  127. end
  128.  
  129. if initialSetup() then
  130.     term.clear()
  131.     term.setCursorPos(1,1)
  132.     term.write("Initial Setup found! Starting...")
  133.     os.sleep(2)
  134. end
  135.  
  136. if readValue("configured") then
  137.     side = readValue("pSide")
  138.     jobName = readValue("job")
  139.     mode =  readValue("mode")
  140.     identifier = readValue("ident")
  141.     term.clear()
  142.     term.setCursorPos(1,1)
  143.     term.write("Job: "..jobName)
  144.     term.setCursorPos(1,2)
  145.     term.write("Job: "..identifier)
  146.     term.setCursorPos(1,3)
  147.     term.write("Mode: "..mode)
  148.     term.setCursorPos(1,4)
  149.     term.write("Side: "..side)
  150.     if mode == "item" then
  151.         secondSide = readValue("pSideTwo")
  152.         term.setCursorPos(1,5)
  153.         term.write("Second Side: "..secondSide)
  154.     else
  155.         est_flowrate = readValue("flowrate")
  156.     end
  157.     os.sleep(2)
  158. else
  159.     print("Something went wrong!")
  160. end
  161.  
  162. local device = peripheral.wrap(side)
  163. local secondDev = 0
  164.  
  165. if mode ~= "fluid" then
  166.     secondDev = peripheral.wrap(secondSide)
  167. else
  168.     flow_timeout = calcFlowTimeout(est_flowrate)
  169. end
  170.  
  171. while (true) do
  172.  
  173.     term.clear()
  174.     term.setCursorPos(1,1)
  175.     term.write("Flowrate Monitoring vom Allerechten \n http://swidan.ddns.net:1337/")
  176.     if mode == "fluid" then
  177.         logData(jobName, identifier.."_Fluid_Flowrate", measureFlowrate(device.getStored), "mB/t")
  178.         logData(jobName, identifier.."_Fluid_Stored", device.getStored()['amount'], "mB")
  179.     elseif mode == "item" then
  180.         logData(jobName, identifier.."_Item_Flowrate", measureItemflow(device, secondDev), "Items/s")
  181.     else
  182.         term.write("Error: Mode Undefined")
  183.     end
  184.        
  185.     os.sleep(5)
  186. end
  187.  
RAW Paste Data