From 4c2d1215627e6e9b8a63130c2655be89b0ff2512 Mon Sep 17 00:00:00 2001 From: "kepler155c@gmail.com" Date: Mon, 8 Jul 2019 13:52:53 -0400 Subject: [PATCH] reduce net traffic --- sys/apps/network/snmp.lua | 54 +++++++++++++++++++---------------- sys/apps/system/diskusage.lua | 23 +++++++-------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/sys/apps/network/snmp.lua b/sys/apps/network/snmp.lua index ad976a4..0fc9a34 100644 --- a/sys/apps/network/snmp.lua +++ b/sys/apps/network/snmp.lua @@ -121,8 +121,7 @@ print('discovery: listening on port 999') Event.on('modem_message', function(_, _, sport, id, info, distance) if sport == 999 and tonumber(id) and type(info) == 'table' then - if info.label and info.id and - type(info.label) == 'string' and type(info.id) == 'number' then + if type(info.label) == 'string' and type(info.id) == 'number' then if not network[id] then network[id] = { } @@ -150,6 +149,15 @@ local info = { } local infoTimer = os.clock() +local function getSlots() + return Util.reduce(turtle.getInventory(), function(acc, v) + if v.count > 0 then + acc[v.index .. ',' .. v.count] = v.key + end + return acc + end, { }) +end + local function sendInfo() if os.clock() - infoTimer >= 1 then -- don't flood infoTimer = os.clock() @@ -160,7 +168,7 @@ local function sendInfo() info.fuel = turtle.getFuelLevel() info.status = turtle.getStatus() info.point = turtle.point - info.inventory = turtle.getInventory() + info.inv = getSlots() info.slotIndex = turtle.getSelectedSlot() end if device.neuralInterface then @@ -168,28 +176,24 @@ local function sendInfo() if not info.status and device.neuralInterface.getMetaOwner then pcall(function() local meta = device.neuralInterface.getMetaOwner() - - if meta.isWet then - info.status = 'Swimming' - elseif meta.isElytraFlying then - info.status = 'Flying' - elseif meta.isBurning then - info.status = 'Burning' - elseif meta.isDead then - info.status = 'Deceased' - elseif meta.isOnLadder then - info.status = 'Climbing' - elseif meta.isRiding then - info.status = 'Riding' - elseif meta.isSneaking then - info.status = 'Sneaking' - elseif meta.isSprinting then - info.status = 'Running' - else - info.status = 'health: ' .. - math.floor(meta.health / - meta.maxHealth * 100) + local states = { + isWet = 'Swimming', + isElytraFlying = 'Flying', + isBurning = 'Burning', + isDead = 'Deceased', + isOnLadder = 'Climbing', + isRiding = 'Riding', + isSneaking = 'Sneaking', + isSprinting = 'Running', + } + for k,v in pairs(states) do + if meta[k] then + info.status = v + break + end end + info.status = info.status or 'health: ' .. + math.floor(meta.health / meta.maxHealth * 100) end) end end @@ -216,4 +220,4 @@ Event.on('turtle_response', function() end end) -sendInfo() +Event.onTimeout(1, sendInfo) diff --git a/sys/apps/system/diskusage.lua b/sys/apps/system/diskusage.lua index 99e3547..8fbdd71 100644 --- a/sys/apps/system/diskusage.lua +++ b/sys/apps/system/diskusage.lua @@ -45,8 +45,8 @@ local tab = UI.Tab { ex = -2, }, percentage = UI.Text { - x = 11, y = -1, - ex = -2, + x = 11, y = -3, + ex = '47%', align = 'center', }, icon = UI.NftImage { @@ -57,19 +57,16 @@ local tab = UI.Tab { local function getDrives() local unique = { ['hdd'] = true, ['virt'] = true } - local exclude = {} - local drives = { - {name = 'hdd', side = ''}, - } + local drives = { { name = 'hdd', side = '' } } + for _, drive in pairs(fs.list('/')) do local side = fs.getDrive(drive) if side and not unique[side] then unique[side] = true - exclude[drive] = true - table.insert(drives, {name=drive, side=side}) + table.insert(drives, { name = drive, side = side }) end end - return drives, exclude + return drives end local function getDriveInfo(p) @@ -125,8 +122,7 @@ function tab:updateInfo() end function tab:updateDrives() - local drives, exclude = getDrives() - self.exclude = exclude + local drives = getDrives() self.drives:setValues(drives) end @@ -139,7 +135,8 @@ end function tab:eventHandler(event) if event.type == 'grid_focus_row' then self:updateInfo() - else return UI.Tab.eventHandler(self, event) + else + return UI.Tab.eventHandler(self, event) end return true end @@ -151,4 +148,4 @@ Event.on({ 'disk', 'disk_eject' }, function() tab:sync() end) -return tab \ No newline at end of file +return tab