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

Collect IDs of all peripherals on a network

Started by nirkbirk, 29 August 2013 - 04:03 AM
nirkbirk #1
Posted 29 August 2013 - 06:03 AM
Title: Collect IDs of all peripherals on a network

Hi,
I've written some automated power plant monitoring software that connects to a battery of redstone energy cells (that function much like batboxes).

The software works fine, but I'm trying to make it more adaptable. At the moment, I have to manually enter all of the energy cell IDs (redstone_energy_cell_0, etc.) and then it takes those and continues with the program. What I'd like it to do is scan the network for these blocks automatically when the program starts. Is this even possible?

I'm aware that I could set up a computer terminal on each of the energy cells and connect through a modem and send out broadcasts to ping each one, but I'd rather connect directly to the cells, to keep things compact and as simple as possible.

Any help would be greatly appreciated.

Edit:

Oh, I managed to fix it myself.
If Anyone is interested I used the peripheral.getNames() function and rattled each name into a table.

Cheers.
KaoS #2
Posted 29 August 2013 - 08:00 AM
peripheral.getNames() returns a table of all peripherals connected to the computer. peripheral.call(sSide,"getNames") with the side of a modem will search for all peripherals linked to that modem alone
nirkbirk #3
Posted 29 August 2013 - 10:57 AM
Yeah, I ended up using the following code in case anyone needs to do something similar:

for _,name in ipairs(peripheral.getNames()) do
  if peripheral.getType(name) == "redstone_energy_cell" then
    cells[num] = {
    ["id"] = name,
    ["cellName"] = "cell0"..num,
    ["energy"] = 0,
    ["cap"] = net.callRemote(name, "getMaxEnergyStored")
    }
    totalCapacity = totalCapacity + cells[num]["cap"]
    num = num + 1
  end
end