Skip to content

Commit

Permalink
v1.1.0 - fix render error on hubs and add an option for max fvolume f…
Browse files Browse the repository at this point in the history
…or hubs and refacto for better performance
  • Loading branch information
Jericho1060 committed Jul 25, 2022
1 parent 27a07c2 commit 747d8ca
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Copy the content of the file config.json then right clik on the board, chose adv
By rightclicking on the board, advanced, edit lua parameters, you can customize these options:

- `fontSize`: the size of the text for all the screen
- `maxVolumeForHub` the max volume from a hub (can't get it from the lua) if 0, the content volume will be displayed on the screen

# Support or donation

Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"slots":{"0":{"name":"container","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}}},"handlers":[{"code":"local list = container.getContent()\nscreen_data[4] = {}\nfor k, v in ipairs(list) do\n local quantity = math.floor(v.quantity*100)/100\n local item = system.getItem(v.id)\n local item_data = {\n v.id,\n item.displayNameWithSize,\n quantity\n }\n table.insert(screen_data[4], item_data)\nend","filter":{"args":[],"signature":"onContentUpdate()","slotKey":"0"},"key":"0"},{"code":"--[[\n\tLUA PARAMETERS\n]]\nfontSize = 20 --export: the size of the text for all the screen\n--[[\n\tINIT\n]]\n\nlocal version = '1.0.0'\n\nsystem.print(\"------------------------------------\")\nsystem.print(\"DU-Container-Monitoring version \" .. version)\nsystem.print(\"------------------------------------\")\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal data = json.decode(getInput()) or {}\n\nlocal rx,ry = getResolution()\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = ]] .. fontSize .. [[\n\nlocal mini=loadFont('Play',12)\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal medV=loadFont('Play-Bold', 25)\nlocal bigV=loadFont('Play-Bold', 30)\nlocal big=loadFont('Play',38)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(a)local b=a;while true do b,k=string.gsub(b,\"^(-?%d+)(%d%d%d)\",'%1 %2')if k==0 then break end end;return b end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g,h,i,j)a=-1*math.cos(a*math.pi)/2+0.5;local k=0;local l=0;local m=0;if a>=.5 then a=(a-0.5)*2;k=e-a*(e-h)l=f-a*(f-i)m=g-a*(g-j)else a=a*2;k=b-a*(b-e)l=c-a*(c-f)m=d-a*(d-g)end;return k,l,m end\n\nfunction renderHeader(title)\n local h_factor = 12\n local h = 35\n if subtitle ~= nil and subtitle ~= \"\" and subtitle ~= \"-\" then\n h = 50\n end\n addLine( back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,small,\"Next query possible in \" .. round(data[3]) .. ' seconds',rx-250,35)\n addText(front,smallBold,title,44,35)\nend\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal storageDark = createLayer()\nsetDefaultFillColor(storageDark,Shape_Text,63/255,92/255,102/255,1)\nsetDefaultFillColor(storageDark,Shape_Box,13/255,24/255,28/255,1)\n\nlocal colorLayer = createLayer()\n\nfunction renderProgressBar(percent)\n local r,g,b = getRGBGradient(percent/100,177/255,42/255,42/255,249/255,212/255,123/255,34/255,177/255,76/255)\n setNextFillColor(colorLayer, r, g, b, 1)\n setNextTextAlign(colorLayer, AlignH_Center, AlignV_Middle)\n addText(colorLayer, itemName, format_number(round(percent*100)/100) ..\"%\", rx/2, 90)\n local w = (rx-90)*(percent)/100\n local x=44\n local y=55\n local h=25\n addBox(storageBar,x,y,rx-88,h)\n setNextFillColor(colorLayer, r, g, b, 1)\n addBox(colorLayer,x+1,y+1,w,h-2)\nend\n\nfunction renderResistanceBar(title, quantity, x, y, w, h, withTitle)\n\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n\n addBox(storageBar,x,y,w,h)\n\n if withTitle then\n addText(storageBar, small, \"ITEMS\", x, y-5)\n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, small, \"QUANTITY\", x+w-15, y-5)\n end\n\n local pos_y = y+(h/2)-2\n\n setNextTextAlign(storageBar, AlignH_Left, AlignV_Middle)\n addText(storageBar, itemName, title, x+10, pos_y)\n\n --setNextFillColor(colorLayer, r, g, b, 1)\n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, itemName, format_number(quantity), w+30, pos_y)\nend\n\nrenderHeader('Container Monitoring v]] .. version .. [[')\n\nstart_h = 100\n\n\nlocal h = font_size + font_size / 2\nfor i,container in ipairs(data[4]) do\n renderResistanceBar(container[2], container[3], 44, start_h, rx-88, h, i==1)\n start_h = start_h+h+5\nend\nrenderProgressBar(data[2]*100/data[1])\nrequestAnimationFrame(10)\n]]\n\nscreens = {}\nfor slot_name, slot in pairs(unit) do\n if\n type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n if slot.getClass():lower() == 'screenunit' then\n slot.slotname = slot_name\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n end\n end\nend\nif #screens == 0 then\n system.print(\"No Screen Detected\")\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" Connected\")\nend\nif container == nil then\n system.print('No Container or Hub dectected')\nelse\n system.print('Storage connected')\nend\nscreen_data={0,0,0,{}}\nrequest_time = container.updateContent()\nunit.setTimer(\"acquireStorage\", 1)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"1"},{"code":"request_time = container.updateContent()\nscreen_data[1] = container.getMaxVolume()\nscreen_data[2] = container.getItemsVolume()\nscreen_data[3] = request_time\nfor _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\nend","filter":{"args":[{"value":"acquireStorage"}],"signature":"onTimer(tag)","slotKey":"-1"},"key":"2"}],"methods":[],"events":[]}
{"events":[],"handlers":[{"code":"--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\nif coroutine.status(MainCoroutine) == \"dead\" then\n MainCoroutine = coroutine.create(runCoroutines)\nend\nif coroutine.status(MainCoroutine) == \"suspended\" then\n assert(coroutine.resume(MainCoroutine))\nend","filter":{"args":[],"signature":"onUpdate()","slotKey":"-4"},"key":"2"},{"code":"--[[\n\tLUA PARAMETERS\n]]\nfontSize = 20 --export: the size of the text for all the screen\nmaxVolumeForHub = 0--export: the max volume from a hub (can't get it from the lua) if 0, the content volume will be displayed on the screen\n--[[\n\tINIT\n]]\n\nlocal version = '1.1.0'\n\nsystem.print(\"------------------------------------\")\nsystem.print(\"DU-Container-Monitoring version \" .. version)\nsystem.print(\"------------------------------------\")\n\nlocal renderScript = [[\nlocal json = require('dkjson')\nlocal data = json.decode(getInput()) or {}\n\nlocal rx,ry = getResolution()\n\nlocal back=createLayer()\nlocal front=createLayer()\n\nfont_size = ]] .. fontSize .. [[\nlocal small=loadFont('Play',14)\nlocal smallBold=loadFont('Play-Bold',18)\nlocal itemName=loadFont('Play-Bold',font_size)\nlocal big=loadFont('Play',38)\n\nsetBackgroundColor( 15/255,24/255,29/255)\n\nsetDefaultStrokeColor( back,Shape_Line,0,0,0,0.5)\nsetDefaultShadow( back,Shape_Line,6,0,0,0,0.5)\n\nsetDefaultFillColor( front,Shape_BoxRounded,249/255,212/255,123/255,1)\nsetDefaultFillColor( front,Shape_Text,0,0,0,1)\nsetDefaultFillColor( front,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor( front,Shape_Text,0.710,0.878,0.941,1)\n\nfunction format_number(a)local b=a;while true do b,k=string.gsub(b,\"^(-?%d+)(%d%d%d)\",'%1 %2')if k==0 then break end end;return b end\n\nfunction round(a,b)if b then return utils.round(a/b)*b end;return a>=0 and math.floor(a+0.5)or math.ceil(a-0.5)end\n\nfunction getRGBGradient(a,b,c,d,e,f,g,h,i,j)a=-1*math.cos(a*math.pi)/2+0.5;local k=0;local l=0;local m=0;if a>=.5 then a=(a-0.5)*2;k=e-a*(e-h)l=f-a*(f-i)m=g-a*(g-j)else a=a*2;k=b-a*(b-e)l=c-a*(c-f)m=d-a*(d-g)end;return k,l,m end\n\nfunction renderHeader(title)\n local h_factor = 12\n local h = 35\n if subtitle ~= nil and subtitle ~= \"\" and subtitle ~= \"-\" then\n h = 50\n end\n addLine( back,0,h+12,rx,h+12)\n addBox(front,0,12,rx,h)\n addText(front,small,\"Next query possible in \" .. round(data[3]) .. ' seconds',rx-250,35)\n addText(front,smallBold,title,44,35)\nend\n\nlocal storageBar = createLayer()\nsetDefaultFillColor(storageBar,Shape_Text,110/255,166/255,181/255,1)\nsetDefaultFillColor(storageBar,Shape_Box,0.075,0.125,0.156,1)\nsetDefaultFillColor(storageBar,Shape_Line,1,1,1,1)\n\nlocal colorLayer = createLayer()\nlocal percent_fill = 0\nlocal r = 110/255\nlocal g = 166/255\nlocal b = 181/255\nif data[1] > 0 then\n percent_fill = data[2]*100/data[1]\n if percent_fill > 100 then percent_fill = 100 end\n r,g,b = getRGBGradient(percent_fill/100,177/255,42/255,42/255,249/255,212/255,123/255,34/255,177/255,76/255)\nend\nsetDefaultFillColor(colorLayer,Shape_Box,r,g,b,1)\nsetDefaultFillColor(colorLayer,Shape_Text,r,g,b,1)\nsetDefaultTextAlign(colorLayer, AlignH_Center, AlignV_Middle)\n\nfunction renderProgressBar(percent)\n if data[1] > 0 then\n addText(colorLayer, itemName, format_number(round(percent*100)/100) ..\"%\", rx/2, 90)\n local w=(rx-90)*(percent)/100\n local x=44\n local y=55\n local h=25\n addBox(storageBar,x,y,rx-88,h)\n addBox(colorLayer,x+1,y+1,w,h-2)\n else\n addText(colorLayer, itemName, format_number(round(data[2]*100)/100) ..\" L\", rx/2, 80)\n end\nend\n\nfunction renderResistanceBar(title, quantity, x, y, w, h, withTitle)\n\n local quantity_x_pos = font_size * 6.7\n local percent_x_pos = font_size * 2\n\n addBox(storageBar,x,y,w,h)\n\n if withTitle then\n addText(storageBar, small, \"ITEMS\", x, y-5)\n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, small, \"QUANTITY\", x+w-15, y-5)\n end\n\n local pos_y = y+(h/2)-2\n\n setNextTextAlign(storageBar, AlignH_Left, AlignV_Middle)\n addText(storageBar, itemName, title, x+10, pos_y)\n \n setNextTextAlign(storageBar, AlignH_Right, AlignV_Middle)\n addText(storageBar, itemName, format_number(quantity), w+30, pos_y)\nend\n\nrenderHeader('Container Monitoring v]] .. version .. [[')\n\nstart_h = 100\n\n\nlocal h = font_size + font_size / 2\nfor i,container in ipairs(data[4]) do\n renderResistanceBar(container[2], container[3], 44, start_h, rx-88, h, i==1)\n start_h = start_h+h+5\nend\nrenderProgressBar(percent_fill)\nrequestAnimationFrame(500)\n]]\n\nscreens = {}\nfor slot_name, slot in pairs(unit) do\n if type(slot) == \"table\"\n and type(slot.export) == \"table\"\n and slot.getClass\n then\n if slot.getClass():lower() == 'screenunit' then\n slot.slotname = slot_name\n table.insert(screens,slot)\n slot.setRenderScript(renderScript)\n end\n end\nend\nif #screens == 0 then\n system.print(\"No Screen Detected\")\n unit.exit()\nelse\n table.sort(screens, function(a,b) return a.slotname < b.slotname end)\n local plural = \"\"\n if #screens > 1 then plural = \"s\" end\n system.print(#screens .. \" screen\" .. plural .. \" Connected\")\nend\nif container == nil then\n system.print('No Container or Hub dectected')\n unit.exit()\nelse\n system.print('Storage connected')\nend\n\nscreen_data={0,0,0,{}}\nrequest_time = 0\n\n--[[\n DU-Nested-Coroutines by Jericho\n Permit to easier avoid CPU Load Errors\n Source available here: https://github.com/Jericho1060/du-nested-coroutines\n]]--\n\ncoroutinesTable = {}\n--all functions here will become a coroutine\nMyCoroutines = {\n function()\n request_time = math.ceil(container.updateContent())\n local max_vol = container.getMaxVolume()\n if max_vol == 0 then\n max_vol = maxVolumeForHub\n end\n screen_data[1] = max_vol\n screen_data[2] = container.getItemsVolume()\n screen_data[3] = request_time\n for _,s in pairs(screens) do\n s.setScriptInput(json.encode(screen_data))\n end\n end\n}\n\nfunction initCoroutines()\n for _,f in pairs(MyCoroutines) do\n local co = coroutine.create(f)\n table.insert(coroutinesTable, co)\n end\nend\n\ninitCoroutines()\n\nrunCoroutines = function()\n for i,co in ipairs(coroutinesTable) do\n if coroutine.status(co) == \"dead\" then\n coroutinesTable[i] = coroutine.create(MyCoroutines[i])\n end\n if coroutine.status(co) == \"suspended\" then\n assert(coroutine.resume(co))\n end\n end\nend\n\nMainCoroutine = coroutine.create(runCoroutines)","filter":{"args":[],"signature":"onStart()","slotKey":"-1"},"key":"1"},{"code":"local list = container.getContent()\nscreen_data[4] = {}\nfor k, v in ipairs(list) do\n local quantity = math.floor(v.quantity*100)/100\n local item = system.getItem(v.id)\n local item_data = {\n v.id,\n item.displayNameWithSize,\n quantity\n }\n table.insert(screen_data[4], item_data)\nend","filter":{"args":[],"signature":"onContentUpdate()","slotKey":"0"},"key":"0"}],"methods":[],"slots":{"0":{"name":"container","type":{"events":[],"methods":[]}},"1":{"name":"slot2","type":{"events":[],"methods":[]}},"2":{"name":"slot3","type":{"events":[],"methods":[]}},"3":{"name":"slot4","type":{"events":[],"methods":[]}},"4":{"name":"slot5","type":{"events":[],"methods":[]}},"5":{"name":"slot6","type":{"events":[],"methods":[]}},"6":{"name":"slot7","type":{"events":[],"methods":[]}},"7":{"name":"slot8","type":{"events":[],"methods":[]}},"8":{"name":"slot9","type":{"events":[],"methods":[]}},"9":{"name":"slot10","type":{"events":[],"methods":[]}},"-5":{"name":"library","type":{"events":[],"methods":[]}},"-4":{"name":"system","type":{"events":[],"methods":[]}},"-3":{"name":"player","type":{"events":[],"methods":[]}},"-2":{"name":"construct","type":{"events":[],"methods":[]}},"-1":{"name":"unit","type":{"events":[],"methods":[]}}}}
12 changes: 12 additions & 0 deletions source/system/onUpdate.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--[[
DU-Nested-Coroutines by Jericho
Permit to easier avoid CPU Load Errors
Source available here: https://github.com/Jericho1060/du-nested-coroutines
]]--

if coroutine.status(MainCoroutine) == "dead" then
MainCoroutine = coroutine.create(runCoroutines)
end
if coroutine.status(MainCoroutine) == "suspended" then
assert(coroutine.resume(MainCoroutine))
end
Loading

0 comments on commit 747d8ca

Please sign in to comment.