Created
March 27, 2018 03:50
-
-
Save kwarodom/f26b66934e656c3c3fe2364e8480df31 to your computer and use it in GitHub Desktop.
javascript to sync firebase database
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
//current energy consumption update picture | |
function update_member(status) { | |
$("#member").text(String(status)); | |
} | |
$( document ).ready(function() { | |
console.log("starting document!!!!"); | |
// Initialize Firebase | |
console.log("Initialize Firebase"); | |
var config = { | |
apiKey: "", | |
authDomain: "", | |
databaseURL: "", | |
projectId: "", | |
storageBucket: "", | |
messagingSenderId: "" | |
}; | |
firebase.initializeApp(config); | |
var ref = firebase.database().ref(); | |
ref.on("value", function(snapshot) { | |
console.log(snapshot.val().test); | |
x = snapshot.val().test; | |
}, function (error) { | |
console.log("Error: " + error.code); | |
}); | |
total_load_activePower = 0; | |
var homeRef = firebase.database().ref("test"); | |
homeRef.on("child_changed", function(data) { | |
console.log(data.key); | |
console.log(data.val()); | |
if(data.key == "1PV221445K1200100") { | |
total_load_activePower = data.val().load_activePower; | |
} else if (data.key == 'node1') { | |
update_member(parseInt(data.val())); | |
} else { | |
console.log("need to parse this key " + data.key) | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment