2018-03-20 10:03:12 -04:00
|
|
|
// vim: set et sw=2 ts=2:
|
|
|
|
//
|
|
|
|
// This file is part of Moonfire NVR, a security camera digital video recorder.
|
|
|
|
// Copyright (C) 2018 Dolf Starreveld <dolf@starreveld.com>
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// In addition, as a special exception, the copyright holders give
|
|
|
|
// permission to link the code of portions of this program with the
|
|
|
|
// OpenSSL library under certain conditions as described in each
|
|
|
|
// individual source file, and distribute linked combinations including
|
|
|
|
// the two.
|
|
|
|
//
|
|
|
|
// You must obey the GNU General Public License in all respects for all
|
|
|
|
// of the code used other than OpenSSL. If you modify file(s) with this
|
|
|
|
// exception, you may extend this exception to your version of the
|
|
|
|
// file(s), but you are not obligated to do so. If you do not wish to do
|
|
|
|
// so, delete this exception statement from your version. If you delete
|
|
|
|
// this exception statement from all source files in the program, then
|
|
|
|
// also delete it here.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// TODO: test abort.
|
|
|
|
// TODO: add error bar on fetch failure.
|
|
|
|
// TODO: live updating.
|
|
|
|
|
2018-03-26 01:18:56 -04:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
|
|
|
// tooltip needs:
|
|
|
|
// css.structure: ../../themes/base/core.css
|
|
|
|
// css.structure: ../../themes/base/tooltip.css
|
|
|
|
// css.theme: ../../themes/base/theme.css
|
|
|
|
|
2018-03-20 10:03:12 -04:00
|
|
|
import 'jquery-ui/themes/base/core.css';
|
|
|
|
import 'jquery-ui/themes/base/tooltip.css';
|
2018-03-26 01:18:56 -04:00
|
|
|
import 'jquery-ui/themes/base/theme.css';
|
2018-03-20 10:03:12 -04:00
|
|
|
|
|
|
|
// This causes our custom css to be loaded after the above!
|
2018-03-26 01:18:56 -04:00
|
|
|
import './assets/index.css';
|
2018-03-20 10:03:12 -04:00
|
|
|
|
2018-03-26 01:18:56 -04:00
|
|
|
// Get ui widgets themselves
|
2018-03-20 10:03:12 -04:00
|
|
|
import 'jquery-ui/ui/widgets/tooltip';
|
|
|
|
|
|
|
|
import Camera from './lib/models/Camera';
|
|
|
|
import CalendarView from './lib/views/CalendarView';
|
2018-03-26 01:18:56 -04:00
|
|
|
import VideoDialogView from './lib/views/VideoDialogView';
|
2018-03-20 10:03:12 -04:00
|
|
|
import NVRSettingsView from './lib/views/NVRSettingsView';
|
|
|
|
import RecordingFormatter from './lib/support/RecordingFormatter';
|
2018-04-27 09:42:39 -04:00
|
|
|
import StreamSelectorView from './lib/views/StreamSelectorView';
|
|
|
|
import StreamView from './lib/views/StreamView';
|
2018-03-26 01:18:56 -04:00
|
|
|
import TimeFormatter from './lib/support/TimeFormatter';
|
|
|
|
import TimeStamp90kFormatter from './lib/support/TimeStamp90kFormatter';
|
2018-03-20 10:03:12 -04:00
|
|
|
import MoonfireAPI from './lib/MoonfireAPI';
|
|
|
|
|
|
|
|
const api = new MoonfireAPI();
|
2018-04-27 09:42:39 -04:00
|
|
|
let streamViews = null; // StreamView objects
|
2018-03-20 10:03:12 -04:00
|
|
|
let calendarView = null; // CalendarView object
|
2018-11-26 00:31:50 -05:00
|
|
|
let loginDialog = null;
|
2018-03-20 10:03:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently selected time format specification.
|
|
|
|
*
|
|
|
|
* @type {String}
|
|
|
|
*/
|
2020-03-02 01:26:26 -05:00
|
|
|
const timeFmt = 'YYYY-MM-DD HH:mm:ss';
|
2018-03-20 10:03:12 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently active time formatter.
|
|
|
|
* This is lazy initialized at the point we receive the timezone information
|
|
|
|
* and never changes afterwards, except possibly for changing the timezone.
|
|
|
|
*
|
|
|
|
* @type {[type]}
|
|
|
|
*/
|
|
|
|
let timeFormatter = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently active time formatter for internal time format.
|
|
|
|
* This is lazy initialized at the point we receive the timezone information
|
|
|
|
* and never changes afterwards, except possibly for changing the timezone.
|
|
|
|
*
|
|
|
|
* @type {[type]}
|
|
|
|
*/
|
|
|
|
let timeFormatter90k = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Globally set a new timezone for the app.
|
|
|
|
*
|
|
|
|
* @param {String} timeZone Timezone name
|
|
|
|
*/
|
|
|
|
function newTimeZone(timeZone) {
|
|
|
|
timeFormatter = new TimeFormatter(timeFmt, timeZone);
|
|
|
|
timeFormatter90k = new TimeStamp90kFormatter(timeZone);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Globally set a new time format for the app.
|
|
|
|
*
|
|
|
|
* @param {String} format Time format specification
|
|
|
|
*/
|
|
|
|
function newTimeFormat(format) {
|
|
|
|
timeFormatter = new TimeFormatter(format, timeFormatter.tz);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event handler for clicking on a video.
|
|
|
|
*
|
|
|
|
* A 'dialog' object is attached to the body of the dom and it
|
|
|
|
* properly initialized with the corrcet src url.
|
|
|
|
*
|
|
|
|
* @param {NVRSettings} nvrSettingsView NVRSettingsView in effect
|
|
|
|
* @param {object} camera Object for the camera
|
2018-04-27 09:42:39 -04:00
|
|
|
* @param {String} streamType "main" or "sub"
|
2018-03-20 10:03:12 -04:00
|
|
|
* @param {object} range Range Object
|
|
|
|
* @param {object} recording Recording object
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2018-04-27 09:42:39 -04:00
|
|
|
function onSelectVideo(nvrSettingsView, camera, streamType, range, recording) {
|
2018-03-20 10:03:12 -04:00
|
|
|
console.log('Recording clicked: ', recording);
|
|
|
|
const trimmedRange = recording.range90k(nvrSettingsView.trim ? range : null);
|
|
|
|
const url = api.videoPlayUrl(
|
2020-03-02 01:26:26 -05:00
|
|
|
camera.uuid,
|
|
|
|
streamType,
|
|
|
|
recording,
|
|
|
|
trimmedRange,
|
|
|
|
nvrSettingsView.timeStampTrack
|
2018-03-20 10:03:12 -04:00
|
|
|
);
|
2018-03-26 01:18:56 -04:00
|
|
|
const [
|
|
|
|
formattedStart,
|
|
|
|
formattedEnd,
|
|
|
|
] = timeFormatter90k.formatSameDayShortened(
|
2020-03-02 01:26:26 -05:00
|
|
|
trimmedRange.startTime90k,
|
|
|
|
trimmedRange.endTime90k
|
2018-03-20 10:03:12 -04:00
|
|
|
);
|
2018-03-26 01:18:56 -04:00
|
|
|
const videoTitle =
|
|
|
|
camera.shortName + ', ' + formattedStart + ' to ' + formattedEnd;
|
|
|
|
new VideoDialogView()
|
2020-03-02 01:26:26 -05:00
|
|
|
.attach($('body'))
|
|
|
|
.play(videoTitle, recording.videoSampleEntryWidth / 4, url);
|
2018-03-20 10:03:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-27 09:42:39 -04:00
|
|
|
* Fetch stream view data for a given date/time range.
|
2018-03-20 10:03:12 -04:00
|
|
|
*
|
|
|
|
* @param {Range90k} selectedRange Desired time range
|
|
|
|
* @param {Number} videoLength Desired length of video segments, or Infinity
|
|
|
|
*/
|
|
|
|
function fetch(selectedRange, videoLength) {
|
|
|
|
if (selectedRange.startTime90k === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(
|
2020-03-02 01:26:26 -05:00
|
|
|
'Fetching> ' +
|
2018-03-20 10:03:12 -04:00
|
|
|
selectedRange.formatTimeStamp90k(selectedRange.startTime90k) +
|
|
|
|
' to ' +
|
|
|
|
selectedRange.formatTimeStamp90k(selectedRange.endTime90k)
|
|
|
|
);
|
2020-03-02 01:26:26 -05:00
|
|
|
for (const streamView of streamViews) {
|
|
|
|
const url = api.recordingsUrl(
|
|
|
|
streamView.camera.uuid,
|
|
|
|
streamView.streamType,
|
|
|
|
selectedRange.startTime90k,
|
|
|
|
selectedRange.endTime90k,
|
|
|
|
videoLength
|
2018-03-20 10:03:12 -04:00
|
|
|
);
|
2018-04-27 09:42:39 -04:00
|
|
|
if (streamView.recordingsReq !== null) {
|
2018-03-20 10:03:12 -04:00
|
|
|
/*
|
|
|
|
* If there is another request, it would be because settings changed
|
|
|
|
* and so an abort is to make room for this new request, now necessary
|
|
|
|
* for the changed situation.
|
|
|
|
*/
|
2018-04-27 09:42:39 -04:00
|
|
|
streamView.recordingsReq.abort();
|
2018-03-20 10:03:12 -04:00
|
|
|
}
|
2018-04-27 09:42:39 -04:00
|
|
|
streamView.delayedShowLoading(500);
|
2020-03-02 01:26:26 -05:00
|
|
|
const r = api.request(url);
|
2018-04-27 09:42:39 -04:00
|
|
|
streamView.recordingsUrl = url;
|
|
|
|
streamView.recordingsReq = r;
|
|
|
|
streamView.recordingsRange = selectedRange.range90k();
|
2018-03-20 10:03:12 -04:00
|
|
|
r.always(function() {
|
2018-04-27 09:42:39 -04:00
|
|
|
streamView.recordingsReq = null;
|
2018-03-20 10:03:12 -04:00
|
|
|
});
|
|
|
|
r
|
2020-03-02 01:26:26 -05:00
|
|
|
.then(function(data /* , status, req */) {
|
2018-03-20 10:03:12 -04:00
|
|
|
// Sort recordings in descending order.
|
2020-03-02 01:26:26 -05:00
|
|
|
data.recordings.sort(function(a, b) {
|
|
|
|
return b.startId - a.startId;
|
|
|
|
});
|
|
|
|
console.log(
|
|
|
|
'Fetched results for "%s-%s" > updating recordings',
|
|
|
|
streamView.camera.shortName, streamView.streamType
|
|
|
|
);
|
|
|
|
streamView.recordingsJSON = data.recordings;
|
|
|
|
})
|
|
|
|
.catch(function(data, status, err) {
|
|
|
|
console.error(url, ' load failed: ', status, ': ', err);
|
2018-03-20 10:03:12 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-11-26 00:31:50 -05:00
|
|
|
* Updates the session bar at the top of the page.
|
|
|
|
*
|
|
|
|
* @param {Object} session the "session" key of the main API request's JSON,
|
|
|
|
* or null.
|
|
|
|
*/
|
|
|
|
function updateSession(session) {
|
2020-03-02 01:26:26 -05:00
|
|
|
const sessionBar = $('#session');
|
2018-11-26 00:31:50 -05:00
|
|
|
sessionBar.empty();
|
2019-06-19 18:17:50 -04:00
|
|
|
if (session === null || session === undefined) {
|
2018-11-26 00:31:50 -05:00
|
|
|
sessionBar.hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sessionBar.append($('<span id="session-username" />').text(session.username));
|
2020-03-02 01:26:26 -05:00
|
|
|
const logout = $('<a>logout</a>');
|
2018-11-26 00:31:50 -05:00
|
|
|
logout.click(() => {
|
|
|
|
api
|
2020-03-02 01:26:26 -05:00
|
|
|
.logout(session.csrf)
|
|
|
|
.done(() => {
|
|
|
|
onReceivedTopLevel(null);
|
|
|
|
loginDialog.dialog('open');
|
|
|
|
});
|
2018-11-26 00:31:50 -05:00
|
|
|
});
|
|
|
|
sessionBar.append(' | ', logout);
|
|
|
|
sessionBar.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the page after receiving top-level data.
|
2018-03-20 10:03:12 -04:00
|
|
|
*
|
|
|
|
* Sets the following globals:
|
|
|
|
* zone - timezone from data received
|
2018-04-27 09:42:39 -04:00
|
|
|
* streamViews - array of views, one per stream
|
2018-03-20 10:03:12 -04:00
|
|
|
*
|
|
|
|
* Builds the dom for the left side controllers
|
|
|
|
*
|
|
|
|
* @param {Object} data JSON resulting from the main API request /api/?days=
|
2018-11-26 00:31:50 -05:00
|
|
|
* or null if the request failed.
|
2018-03-20 10:03:12 -04:00
|
|
|
*/
|
2018-11-26 00:31:50 -05:00
|
|
|
function onReceivedTopLevel(data) {
|
|
|
|
if (data === null) {
|
2019-06-19 18:17:50 -04:00
|
|
|
data = {cameras: [], timeZoneName: null};
|
2018-11-26 00:31:50 -05:00
|
|
|
}
|
|
|
|
|
2018-11-28 18:10:45 -05:00
|
|
|
newTimeZone(data.timeZoneName);
|
2018-11-26 00:31:50 -05:00
|
|
|
updateSession(data.session);
|
2018-03-20 10:03:12 -04:00
|
|
|
|
|
|
|
// Set up controls and values
|
|
|
|
const nvrSettingsView = new NVRSettingsView();
|
|
|
|
nvrSettingsView.onVideoLengthChange = (vl) =>
|
|
|
|
fetch(calendarView.selectedRange, vl);
|
|
|
|
nvrSettingsView.onTimeFormatChange = (format) =>
|
2018-04-27 09:42:39 -04:00
|
|
|
streamViews.forEach((view) => (view.timeFormat = format));
|
2018-03-20 10:03:12 -04:00
|
|
|
nvrSettingsView.onTrimChange = (t) =>
|
2018-04-27 09:42:39 -04:00
|
|
|
streamViews.forEach((view) => (view.trimmed = t));
|
2018-03-20 10:03:12 -04:00
|
|
|
newTimeFormat(nvrSettingsView.timeFormatString);
|
|
|
|
|
|
|
|
calendarView = new CalendarView({timeZone: timeFormatter.tz});
|
|
|
|
calendarView.onRangeChange = (selectedRange) =>
|
|
|
|
fetch(selectedRange, nvrSettingsView.videoLength);
|
|
|
|
|
2018-04-27 09:42:39 -04:00
|
|
|
const streamsParent = $('#streams');
|
2018-03-20 10:03:12 -04:00
|
|
|
const videos = $('#videos');
|
|
|
|
|
2018-11-26 00:31:50 -05:00
|
|
|
streamsParent.empty();
|
|
|
|
videos.empty();
|
|
|
|
|
2018-04-27 09:42:39 -04:00
|
|
|
streamViews = [];
|
2020-03-02 01:26:26 -05:00
|
|
|
const streamSelectorCameras = [];
|
2018-04-27 09:42:39 -04:00
|
|
|
for (const cameraJson of data.cameras) {
|
2018-03-20 10:03:12 -04:00
|
|
|
const camera = new Camera(cameraJson);
|
2020-03-02 01:26:26 -05:00
|
|
|
const cameraStreams = {};
|
2018-04-27 09:42:39 -04:00
|
|
|
Object.keys(camera.streams).forEach((streamType) => {
|
|
|
|
const sv = new StreamView(
|
|
|
|
camera,
|
|
|
|
streamType,
|
|
|
|
new RecordingFormatter(timeFormatter.formatStr, timeFormatter.tz),
|
|
|
|
nvrSettingsView.trim,
|
|
|
|
videos);
|
|
|
|
sv.onRecordingClicked = (recordingModel) => {
|
|
|
|
console.log('Recording clicked', recordingModel);
|
|
|
|
onSelectVideo(
|
2020-03-02 01:26:26 -05:00
|
|
|
nvrSettingsView,
|
|
|
|
camera,
|
|
|
|
streamType,
|
|
|
|
calendarView.selectedRange,
|
|
|
|
recordingModel
|
2018-04-27 09:42:39 -04:00
|
|
|
);
|
|
|
|
};
|
|
|
|
streamViews.push(sv);
|
|
|
|
cameraStreams[streamType] = sv;
|
|
|
|
});
|
|
|
|
streamSelectorCameras.push({
|
|
|
|
camera: camera,
|
|
|
|
streamViews: cameraStreams,
|
|
|
|
});
|
2018-03-20 10:03:12 -04:00
|
|
|
};
|
|
|
|
|
2018-04-27 09:42:39 -04:00
|
|
|
// Create stream enable checkboxes
|
|
|
|
const streamSelector =
|
|
|
|
new StreamSelectorView(streamSelectorCameras, streamsParent);
|
|
|
|
streamSelector.onChange = () => calendarView.initializeWith(streamViews);
|
|
|
|
calendarView.initializeWith(streamViews);
|
2018-03-20 10:03:12 -04:00
|
|
|
|
2018-04-27 09:42:39 -04:00
|
|
|
console.log('Loaded: ' + streamViews.length + ' stream views');
|
2018-03-20 10:03:12 -04:00
|
|
|
}
|
|
|
|
|
2018-11-26 00:31:50 -05:00
|
|
|
/**
|
|
|
|
* Handles the submit action on the login form.
|
|
|
|
*/
|
|
|
|
function sendLoginRequest() {
|
|
|
|
if (loginDialog.pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-02 01:26:26 -05:00
|
|
|
const username = $('#login-username').val();
|
|
|
|
const password = $('#login-password').val();
|
|
|
|
const submit = $('#login-submit');
|
|
|
|
const error = $('#login-error');
|
2018-11-26 00:31:50 -05:00
|
|
|
|
|
|
|
error.empty();
|
|
|
|
error.removeClass('ui-state-highlight');
|
|
|
|
submit.button('option', 'disabled', true);
|
|
|
|
loginDialog.pending = true;
|
|
|
|
console.info('logging in as', username);
|
|
|
|
api
|
2020-03-02 01:26:26 -05:00
|
|
|
.login(username, password)
|
|
|
|
.done(() => {
|
|
|
|
console.info('login successful');
|
|
|
|
loginDialog.dialog('close');
|
|
|
|
sendTopLevelRequest();
|
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.info('login failed:', e);
|
|
|
|
error.show();
|
|
|
|
error.addClass('ui-state-highlight');
|
|
|
|
error.text(e.responseText);
|
|
|
|
})
|
|
|
|
.always(() => {
|
|
|
|
submit.button('option', 'disabled', false);
|
|
|
|
loginDialog.pending = false;
|
|
|
|
});
|
2018-11-26 00:31:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sends the top-level api request. */
|
|
|
|
function sendTopLevelRequest() {
|
|
|
|
api
|
2020-03-02 01:26:26 -05:00
|
|
|
.request(api.nvrUrl(true))
|
|
|
|
.done((data) => onReceivedTopLevel(data))
|
|
|
|
.catch((e) => {
|
|
|
|
console.error('NVR load exception: ', e);
|
|
|
|
onReceivedTopLevel(null);
|
|
|
|
if (e.status == 401) {
|
|
|
|
loginDialog.dialog('open');
|
|
|
|
}
|
|
|
|
});
|
2018-11-26 00:31:50 -05:00
|
|
|
}
|
|
|
|
|
2018-03-20 10:03:12 -04:00
|
|
|
/**
|
|
|
|
* Class representing the entire application.
|
|
|
|
*/
|
|
|
|
export default class NVRApplication {
|
|
|
|
/**
|
|
|
|
* Construct the application object.
|
|
|
|
*/
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the application.
|
|
|
|
*/
|
|
|
|
start() {
|
2018-11-26 00:31:50 -05:00
|
|
|
loginDialog = $('#login').dialog({
|
|
|
|
autoOpen: false,
|
|
|
|
modal: true,
|
|
|
|
buttons: [
|
|
|
|
{
|
|
|
|
id: 'login-submit',
|
|
|
|
text: 'Login',
|
|
|
|
click: sendLoginRequest,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
loginDialog.pending = false;
|
|
|
|
loginDialog.find('form').on('submit', function(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
sendLoginRequest();
|
|
|
|
});
|
|
|
|
sendTopLevelRequest();
|
2018-03-20 10:03:12 -04:00
|
|
|
}
|
|
|
|
}
|