Files
DataLogger-Generic/www/public/js/controller.js
2016-04-18 14:54:14 -05:00

630 lines
17 KiB
JavaScript

var tsCtrlrs = angular.module('tsCtrlrs', ['ngJustGage', 'n3-line-chart', "ngQuickDate"]);
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
tsCtrlrs.config(function(ngQuickDateDefaultsProvider) {
// Configure with icons from font-awesome
return ngQuickDateDefaultsProvider.set({
closeButtonHtml: "<i class='fa fa-times'></i>",
buttonIconHtml: "<i class='fa fa-clock-o'></i>",
nextLinkHtml: "<i class='fa fa-chevron-right'></i>",
prevLinkHtml: "<i class='fa fa-chevron-left'></i>",
// Take advantage of Sugar.js date parsing
parseDateFunction: function(str) {
d = Date.create(str);
return d.isValid() ? d : null;
}
});
});
var dString_to_sqlite = function(dString){
/**
* Takes a date string in the form YYYYMMDD_HHmmSS and returns it in SQLite format (YYYY-MM-DD HH:mm:SS)
* @param {String} dString
* @return {String} sqliteString
*/
var re = /(\d{4})(\d{2})(\d{2})_(\d{2})(\d{2})(\d{2})/;
var fd = re.exec(dString);
if (fd){
var sqliteString = "";
return sqliteString.concat(fd[1], "-", fd[2], "-", fd[3], " ", fd[4], ":", fd[5], ":", fd[6]);
} else {
return null;
}
};
var sqlite_to_dString = function(sqliteDate){
/**
* Takes a sqlite date string in the form YYYY-MM-DD HH:mm:SS and returns it in format YYYYMMDD_HHmmSS
* @param {String} sqliteDate
* @return {String} dString
*/
var re = /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/;
var fd = re.exec(sqliteDate);
if (fd){
var dString = "";
return dString.concat(fd[1], fd[2], fd[3], "_", fd[4], fd[5], fd[6]);
} else {
return null;
}
};
var date_to_dString = function(inpDate){
var year = inpDate.getFullYear().pad(4);
var month = (inpDate.getMonth() + 1).pad(2);
var day = inpDate.getDate().pad(2);
var hour = inpDate.getHours().pad(2);
var min = inpDate.getMinutes().pad(2);
var sec = inpDate.getSeconds().pad(2);
return "".concat(year, month, day, "_", hour, min, sec);
};
tsCtrlrs.factory('Page', function($log) {
var title = 'default';
var page = 'default';
return {
title: function() {
return title;
},
setTitle: function(newTitle) {
title = newTitle;
},
page: function() {
return page;
},
setPage: function(newPage) {
page = newPage;
}
};
});
tsCtrlrs.factory('Alerts', function($log) {
var alerts = [];
return {
add: function(alt) {
alerts.push(alt);
},
remove: function(indx) {
alerts.splice(indx, 1);
},
clear: function() {
alerts = [];
},
get: function() {
return alerts;
}
};
});
tsCtrlrs.factory('tags',function($q, $http, $log){
var getTag = function(id) {
var deferred = $q.defer();
$http.get('/json/tag/' + id).success(function(data) {
if(data.status == "OK"){
deferred.resolve({
tag:data.tags[0],
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var getTagList = function() {
var deferred = $q.defer();
$http.get('/json/tag/').success(function(data) {
if(data.status == "OK"){
deferred.resolve({
tags:data.tags,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var getTagHistory = function(id){
var deferred = $q.defer();
$http.get('/json/series/'+ id + "/24").success(function(data) {
if(data.status == "OK"){
deferred.resolve({
vals:data.vals,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var getTagHistoryBetween = function(id, start, end){
var deferred = $q.defer();
var url = '/json/valBetween/'+ id + "/" + date_to_dString(start) + "/" + date_to_dString(end);
$log.info("getting value for: " + url);
$http.get(url).success(function(data) {
if(data.status == "OK"){
deferred.resolve({
vals:data.vals,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var getCurrentValues = function(){
var deferred = $q.defer();
$http.get('/json/all').success(function(data) {
if(data.status == "OK"){
deferred.resolve({
vals:data.vals,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var createTag = function(tag){
$http.post('/json/tag/add', {
tag: tag.tag,
name: tag.name,
units: tag.units,
minExpected: tag.minExpected,
maxExpected: tag.maxExpected,
guarantee_sec: tag.guarantee_sec,
change_threshold: tag.change_threshold,
description: tag.description,
data_type: tag.data_type
}).success(function(data){
return data;
});
};
var updateTag = function(tag){
$log.info("updateTag called with "+ JSON.stringify(tag));
$http.post('/json/tag/update', {
id: tag.id,
tag: tag.tag,
name: tag.name,
units: tag.units,
minExpected: tag.minExpected,
maxExpected: tag.maxExpected,
guarantee_sec: tag.guarantee_sec,
change_threshold: tag.change_threshold,
description: tag.description,
data_type: tag.data_type
}).success(function(data){
return data;
}).error(function(err){
$log.warn("updateTag Error: " + err);
});
};
var deleteTag = function(id){
var deferred = $q.defer();
var url = '/json/tag/delete/' + id;
$http.get(url).success(function(data) {
deferred.resolve({
status: data.status
});
});
return deferred.promise;
};
var clearSingleTagData = function(id){
var deferred = $q.defer();
var url = '/json/clearDatabase/' + id;
$http.get(url).success(function(data) {
deferred.resolve({
status: data.status
});
});
return deferred.promise;
};
var clearAllTagData = function(){
var deferred = $q.defer();
var url = '/json/clearDatabase/all';
$http.get(url).success(function(data) {
deferred.resolve({
status: data.status
});
});
return deferred.promise;
};
return {
getTag: getTag,
getTagList: getTagList,
getTagHistory: getTagHistory,
getTagHistoryBetween: getTagHistoryBetween,
getCurrentValues: getCurrentValues,
createTag: createTag,
updateTag: updateTag,
deleteTag: deleteTag,
clearSingleTagData: clearSingleTagData,
clearAllTagData: clearAllTagData
};
});
tsCtrlrs.factory('config',function($q, $http, $log){
var getConfig = function(){
var deferred = $q.defer();
$http.get('/json/config/').success(function(data) {
if(data.status == "OK"){
deferred.resolve({
config:data.config,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
var submitParameter = function(entry){
$http.post('/json/config', {
parameter: entry.parameter,
val: entry.val
}).success(function(data){
return data;
});
};
var getLoggerStatus = function(){
var deferred = $q.defer();
$http.get('/json/logger/status').success(function(data) {
if(data.status == "OK"){
deferred.resolve({
running:data.running,
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message,
});
}
});
return deferred.promise;
};
var restartLogger = function(){
var deferred = $q.defer();
$http.get('/json/logger/restart').success(function(data) {
if(data.status == "OK"){
deferred.resolve({
status: data.status
});
} else {
deferred.resolve({
status:data.status,
message: data.message
});
}
});
return deferred.promise;
};
return {
getConfig:getConfig,
submitParameter: submitParameter,
getLoggerStatus: getLoggerStatus,
restartLogger: restartLogger
};
});
tsCtrlrs.controller('mainCtrl', function($scope, Page, Alerts) {
$scope.Page = Page;
$scope.Alerts = Alerts;
});
tsCtrlrs.controller('dashboardCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) {
Page.setTitle('Dashboard');
Page.setPage('dashboard');
$scope.loadDashboard = function(){
$scope.loading = true;
var getCurrentValues = tags.getCurrentValues();
getCurrentValues.then(function(data) {
$scope.loading = false;
if (data.status == "OK"){
$scope.vals = data.vals;
$scope.error = false;
} else {
$scope.error = data.message;
}
});
};
$scope.loadDashboard();
});
tsCtrlrs.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) {
Page.setTitle('Tags');
Page.setPage('tags');
$scope.loadTagList = function(){
$scope.loading = true;
var getTagList = tags.getTagList();
getTagList.then(function(data) {
$scope.loading = false;
if (data.status == "OK"){
$scope.tags = data.tags;
$scope.error = false;
} else {
$scope.error = data.message;
}
});
};
$scope.loadTagList();
$scope.submitAddTag = function(){
var createStatus = tags.createTag($scope.newTag);
$scope.createStatus = createStatus.status;
$scope.loadTagList();
};
$scope.openDeleteTag = function(id){
var getTag = tags.getTag(id);
getTag.then(function(data){
if (data.status == "OK"){
$scope.error = false;
$scope.dTag = data.tag;
$log.info("Thinking about deleting tag with parameters: "+ JSON.stringify($scope.dTag));
} else {
$scope.error = data.message;
}
});
};
$scope.deleteTag = function(id){
var deleteTag = tags.deleteTag(id);
deleteTag.then(function(data){
$log.info("deleting tag "+ id + " status: " + data.status);
if (data.status == "OK"){
$scope.error = false;
$scope.loadTagList();
} else {
$scope.error = data.message;
}
});
};
$scope.openClearTagData = function(id){
var getTag = tags.getTag(id);
getTag.then(function(data){
if (data.status == "OK"){
$scope.error = false;
$scope.dTagValues = data.tag;
$log.info("Thinking about deleting tag data with parameters: "+ JSON.stringify($scope.dTagValues));
} else {
$scope.error = data.message;
}
});
};
$scope.deleteTagValues = function(id){
var clearSingleTagData = tags.clearSingleTagData(id);
clearSingleTagData.then(function(data){
$log.info("deleting tag "+ id + " status: " + data.status);
if (data.status == "OK"){
$scope.error = false;
$scope.loadTagList();
} else {
$scope.error = data.message;
}
});
};
$scope.openEditTag = function(id){
var getTag = tags.getTag(id);
getTag.then(function(data){
if (data.status == "OK"){
$scope.error = false;
$scope.editTag = data.tag;
$log.info("Started editing tag with parameters: "+ JSON.stringify($scope.editTag));
} else {
$scope.error = data.message;
}
});
};
$scope.submitEditTag = function(){
var editStatus = tags.updateTag($scope.editTag);
$log.info(editStatus);
$scope.editStatus = editStatus.status;
$scope.loadTagList();
};
});
tsCtrlrs.controller('tagValsCtrl', function($scope, $route, $http, $routeParams, Page, Alerts, $log, tags) {
Page.setTitle('Tag Series');
Page.setPage('tags');
if ($routeParams.endDatetime){
$scope.endDatetime = Date.parse(dString_to_sqlite($routeParams.endDatetime));
} else {
$scope.endDatetime = new Date();
}
if ($routeParams.startDatetime){
$scope.startDatetime = Date.parse(dString_to_sqlite($routeParams.startDatetime));
} else {
$scope.startDatetime = new Date();
$scope.startDatetime.setHours($scope.endDatetime.getHours() - 2);
}
$scope.loadTagVals = function(sDTime, eDTime){
$scope.loading = true;
var getTag = tags.getTag($routeParams.tagID);
getTag.then(function(tagData){
if (tagData.status == "OK"){
$scope.tag = tagData.tag;
Page.setTitle('Tag Series: '+ tagData.tag.tagName);
var getTagHistoryBetween = tags.getTagHistoryBetween($routeParams.tagID, sDTime, eDTime);
getTagHistoryBetween.then(function(data) {
$scope.loading = false;
if (data.status == "OK"){
$scope.data = data;
$scope.data.vals = $scope.data.vals.map(function(x){
return {id: x.id, tagID: x.tagID, val: x.val, dtime: new Date(x.dtime)};
});
$scope.error = false;
$scope.options = {
series: [
{
axis: "y",
dataset: "vals",
key: "val",
label: "Tag Value",
color: "#1f77b4",
type: ['line'],
id: 'tagChart'
}
],
axes: {
x: {
key: "dtime",
type: "date"
}
}
};
} else {
$scope.error = data.message;
}
});
} else {
$scope.loading = false;
$scope.error = data.message;
}
});
};
$scope.loadTagVals($scope.startDatetime, $scope.endDatetime);
});
tsCtrlrs.controller('configCtrl', function($scope, Page, Alerts, $log, config) {
Page.setTitle('Configuration');
Page.setPage('configuration');
$scope.loadConfig = function(){
$scope.loading = true;
var getConfig = config.getConfig();
getConfig.then(function(data) {
$scope.loading = false;
if (data.status == "OK"){
$scope.config = data.config;
$scope.error = false;
} else {
$scope.error = data.message;
}
});
};
$scope.loadConfig();
$scope.paramError = null;
var selString = "Select a parameter...";
$scope.newParam = {
pSelected: selString,
parameter: null,
val: null
};
$scope.addParameter = function(){
if ($scope.newParam.pSelected == selString){
$scope.paramError = "No parameter selected";
} else {
if ($scope.newParam.pSelected == "other") {
$scope.newParam.parameter = $scope.newParam.pEntry;
} else {
$scope.newParam.parameter = $scope.newParam.pSelected;
}
config.submitParameter($scope.newParam);
$scope.loadConfig();
}
};
$scope.checkLogger = function(){
$scope.loggerLoading = true;
var checkLoggerStatus = config.getLoggerStatus();
checkLoggerStatus.then(function(data){
$scope.loggerLoading = false;
if (data.status == "OK"){
$scope.loggerRunning = data.running;
$scope.error = false;
} else {
$scope.error = data.message;
}
});
};
$scope.checkLogger();
$scope.restartLogger = function(){
var restartLogger = config.restartLogger();
restartLogger.then(function(data){
if (data.status == "OK"){
$scope.error = false;
$scope.checkLogger();
} else {
$scope.error = data.message;
}
});
};
});
//*---- FILTERS -----*//
tsCtrlrs.filter('dString', function myDateFormat($filter){
return function(text){
var tempdate= new Date(text);
return $filter('date')(tempdate, "yyyyMMdd'_'HHmmss");
};
});
tsCtrlrs.filter('sqlite_to_local', function sqliteformat($filter){
return function(text){
var utcdate= new Date(text + " UTC");
return $filter('date')(utcdate, "yyyy-MM-dd hh:mm:ss a");
};
});