Created
February 18, 2018 03:36
-
-
Save kumamotone/02c641862c6e12acb9b0fa5675d05d06 to your computer and use it in GitHub Desktop.
rmminiで家電を操作
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var firebase = require("firebase"); | |
var config = { | |
apiKey: "秘密", | |
authDomain: "秘密.firebaseapp.com", | |
databaseURL: "https://秘密.firebaseio.com", | |
projectId: "秘密", | |
storageBucket: "", | |
messagingSenderId: "秘密" | |
}; | |
firebase.initializeApp(config); | |
//jsonからvalueに一致する値取得 | |
const getJsonData = (value, json) => { | |
for (var word in json) if (value == word) return json[word] | |
return json["default"] | |
} | |
//RM-mini3 | |
const broadlink = require("./getDevice") | |
const rmlist = require("./rmlist") | |
//RM mini3 Device Set | |
const rmMac = "秘密" | |
let rm = {} | |
const timer = setInterval(function() { | |
rm = broadlink({host: rmMac}) | |
if (rm) {clearInterval(timer)} | |
}, 100) | |
//RM mini3 ir send | |
const rmSend = (command) => { | |
const hexDataBuffer = new Buffer(rmlist[command], "hex") | |
rm.sendData(hexDataBuffer) | |
} | |
//database更新時 | |
const path = "/googlehome" | |
const key = "word" | |
const db = firebase.database() | |
db.ref(path).set({[key]: ""}) | |
db.ref(path).on("value", function(changedSnapshot) { | |
//値取得 | |
const value = changedSnapshot.child(key).val() | |
if (value) { | |
console.log(value) | |
//コマンド生成 | |
const command = getJsonData(value.split(" ")[0], { | |
// light | |
"light": () => { | |
let index = 1 | |
if (value.split(" ")[index] == "の" || value.split(" ")[index] == "を") index++ | |
const command = getJsonData(value.split(" ")[index], { | |
"つけ": "light_on", | |
"オン": "light_on", | |
"消し": "light_off", | |
"けし": "light_off", | |
"オフ": "light_off", | |
"default": false | |
}) | |
return command ? () => rmSend(command) : command | |
}, | |
"aircon": () => { | |
let index = 1 | |
if (value.split(" ")[index] == "の" || value.split(" ")[index] == "を") index++ | |
const command = getJsonData(value.split(" ")[index], { | |
"暖房": "heat", | |
"冷房": "cool", | |
"除湿": "dehumid", | |
"温度": getJsonData(value.split(" ")[index+1], { | |
"上げ": "up", | |
"下げ": "down", | |
"default": false | |
}), | |
"つけ": "heat", | |
"上げ": "up", | |
"下げ": "down", | |
"消し": "aircon_off", | |
"けし": "aircon_off", | |
"止め": "aircon_off", | |
"とめ": "aircon_off", | |
"停止": "aircon_off", | |
"default": false | |
}) | |
return command ? () => rmSend(command) : command | |
}, | |
"imhome": () =>"imhome", | |
"imgoing": () =>"imgoing", | |
"oyasumi": () =>"oyasumi", | |
//default | |
"default": () => false, | |
})() | |
// console.log(command) | |
//コマンド実行 | |
if (command) { | |
//typeof | |
if (typeof command === "string") { | |
if (command == "imhome") { | |
rmSend("heat") | |
rmSend("light_on") | |
console.log("imhome") | |
} | |
if (command == "imgoing") { | |
rmSend("aircon_off") | |
rmSend("light_off") | |
console.log("imgoing") | |
} | |
if (command == "oyasumi") { | |
rmSend("aircon_off") | |
rmSend("light_off") | |
rmSend("on7hour") | |
console.log("oyasumi") | |
} | |
} else if (typeof command === "function") { | |
command() | |
} | |
//firebase clear | |
db.ref(path).set({[key]: ""}) | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment