The NetSwitch Class

This application interfaces to network accessible Raritan power strips. It is capable of both reading data from them and controlling their outlets. Switches are automatically logged into as read only when declared in the ini file. Switches can be added or replaced using the login_to_switch command, in which a switch interface can be granted write permissions. When a switch is logged into with write permissions, a password is returned. This password must then be used for all subsequent write operations.

Configuration parameters (in self.config either via defaults, command line or config.ini file):
  • service (str, optional, default = NetSwitch) Service name for advertiser
  • device_mode (bool, optional, default = True) Operate in device mode
  • telemetry_loop_delay (float, optional, default = 10.0) Delay between telemetry updates
  • username (str, optional) The username to use with all network controlled switch
  • userpassword (str, optional) The password to use with all network controlled switch
  • switches (list, required) List of switches controlled by this application
Switch configuration (from the .ini file for each switch)
  • address (str, required): IP addresses of the network controlled switch
  • username (str, optional): The username to use with the switch
  • userpassword (str, optional): The password to use with the switch
For example, a netswitch role starting with two switches would be as follows:

[[NS]]
product = NetSwitch
application_name = NetSwitch
switches = TEST1, TEST2,
username = observer_general
userpassword = password_general
[[[TEST1]]]
address = 192.168.1.1
username = observer_specific
userpassword = password_specific
userwriter = True
[[[TEST2]]]
address = 192.168.1.2

If no username or no userpassword is specified in the configuration file or on the command line, they are taken instead from environment variables, specifically DOS_NETSWITCH_USER and DOS_NETSWITCH_PASS.

class NetSwitch.NetSwitch(device_mode=False, noSVE=None, **extra_args)

A class to control the DESI network controlled (power) switches. It can contain interfaces for multiple switches. Each switch is give a name in the application, and each outlet on the switch is given a name as well. These are used to reference specific switches and outlets.

commands = ['configure', 'set_outlets', 'get', 'reset_names', 'login_to_switch', 'is_alive', 'set']

PML accessible commands

defaults = {'constants_version': 'DEFAULT', 'controller_type': 'HARDWARE', 'device_mode': False, 'service': 'NetSwitch', 'switches': [], 'telemetry_loop_delay': 2, 'update_telemetry': True, 'username': None, 'userpassword': None, 'userwriter': False}

Default configuration paramters

init()

The init function for the NetSwitch application. It may take a minute or two to complete if many switches must be set up.

configure(constants='DEFAULT')

Restores the system to a known state. This includes the constants specified, and uses the default set if none are specified. Does not changes what switches are connected, or what authentication level they are connected at.

Keyword Arguments:
 constants (str,optional) – The set of constants to use.
Raises:RuntimeError – Failed to configure correctly
set_outlets(switch, write_pass, *args, **kwargs)

Set outlets to the requested state. State can be ON or OFF. Multiple outlets on the same switch can be passed at a time.

Example: Setting outlets named ‘Coffee’ and ‘Lightbulb’ on, and ‘Clock’ off for switch TESTSWITCH. The write_pass is obtained when logging into the switch with write permissions. See NetSwitch.NetSwitch.login_to_switch:

>>> ns.set_outlets('TESTSWITCH', 'somepassword', Coffee='ON', Lightbulb='ON', Clock='OFF')
Keyword Arguments:
 
  • switch – The switch to toggle outlets on
  • write_pass – The password for write privilges
  • **kwargs – outlets and their new values, such as Petals=’OFF’.
Raises:

RuntimeError – Unable to complete the setting of switches

Returns str:

SUCCESS

is_alive(switch)

Checks if a switch is still responding to commands. Returns True if it responds.

Raises:RuntimeError – Unknown switch.
Returns:True/False
set(*args, **kwargs)

Standard set function.

Keyword Arguments:
 
  • **kwargs – A key/value pair to set. Keys can be delay_time or updating.
  • **kwargs – outlets and their new values, such as delay_time=2.0.
Raises:

RuntimeError – Invalid entry.

Returns str:

SUCCESS

