1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
|
/* jshint -W041 */
/* jslint browser: true*/
/* global saveAs, cordova,StatusBar,angular,console,moment */
angular.module('zmApp.controllers').controller('zmApp.LogCtrl', ['$scope', '$rootScope', 'zm', '$ionicModal', 'NVR', '$ionicSideMenuDelegate', '$fileLogger', '$cordovaEmailComposer', '$ionicPopup', '$timeout', '$ionicHistory', '$state', '$interval', '$ionicLoading', '$translate', '$http', 'SecuredPopups', function ($scope, $rootScope, zm, $ionicModal, NVR, $ionicSideMenuDelegate, $fileLogger, $cordovaEmailComposer, $ionicPopup, $timeout, $ionicHistory, $state, $interval, $ionicLoading, $translate, $http, SecuredPopups) {
$scope.openMenu = function () {
$ionicSideMenuDelegate.toggleLeft();
};
//---------------------------------------------------------------
// Controller main
//---------------------------------------------------------------
$scope.flipLogs = function () {
if ($scope.logEntity == 'ZoneMinder')
$scope.logEntity = $rootScope.appName;
else
$scope.logEntity = 'ZoneMinder';
//console.log ("Flipped");
loadLogs();
};
$scope.deleteLogs = function () {
$rootScope.zmPopup = $ionicPopup.confirm({
title: $translate.instant('kPleaseConfirm'),
template: $translate.instant('kDeleteLogsConfirm'),
okText: $translate.instant('kButtonOk'),
cancelText: $translate.instant('kButtonCancel'),
});
$rootScope.zmPopup.then(function (res) {
if (res) {
$fileLogger.deleteLogfile().then(function () {
//console.log('Logfile deleted');
$fileLogger.setStorageFilename(zm.logFile);
$scope.log.logString = "";
});
}
});
};
//----------------------------------------------------------------
// Alarm notification handling
//----------------------------------------------------------------
$scope.handleAlarms = function () {
$rootScope.isAlarm = !$rootScope.isAlarm;
if (!$rootScope.isAlarm) {
$rootScope.alarmCount = "0";
$ionicHistory.nextViewOptions({
disableBack: true
});
$state.go("app.events", {
"id": 0,
"playEvent": false
}, {
reload: true
});
return;
}
};
// desktop download
$scope.downloadLogs = function () {
var body = "zmNinja version:" + $scope.zmAppVersion +
" (" + $rootScope.platformOS + ")\n" +
"ZoneMinder version:" + NVR.getCurrentServerVersion()+"\n\n";
body = $translate.instant('kSensitiveBody') + '\n\n\n' + body;
var fname = $rootScope.appName + "-logs-" +
moment().format('MMM-DD-YY_HH-mm-ss') + ".txt";
$fileLogger.checkFile()
.then(function (d) {
body = body + window.localStorage[d.name];
var file = new Blob([body], {
type: 'text/plain'
});
var url = URL.createObjectURL(file);
$rootScope.zmPopup = SecuredPopups.show('alert', {
title: $translate.instant('kNote'),
template: $translate.instant('kTapDownloadLogs') + "<br/><br/><center><a href='" + url + "' class='button button-assertive icon ion-android-download' download='" + fname + "'>" + " " + $translate.instant('kDownload') + "</a></center>",
okText: $translate.instant('kDismiss'),
okType: 'button-stable'
},
function (e) {
NVR.debug("Error getting log file:" + JSON.stringify(e));
}
);
});
};
// mobile - picks up applogs on the FS and sends an email with it
$scope.attachLogs = function () {
var body = "zmNinja version:" + $scope.zmAppVersion +
" (" + $rootScope.platformOS + ")<br/>" +
"ZoneMinder version:" + NVR.getCurrentServerVersion() + "<br/>";
body = '<b>' + $translate.instant('kSensitiveBody') + '</b><br/><br/>' + body;
$fileLogger.checkFile()
.then(function (d) {
var fileWithPath = cordova.file.dataDirectory + d.name;
NVR.log("file location:" + fileWithPath);
var onSuccess = function (result) {
NVR.log("Share completed? " + result.completed);
NVR.log("Shared to app: " + result.app);
};
var onError = function (msg) {
NVR.log("Sharing failed with message: " + msg);
};
window.plugins.socialsharing.shareViaEmail(
body, //body
'zmNinja Logs attached', // subject
[zm.authoremail], //to
null, // cc
null, //bcc
[fileWithPath],
onSuccess,
onError
);
},
function (e) {
NVR.debug("Error attaching log file:" + JSON.stringify(e));
});
};
function loadZMlogs() {
var ld = NVR.getLogin();
var lapi = ld.apiurl + "/logs.json?sort=TimeKey&direction=desc&page=" + $scope.zmPage;
$http.get(lapi)
.then(function (success) {
$ionicLoading.hide();
$scope.zmMaxPage = success.data.pagination.pageCount;
// console.log ("PAGES="+$scope.zmMaxPage);
var tLogs = "";
// console.log (JSON.stringify(success));
for (var i = 0; i < success.data.logs.length; i++) {
tLogs = tLogs + moment.unix(success.data.logs[i].Log.TimeKey).format("MM/DD/YY hh:mm:ss") + " " +
success.data.logs[i].Log.Code + " " +
success.data.logs[i].Log.Message + "\n";
}
$scope.log.logString = tLogs;
},
function (error) {
NVR.log("Error getting ZM logs:" + JSON.stringify(error));
$scope.log.logString = "Error getting log: " + JSON.stringify(error);
});
}
$scope.selectToggle = function () {
$scope.selectOn = !$scope.selectOn;
};
$scope.changePage = function (p) {
$scope.zmPage = $scope.zmPage + p;
if ($scope.zmPage < 1) $scope.zmPage = 1;
if ($scope.zmPage > $scope.zmMaxPage) $scope.zmPage = $scope.zmMaxPage;
loadLogs();
};
function loadLogs() {
//console.log ("GETTING LOGS");
$ionicLoading.show({
template: $translate.instant('kLoading'),
noBackdrop: true,
duration: zm.loadingTimeout
});
if ($scope.logEntity == $rootScope.appName) {
$fileLogger.getLogfile().then(function (l) {
$scope.log.logString = l.split('\n').reverse().join('\n');
//$scope.log.logString = l;
$ionicLoading.hide();
},
function (error) {
$scope.log.logString = "Error getting log: " + JSON.stringify(error);
$ionicLoading.hide();
});
} else
loadZMlogs();
}
//-------------------------------------------------------------------------
// Lets make sure we set screen dim properly as we enter
// The problem is we enter other states before we leave previous states
// from a callback perspective in ionic, so we really can't predictably
// reset power state on exit as if it is called after we enter another
// state, that effectively overwrites current view power management needs
//------------------------------------------------------------------------
$scope.$on('$ionic.beforeEnter', function () {
$scope.$on("process-push", function () {
NVR.debug(">> LogCtrl: push handler");
var s = NVR.evaluateTappedNotification();
NVR.debug("tapped Notification evaluation:" + JSON.stringify(s));
$ionicHistory.nextViewOptions({
disableAnimate: true,
disableBack: true
});
$state.go(s[0], s[1], s[2]);
});
});
$scope.$on('$ionicView.enter', function () {
//console.log("**VIEW ** Log Ctrl Entered");
/*n $scope.cloudString = "loading...";
window.cordova.plugin.cloudsettings.load(
function (succ) {$scope.cloudString = JSON.stringify(succ);},
function (err) {$scope.cloudString = JSON.stringify(err);}
);*/
$ionicSideMenuDelegate.canDragContent(false);
$scope.selectOn = false;
NVR.setAwake(false);
$scope.logEntity = $rootScope.appName;
$scope.zmPage = 1;
$scope.zmMaxPage = 1;
$scope.log = {
logString: ""
};
$scope.zmAppVersion = NVR.getAppVersion();
$scope.zmVersion = NVR.getCurrentServerVersion();
/* intervalLogUpdateHandle = $interval(function ()
{
loadLogs();
}.bind(this), 3000);*/
loadLogs();
});
$scope.$on('$ionicView.leave', function () {
//console.log ("Deleting Log interval...");
// $interval.cancel(intervalLogUpdateHandle);
});
}]);
|