├── .gitignore ├── README.md ├── bin ├── switch └── switchbot ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | package-lock.json 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SwitchBot 2 | [SwitchBot](https://www.switch-bot.com/) api for Node.js (unofficial) 3 | 4 | # Usabe 5 | 6 | ``` 7 | const Switchbot = require('switchbot'); 8 | const switchbot = Switchbot('bluetooth address'); 9 | switchbot.turnOn(); 10 | switchbot.turnOff(); 11 | switchbot.press(); 12 | ``` 13 | 14 | # Depends 15 | gatttool is required 16 | 17 | # License 18 | Apache-2.0 19 | -------------------------------------------------------------------------------- /bin/switch: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | const Switchbot = require('../index'); 4 | const switchbot = Switchbot(process.argv[2]); 5 | switchbot[process.argv[3] || "press"](); 6 | 7 | -------------------------------------------------------------------------------- /bin/switchbot: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 'use strict'; 3 | const Switchbot = require('../index'); 4 | const switchbot = Switchbot(process.argv[2]); 5 | switchbot[process.argv[3] || "press"](); 6 | 7 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const spawn = require('child_process').spawn; 2 | const trigger = (triggerCommand, address) => { 3 | const command = { 4 | turnOn: "570101", 5 | turnOff: "570102", 6 | press: "570100" 7 | } 8 | return new Promise((resolve, reject) => { 9 | if (!command[triggerCommand]) { 10 | reject("no such command [" + triggerCommand + "]"); 11 | return; 12 | } 13 | if (!triggerCommand) { 14 | triggerCommand = "press" 15 | } 16 | const gatttool = spawn('gatttool', ['-b', address, '-t', 'random', '-I']) 17 | var connectSended = false; 18 | 19 | gatttool.stdout.on('data', (data) => { 20 | if (!connectSended && !data.toString().startsWith("connect")) { 21 | gatttool.stdin.write("connect\n") 22 | connectSended = true; 23 | } else if (data.indexOf("Connection successful") >= 0) { 24 | gatttool.stdin.write("char-write-cmd 0x0016 " + command[triggerCommand] + "\n") 25 | setTimeout(() => { 26 | gatttool.kill(); 27 | resolve(); 28 | }, 1000); 29 | } else if (data.indexOf("Error") >= 0) { 30 | gatttool.kill(); 31 | reject(); 32 | } 33 | }); 34 | gatttool.stderr.on('data', (data) => { 35 | gatttool.kill(); 36 | reject(); 37 | }); 38 | }); 39 | } 40 | module.exports = (address) => { 41 | return { 42 | "turnOn" : () => { 43 | return trigger("turnOn", address); 44 | }, 45 | "turnOff" : () => { 46 | return trigger("turnOff", address); 47 | }, 48 | "press" : () => { 49 | return trigger("press", address); 50 | } 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "switchbot", 3 | "version": "0.0.4", 4 | "description": "switchbot controller (unofficeal)\nrequires gatttool", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": ["switchbot"], 10 | "author": "ukiuni", 11 | "license": "Apache-2.0", 12 | "bin": { 13 | "switchbot": "bin/switchbot" 14 | }, 15 | "bugs": { 16 | "url": "https://github.com/ukiuni/Switchbot/issues" 17 | }, 18 | "homepage": "https://github.com/ukiuni/Switchbot", 19 | "repository": { 20 | "type": "git", 21 | "url": "git+https://github.com/ukiuni/Switchbot.git" 22 | } 23 | } 24 | --------------------------------------------------------------------------------