Close

New and improved: Switchboard Clients

A project log for Switchboard: Distributed Automation made Easy

A small Python framework for the tinkerer to make distributed automation as simple as possible

josefschneiderjosefschneider 05/09/2017 at 19:220 Comments

Writing a Switchboard client has gotten even easier. The ClientApp class takes care of parsing application arguments and starting the server. "--client_port" is now a mandatory argument, and specifies the port on which the client is listening. As an example, it is now possible to create a client that publishes CPU and memory usage, all within 15 lines:

#!/usr/bin/env python
import psutil
from client_app import ClientApp
from switchboard_client.client import SwitchboardInputDevice

def main():
    app = ClientApp()
    app.add_device(SwitchboardInputDevice('cpu_usage.i', lambda: psutil.cpu_percent()))
    app.add_device(SwitchboardInputDevice('memory_usage.i', lambda: psutil.virtual_memory().percent))
    app.run()

if __name__ == "__main__":
    main()

The latest code changes include a swbc_system_info app very similar to the example. It becomes available as a terminal application after installing Switchboard. To run it type

swb_system_info --client_port <port_number>
where <port_numer> is the port you want the client to listen on. Because the client data is made available through a simple HTTP get request you can see part of its interface in a browser. Open a tab and enter the following address:
localhost:<port_number>/devices_info

This displays the devices that are available from this Switchboard client. To get the values have a look at:

localhost:<port_number>/devices_value

The best thing about ClientApps is that it provides all clients with common functionality we'll be exploiting very shortly!

Discussions