Controlling Belkin WeMo Switches from Java

A key part of building reliable software is handling the unexpected gracefully—be it invalid input, failing services, or infrastructure failures like power outages. Most bad situations can be simulated with unit tests, fault injection or integration tests, but how do you test that your system preserves data and recovers gracefully in the face of a power outage? Killing your service forcibly doesn’t really do it because your server’s drives will still have power to flush their buffers. The only way to really test how you do in a power outage is to somehow kill power to the server. But how to do so programmatically without combining Twilio and an intern?

belkin_insight_switch_resizedOne simple, cost-effective way is with Belkin WeMo insight switches ($59 from Amazon). Each switch exposes a series of UPnP services via WiFi that can turn the switch on and off, measure power usage, and query usage history. This means that with our simple wrapper library you can do things like simulate a power failure during a sensitive operation, examine the impact of periodic failures on a workload and measure power draw during demanding workloads.

First you need to configure the switches. Unfortunately, this part can be frustrating and time consuming. The step-by-step instructions Belkin’s iPhone app provides are accurate but they fail to mention that the WiFi network broadcast by the switch is extremely weak and difficult to connect to. Expect to need multiple attempts to configure each switch. Once you succeed in configuring the switch however, no more fiddling or resets will be needed. Also, definitely be aware of the security impact. The access control features of Belkin’s WeMo switches are minimal at best. Disable all remote access and configure your firewall to prevent the Belkin switches from sending or receiving traffic outside your network.

Before hopping into code you’ll want to give your switches useful names using the iPhone or Android app. For example, you might have “Test Server 1″ and “Test Server 2″. This will make code that uses the switches easier to understand.

Now things get easier. Add the wrapper library to your maven pom.xml:

<repository>
  <id>maven-snapshots<id>
  <name>Maven Snapshots<name>
  <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
  <snapshots><enabled>true</enabled></snapshots>
</repository>
...
<dependency>
  <groupId>com.palominolabs.wemo</groupId>
  <artifactId>wemo</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

From there you can search for switches and give them commands. For example, if you had a switch named “Floodlight” this code would start a disco rave:

        String switchFriendlyName = "Floodlight";
        try (InsightSwitchFinder insightSwitchFinder = new InsightSwitchFinder(switchFriendlyName)) {
            if (!insightSwitchFinder.findSwitches()) {
                throw new IllegalStateException("Unable to find switches");
            }

            InsightSwitch insightSwitch = insightSwitchFinder.getSwitch(switchFriendlyName);

            while (true) {
                insightSwitch.switchOn();
                Thread.sleep(100);
                insightSwitch.switchOff();
                Thread.sleep(100);
            }
        }

Once you’ve got Java controlling your switches you can do a number of interesting things beyond simulating hard power failures. You could implement Netflix-style chaos monkey behavior for your lab. Or you could measure power usage during benchmarks to calculate metrics like performance-per-watt.

Posted by Manuel Wudka-Robles

Manuel is a sponge for software knowledge. Manuel’s software development expertise ranges from Rails web development to obscure 3rd party APIs and long-forgotten web properties. Manuel was the “API guy” at Genius.com, recognized for his deep knowledge of how to build and scale APIs. At Turn, Manuel focused on improving the exciting world of display advertising. Most recently at Tello, Manuel led the integration with Twilio and KISSMetrics. At Palomino Labs, Manuel also serves as Director of IE Compatibility.

About Palomino Labs

Palomino Labs unlocks the potential of software to change people and industries. Our team of experienced software developers, designers, and product strategists can help turn any idea into reality.

See the Palomino Labs website for more information, or send us an email and let's start talking about how we can work together.