The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Device::Power::Synaccess::NP05B -- Manage and monitor the Synaccess NP05B networked power strip

SYNOPSIS

my $np = Device::Power::Synaccess::NP05B->new(addr => '10.0.0.1');

# must initiate a connection and log in before issuing commands:
($ok, $err) = $np->connect;
($ok, $err) = $np->login;

# are we still connected?
$np->is_connected or die "whoops";

# get the status of the connection:
say $np->cond;

# get the on/off status of the power outlets:
($ok, $hashref) = $np->power_status;

# turn on outlet 2:
($ok, $err) = $np->power_set(2, 1)

# get the full system status, including network attributes:
($ok, $hashref) = $np->status;

# must log out cleanly or device can get confused:
($ok, $err) = $np->logout;

ABSTRACT

Synaccess makes a power strip product called the NP-05B which can be remotely accessed and controlled via telnet or http.

Device::Power::Synaccess::NP05B accesses the NP-05B via telnet and provides programmatic access to some of its functions, notably system status and turning on/off specific power outlets.

METHODS

new

my $np = Device::Power::Synaccess::NP05B->new();
my $np = Device::Power::Synaccess::NP05B->new(addr => '10.0.0.6', ...);

Instantiates an Device::Power::Synaccess object. It takes some optional named parameters:

  • addr => string

    Specify the IP address of the NP-05B device. Defaults to "192.168.1.100", which was the factory default of the device sold to me.

  • user => string

    Specify the login username. Defaults to "admin", which was the factory default of the device sold to me.

  • pass => string

    Specify the login password. Defaults to "admin", which was the factory default of the device sold to me.

A new NP05B object will have a condition of "disconnected".

connect

my ($ok, $err) = $np->connect;
die "connect: $err" unless ($ok eq 'OK');

Attempt to open a telnet connection to the NP-05B device. This must be done before attempting login or any other method.

After successful connection, the NP-05B object will have a condition of "connected".

Returns ('OK', '') on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

login

my ($ok, $err) = $np->login;

Attempt to log in to the NP-05B device. This must be done before attempting any other access or control methods.

Once successfully logged in, it is inadvisable to terminate the connection without first calling the logout method. The device can get into a sick state otherwise and misbehave in subsequent connections.

After successful login, the NP-05B object will have a condition of "authenticated".

Returns ('OK', '') on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

is_connected

say $np->is_connected ? "still connected" : "not connected";

Check the connection status. Returns 1 if NP-05B condition is "connected" or "authenticated", or 0 otherwise.

logout

my ($ok, $err) = $np->logout;

Needed to cleanly terminate the remote connection.

After successful logout, the NP05B object will have a condition of "disconnected", and further access will require calling connect and login.

Returns ('OK', '') on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

power_status

my ($ok, $hashref) = $np->power_status;

Retrieves the on/off status of the NP-05B device's power outlets in the form of a hashref which keys on the port number to either 0 (off) or 1 (on).

For instance, if ports 1 2 and 3 are on and ports 4 and 5 are off, $hashref will reference:

{1 => 1, 2 => 1, 3 => 1, 4 => 0, 5 => 0}

Returns ('OK', $hashref) on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

power_set

my ($ok, $hashref) = $np->power_set(3, 1);

Turns a specified NP-05B device's power outlet on or off. Its first parameter is the outlet number (1..5 on my device), and the second parameter is either 0 (to turn it off) or 1 (to turn it on).

Upon success, the returned $hashref is identical in format and semantics to the one returned by power_status.

Returns ('OK', $hashref) on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

status

my ($ok, $hashref) = $np->status;

Retrieves the full system status of the NP-05B device. The returned hashref is a bit complex:

{
  'src_ip' => '0.0.0.0',
  's_mask' => '255.255.0.0',
  'source' => 'static',
  'port_telnet' => '23',
  'port_http' => '80',
  'model' => 'NP-05B',
  'mask' => '255.255.0.0',
  'eth' => 'on',
  'ip' => '192.168.1.100',
  's_ip' => '192.168.1.100',
  's_gw' => '192.168.1.1',
  'mac' => '00:90:c2:12:34:56',
  'power_hr' => {
    '2' => 1,
    '5' => 1,
    '3' => 1,
    '1' => 1,
    '4' => 1
  },
  'gw' => '192.168.1.1'
}

Returns ('OK', $hashref) on success, or ('ERROR', $error_message) on failure, where $error_message is a short string describing the error (or, in some cases, the exception string thrown by Net::Telnet).

ACCESSORS

addr

my $address = $np->addr;
$np->addr = '10.0.0.6';

Get/set the addr attribute, which determines where connect will attempt to open a connection.

user

my $username = $np->user;
$np->addr = 'bob';

Get/set the user attribute, which must be correct for login to work.

pass

my $password = $np->pass;
$np->pass = 'sekrit';

Get/set the pass attribute, which must be correct for login to work.

cond

my $condition = $np->cond;
$np->addr = 'disconnected';

Get/set the cond attribute, which reflects the connectedness/authentication status of the object.

Setting this attribute yourself is not recommended.

CAVEATS

This module works for the specific device shipped to the author, and might not work for you if Synaccess changes the behavior of their product.

The NP-05B can misbehave in odd ways if commands are sent to it too quickly or if connections are not terminated cleanly. The module uses short delays which helps mitigate some of these problems. (Despite these problems, the NP-05B is pretty good value for the price.)

TO DO

  • Support commands for changing the NP-05B network configuration.

  • Improve the unit tests, which are a little shallow.

  • Support nonstandard port mapping.

SEE ALSO

App::np05bctl - a light CLI utility wrapping this module. Not distributed with Device::Power::Synaccess::NP05B to avoid spurious dependencies.

AUTHOR

TTK Ciar <ttk@ciar.org>

COPYRIGHT

You may use and distribute this module under the same terms as Perl itself.