-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gkca
committed
Feb 28, 2019
1 parent
5ca14cf
commit 69d2be9
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "Raspberry-Relay-Control", | ||
"version": "1.0.0", | ||
"description": "Raspberry Pi Code", | ||
"main": "server.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node server.js" | ||
}, | ||
"author": "AG & SA", | ||
"license": "ISC", | ||
"dependencies": { | ||
"firebase-admin": "^6.4.0" | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Firebase Admin SDK Configuration */ | ||
var admin = require("firebase-admin"); | ||
var fs = require('fs'); | ||
|
||
const ACTIVATE = "activate"; | ||
const DEACTIVATE = "deactivate"; | ||
const RELAY = "number"; | ||
|
||
const PATH_TO_RELAYS = { | ||
1: "/sys/class/gpio/gpio23/value", | ||
2: "/sys/class/gpio/gpio24/value" | ||
}; | ||
|
||
var serviceAccount = require("<PATH to Your serviceAccountKey.json File>"); | ||
|
||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
databaseURL: "<PATH to Your Database URL>" | ||
}); | ||
|
||
// Database Init | ||
var defaultDatabase = admin.database(); | ||
var ref = defaultDatabase.ref(); | ||
|
||
// You can retrieve services via the defaultApp variable... | ||
var defaultDatabase = admin.database(); | ||
var ref = defaultDatabase.ref(); | ||
|
||
entitiesFromAssistant = {} | ||
|
||
var ref = defaultDatabase.ref(); | ||
|
||
ref.on('value', function (snapshot) { | ||
snapshot.forEach(function (childSnapshot) { | ||
|
||
var key = childSnapshot.key; | ||
var val = childSnapshot.val(); | ||
|
||
entitiesFromAssistant[key] = val; | ||
|
||
}); // snapshot.forEach(function (childSnapshot) | ||
|
||
console.log(entitiesFromAssistant); | ||
|
||
Object.entries(PATH_TO_RELAYS).forEach(([relay, path]) => { | ||
if (relay == entitiesFromAssistant[RELAY]) { | ||
if (entitiesFromAssistant[ACTIVATE]) { | ||
fs.writeFileSync(path, entitiesFromAssistant[ACTIVATE], function (err) { | ||
if (err) throw err; | ||
}); // fs.writeFileSync | ||
} else { | ||
fs.writeFileSync(path, entitiesFromAssistant[DEACTIVATE], function (err) { | ||
if (err) throw err; | ||
}); // fs.writeFileSync | ||
} // else | ||
} // if (relay == entitiesFromAssistant[RELAY]) | ||
}); // Object.entries(PATH_TO_RELAYS).forEach | ||
}); // ref.on('value', function (snapshot) |