PHP_SMART_HOME_V3/app/templates/js/script.js

304 lines
8.9 KiB
JavaScript
Raw Normal View History

var pending = false;
2019-10-08 09:12:47 +00:00
var firebaseConfig = {
apiKey: "AIzaSyBFZjXvnCMpGurSWEuVgHkE9jD9jxGJhx8",
authDomain: "test-push-notf.firebaseapp.com",
databaseURL: "https://test-push-notf.firebaseio.com",
projectId: "test-push-notf",
storageBucket: "",
messagingSenderId: "93473765978",
appId: "1:93473765978:web:5d959a487fe5382480f663"
};
firebase.initializeApp(firebaseConfig);
const messaging = firebase.messaging();
2019-08-23 11:39:42 +00:00
if ('serviceWorker' in navigator) {
window.addEventListener('load', () => {
navigator.serviceWorker.register('serviceWorker.js')
.then(registration => {
console.log('Service Worker is registered', registration);
2019-10-08 09:12:47 +00:00
messaging.useServiceWorker(registration);
messaging.usePublicVapidKey('BDYQ7X7J7PX0aOFNqB-CivQeqLq4-SqCxQJlDfJ6yNnQeYRoK8H2KOqxHRh47fLrbUhC8O3tve67MqJAIqox7Ng');
messaging.requestPermission().then(function () {
console.log("Notification permission granted.");
return messaging.getToken()
})
.then(function(token) {
console.log("token is : " + token);
2019-10-08 14:49:08 +00:00
$.ajax({
url: 'ajax',
type: 'POST',
data: {
2019-10-11 08:45:07 +00:00
"notification" : 'X',
"action": 'subscribe',
2019-10-08 14:49:08 +00:00
"token": token
},
success: function(data){
console.log('saved', data);
},
error: function (request, status, error) {
console.log("ERROR ", request, error);
}
});
2019-10-08 09:12:47 +00:00
})
.catch(function (err) {
console.log("Unable to get permission to notify.", err);
});
messaging.onMessage(function(payload) {
console.log("Message received. ", payload);
2019-10-08 14:49:08 +00:00
var notification = new Notification('hello', {
body: "Hey there!",
});
notification.onclick = function () {
window.open("http://google.com");
};
2019-10-08 09:12:47 +00:00
});
2019-08-23 11:39:42 +00:00
})
.catch(err => {
console.error('Registration failed:', err);
});
});
}
2019-08-24 11:07:07 +00:00
2019-09-20 10:49:07 +00:00
$('select[name="atSelector"]').change(function(e) {
2019-08-23 11:39:42 +00:00
console.log($(this).val());
if( $(this).val() == 'time') {
2019-09-20 10:49:07 +00:00
$('input[name="atTime"]').prop( "disabled", false );
2019-09-20 10:49:07 +00:00
$('select[name="atDeviceValueInt"]').prop( "disabled", true );
$('input[name="atDeviceValue"]').prop( "disabled", true );
2019-08-23 11:39:42 +00:00
} else if( $(this).val() == 'atDeviceValue') {
2019-09-20 10:49:07 +00:00
$('select[name="atDeviceValue"]').prop( "disabled", false );
$('input[name="atDeviceValueInt"]').prop( "disabled", false );
2019-09-20 10:49:07 +00:00
$('input[name="atTime"]').prop( "disabled", true );
2019-08-23 11:39:42 +00:00
}
});
2019-09-19 17:14:01 +00:00
var pressTimer;
2019-08-23 11:39:42 +00:00
var touch = 0;
var touchSubId = "";
2019-09-19 17:14:01 +00:00
$("div.square-content").on('touchend', function (e){
2019-08-23 11:39:42 +00:00
clearTimeout(pressTimer);
});
$("div.square-content").on('touchstart', function (eTarget) {
2019-08-24 11:07:07 +00:00
navigator.vibrate([500]);
var id = '';
2019-08-24 11:07:07 +00:00
var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log(windowLoc);
2019-09-19 17:14:01 +00:00
if (windowLoc == "/") {
2019-08-24 11:07:07 +00:00
id = $(this).attr('id').replace('device-', '');
} else if (windowLoc == "/scene") {
id = $(this).attr('id').replace('scene-', '');
} else if (windowLoc == "/automation") {
id = $(this).attr('id').replace('automation-', '');
}
2019-08-23 11:39:42 +00:00
var subId = $(this).attr('data-sub-device-id');
2019-08-23 11:39:42 +00:00
touch++;
if(touch == 2 && touchSubId == subId){
console.log("Detail");
2019-09-19 17:14:01 +00:00
if (windowLoc == "/") {
2019-08-24 11:07:07 +00:00
$("#modal-detail-"+subId).removeClass('modal-container-hiden').show();
ajaxChart(subId);
} else if (windowLoc == "/scene") {
2019-08-24 11:07:07 +00:00
} else if (windowLoc == "/automation") {
}
2019-08-23 11:39:42 +00:00
touch = 0;
touchSubId = "";
return;
}
touchSubId = subId;
2019-08-23 11:39:42 +00:00
pressTimer = window.setTimeout(function (e) {
console.log("Setting");
$("#modal-setting-"+id).removeClass('modal-container-hiden').show();
2019-08-24 11:07:07 +00:00
touch = 0;
2019-08-23 11:39:42 +00:00
}, 500);
});
$("div.square-content").mousedown(function(e) {
if (event.which == 3) {
var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log(windowLoc);
var id = null;
2019-09-19 17:14:01 +00:00
if (windowLoc == "/") {
2019-08-23 11:39:42 +00:00
id = $(this).attr('id').replace('device-', '');
} else if (windowLoc == "/scene") {
id = $(this).attr('id').replace('scene-', '');
2019-08-24 11:07:07 +00:00
} else if (windowLoc == "/automation") {
id = $(this).attr('id').replace('automation-', '');
2019-08-23 11:39:42 +00:00
}
$("#modal-setting-"+id).removeClass('modal-container-hiden').show();
console.log("Setting");
console.log("modal" + id);
2019-09-19 17:14:01 +00:00
}
2019-08-23 11:39:42 +00:00
});
$(".close").on('click', function (e) {
var a = $(this).parent().parent();
a.hide();
});
$(this).bind("contextmenu", function(e) {
e.preventDefault();
});
$("div.square-content").on('dblclick', function (eTarget) {
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
if (windowLoc == "/") {
console.log("Detail");
var subId = $(this).attr('data-sub-device-id');
ajaxChart(subId);
$("#modal-detail-"+subId).removeClass('modal-container-hiden').show();
}
});
$("input#sleepTime").change(function() {
console.log("Input text changed!");
});
var element = $('div.delete');
element.hide();
$("a#remove").on('click', function (e) {
console.log("Show/Hide Button");
var element = $('div.delete');
element.toggle();
});
function ajaxChart(id, period = 'day', group = 'hour'){
$.ajax({
url: 'ajax',
type: 'POST',
dataType: 'json',
data: {
2019-10-09 17:26:29 +00:00
"subDevice_id": id,
2019-08-23 11:39:42 +00:00
"action": 'chart',
"period": period,
"group": group
},
success: function(data){
console.log('ID: ',id, 'DATA: ', data);
var ctx = document.getElementById('canvas-'+id).getContext('2d');
var myChart = new Chart(ctx, data);
},
error: function (request, status, error) {
console.log("ERROR ajaxChart():", request, error);
}
});
}
//select room on load
var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log();
if (windowLoc == "/") {
2019-08-23 11:39:42 +00:00
var selectRoomId = localStorage.getItem("selectedRoomId");
if (selectRoomId == null) {
selectRoomId = 'all';
}
2019-08-23 11:39:42 +00:00
console.log('Saved Selected Room ID '+ selectRoomId);
2019-09-19 17:14:01 +00:00
$('[name="room"]').val(selectRoomId);
2019-08-23 11:39:42 +00:00
$('.device-button').each(function(){
2019-09-19 17:14:01 +00:00
if (selectRoomId != 'all'){
2019-08-23 11:39:42 +00:00
if($(this).data('room-id') != selectRoomId){
$(this).hide();
} else {
$(this).show();
}
}
});
2019-08-23 11:39:42 +00:00
}
//Room selector
2019-09-19 17:14:01 +00:00
$( '[name="room"]' ).change(function (e) {
2019-08-23 11:39:42 +00:00
console.log('Selected Room ID ' + this.value)
var roomId = this.value;
localStorage.setItem("selectedRoomId", roomId);
$('.device-button').show();
2019-09-19 17:14:01 +00:00
if (roomId != 'all'){
2019-08-23 11:39:42 +00:00
$('.device-button').each(function(){
if($(this).data('room-id') != roomId){
$(this).hide();
}
});
}
2019-09-19 17:14:01 +00:00
});
2019-10-08 09:12:47 +00:00
/*
2019-09-21 14:35:52 +00:00
var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log();
if (windowLoc == "/") {
2019-10-08 09:12:47 +00:00
var autoUpdate = setInterval(function(){
if (pending == false) {
pending = true;
$.ajax({
url: 'ajax',
type: 'POST',
dataType: 'json',
data: {
"action": 'getState'
},
success: function(data){
console.log(data);
for (const key in data) {
if (data.hasOwnProperty(key)) {
const device = data[key];
$('[data-sub-device-id="'+key+'"]')
.find('.device-button-value')
.text(device['value'])
.attr('title',device['time'])
2019-09-21 14:35:52 +00:00
}
2019-10-08 09:12:47 +00:00
}
},
error: function (request, status, error) {
console.log("ERROR ajaxChart():", request, error);
},
complete: function (){
pending = false;
}
});
}
},4000);
}*/
2019-08-23 11:39:42 +00:00
//Graphs
$('.graph-period').on('click', function (e) {
var subId = $(this).attr('data-sub-device-id');
var period = $(this).attr('data-period');
var groupBy = $(this).attr('data-group');
2019-10-08 09:12:47 +00:00
2019-08-23 11:39:42 +00:00
ajaxChart(subId, period, groupBy);
});
2019-08-24 11:07:07 +00:00
$( "button[name=remove]" ).click(function(e) {
if (confirm('Are you shure ?')) {
var windowLoc = $(location).attr('pathname');
windowLoc = windowLoc.substring(windowLoc.lastIndexOf("/"));
console.log(windowLoc);
var id = null;
if (windowLoc == "/scene") {
id = $(this).data('scene-id');
$("#scene-"+id+"-content").remove();
} else if (windowLoc == "/automation") {
2019-09-19 17:14:01 +00:00
$(this).parent().remove();
2019-08-24 11:07:07 +00:00
}
}
});