├── .gitignore ├── LICENSE ├── README.md ├── app.js ├── examples └── devices.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Amanpreet Singh 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Wemo Emulation Server 2 | Emulates multiple devices using the ["Wemore"](https://github.com/dhleong/wemore) Library on RaspberryPi (or anything running Node.Js) as Wemo Switches and executes commands described in the configuration. 3 | 4 | ```javascript 5 | // Example Configuration 6 | { 7 | "Terminal A":{ 8 | "oncommand": "/bin/chvt 1" 9 | }, 10 | 11 | "Display": { 12 | "oncommand" : "/opt/vc/bin/tvservice -p", 13 | "offcommand" : "/opt/vc/bin/tvservice -o" 14 | } 15 | 16 | } 17 | ``` 18 | 19 | ## Installation 20 | ```bash 21 | # Install Globally 22 | sudo npm install alexa-raspberry -g 23 | ``` 24 | 25 | ## Execution 26 | ```bash 27 | # Place devices.json in the execution path 28 | alexa-raspberry 29 | ``` 30 | or 31 | 32 | ```bash 33 | # Provide file path as 34 | alexa-raspberry ./examples/devices.json 35 | alexa-raspberry /etc/wemo/devices.json 36 | ``` 37 | 38 | 39 | ## Usage 40 | Use the alexa app on your cell phone/tablet to search for devices 41 | 42 | ``` 43 | Alexa search for devices 44 | Alexa turn on 'Display' 45 | ``` 46 | 47 | ## Start as a service on RaspberryPi 48 | Use your favorite text editor to create the systemd config file and devices.json file 49 | 50 | ```bash 51 | #@file: /etc/systemd/system/alexaraspberry.service 52 | 53 | [Unit] 54 | Description=Wemo emulation server for Alexa 55 | After=network.target 56 | 57 | [Service] 58 | ExecStart=/usr/bin/alexa-raspberry /etc/alexa/devices.json 59 | Restart=always 60 | RestartSec=10 61 | StandardOutput=syslog 62 | StandardError=syslog 63 | SyslogIdentifier=Alexa-Raspberry 64 | 65 | [Install] 66 | WantedBy=multi-user.target 67 | 68 | ``` 69 | 70 | ```bash 71 | #Enable the service 72 | systemctl enable alexaraspberry.service 73 | #Start the service 74 | systemctl start alexaraspberry.service 75 | ``` 76 | 77 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /* 4 | * @Author: Amanpreet Singh 5 | * @Email : Amanpreet.dev@gmail.com 6 | */ 7 | 8 | var wemore = require('wemore'); 9 | var exec = require('child_process').exec; 10 | var path = require('path'); 11 | 12 | var deviceRefs = [], i = 0; 13 | 14 | var loadDevices = function( devices ){ 15 | 16 | for( var device in devices ){ 17 | var dev = wemore.Emulate({ friendlyName: device, port: 9000 + i++ }); 18 | var devprop = devices[device]; 19 | 20 | dev.on( 'listening', (function(device, devprop) { 21 | return function(){ 22 | console.log( device + " Now online, Now listening on", this.port ); 23 | } 24 | })(device, devprop)); 25 | 26 | dev.on('on', (function(device, devprop) { 27 | var command = devprop.oncommand; 28 | return function(){ 29 | console.log("Device", device, "recieved on" ); 30 | if( command ){ 31 | console.log( 'Executing command "', command, '"' ); 32 | exec( command, function(error, stdout, stderr) { 33 | //console.log( arguments ); 34 | }); 35 | } 36 | } 37 | })(device, devprop)); 38 | 39 | dev.on('off', (function(device, devprop) { 40 | var command = devprop.offcommand; 41 | return function(){ 42 | console.log("Device", device, "recieved off" ); 43 | if( command ){ 44 | console.log( 'Executing command "', command, '"'); 45 | exec( command, function(error, stdout, stderr) { 46 | //console.log( arguments ); 47 | }); 48 | } 49 | } 50 | })(device, devprop)); 51 | } 52 | 53 | }; 54 | 55 | var ProjectRoot = process.cwd(); 56 | var loadWemoEmulation = function( args ){ 57 | var configpath = ProjectRoot + "/devices.json"; 58 | if( args.length > 0 ){ configpath = path.resolve(args[0]); } 59 | try{ 60 | console.log("Loading config file: '" + configpath + "'" ); 61 | var deviceFile = require( configpath ); 62 | loadDevices( deviceFile ); 63 | }catch(e){ 64 | console.log("Error:", e.message ); 65 | console.log("Invalid Device Configuration File! Exiting.."); 66 | } 67 | }; 68 | 69 | var args = process.argv.slice(2); 70 | loadWemoEmulation( args ); -------------------------------------------------------------------------------- /examples/devices.json: -------------------------------------------------------------------------------- 1 | { 2 | "Terminal A":{ 3 | "oncommand": "/bin/chvt 1" 4 | }, 5 | 6 | "Terminal B": { 7 | "oncommand": "/bin/chvt 2" 8 | }, 9 | 10 | "Terminal C": { 11 | "oncommand": "/bin/chvt 3" 12 | }, 13 | 14 | "Display": { 15 | "oncommand" : "/opt/vc/bin/tvservice -p", 16 | "offcommand" : "/opt/vc/bin/tvservice -o" 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alexa-raspberry", 3 | "version": "0.1.3", 4 | "description": "Emulates devices on Raspberry Pi and accepts commands", 5 | "main": "app.js", 6 | "dependencies": { 7 | "wemore": "^0.3.0" 8 | }, 9 | "devDependencies": {}, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/amansx/alexa-raspberry.git" 16 | }, 17 | "author": "Amanpreet Singh", 18 | "license": "MIT", 19 | "bin": { 20 | "alexa-raspberry": "app.js" 21 | }, 22 | "bugs": { 23 | "url": "https://github.com/amansx/alexa-raspberry/issues" 24 | }, 25 | "homepage": "https://github.com/amansx/alexa-raspberry#readme" 26 | } 27 | --------------------------------------------------------------------------------