PHP_SMART_HOME_V3/app/templates/js/script.js

300 lines
9.1 KiB
JavaScript
Raw Permalink 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);
2020-03-25 14:58:47 +00:00
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()
})
2020-03-25 14:58:47 +00:00
.then(function (token) {
2019-10-08 09:12:47 +00:00
console.log("token is : " + token);
2019-10-08 14:49:08 +00:00
$.ajax({
url: 'ajax',
type: 'POST',
data: {
2020-03-25 14:58:47 +00:00
"notification": 'X',
2019-10-11 08:45:07 +00:00
"action": 'subscribe',
2019-10-08 14:49:08 +00:00
"token": token
},
2020-03-25 14:58:47 +00:00
success: function (data) {
2019-10-08 14:49:08 +00:00
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);
});
2019-08-23 11:39:42 +00:00
})
.catch(err => {
console.error('Registration failed:', err);
});
});
}
2019-08-24 11:07:07 +00:00
2020-03-25 14:58:47 +00:00
$('select[name="atSelector"]').change(function (e) {
2019-08-23 11:39:42 +00:00
console.log($(this).val());
2020-03-25 14:58:47 +00:00
if ($(this).val() == 'time') {
$('input[name="atTime"]').prop("disabled", false);
$('select[name="atDeviceValueInt"]').prop("disabled", true);
$('input[name="atDeviceValue"]').prop("disabled", true);
} else if ($(this).val() == 'atDeviceValue') {
$('select[name="atDeviceValue"]').prop("disabled", false);
$('input[name="atDeviceValueInt"]').prop("disabled", false);
$('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 = "";
2020-03-25 14:58:47 +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 = '';
2020-03-25 14:58:47 +00:00
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-', '');
}
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
var subId = $(this).attr('data-sub-device-id');
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
touch++;
2020-03-25 14:58:47 +00:00
if (touch == 2 && touchSubId == subId) {
2019-08-23 11:39:42 +00:00
console.log("Detail");
2019-09-19 17:14:01 +00:00
if (windowLoc == "/") {
2020-03-25 14:58:47 +00:00
$("#modal-detail-" + subId).removeClass('modal-container-hiden').show();
2019-08-24 11:07:07 +00:00
ajaxChart(subId);
} else if (windowLoc == "/scene") {
2020-03-25 14:58:47 +00:00
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;
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
pressTimer = window.setTimeout(function (e) {
console.log("Setting");
2020-03-25 14:58:47 +00:00
$("#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);
});
2020-03-25 14:58:47 +00:00
$("div.square-content").mousedown(function (e) {
2019-08-23 11:39:42 +00:00
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
}
2020-03-25 14:58:47 +00:00
$("#modal-setting-" + id).removeClass('modal-container-hiden').show();
2019-08-23 11:39:42 +00:00
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();
});
2020-03-25 14:58:47 +00:00
$(this).bind("contextmenu", function (e) {
2019-08-23 11:39:42 +00:00
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);
2020-03-25 14:58:47 +00:00
$("#modal-detail-" + subId).removeClass('modal-container-hiden').show();
2019-08-23 11:39:42 +00:00
}
});
2020-03-25 14:58:47 +00:00
$("input#sleepTime").change(function () {
2019-08-23 11:39:42 +00:00
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();
});
2020-03-25 14:58:47 +00:00
function ajaxChart(id, period = 'day', group = 'hour') {
2019-08-23 11:39:42 +00:00
$.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
},
2020-03-25 14:58:47 +00:00
success: function (data) {
console.log('ID: ', id, 'DATA: ', data);
var ctx = document.getElementById('canvas-' + id).getContext('2d');
2019-08-23 11:39:42 +00:00
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 == "/") {
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
var selectRoomId = localStorage.getItem("selectedRoomId");
2020-03-25 14:58:47 +00:00
if (selectRoomId == null) {
selectRoomId = 'all';
}
2020-03-25 14:58:47 +00:00
console.log('Saved Selected Room ID ' + selectRoomId);
2019-09-19 17:14:01 +00:00
$('[name="room"]').val(selectRoomId);
2020-03-25 14:58:47 +00:00
$('.device-button').each(function () {
if (selectRoomId != 'all') {
if ($(this).data('room-id') != selectRoomId) {
2019-08-23 11:39:42 +00:00
$(this).hide();
} else {
$(this).show();
}
}
});
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
}
//Room selector
2020-03-25 14:58:47 +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();
2020-03-25 14:58:47 +00:00
if (roomId != 'all') {
$('.device-button').each(function () {
if ($(this).data('room-id') != roomId) {
2019-08-23 11:39:42 +00:00
$(this).hide();
}
});
}
2019-09-19 17:14:01 +00:00
});
2020-02-18 20:30:44 +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 == "/") {
2020-02-18 20:30:44 +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'])
}
}
},
error: function (request, status, error) {
console.log("ERROR ajaxChart():", request, error);
},
complete: function (){
pending = false;
}
});
}
},4000);
2019-10-08 09:12:47 +00:00
}*/
2019-08-23 11:39:42 +00:00
2020-02-18 20:30:44 +00:00
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');
2020-03-25 14:58:47 +00:00
2019-08-23 11:39:42 +00:00
ajaxChart(subId, period, groupBy);
});
2020-03-25 14:58:47 +00:00
$("button[name=remove]").click(function (e) {
2019-08-24 11:07:07 +00:00
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');
2020-03-25 14:58:47 +00:00
$("#scene-" + id + "-content").remove();
2019-08-24 11:07:07 +00:00
} else if (windowLoc == "/automation") {
2019-09-19 17:14:01 +00:00
$(this).parent().remove();
2019-08-24 11:07:07 +00:00
}
}
});