get(*args, **kwargs)
Return status and configuration information. The availible information is:
  • version - The application version.
  • status - The status of the application.
  • delay_time - The delay time between telemetry updates.
  • updating - Whether or not telemetry updates are being run.
  • peripherals <switch> - A dictionary of peripherals for the switch. Each peripheral has a dictionary of alarm latches, the value, and the slot.
  • switch_info <switch> - A dictionary of information about the switch, including username, writer access, address, and outlet values.
  • switch_status <switch/optional> - Returns either a dictionary of switch dictionaries keyed by names, or the single specified one. The dictionaries contain the current outlet statuses.
  • outlet_name <switch> <slot/optional> - Returns the switches outlet names as a list, or a specific name as a string if the slot is given.
  • switches - A list of the switches.
Parameters:*args (str) – The field to return.
Raises:RuntimeError – Unable to return request.
Returns:The value requested.
get_peripheral(switch, name)

Get the value of a peripheral.

Keyword Arguments:
 
  • switch (str) – The name of the switch the peripheral is attached to.
  • name (str) – The name of the peripheral (may include spaces).
Raises:

RuntimeError – Unable to get peripheral value.

Returns:

The value of the peripheral.

get_switch_status(switches='ALL')

Get the state of all or selected switches’s outlets.

Keyword Arguments:
 switches (str/list,default='ALL') – The argument can be ALL, a switch name or a list of switches.
Raises:RuntimeError – Unable to complete request.
Returns dict:A dictionary of switch states keyed by switch name. The dictionaries representing the states are keyed by outlet.
get_outlet_name(switch, outlet=None)

Get outlet names for the selected switch (from the switches dictionary).

Keyword Arguments:
 
  • switch (str) – The switch to check.
  • outlet (int,default=None) – The outlet to get the name of. If None, return list of outlet names.
Raises:

RuntimeError – Unable to find outlet or switch.

Returns str/list:
 

The outlet name(s).

reset_names(sw)

Set the controller’s outlet and peripheral names to match the switches.

Keyword Arguments:
 sw (str) – The name of the switch to reset names for.
Raises:RuntimeError – Unable to complete requests.
Returns str:SUCCESS.
login_to_switch(switchname, address, username=None, userpassword=None, writer=False, startup_state='KEEP')

Login to a new switch. Will overwrite any switches with the same name. Will delete any switches with the same address.

For example, to login to a new switch at 192.168.1.1 and name it READER1 using the default username and password::
>>> ns.login_to_switch('READER1', '192.168.1.1')
To use a specific username and password::
>>> ns.login_to_switch('READER1', '192.168.1.1', 'observer', 'observer_pass')
To login as a writer, name it WRITER1, and keep all outlets in their current state::
>>> write_password = ns.login_to_switch('WRITER1', '192.168.1.1', 'writer_user', 'writer_pass', writer=True)
To login as a writer, name it WRITER1, and set some new outlet states::
>>> write_password = ns.login_to_switch('WRITER1', '192.168.1.1', 'writer_user', 'writer_pass', writer=True,
                                        outlet_states=['ON', 'OFF', 'ON', 'OFF', 'ON', 'OFF', 'ON', 'OFF'])
Keyword Arguments:
 
  • switchname (str) – The name of the new switch.
  • address (str) – The address of the switch.
  • username (str,default=None) – The username to login to the switch with. If none, uses default.
  • userpassword (str,default=None) – The password matching the login username. If none, uses default.
  • writer (bool) – Whether or not this switch has write permissions to the outlets (must match user on the switch).
  • startup_state (list/str) – ON, OFF, KEEP, or a list of these values the same as the number of outlets. This state will be applied to the switch’s outlets when first connected.
Raises:

RuntimeError – Unable to complete login fully

Returns str:

The writer password if the switch has write permissions, otherwise SUCCESS.

about_to_connect_to_instance(*args, **kwargs)

To be overridden by the user so that any actions can be taken when the device application is about to connect to an instance

did_connect_to_instance(*args, **kwargs)

To be overridden by the user so that any actions can be taken when the device application connects to an instance

about_to_disconnect_from_instance(*args, **kwargs)

To be overridden by the user so that any actions can be taken when the device application is about to disconnect from an instance

did_disconnect_from_instance(*args, **kwargs)

To be overridden by the user so that any actions can be taken when the device application disconnects from an instance