zoneminder/web/skins/classic/views/js/watch.js

865 lines
26 KiB
JavaScript
Raw Normal View History

function showEvents() {
2020-03-05 00:22:08 +08:00
$('ptzControls').addClass('hidden');
$('events').removeClass('hidden');
if ( $('eventsControl') ) {
$('eventsControl').addClass('hidden');
}
if ( $('controlControl') ) {
$('controlControl').removeClass('hidden');
}
2020-03-05 00:22:08 +08:00
showMode = 'events';
}
function showPtzControls() {
2020-03-05 00:22:08 +08:00
$('events').addClass('hidden');
$('ptzControls').removeClass('hidden');
if ( $('eventsControl') ) {
$('eventsControl').removeClass('hidden');
}
if ( $('controlControl') ) {
$('controlControl').addClass('hidden');
}
2020-03-05 00:22:08 +08:00
showMode = 'control';
}
function changeScale() {
var scale = $('scale').get('value');
2017-11-21 03:25:13 +08:00
var newWidth;
var newHeight;
2020-03-05 00:22:08 +08:00
if ( scale == '0' || scale == 'auto' ) {
var newSize = scaleToFit(monitorWidth, monitorHeight, $j('#liveStream'+monitorId), $j('#replayStatus'));
2017-11-21 03:25:13 +08:00
newWidth = newSize.width;
newHeight = newSize.height;
autoScale = newSize.autoScale;
} else {
$j(window).off('resize', endOfResize); //remove resize handler when Scale to Fit is not active
newWidth = monitorWidth * scale / SCALE_BASE;
newHeight = monitorHeight * scale / SCALE_BASE;
}
2020-01-06 06:33:53 +08:00
Cookie.write('zmWatchScale'+monitorId, scale, {duration: 10*365});
2017-05-19 01:50:56 +08:00
/*Stream could be an applet so can't use moo tools*/
2017-08-04 00:41:43 +08:00
var streamImg = $('liveStream'+monitorId);
if ( streamImg ) {
2020-01-06 06:33:53 +08:00
streamImg.style.width = newWidth + 'px';
streamImg.style.height = newHeight + 'px';
2017-11-21 03:25:13 +08:00
streamImg.src = streamImg.src.replace(/scale=\d+/i, 'scale='+(scale== 'auto' ? autoScale : scale));
} else {
console.error('No element found for liveStream'+monitorId);
}
}
var alarmState = STATE_IDLE;
var lastAlarmState = STATE_IDLE;
function setAlarmState( currentAlarmState ) {
alarmState = currentAlarmState;
2020-01-06 06:33:53 +08:00
var stateClass = '';
if ( alarmState == STATE_ALARM ) {
2020-01-06 06:33:53 +08:00
stateClass = 'alarm';
} else if ( alarmState == STATE_ALERT ) {
2020-01-06 06:33:53 +08:00
stateClass = 'alert';
}
2020-01-06 06:33:53 +08:00
$('stateValue').set('text', stateStrings[alarmState]);
if ( stateClass ) {
2020-01-06 06:33:53 +08:00
$('stateValue').setProperty('class', stateClass);
} else {
2020-01-06 06:33:53 +08:00
$('stateValue').removeProperty('class');
}
var isAlarmed = ( alarmState == STATE_ALARM || alarmState == STATE_ALERT );
var wasAlarmed = ( lastAlarmState == STATE_ALARM || lastAlarmState == STATE_ALERT );
var newAlarm = ( isAlarmed && !wasAlarmed );
var oldAlarm = ( !isAlarmed && wasAlarmed );
if ( newAlarm ) {
if ( SOUND_ON_ALARM ) {
// Enable the alarm sound
if ( !canPlayPauseAudio ) {
2020-03-05 00:22:08 +08:00
$('alarmSound').removeClass('hidden');
} else {
$('MediaPlayer').Play();
}
}
if ( POPUP_ON_ALARM ) {
window.focus();
}
}
2020-03-05 00:22:08 +08:00
if ( oldAlarm ) { // done with an event do a refresh
if ( SOUND_ON_ALARM ) {
// Disable alarm sound
if ( !canPlayPauseAudio ) {
2020-03-05 00:22:08 +08:00
$('alarmSound').addClass('hidden');
} else {
$('MediaPlayer').Stop();
}
}
eventCmdQuery();
}
lastAlarmState = alarmState;
2020-03-05 00:22:08 +08:00
} // end function setAlarmState( currentAlarmState )
if ( monitorType != 'WebSite' ) {
2020-01-06 06:33:53 +08:00
var streamCmdParms = 'view=request&request=stream&connkey='+connKey;
if ( auth_hash ) {
streamCmdParms += '&auth='+auth_hash;
}
var streamCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
timeout: AJAX_TIMEOUT,
link: 'chain',
onError: getStreamCmdError,
onSuccess: getStreamCmdResponse,
onFailure: getStreamCmdFailure
} );
var streamCmdTimer = null;
}
var streamStatus;
function getStreamCmdError(text, error) {
console.log(error);
// Error are normally due to failed auth. reload the page.
window.location.reload();
}
function getStreamCmdFailure(xhr) {
console.log(xhr);
}
function getStreamCmdResponse(respObj, respText) {
2020-01-06 06:33:53 +08:00
watchdogOk('stream');
if ( streamCmdTimer ) {
streamCmdTimer = clearTimeout(streamCmdTimer);
}
if ( respObj.result == 'Ok' ) {
// The get status command can get backed up, in which case we won't be able to get the semaphore and will exit.
if ( respObj.status ) {
streamStatus = respObj.status;
$('fpsValue').set('text', streamStatus.fps);
setAlarmState(streamStatus.state);
$('levelValue').set('text', streamStatus.level);
if ( streamStatus.level > 95 ) {
2020-03-05 00:22:08 +08:00
$('levelValue').className = 'alarm';
} else if ( streamStatus.level > 80 ) {
2020-03-05 00:22:08 +08:00
$('levelValue').className = 'alert';
} else {
2020-03-05 00:22:08 +08:00
$('levelValue').className = 'ok';
}
var delayString = secsToTime(streamStatus.delay);
if ( streamStatus.paused == true ) {
$('modeValue').set('text', 'Paused');
$('rate').addClass('hidden');
$('delayValue').set('text', delayString);
$('delay').removeClass('hidden');
$('level').removeClass('hidden');
streamCmdPause(false);
} else if ( streamStatus.delayed == true ) {
$('modeValue').set('text', 'Replay');
$('rateValue').set('text', streamStatus.rate);
$('rate').removeClass('hidden');
$('delayValue').set('text', delayString);
$('delay').removeClass('hidden');
$('level').removeClass('hidden');
if ( streamStatus.rate == 1 ) {
streamCmdPlay(false);
} else if ( streamStatus.rate > 0 ) {
if ( streamStatus.rate < 1 ) {
streamCmdSlowFwd(false);
} else {
streamCmdFastFwd(false);
}
} else {
if ( streamStatus.rate > -1 ) {
streamCmdSlowRev(false);
} else {
streamCmdFastRev(false);
}
} // rate
} else {
2020-03-05 00:22:08 +08:00
$('modeValue').set( 'text', 'Live' );
$('rate').addClass( 'hidden' );
$('delay').addClass( 'hidden' );
$('level').addClass( 'hidden' );
2019-09-27 04:13:49 +08:00
streamCmdPlay(false);
} // end if paused or delayed
2019-09-27 04:13:49 +08:00
$('zoomValue').set('text', streamStatus.zoom);
if ( streamStatus.zoom == '1.0' ) {
setButtonState('zoomOutBtn', 'unavail');
} else {
setButtonState('zoomOutBtn', 'inactive');
}
if ( canEditMonitors ) {
if ( streamStatus.enabled ) {
2019-09-27 04:13:49 +08:00
$('enableAlarmsLink').addClass('hidden');
$('disableAlarmsLink').removeClass('hidden');
if ( streamStatus.forced ) {
2019-09-27 04:13:49 +08:00
$('forceAlarmLink').addClass('hidden');
$('cancelAlarmLink').removeClass('hidden');
} else {
2019-09-27 04:13:49 +08:00
$('forceAlarmLink').removeClass('hidden');
$('cancelAlarmLink').addClass('hidden');
}
2019-09-27 04:13:49 +08:00
$('forceCancelAlarm').removeClass('hidden');
} else {
2019-09-27 04:13:49 +08:00
$('enableAlarmsLink').removeClass('hidden');
$('disableAlarmsLink').addClass('hidden');
$('forceCancelAlarm').addClass('hidden');
}
2019-09-27 04:13:49 +08:00
$('enableDisableAlarms').removeClass('hidden');
} // end if canEditMonitors
if ( streamStatus.auth ) {
auth_hash = streamStatus.auth;
// Try to reload the image stream.
var streamImg = $('liveStream');
if ( streamImg ) {
streamImg.src = streamImg.src.replace(/auth=\w+/i, 'auth='+streamStatus.auth);
}
streamCmdParms = streamCmdParms.replace(/auth=\w+/i, 'auth='+streamStatus.auth);
statusCmdParms = statusCmdParms.replace(/auth=\w+/i, 'auth='+streamStatus.auth);
eventCmdParms = eventCmdParms.replace(/auth=\w+/i, 'auth='+streamStatus.auth);
controlParms = controlParms.replace(/auth=\w+/i, 'auth='+streamStatus.auth);
} // end if have a new auth hash
} // end if respObj.status
} else {
2020-03-05 00:22:08 +08:00
checkStreamForErrors('getStreamCmdResponse', respObj);//log them
// Try to reload the image stream.
// If it's an auth error, we should reload the whole page.
window.location.reload();
if ( 0 ) {
var streamImg = $('liveStream'+monitorId);
if ( streamImg ) {
streamImg.src = streamImg.src.replace(/rand=\d+/i, 'rand='+Math.floor((Math.random() * 1000000) ));
2020-03-05 00:22:08 +08:00
console.log('Changing livestream src to ' + streamImg.src);
} else {
2020-03-05 00:22:08 +08:00
console.log('Unable to find streamImg liveStream');
}
}
}
var streamCmdTimeout = statusRefreshTimeout;
if ( alarmState == STATE_ALARM || alarmState == STATE_ALERT ) {
streamCmdTimeout = streamCmdTimeout/5;
}
2020-03-05 00:22:08 +08:00
streamCmdTimer = streamCmdQuery.delay(streamCmdTimeout);
2017-05-19 01:50:56 +08:00
}
function streamCmdPause( action ) {
setButtonState('pauseBtn', 'active');
setButtonState('playBtn', 'inactive');
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'inactive');
setButtonState('slowRevBtn', 'inactive');
setButtonState('fastRevBtn', 'inactive');
}
if ( action ) {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_PAUSE);
}
}
function streamCmdPlay( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'active');
if ( streamStatus.delayed == true ) {
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'inactive');
setButtonState('slowRevBtn', 'inactive');
setButtonState('fastRevBtn', 'inactive');
}
} else {
setButtonState('stopBtn', 'unavail');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'unavail');
setButtonState('slowFwdBtn', 'unavail');
setButtonState('slowRevBtn', 'unavail');
setButtonState('fastRevBtn', 'unavail');
}
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_PLAY);
}
}
function streamCmdStop( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'unavail');
setButtonState('stopBtn', 'active');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'unavail');
setButtonState('slowFwdBtn', 'unavail');
setButtonState('slowRevBtn', 'unavail');
setButtonState('fastRevBtn', 'unavail');
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_STOP);
}
setButtonState('stopBtn', 'unavail');
setButtonState('playBtn', 'active');
}
function streamCmdFastFwd( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'inactive');
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'inactive');
setButtonState('slowRevBtn', 'inactive');
setButtonState('fastRevBtn', 'inactive');
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_FASTFWD);
}
}
function streamCmdSlowFwd( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'inactive');
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'active');
setButtonState('slowRevBtn', 'inactive');
setButtonState('fastRevBtn', 'inactive');
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_SLOWFWD);
}
setButtonState('pauseBtn', 'active');
if ( monitorStreamReplayBuffer ) {
setButtonState('slowFwdBtn', 'inactive');
}
}
function streamCmdSlowRev( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'inactive');
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'inactive');
setButtonState('slowRevBtn', 'active');
setButtonState('fastRevBtn', 'inactive');
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_SLOWREV);
}
setButtonState('pauseBtn', 'active');
if ( monitorStreamReplayBuffer ) {
setButtonState('slowRevBtn', 'inactive');
}
}
function streamCmdFastRev( action ) {
setButtonState('pauseBtn', 'inactive');
setButtonState('playBtn', 'inactive');
setButtonState('stopBtn', 'inactive');
if ( monitorStreamReplayBuffer ) {
setButtonState('fastFwdBtn', 'inactive');
setButtonState('slowFwdBtn', 'inactive');
setButtonState('slowRevBtn', 'inactive');
setButtonState('fastRevBtn', 'inactive');
}
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_FASTREV);
}
}
function streamCmdZoomIn( x, y ) {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_ZOOMIN+"&x="+x+"&y="+y);
}
function streamCmdZoomOut() {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_ZOOMOUT);
}
function streamCmdScale( scale ) {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_SCALE+"&scale="+scale);
}
function streamCmdPan( x, y ) {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_PAN+"&x="+x+"&y="+y);
}
function streamCmdQuery() {
2020-03-05 00:22:08 +08:00
streamCmdReq.send(streamCmdParms+"&command="+CMD_QUERY);
}
if ( monitorType != 'WebSite' ) {
var statusCmdParms = "view=request&request=status&entity=monitor&id="+monitorId+"&element[]=Status&element[]=FrameRate";
if ( auth_hash ) {
statusCmdParms += '&auth='+auth_hash;
}
2020-03-05 00:22:08 +08:00
var statusCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getStatusCmdResponse
} );
var statusCmdTimer = null;
}
function getStatusCmdResponse(respObj, respText) {
2020-03-05 00:22:08 +08:00
watchdogOk('status');
if ( statusCmdTimer ) {
statusCmdTimer = clearTimeout(statusCmdTimer);
}
if ( respObj.result == 'Ok' ) {
$('fpsValue').set('text', respObj.monitor.FrameRate);
setAlarmState(respObj.monitor.Status);
} else {
2020-03-05 00:22:08 +08:00
checkStreamForErrors('getStatusCmdResponse', respObj);
}
var statusCmdTimeout = statusRefreshTimeout;
if ( alarmState == STATE_ALARM || alarmState == STATE_ALERT ) {
statusCmdTimeout = statusCmdTimeout/5;
}
2020-03-05 00:22:08 +08:00
statusCmdTimer = statusCmdQuery.delay(statusCmdTimeout);
}
function statusCmdQuery() {
2020-03-05 00:22:08 +08:00
statusCmdReq.send(statusCmdParms);
}
if ( monitorType != 'WebSite' ) {
2020-03-05 00:22:08 +08:00
var alarmCmdParms = 'view=request&request=alarm&id='+monitorId;
if ( auth_hash ) {
alarmCmdParms += '&auth='+auth_hash;
}
var alarmCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getAlarmCmdResponse,
onTimeout: streamCmdQuery
} );
var alarmCmdFirst = true;
}
2020-03-05 00:22:08 +08:00
function getAlarmCmdResponse(respObj, respText) {
checkStreamForErrors('getAlarmCmdResponse', respObj);
}
function cmdDisableAlarms() {
alarmCmdReq.send(alarmCmdParms+"&command=disableAlarms");
}
function cmdEnableAlarms() {
alarmCmdReq.send(alarmCmdParms+"&command=enableAlarms");
}
function cmdForceAlarm() {
alarmCmdReq.send(alarmCmdParms+"&command=forceAlarm");
2020-03-05 00:22:08 +08:00
if ( window.event ) {
window.event.preventDefault();
}
}
function cmdCancelForcedAlarm() {
alarmCmdReq.send(alarmCmdParms+"&command=cancelForcedAlarm");
2020-03-05 00:22:08 +08:00
if ( window.event ) {
window.event.preventDefault();
}
2017-08-04 00:41:43 +08:00
return false;
}
function getActResponse( respObj, respText ) {
if ( respObj.result == 'Ok' ) {
if ( respObj.refreshParent && window.opener ) {
console.log('refreshing parent');
window.opener.location.reload();
}
}
eventCmdQuery();
}
2020-03-05 00:22:08 +08:00
function deleteEvent(event, eventId) {
var actParms = 'view=request&request=event&action=delete&id='+eventId;
if ( auth_hash ) {
actParms += '&auth='+auth_hash;
}
var actReq = new Request.JSON( {
url: thisUrl,
method: 'post',
timeout: 3000,
onSuccess: getActResponse
} );
2020-03-05 00:22:08 +08:00
actReq.send(actParms);
event.stop();
}
if ( monitorType != 'WebSite' ) {
var eventCmdParms = "view=request&request=status&entity=events&id="+monitorId+"&count="+maxDisplayEvents+"&sort=Id%20desc";
if ( auth_hash ) {
eventCmdParms += '&auth='+auth_hash;
}
var eventCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getEventCmdResponse,
onTimeout: eventCmdQuery
} );
var eventCmdTimer = null;
var eventCmdFirst = true;
}
function highlightRow( row ) {
$(row).toggleClass('highlight');
}
function getEventCmdResponse( respObj, respText ) {
2020-03-05 00:22:08 +08:00
watchdogOk('event');
if ( eventCmdTimer ) {
2020-03-05 00:22:08 +08:00
eventCmdTimer = clearTimeout(eventCmdTimer);
}
if ( respObj.result == 'Ok' ) {
var dbEvents = respObj.events.reverse();
var eventList = $('eventList');
2020-03-05 00:22:08 +08:00
var eventListBody = $(eventList).getElement('tbody');
var eventListRows = $(eventListBody).getElements('tr');
2020-03-05 00:22:08 +08:00
eventListRows.each( function(row) {
row.removeClass('updated');
} );
for ( var i = 0; i < dbEvents.length; i++ ) {
var zm_event = dbEvents[i];
var row = $('event'+zm_event.Id);
var newEvent = (row == null ? true : false);
if ( newEvent ) {
row = new Element('tr', {'id': 'event'+zm_event.Id});
new Element('td', {'class': 'colId'}).inject(row);
new Element('td', {'class': 'colName'}).inject(row);
new Element('td', {'class': 'colTime'}).inject(row);
new Element('td', {'class': 'colSecs'}).inject(row);
new Element('td', {'class': 'colFrames'}).inject(row);
new Element('td', {'class': 'colScore'}).inject(row);
new Element('td', {'class': 'colDelete'}).inject(row);
var link = new Element('a', {
'href': '#',
'events': {
'click': createEventPopup.pass( [
zm_event.Id,
'&filter[Query][terms][0][attr]=MonitorId&filter[Query][terms][0][op]=%3d&filter[Query][terms][0][val]='+monitorId+'&page=1&popup=1',
zm_event.Width,
zm_event.Height
] )
}
});
link.set('text', zm_event.Id);
link.inject(row.getElement('td.colId'));
link = new Element('a', {
'href': '#',
'events': {
'click': createEventPopup.pass( [
zm_event.Id,
'&filter[Query][terms][0][attr]=MonitorId&filter[Query][terms][0][op]=%3d&filter[Query][terms][0][val]='+monitorId+'&page=1&popup=1',
zm_event.Width,
zm_event.Height
] )
}
});
link.set('text', zm_event.Name);
link.inject(row.getElement('td.colName'));
row.getElement('td.colTime').set('text', zm_event.StartTime);
row.getElement('td.colSecs').set('text', zm_event.Length);
link = new Element('a', {'href': '#', 'events': {'click': createFramesPopup.pass( [zm_event.Id, zm_event.Width, zm_event.Height] )}});
link.set('text', zm_event.Frames+'/'+zm_event.AlarmFrames);
link.inject(row.getElement('td.colFrames'));
link = new Element('a', {'href': '#', 'events': {'click': createFramePopup.pass( [zm_event.Id, '0', zm_event.Width, zm_event.Height] )}});
link.set('text', zm_event.AvgScore+'/'+zm_event.MaxScore);
link.inject(row.getElement('td.colScore'));
link = new Element('button', {
'type': 'button',
'title': deleteString,
'data-event-id': zm_event.Id,
'events': {
'click': function(e) {
var event_id = e.target.getAttribute('data-event-id');
if ( !event_id ) {
console.log('No event id in deleteEvent');
console.log(e);
} else {
deleteEvent(e, event_id);
}
},
'mouseover': highlightRow.pass(row),
'mouseout': highlightRow.pass(row)
}
});
2020-03-05 00:22:08 +08:00
link.set('text', 'X');
link.inject(row.getElement('td.colDelete'));
if ( i == 0 ) {
2020-03-05 00:22:08 +08:00
row.inject($(eventListBody));
} else {
2020-03-05 00:22:08 +08:00
row.inject($(eventListBody), 'top');
if ( !eventCmdFirst ) {
2020-03-05 00:22:08 +08:00
row.addClass('recent');
}
}
} else {
row.getElement('td.colName a').set('text', zm_event.Name);
row.getElement('td.colSecs').set('text', zm_event.Length);
row.getElement('td.colFrames a').set('text', zm_event.Frames+'/'+zm_event.AlarmFrames);
row.getElement('td.colScore a').set('text', zm_event.AvgScore+'/'+zm_event.MaxScore);
2020-03-05 00:22:08 +08:00
row.removeClass('recent');
}
2020-03-05 00:22:08 +08:00
row.addClass('updated');
2020-05-30 01:28:33 +08:00
} // end foreach event
2020-03-05 00:22:08 +08:00
var rows = $(eventListBody).getElements('tr');
for ( var i = 0; i < rows.length; i++ ) {
2020-03-05 00:22:08 +08:00
if ( !rows[i].hasClass('updated') ) {
rows[i].destroy();
rows.splice( i, 1 );
i--;
}
}
while ( rows.length > maxDisplayEvents ) {
rows[rows.length-1].destroy();
rows.length--;
}
} else {
2020-03-05 00:22:08 +08:00
checkStreamForErrors('getEventCmdResponse', respObj);
2020-05-30 01:28:33 +08:00
} // end if objresult == ok
var eventCmdTimeout = eventsRefreshTimeout;
if ( alarmState == STATE_ALARM || alarmState == STATE_ALERT ) {
eventCmdTimeout = eventCmdTimeout/5;
}
2020-03-05 00:22:08 +08:00
eventCmdTimer = eventCmdQuery.delay(eventCmdTimeout);
eventCmdFirst = false;
}
function eventCmdQuery() {
if ( eventCmdTimer ) { // avoid firing another if we are firing one
2020-03-05 00:22:08 +08:00
eventCmdTimer = clearTimeout(eventCmdTimer);
}
eventCmdReq.send(eventCmdParms);
}
if ( monitorType != 'WebSite' ) {
2020-03-05 00:22:08 +08:00
var controlParms = 'view=request&request=control&id='+monitorId;
if ( auth_hash ) {
controlParms += '&auth='+auth_hash;
}
var controlReq = new Request.JSON( {
url: monitorUrl,
method: 'post',
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getControlResponse
} );
}
2020-03-05 00:22:08 +08:00
function getControlResponse(respObj, respText) {
if ( !respObj ) {
return;
}
//console.log( respText );
if ( respObj.result != 'Ok' ) {
2020-03-05 00:22:08 +08:00
alert("Control response was status = "+respObj.status+"\nmessage = "+respObj.message);
}
}
function controlCmd(event) {
button = event.target;
2020-04-10 23:30:30 +08:00
control = button.getAttribute('value');
xtell = button.getAttribute('data-xtell');
ytell = button.getAttribute('data-ytell');
2020-04-10 23:30:30 +08:00
var locParms = '';
if ( event && (xtell || ytell) ) {
var target = event.target;
var coords = $(target).getCoordinates();
var x = event.pageX - coords.left;
var y = event.pageY - coords.top;
2017-05-19 01:50:56 +08:00
if ( xtell ) {
var xge = parseInt((x*100)/coords.width);
if ( xtell == -1 ) {
xge = 100 - xge;
} else if ( xtell == 2 ) {
xge = 2*(50 - xge);
}
2020-04-10 23:30:30 +08:00
locParms += '&xge='+xge;
}
if ( ytell ) {
var yge = parseInt((y*100)/coords.height);
if ( ytell == -1 ) {
yge = 100 - yge;
} else if ( ytell == 2 ) {
yge = 2*(50 - yge);
}
2020-04-10 23:30:30 +08:00
locParms += '&yge='+yge;
}
}
2020-03-05 00:22:08 +08:00
controlReq.send(controlParms+"&control="+control+locParms);
if ( streamMode == 'single' ) {
fetchImage.pass($('imageFeed').getElement('img')).delay(1000);
}
}
function controlCmdImage( x, y ) {
var imageControlParms = controlParms;
2020-03-05 00:22:08 +08:00
imageControlParms += '&scale='+scale;
imageControlParms += '&control='+imageControlMode;
controlReq.send( imageControlParms+"&x="+x+"&y="+y );
2020-03-05 00:22:08 +08:00
if ( streamMode == 'single' ) {
fetchImage.pass( $('imageFeed').getElement('img') ).delay( 1000 );
}
}
function fetchImage( streamImage ) {
streamImage.src = streamImage.src.replace(/rand=\d+/i, 'rand='+Math.floor((Math.random() * 1000000) ));
}
function handleClick( event ) {
var target = event.target;
var x = event.page.x - $(target).getLeft();
var y = event.page.y - $(target).getTop();
2020-03-05 00:22:08 +08:00
if ( showMode == 'events' || !imageControlMode ) {
if ( event.shift ) {
streamCmdPan( x, y );
} else if ( event.event.ctrlKey ) {
streamCmdZoomOut();
} else {
2019-09-27 04:13:49 +08:00
streamCmdZoomIn(x, y);
}
} else {
2019-09-27 04:13:49 +08:00
controlCmdImage(x, y);
}
}
function appletRefresh() {
if ( streamStatus && (!streamStatus.paused && !streamStatus.delayed) ) {
var streamImg = $('liveStream'+monitorId);
if ( streamImg ) {
var parent = streamImg.getParent();
streamImg.dispose();
streamImg.inject( parent );
} else {
console.error("Nothing found for liveStream"+monitorId);
}
if ( appletRefreshTime ) {
appletRefresh.delay( appletRefreshTime*1000 );
}
} else {
appletRefresh.delay( 15*1000 ); //if we are paused or delayed check every 15 seconds if we are live yet...
}
}
var watchdogInactive = {
'stream': false,
'status': false,
'event': false
};
var watchdogFunctions = {
'stream': streamCmdQuery,
'status': statusCmdQuery,
'event': eventCmdQuery
};
//Make sure the various refreshes are still taking effect
function watchdogCheck( type ) {
if ( watchdogInactive[type] ) {
console.log("Detected streamWatch of type: " + type + " stopped, restarting");
watchdogFunctions[type]();
watchdogInactive[type] = false;
} else {
watchdogInactive[type] = true;
}
}
function watchdogOk( type ) {
watchdogInactive[type] = false;
}
function reloadWebSite() {
document.getElementById('imageFeed').innerHTML = document.getElementById('imageFeed').innerHTML;
}
function initPage() {
if ( monitorType != 'WebSite' ) {
if ( streamMode == 'single' ) {
statusCmdTimer = statusCmdQuery.delay( (Math.random()+0.1)*statusRefreshTimeout );
watchdogCheck.pass('status').periodical(statusRefreshTimeout*2);
} else {
streamCmdTimer = streamCmdQuery.delay( (Math.random()+0.1)*statusRefreshTimeout );
watchdogCheck.pass('stream').periodical(statusRefreshTimeout*2);
}
eventCmdTimer = eventCmdQuery.delay( (Math.random()+0.1)*statusRefreshTimeout );
watchdogCheck.pass('event').periodical(eventsRefreshTimeout*2);
if ( canStreamNative || (streamMode == 'single') ) {
var streamImg = $('imageFeed').getElement('img');
if ( !streamImg ) {
streamImg = $('imageFeed').getElement('object');
}
if ( !streamImg ) {
console.error('No streamImg found for imageFeed');
} else {
if ( streamMode == 'single' ) {
streamImg.addEvent('click', fetchImage.pass(streamImg));
fetchImage.pass(streamImg).periodical(imageRefreshTimeout);
} else {
streamImg.addEvent('click', function(event) {
handleClick(event);
});
}
} // end if have streamImg
} // streamMode native or single
if ( refreshApplet && appletRefreshTime ) {
appletRefresh.delay(appletRefreshTime*1000);
}
if ( scale == 'auto' || scale == '0' ) changeScale();
if ( window.history.length == 1 ) {
$j('#closeControl').html('');
}
2019-09-20 22:44:38 +08:00
document.querySelectorAll('select[name="scale"]').forEach(function(el) {
2019-09-20 02:57:17 +08:00
el.onchange = window['changeScale'];
});
} else if ( monitorRefresh > 0 ) {
setInterval(reloadWebSite, monitorRefresh*1000);
}
} // initPage
// Kick everything off
window.addEventListener('DOMContentLoaded', initPage);