This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Letherin's profile picture

Looking for example code - how to detect items on a network.

Started by Letherin, 25 December 2013 - 10:27 AM
Letherin #1
Posted 25 December 2013 - 11:27 AM
I'm trying to work out how to detect all the batboxes, MFSU's, CESU's etc… on a network ?

Just I'm sure someone has done it before… if anyone can point me at where, I'd be obliged.

Thanks for any help.
LBPHacker #2
Posted 25 December 2013 - 12:04 PM
I assume you're talking about peripheral networks. If that's the case, all the batboxes, MFSU's and things like that are peripherals. Suppose you have them connected to a computer. You can go and call peripheral.getNames() to get a list of all connected peripherals on that network (and the peripherals which connect to the computer directly).
local connectedPeripherals = peripheral.getNames()
for ix = 1, #connectedPeripherals do
    print(connectedPeripherals[ix] .. ": " .. peripheral.getType(connectedPeripherals[ix]))
end
Edited on 25 December 2013 - 11:04 AM
Letherin #3
Posted 25 December 2013 - 12:34 PM
Ah yeah sorry, I probably wasn't clear enough - I'm really after the function that handles the array with all the names in it and "does something with that" :)/>

I just want to add up the power for particular parts of the network and send it to screen.

I will handle the screen handling just not too sure about the array itself. That's why I'm after example code, it will have everything in it.
Edited on 25 December 2013 - 11:36 AM
TheOddByte #4
Posted 25 December 2013 - 01:32 PM
To simplify LBPHacker's code

for _, name in ipairs( peripheral.getNames() ) do
	print( name .. ":" .. peripheral.getType( name ) )
end
So you know, peripheral.getNames() returns a table.

EDIT: So you want to use the batboxes etc? Well then you should wrap them

local batboxes = {}

--# Get all batboxes attached
for _, name in ipairs( peripheral.getNames() ) do
	if peripheral.getType( name ) == "batbox" then
		nBox = {}
		nBox.name = name
		table.insert( batboxes, batbox )
	end
end


--# Wrap them
for i = 1, #batboxes do
	batboxes[i].name = peripheral.wrap( batboxes[i].name )
end
But I'm not sure what the type for batbox is so I just put batbox, Run the first code to check and then change batbox in this part if it is something else than that.

if peripheral.getType( name ) == "batbox" then
Edited on 27 December 2013 - 09:01 AM
LBPHacker #5
Posted 25 December 2013 - 06:48 PM

if type( name ) == "batbox" then
string…
TheOddByte #6
Posted 26 December 2013 - 11:41 AM

if type( name ) == "batbox" then
string…
What? You check with strings when you check peripherals

for _, name in ipairs( peripheral.getNames() ) do
	if peripheral.getType( name ) == "monitor" then
		print("This was a monitor!")
		break
	end
end
Edited on 27 December 2013 - 09:02 AM
LBPHacker #7
Posted 26 December 2013 - 12:24 PM
-snip-
But checking the type of a string returns "string". Did you mean peripheral.getType?
TheOddByte #8
Posted 27 December 2013 - 10:00 AM
-snip-
But checking the type of a string returns "string". Did you mean peripheral.getType?
Oh.. Derp xD
Yeah, I'll edit the post above, Thanks! :)/>
Letherin #9
Posted 28 December 2013 - 02:37 AM
I should be able to replace a modem/cable with a wireless modem on the batbox ?

Also, I notice nothing happens when I right-click the wireless modems, unlike the wired ones.

table.insert( batboxes, batbox )

Did you mean nBox here? I don't see a reference to batbox anywhere ?

This is what I have so far - http://pastebin.com/ZScuw3fY

However, it outputs nothing to screen (perhaps I handled that wrong?)

There's a wireless modem both on a batbox and a CESU just for testing.

Thanks.
Edited on 28 December 2013 - 01:48 AM
theoriginalbit #10
Posted 28 December 2013 - 03:26 AM
I should be able to replace a modem/cable with a wireless modem on the batbox ?
No, only wired modems can act as peripheral handlers, wireless modems are only for wireless communications.
Letherin #11
Posted 28 December 2013 - 05:02 AM
argh, no wonder. Thanks for that bit of info.