diff --git a/seadssite/static/css/myapp.css b/seadssite/static/css/myapp.css index 9bdd65f..5052f74 100644 --- a/seadssite/static/css/myapp.css +++ b/seadssite/static/css/myapp.css @@ -4,6 +4,14 @@ color: #bb8833; } +#reportrange { + margin-right: 10px; +} + +#live-data-toggle { + margin-top: -50px; +} + body { over-flow-x: hidden; margin: 0px; @@ -14,7 +22,7 @@ body { } #space-for-search-bar { - margin-top: 80px; + margin-top: 60px; padding: 10px; wdith: 300px; } @@ -201,7 +209,8 @@ body { height: 473px; } .module-wrap .module3 { - width: 31%; + margin-right: -20px; + width: 34%; height: 473px; } diff --git a/seadssite/static/js/areaspline.js b/seadssite/static/js/areaspline.js index 660152d..c683be3 100644 --- a/seadssite/static/js/areaspline.js +++ b/seadssite/static/js/areaspline.js @@ -1,4 +1,10 @@ -var areaspline = function (room, mod_i, payload) { +var areaspline = function (room, mod_i) { + Highcharts.setOptions({ + global: { + useUTC: false + } + }); + room.modules[mod_i].chart = Highcharts.chart(room.modules[mod_i].el_id, { chart: { type: 'areaspline', @@ -20,7 +26,8 @@ var areaspline = function (room, mod_i, payload) { xAxis: { type: 'datetime', dateTimeLabelFormats: { - day: '%b %e' + day: '%b %e', + hour: '%I:%M' } }, yAxis: { @@ -30,7 +37,7 @@ var areaspline = function (room, mod_i, payload) { }, tooltip: { valueSuffix: ' kWh', - valueDecimals: 3, + //valueDecimals: 3, shared: true }, credits: { @@ -38,9 +45,10 @@ var areaspline = function (room, mod_i, payload) { }, plotOptions: { areaspline: { - fillOpacity: 0.8 + fillOpacity: 0.6 } }, - series: payload + series: {} }); + room.modules[mod_i].chart.showLoading(); } diff --git a/seadssite/static/js/bar.js b/seadssite/static/js/bar.js index 1082f9c..b4c68f1 100644 --- a/seadssite/static/js/bar.js +++ b/seadssite/static/js/bar.js @@ -1,4 +1,10 @@ -var bar = function(room, mod_i, categories, payload) { +var bar = function (room, mod_i) { + var categories = []; + var month = moment().month() + 1; + for (var i = 0; i < 12; i++) { + categories.push(moment().month(month).format('MMMM')); + month = (month + 1) % 12; + } room.modules[mod_i].chart = Highcharts.chart(room.modules[mod_i].el_id, { chart: { type: 'column', @@ -14,7 +20,7 @@ var bar = function(room, mod_i, categories, payload) { yAxis: { min: 0, title: { - text: 'Electricity (kWh)' + text: 'kWh' } }, tooltip: { @@ -28,55 +34,7 @@ var bar = function(room, mod_i, categories, payload) { borderWidth: 0 } }, - series: payload, - // exporting: { - // buttons: { - // customButton: { - // x: 0, - // onclick: function() { - // this.update({ - // chart: { - // inverted: false, - // polar: false, - // }, - // subtitle: {} - // }); - // }, - // symbol: 'circle', - // text: 'Standard', - // }, - // customButton1: { - // x: 0, - // y: 30, - // onclick: function() { - // this.update({ - // chart: { - // inverted: true, - // polar: false, - // }, - // subtitle: {} - // }); - // }, - // symbol: 'circle', - // text: 'Inverted', - // }, - // customButton2: { - // x: 0, - // y: 60, - // onclick: function() { - // this.update({ - // chart: { - // inverted: false, - // polar: true, - // }, - // subtitle: {} - // }); - // }, - // symbol: 'circle', - // text: 'Polar', - // }, - // } - // - // } + series: {} }); + room.modules[mod_i].chart.showLoading(); } diff --git a/seadssite/static/js/device.js b/seadssite/static/js/device.js index 85c3640..6e0785f 100644 --- a/seadssite/static/js/device.js +++ b/seadssite/static/js/device.js @@ -1,659 +1,741 @@ var app = function () { - var self = {}; + var self = {}; - Vue.config.silent = false; // show all warnings + Vue.config.silent = false; // show all warnings - var enumerate = function (v) { - var k = 0; - return v.map(function (e) { - e._idx = k++; - }); - }; + var enumerate = function (v) { + var k = 0; + return v.map(function (e) { + e._idx = k++; + }); + }; - var new_id = function () { - self.vue.id_tracker += 1; - return String(self.vue.id_tracker); - }; + var new_id = function () { + self.vue.id_tracker += 1; + return String(self.vue.id_tracker); + }; - var editing_room = function () { - self.vue.modal_room_name = self.vue.rooms[self.vue.action_room].name; - self.vue.modal_chosen_icon_path = self.vue.rooms[self.vue.action_room].icon_path; - }; + var editing_room = function () { + self.vue.modal_room_name = self.vue.rooms[self.vue.action_room].name; + self.vue.modal_chosen_icon_path = self.vue.rooms[self.vue.action_room].icon_path; + }; - var default_room_name = function (path) { - var dirname = path.substring(path.lastIndexOf("/") + 1); - dirname = dirname.substring(0, dirname.lastIndexOf(".")).capitalize(); - return dirname; - }; + var default_room_name = function (path) { + var dirname = path.substring(path.lastIndexOf("/") + 1); + dirname = dirname.substring(0, dirname.lastIndexOf(".")).capitalize(); + return dirname; + }; - String.prototype.capitalize = function () { - return this.charAt(0).toUpperCase() + this.slice(1); - }; + String.prototype.capitalize = function () { + return this.charAt(0).toUpperCase() + this.slice(1); + }; - //-----------------init----------------- - var modal_event_init = function () { - console.log("modal event inited"); - $(function () { // after all dom elements are loaded + //-----------------init----------------- + var modal_event_init = function () { + console.log("modal event inited"); + $(function () { // after all dom elements are loaded - var add_room_modal = $("#add-room-modal"); + var add_room_modal = $("#add-room-modal"); - add_room_modal.on("shown.bs.modal", function (w) { - $("#modal-input").focus(); - }); + add_room_modal.on("shown.bs.modal", function (w) { + $("#modal-input").focus(); + }); - add_room_modal.on("hidden.bs.modal", function (e) { - self.modal_reinit(); - }); + add_room_modal.on("hidden.bs.modal", function (e) { + self.modal_reinit(); + }); - var del_room_modal = $("#del-room-modal"); - // console.log(del_room_modal); - del_room_modal.on("show.bs.modal", function (w) { - console.log("modal2"); - $('.del-room-list').first().addClass('disabled'); + var del_room_modal = $("#del-room-modal"); + // console.log(del_room_modal); + del_room_modal.on("show.bs.modal", function (w) { + console.log("modal2"); + $('.del-room-list').first().addClass('disabled'); + }); }); - }); - }; - - function date_picker() { - var start = moment().subtract(7, 'days'); - var end = moment(); - - function cb(start, end) { - $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY')); - self.vue.start_date = Math.floor(start / 1000); - self.vue.end_date = Math.floor(end / 1000); - console.log(moment.unix(self.vue.start_date).format("MM/DD/YYYY h:mm a") + " to " + moment.unix(self.vue.end_date).format("MM/DD/YYYY h:mm a")); - refresh_data(); - self.create_chart(self.vue.action_room, 0); - } + }; - $('#reportrange').daterangepicker({ - dateLimit: { - "months": 1, - }, - startDate: start, - endDate: end, - ranges: { - 'Today': [moment(), moment()], - 'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], - 'Last 7 Days': [moment().subtract(6, 'days'), moment()], - 'Last 30 Days': [moment().subtract(29, 'days'), moment()], - 'This Month': [moment().startOf('month'), moment()], - 'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')] + function date_picker() { + var start = moment().subtract(7, 'days'); + var end = moment(); + + function cb(start, end) { + $('#reportrange span').html(start.format('MMMM D, YYYY') + ' - ' + end.format('MMMM D, YYYY')); + self.vue.start_date = start.unix(); + self.vue.end_date = end.unix(); + console.log(moment.unix(self.vue.start_date).format("MM/DD/YYYY h:mm a") + " to " + moment.unix(self.vue.end_date).format("MM/DD/YYYY h:mm a")); + + // Clear live data refresh if it is active + if (self.vue.live_data) { + self.vue.live_data = false; + clearInterval(self.vue.live_timer); + } + + // Update the current room's areaspline + self.vue.rooms[self.vue.action_room].modules[0].chart.showLoading(); + update_room_data(self.vue.action_room, self.vue.start_date, self.vue.end_date, 30, update_areaspline); + + // Mark areaspline for other rooms as not updated + // Should probaby be done differently but no time :( + for (var i = 0; i < self.vue.rooms.length; i++) { + if (i != self.vue.action_room) + self.vue.rooms[i].modules[0].updated = false; + } } - }, cb); - cb(start, end); - } + $('#reportrange').daterangepicker({ + dateLimit: { + "months": 1, + }, + startDate: start, + endDate: end, + ranges: { + 'Today': [moment(), moment()], + 'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], + 'Last 7 Days': [moment().subtract(6, 'days'), moment()], + 'Last 30 Days': [moment().subtract(29, 'days'), moment()], + 'This Month': [moment().startOf('month'), moment()], + 'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')] + } + }, cb); - // Generates a power data structure and generates monthly power usage data for each appliance - function init_data() { - Object.keys(device.rooms).forEach(function (room) { - Object.keys(device.rooms[room].appliances).forEach(function (appliance) { - var appl_id = device.rooms[room].appliances[appliance].id; - self.vue.power_data[appl_id] = { - name: appliance, - monthly_data: gen_monthly_appl_data(self.vue.device_id, appliance, appl_id), - data: undefined, - }; + cb(start, end); + } + + + var init_rooms = function () { + var rooms = self.vue.room_structure.rooms; + Object.keys(rooms).forEach(function (room_name) { + var room = rooms[room_name]; + self.vue.modal_chosen_icon_path = img_path + "/" + room.icon + ".png"; + self.vue.modal_room_name = room_name; + self.vue.modal_appliances = room.appliances; + self.vue.add_room(); + self.modal_reinit(); }); - }); - } + enumerate(self.vue.rooms); + self.modal_reinit(); + self.manage_btn_toggle(0); + self.vue.isInitialized = true; + } - // Refreshes saved power data based on current start and end date - function refresh_data() { - console.log("refresh_data"); - Object.keys(self.vue.power_data).forEach(function (appl_id) { - self.vue.power_data[appl_id].data = gen_cont_appl_data(self.vue.device_id, self.vue.power_data[appl_id].name, - appl_id, self.vue.start_date, self.vue.end_date, 30); - }); - } + // Creates empty charts for the specified room + var init_charts = function (room_i) { + for (var i = 0; i < self.vue.rooms[room_i].modules.length; i++) { + self.create_chart(room_i, i); + } + self.vue.rooms[room_i].initialized = true; + } - var init_home = function () { - for (var i = 0; i < self.vue.rooms[0].modules.length; i++) { - self.create_chart(0, i); + // Creates empty series for the appliances of the specified room and chart + function create_series(room_i, mod_i) { + var chart = self.vue.rooms[room_i].modules[mod_i].chart; + + if (room_i == 0) { + for (var i = 1; i < self.vue.rooms.length; i++) { + chart.addSeries({ + id: self.vue.rooms[i].name, + name: self.vue.rooms[i].name, + data: [] + }) + } + } else { + var appliances = self.vue.rooms[room_i].appliances; + Object.keys(appliances).forEach(function (key) { + chart.addSeries({ + id: appliances[key].id, + name: key, + data: [] + }); + }); + } } - } - var init_rooms = function () { - var rooms = self.vue.room_structure.rooms; - Object.keys(rooms).forEach(function (room_name) { - var room = rooms[room_name]; - self.vue.modal_chosen_icon_path = img_path + "/" + room.icon + ".png"; - self.vue.modal_room_name = room_name; - self.vue.modal_appliances = room.appliances; - self.vue.add_room(); - self.modal_reinit(); - }); - enumerate(self.vue.rooms); - self.modal_reinit(); - self.manage_btn_toggle(0); - self.vue.isInitialized = true; - } + // Returns a request URL + // Overloaded such that if only two arguments are provided, it creates a URL for requesting cumulative usage + // up to a specified time - this is used for calculating power usage over a period of time, i.e. for monthly usage + // Otherwise it creates a URL for requesting continuous usage data over a specified period of time, with ~data_points + // number of data points + function create_url(appliance_id, start, end, data_points) { + var url = "http://db.sead.systems:8080/" + self.vue.device_id + "?type=P&list_format=energy&device=" + appliance_id + "&start_time=" + start; + if (arguments.length == 2) + url += "&end_time=" + start; + else + url += "&end_time=" + end + "&granularity=" + Math.floor((end - start) / data_points); + return url; + } - // Generates monthly usage of specified panel for the last 12 months - function gen_monthly_appl_data(device_id, appl_name, appl_id) { - var result = []; - - var points = []; - var start = moment().subtract(12, "months").startOf("month"); - var end = moment().subtract(5, "minutes"); - var time; - for (var i = 0; i <= 12; i++) { - time = Math.floor(start / 1000); - $.ajax({ - url: "http://db.sead.systems:8080/" + device_id + "?start_time=" + time + "&end_time=" + time + "&device=" + appl_id + "&type=P", + // Performs GET request for specified API call + function fetch_data(url, callback) { + return $.ajax({ + url: url, dataType: 'json', - async: false, - success: function (data_test) { - points.push(data_test[1]); + dataFilter: function (response, type) { + var data = []; + $.map(JSON.parse(response).data, function (value, key) { + data.push([moment(value.time, "YYYY-MM-DD HH:mm:ss").valueOf(), parseFloat(value.energy)]); + }); + data.reverse(); + return JSON.stringify(data); + }, + success: function (response) { + callback(response); } }); - if (i >= 11) - start = end; - else - start.add(1, "month"); } - for (var i = 1; i < points.length; i++) { - if (points[i] !== undefined && points[i - 1] !== undefined) { - result.push( - Math.abs((points[i - 1][1] - points[i][1]) / 3600000) - ) - } else - result.push(null); + // Updates continuous power data for all appliances in specified room with specified parameters + // Does not work for home room (index 0) as it has no appliances associated + function update_room_data(room_i, start, end, data_points, callback) { + if (room_i == 0) { + var c = self.vue.rooms.length - 1; + for (var i = 1; i < self.vue.rooms.length; i++) { + update_room_data(i, start, end, data_points, function () { + c--; + if (c == 0) { + callback(room_i); + } + }) + } + } else { + var appliances = self.vue.rooms[room_i].appliances; + var promises = []; + Object.keys(appliances).forEach(function (appl) { + promises.push(fetch_data(create_url(appliances[appl].id, start, end, data_points), function (response) { + self.vue.appliance_data[appliances[appl].id] = self.vue.appliance_data[appliances[appl].id] || {}; + self.vue.appliance_data[appliances[appl].id].data = response; + })); + }); + $.when.apply(this, promises).done(function () { + callback(room_i); + }); + } } - return result; - } - // Generates continous power usage data for the specified parameters - function gen_cont_appl_data(device_id, name, appliance_id, start, end, data_points) { - var granularity = Math.floor((end - start) / data_points); - - var result = []; - $.ajax({ - url: "http://db.sead.systems:8080/" + device_id + "?start_time=" + start + "&end_time=" + end + - "&list_format=energy&type=P&device=" + appliance_id + "&granularity=" + granularity, - dataType: 'json', - async: false, - success: function (response) { - result = Object.keys(response.data).reverse().map(function (key) { - return [moment(response.data[key]["time"], "YYYY-MM-DD HH:mm:ss").valueOf(), parseFloat(response.data[key]["energy"])]; - }) + // Updates areaspline chart for specified room with data from buffered data at self.vue.appliance_data + function update_areaspline(room_i) { + var chart = self.vue.rooms[room_i].modules[0].chart; + + if (room_i == 0) { + for (var i = 1; i < self.vue.rooms.length; i++) { + var appliances = self.vue.rooms[i].appliances; + var aggregate = []; + Object.keys(appliances).forEach(function (key, index) { + var data = self.vue.appliance_data[appliances[key].id].data; + for (var j = 0; j < data.length; j++) { + if (index == 0) + aggregate[j] = data[j].slice(0); + else + aggregate[j][1] = aggregate[j][1] + data[j][1]; + } + }); + chart.get(self.vue.rooms[i].name).update({ + data: aggregate + }); + } + } else { + chart.series.forEach(function (series) { + chart.get(series.options.id).update({ + data: self.vue.appliance_data[series.options.id].data + }); + }); } - }); - return result; - } + self.vue.rooms[room_i].modules[0].updated = true; + chart.hideLoading(); + } - // Generates areaspline graph for the specified room - function gen_line_chart(room_i, mod_i) { - var payload = []; - var appliances; - - var temp = []; - if (room_i == 0) { - var appl_data; - for (var i = 1; i < self.vue.rooms.length; i++) { - appliances = self.vue.rooms[i].appliances; - Object.keys(appliances).forEach(function (key, index) { - appl_data = self.vue.power_data[appliances[key].id].data; - for (var j = 0; j < appl_data.length; j++) { - if (index == 0) - temp[j] = appl_data[j].slice(0); + function live_data(room_i) { + self.vue.live_timer = setInterval(function x() { + console.log("updating chart"); + var end = Math.floor(Date.now() / 1000); + var start = end - 600; + update_room_data(room_i, start, end, 60, update_areaspline); + return x; + }(), 20000); + } + + function update_monthly_room_data(room_i, callback) { + if (room_i == 0) { + var c = self.vue.rooms.length - 1; + for (var i = 1; i < self.vue.rooms.length; i++) { + update_monthly_room_data(i, function () { + c--; + if (c == 0) + callback(room_i); + }) + } + } else { + var appliances = self.vue.rooms[room_i].appliances; + var c = Object.keys(self.vue.rooms[room_i].appliances).length; + Object.keys(appliances).forEach(function (appl_name) { + var start = moment().subtract(12, "months").startOf("month"); + var end = moment().subtract(5, "minutes"); + + var promises = []; + var points = []; + for (var i = 0; i <= 12; i++) { + var time = Math.floor(start / 1000); + promises.push(fetch_data(create_url(appliances[appl_name].id, time), function (response) { + points.push(response[0]); + })); + if (i >= 11) + start = end; else - temp[j][1] = temp[j][1] + appl_data[j][1]; + start.add(1, "month"); } + $.when.apply(this, promises).done(function () { + var result = []; + for (var i = 1; i < points.length; i++) { + if (points[i] !== undefined && points[i - 1] !== undefined) { + result.push( + Math.abs((points[i - 1][1] - points[i][1]) / 3600000) + ) + } else + result.push(null); + } + self.vue.appliance_data[appliances[appl_name].id] = self.vue.appliance_data[appliances[appl_name].id] || {}; + self.vue.appliance_data[appliances[appl_name].id].monthly_data = result; + c--; + if (c == 0) + callback(room_i); + }); }); - payload.push({ - name: self.vue.rooms[i].name, - data: temp - }); - temp = []; } - } else { - appliances = self.vue.rooms[room_i].appliances; - Object.keys(appliances).forEach(function (key) { - payload.push({ - name: key, - data: self.vue.power_data[appliances[key].id].data, - }); - }); } - areaspline(self.vue.rooms[room_i], mod_i, payload); - } - // Generates bar chart for specified room - function gen_bar_chart(room_i, mod_i) { - var categories = []; - var month = moment().month() + 1; - for (var i = 0; i < 12; i++) { - categories.push(moment().month(month).format('MMMM')); - month = (month + 1) % 12; - } + function update_bar(room_i) { + var chart = self.vue.rooms[room_i].modules[1].chart; - var payload = []; - var appliances; - if (room_i == 0) { - var temp = []; - var appl_data; - for (var i = 1; i < self.vue.rooms.length; i++) { - appliances = self.vue.rooms[i].appliances; - Object.keys(appliances).forEach(function (key, index) { - appl_data = self.vue.power_data[appliances[key].id].monthly_data; - if (index == 0) - temp = appl_data.slice(); - else { - for (var j = 0; j < appl_data.length; j++) { - if (appl_data[j] != null) - temp[j] += appl_data[j]; + if (room_i == 0) { + for (var i = 1; i < self.vue.rooms.length; i++) { + var appliances = self.vue.rooms[i].appliances; + + var aggregate = []; + Object.keys(appliances).forEach(function (key, index) { + var curr_appl_data = self.vue.appliance_data[appliances[key].id].monthly_data; + if (index == 0) + aggregate = curr_appl_data.slice(); + else { + for (var j = 0; j < curr_appl_data.length; j++) { + if (curr_appl_data[j] != null) + aggregate[j] += curr_appl_data[j]; + } } - } + }); + chart.get(self.vue.rooms[i].name).update({ + data: aggregate + }); + } + } else { + chart.series.forEach(function (series) { + chart.get(series.options.id).update({ + data: self.vue.appliance_data[series.options.id].monthly_data + }); }); - payload.push({ - name: self.vue.rooms[i].name, - data: temp - }) } - } else { - appliances = self.vue.rooms[room_i].appliances; - Object.keys(appliances).forEach(function (key) { - payload.push({ - name: key, - data: self.vue.power_data[appliances[key].id].monthly_data - }); - }); + self.vue.rooms[room_i].modules[1].updated = true; + chart.hideLoading(); } - bar(self.vue.rooms[room_i], mod_i, categories, payload); - } - self.modal_reinit = function () { - self.vue.modal_room_name = ''; - self.vue.modal_chosen_icon_path = self.vue.default_icon_path; - } - self.adding_editing_room = function (action) { - // console.log('adding_editing_room'); - if (action == 'edit') { - self.vue.adding_room = false; - editing_room(); - } else if (action == 'add') { - self.vue.adding_room = true; - } else { - console.log('error adding_editing_room'); + self.modal_reinit = function () { + self.vue.modal_room_name = ''; + self.vue.modal_chosen_icon_path = self.vue.default_icon_path; } - } - self.add_edit_room_enter = function (action) { - // console.log('adding_editing_room'); - if (self.vue.adding_room) { - self.add_room(); - } else if (!self.vue.adding_room) { - self.edit_room(); - } else { - console.log('error adding_editing_room'); + self.adding_editing_room = function (action) { + // console.log('adding_editing_room'); + if (action == 'edit') { + self.vue.adding_room = false; + editing_room(); + } else if (action == 'add') { + self.vue.adding_room = true; + } else { + console.log('error adding_editing_room'); + } } - $('#add-room-modal').modal('hide'); - }; - - self.edit_room = function () { - // console.log('edit_room'); - var room = self.vue.rooms[self.vue.action_room]; - room.name = self.vue.modal_room_name; - room.icon_path = self.vue.modal_chosen_icon_path; - self.vue.icon_path = room.icon_path; - - // re-initialize - self.modal_reinit(); - } - self.add_room = function () { - var name = (self.vue.modal_room_name ? self.vue.modal_room_name : default_room_name(self.vue.modal_chosen_icon_path)); - var new_room = { - 'name': name, - '_idx': self.vue.rooms.length, - 'notice': 0, - 'data': [], - 'appliances': self.vue.modal_appliances, - 'modules': [], - 'isActive': false, - 'icon_path': self.vue.modal_chosen_icon_path, + self.add_edit_room_enter = function (action) { + // console.log('adding_editing_room'); + if (self.vue.adding_room) { + self.add_room(); + } else if (!self.vue.adding_room) { + self.edit_room(); + } else { + console.log('error adding_editing_room'); + } + $('#add-room-modal').modal('hide'); }; - var add_room_action = function () { - self.vue.rooms.push(new_room); - self.vue.action_room = self.vue.rooms.length - 1; - self.add_module(1, 'activity'); // number refers to module type - //self.add_module(3, 'devices'); - self.add_module(2, 'graph'); - self.add_module(3, 'consumption'); - //self.add_module(2, 'notification'); - if (self.vue.isInitialized) { - self.manage_btn_toggle(self.vue.rooms.length - 1); //set this to active (jump to this page) - } - enumerate(self.vue.rooms); + self.edit_room = function () { + // console.log('edit_room'); + var room = self.vue.rooms[self.vue.action_room]; + room.name = self.vue.modal_room_name; + room.icon_path = self.vue.modal_chosen_icon_path; + self.vue.icon_path = room.icon_path; + + // re-initialize + self.modal_reinit(); } - if (self.vue.isInitialized) { - var icon = self.vue.modal_chosen_icon_path.split('\\').pop().split('/').pop(); - icon = icon.substring(0, icon.length - 4); - console.log(icon); - self.vue.room_structure.rooms[name] = { - icon: icon + self.add_room = function () { + var name = (self.vue.modal_room_name ? self.vue.modal_room_name : default_room_name(self.vue.modal_chosen_icon_path)); + var new_room = { + 'name': name, + '_idx': self.vue.rooms.length, + 'notice': 0, + 'initialized': false, + 'data': [], + 'appliances': self.vue.modal_appliances, + 'modules': [], + 'isActive': false, + 'icon_path': self.vue.modal_chosen_icon_path, }; - $.post("/update_info/", - { - csrfmiddlewaretoken: self.vue.csrf_token, - device_id: self.vue.device_id, - data: JSON.stringify(self.vue.room_structure) - }, function () { - console.log('add room success'); - add_room_action(); - setTimeout(function () { - self.reload_room(new_room._idx); - }, 2); - } - ).fail( - function (response) { - alert('Error: ' + response.responseText); + + var add_room_action = function () { + self.vue.rooms.push(new_room); + self.vue.action_room = self.vue.rooms.length - 1; + self.add_module(1, 'activity'); // number refers to module type + //self.add_module(3, 'devices'); + self.add_module(2, 'graph'); + self.add_module(3, 'consumption'); + //self.add_module(2, 'notification'); + if (self.vue.isInitialized) { + self.manage_btn_toggle(self.vue.rooms.length - 1); //set this to active (jump to this page) } - ); - } else { - add_room_action(); - } + enumerate(self.vue.rooms); + } + + if (self.vue.isInitialized) { + var icon = self.vue.modal_chosen_icon_path.split('\\').pop().split('/').pop(); + icon = icon.substring(0, icon.length - 4); + console.log(icon); + self.vue.room_structure.rooms[name] = { + icon: icon + }; + $.post("/update_info/", + { + csrfmiddlewaretoken: self.vue.csrf_token, + device_id: self.vue.device_id, + data: JSON.stringify(self.vue.room_structure) + }, function () { + console.log('add room success'); + add_room_action(); + setTimeout(function () { + self.reload_room(new_room._idx); + }, 2); + } + ).fail( + function (response) { + alert('Error: ' + response.responseText); + } + ); + } else { + add_room_action(); + } - enumerate(self.vue.rooms); //just for check + enumerate(self.vue.rooms); //just for check - // re-initialize ( clear modal ) - self.modal_reinit(); - }; + // re-initialize ( clear modal ) + self.modal_reinit(); + }; - self.reload_house = function () { - console.log(self.vue.rooms.length) - for (var i = 0; i < self.vue.rooms.length; i++) { - console.log('here'); - self.reload_room(i); + self.reload_house = function () { + console.log(self.vue.rooms.length) + for (var i = 0; i < self.vue.rooms.length; i++) { + console.log('here'); + self.reload_room(i); + } } - } - self.reload_room = function (room_i) { - var room = self.vue.rooms[room_i]; - console.log('Reload Room ' + room.name); - for (var i = 0; i < room.modules.length; i++) { - self.reload_mod(room_i, i); + self.reload_room = function (room_i) { + var room = self.vue.rooms[room_i]; + console.log('Reload Room ' + room.name); + for (var i = 0; i < room.modules.length; i++) { + self.reload_mod(room_i, i); + } } - } - self.reload_mod = function (room_i, mod_i) { - var mod = self.vue.rooms[room_i].modules[mod_i]; - if (mod.chart != "") { - mod.chart.destroy(); - } else if (mod.header == "devices") { - $('#' + mod.el_id).empty(); + self.reload_mod = function (room_i, mod_i) { + var mod = self.vue.rooms[room_i].modules[mod_i]; + if (mod.chart != "") { + mod.chart.destroy(); + } else if (mod.header == "devices") { + $('#' + mod.el_id).empty(); + } + self.create_chart(room_i, mod_i); } - self.create_chart(room_i, mod_i); - } - var isSortInitialized = false; - - self.create_chart = function (room_i, mod_i) { - var mod = self.vue.rooms[room_i].modules[mod_i]; - if (mod.header == "activity") { - gen_line_chart(room_i, mod_i); - } else if (mod.header == "devices") { - $('#' + mod.el_id).empty(); - htmltag = gen_dev_list(room_i); - tmp = self.vue.rooms[room_i].modules[mod_i]; - $('#' + tmp.el_id).append("\ + var isSortInitialized = false; + + self.create_chart = function (room_i, mod_i) { + var mod = self.vue.rooms[room_i].modules[mod_i]; + if (mod.header == "activity") { + areaspline(self.vue.rooms[room_i], mod_i); + create_series(room_i, mod_i); + } else if (mod.header == "devices") { + $('#' + mod.el_id).empty(); + htmltag = gen_dev_list(room_i); + tmp = self.vue.rooms[room_i].modules[mod_i]; + $('#' + tmp.el_id).append("\
| ' +
- ' ' +
- ' ' +
- ' ' + roomName + '' +
- '
|