Giter Site home page Giter Site logo

weectrl's Introduction

weectrl License: MIT License

A cross platform library and application for controlling Belkin WeMo switches and Sockets. Written in Rust.

WeeApp, the application

Screenshots

Screenshot-macOS info Screenshot-Windows Screenshot-Linux

Functionality

The "WeeApp" application will scan the local network for Belkin WeMo devices and list what's found. They can be switched on/off from the app and if a device changes state due to external activity (e.g. physical toggle or schedule) this will be reflected in the app UI due to the notification feature.

Searching for new devices can take 5-6 seconds but the app benefits from the caching feature of the library so previously known devices will be displayed very quickly upon restart.

  • The "paper basket" will forget all devices and erase the disk cache.
  • The reload button will load known devices from cache and rescan the network.
  • The application remembers size and position of its window.

Platforms

Current version tested on Windows 11, Linux and maOS x84_64.

Building

The (example) App has certain FLTK_rs dependencies.

cargo build --release --example weeapp

Windows addendum

An application icon can be found in examples/images/icon.ico. To insert it into the Windows binary use rcedit:

rcedit target\release\examples\weeapp.exe --set-icon examples\images\icon.ico

weectrl, the library

Functionality

  • Discover devices on the network.
  • Retrieve detailed device information.
  • Switch devices on or off.
  • Cache known devices on disk for quick reload.
  • Subscription to state change notifications from devices if they're toggled.
  • Uses the Tracing crate for logging.

API examples

Initialization and discovery

Create new instance of controller:

use weectrl::{DeviceInfo, DiscoveryMode, State, StateNotification, WeeController};

let controller = WeeController::new();

To discover devices on network or/and in cache. starting by reading list of known devices from the disk cache, then broadcasts network query:

let rx: std::sync::mpsc::Receiver<DeviceInfo> = controller.discover(
    DiscoveryMode::CacheAndBroadcast, 
    true, 
    Duration::from_secs(5), 
    );

Scans both disk cache file and network, will "forget" in-memory list first. Give network devices maximum 5 seconds to respond. When discovery ends the channel will be closed.

Futures version of the discover function.

let notifications: futures::channel::mpsc::UnboundedReceiver<DeviceInfo> = controller.discover_future(
    DiscoveryMode::CacheAndBroadcast, 
    true, 
    Duration::from_secs(5), 
    );

Switching and notifications

Let unique_id be a DeviceInfo::unique_id returned by previous discovery.

Starting listening for notifications from subscribed devices. If called several times only the most recent rx channel will receive notifications:

let rx: std::sync::mpsc::Receiver<StateNotification> = controller.notify();

Whenever a switch is toggled a message will appear on the Receiver.

Futures version.

let notifications_fut: futures::channel::mpsc::UnboundedReceiver<StateNotification> = controller.notify_future();

Subscribe to notifications for a device and instruct WeeController to automatically refresh subscriptions every 120 seconds before they expire. As this causes a network request to be made to the switch a 5 second network timeout is set for the request in case the switch doesn't respond:

let result = controller.subscribe(&unique_id, Duration::from_secs(120), true, Duration::from_secs(5));

Unsubscribe from notifications from one device:

let result = controller.unsubscribe(&unique_id, Duration::from_secs(5));

Unsubscribe from all notifications:

controller.unsubscribe_all();

To toggle a switch (on or off):

let result = controller.set_binary_state(unique_id, State::On, Duration::from_secs(5));
 match result {
    Ok(state) => println!("Ok({:?})", state),
    Err(err) => println!("Err({:?})", err),
}

To get a switch state:

let result = controller.get_binary_state(unique_id, Duration::from_secs(5));
 match result {
    Ok(state) => println!("Ok({:?})", state),
    Err(err) => println!("Err({:?})", err),
}

Retrive icons stored in a switch:

let list: Result<Vec<Icon>, Error> = controller.get_icons(unique_id, Duration::from_secs(5));

Retrieve structure with information stored about a switch, from the controller. This is the same structure returned earlier during Discovery:

let result = controller.get_info(unique_id);

Compatibility

Theoretically supports and device advertising the urn:Belkin:service:basicevent:1 service. Tested with Lightswitch and Socket. Currently only tested with Belkin WeMo LightSwitch and Socket.

License

Licensed under either of

weectrl's People

Contributors

hyperchaotic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

weectrl's Issues

Hyper update

Move to new Hyper (currently on master) once released. Use OS assigned port number instead of hard coded, for notification listener and set much shorter TCP connect timeout for probing previously known devices during discovery.

Ubuntu Install & GLIBC 2.38 not found

Hello all, I'm writing this issue with the hope of saving others time. I was unable to run the program under Ubuntu 23.04 due to 'GLIBC_2.38' not being found. sudo apt install libc6 only provided GLIBC 2.37. Online forum recommended against forcing a 2.38 install, stating it's better practice to update the OS to 23.10 if version 2.38 is needed. So a quick do-release-upgrade command and 15 minutes later I was on Ubuntu 23.10. As promised all now works without an issue. For the noob like me take the following step.

  • Download & unzip weectrl

  • In the terminal app, move into the directory that was created when you unzipped the filed. Mine was in the Download folder.
    cd Download/weeapp-2.0.2-x86_64-linux/

  • Give the weeapp file the needed permissions.
    cdmod -x weeapp

  • Run the program.
    ./weeapp -h

  • Done, my wemo smart plug was recognize immediately.

Thank you Hyperchaotic.

Only supports WeMo Lightswitch and Socket.

Would be nice to support more devices.

Edit: in theory supports "binaryevents" of any device offering the urn:Belkin:service:basicevent:1 service, but only tested on the two mentioned. Would be nice to support other type devices with more functions.

Example app Display scaling

The fltk_rs toolkit have an issue where it forgets the display scaling when the window have been hidden. However if trying to set the scaling to the chosen value on the Window Show message the UI deadlocks.

This can also cause the app to continue running even if the UI has exited, making it necessary to kill the application process in the Task Manager.

When expanding the app window, the list of Wemo's stays very small

If you have more than 6 Wemo plugs, and you drag the app window down to expand it, the list of additional wemos does not expand.

The list of only the first 5-6 wemos stays static and you still need to scroll down within the windows even thought the app window has enough room to display them all.

Thanks for this app!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.