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

822 lines
27 KiB
JavaScript
Raw Normal View History

2017-04-06 04:09:40 +08:00
function validateForm( form ) {
2017-05-19 02:06:13 +08:00
var errors = new Array();
if ( selfIntersecting ) {
errors[errors.length] = selfIntersectingString;
}
if ( form.elements['newZone[Type]'].value != 'Inactive' && form.elements['newZone[Type]'].value != 'Privacy' ) {
if ( !form.newAlarmRgbR.value || !form.newAlarmRgbG.value || !form.newAlarmRgbB.value ) {
errors[errors.length] = alarmRGBUnsetString;
}
2017-05-19 02:06:13 +08:00
form.elements['newZone[AlarmRGB]'].value = (form.newAlarmRgbR.value<<16)|(form.newAlarmRgbG.value<<8)|form.newAlarmRgbB.value;
if ( !form.elements['newZone[MinPixelThreshold]'].value || (parseInt(form.elements['newZone[MinPixelThreshold]'].value) <= 0 ) ) {
errors[errors.length] = minPixelThresUnsetString;
} else if ( (parseInt(form.elements['newZone[MinPixelThreshold]'].value) >= parseInt(form.elements['newZone[MaxPixelThreshold]'].value)) && (parseInt(form.elements['newZone[MaxPixelThreshold]'].value) > 0) ) {
errors[errors.length] = minPixelThresLtMaxString;
}
if ( form.elements['newZone[CheckMethod]'].value == 'FilteredPixels' || form.elements['newZone[CheckMethod]'].value == 'Blobs' ) {
if ( !form.elements['newZone[FilterX]'].value || !form.elements['newZone[FilterY]'].value ) {
errors[errors.length] = filterUnsetString;
}
}
if ( !form.elements['newZone[MinAlarmPixels]'].value || (parseFloat(form.elements['newZone[MinAlarmPixels]'].value) <= 0 ) ) {
errors[errors.length] = minAlarmAreaUnsetString;
} else if ( (parseFloat(form.elements['newZone[MinAlarmPixels]'].value) >= parseFloat(form.elements['newZone[MaxAlarmPixels]'].value)) && (parseFloat(form.elements['newZone[MaxAlarmPixels]'].value) > 0) ) {
errors[errors.length] = minAlarmAreaLtMaxString;
}
if ( form.elements['newZone[CheckMethod]'].value == 'FilteredPixels' || form.elements['newZone[CheckMethod]'].value == 'Blobs' ) {
if ( !form.elements['newZone[MinFilterPixels]'].value || (parseFloat(form.elements['newZone[MinFilterPixels]'].value) <= 0 ) ) {
errors[errors.length] = minFilterAreaUnsetString;
} else if ( (parseFloat(form.elements['newZone[MinFilterPixels]'].value) >= parseFloat(form.elements['newZone[MaxFilterPixels]'].value)) && (parseFloat(form.elements['newZone[MaxFilterPixels]'].value) > 0) ) {
errors[errors.length] = minFilterAreaLtMaxString;
} else if ( parseFloat(form.elements['newZone[MinAlarmPixels]'].value) < parseFloat(form.elements['newZone[MinFilterPixels]'].value) ) {
errors[errors.length] = minFilterLtMinAlarmString;
}
if ( form.elements['newZone[CheckMethod]'].value == 'Blobs' ) {
if ( !form.elements['newZone[MinBlobPixels]'].value || (parseFloat(form.elements['newZone[MinBlobPixels]'].value) <= 0 ) ) {
errors[errors.length] = minBlobAreaUnsetString;
} else if ( (parseFloat(form.elements['newZone[MinBlobPixels]'].value) >= parseFloat(form.elements['newZone[MaxBlobPixels]'].value)) && (parseFloat(form.elements['newZone[MaxBlobPixels]'].value) > 0) ) {
errors[errors.length] = minBlobAreaLtMaxString;
} else if ( parseFloat(form.elements['newZone[MinFilterPixels]'].value) < parseFloat(form.elements['newZone[MinBlobPixels]'].value) ) {
errors[errors.length] = minBlobLtMinFilterString;
}
2017-05-19 02:06:13 +08:00
if ( !form.elements['newZone[MinBlobs]'].value || (parseInt(form.elements['newZone[MinBlobs]'].value) <= 0 ) ) {
errors[errors.length] = minBlobsUnsetString;
} else if ( (parseInt(form.elements['newZone[MinBlobs]'].value) >= parseInt(form.elements['newZone[MaxBlobs]'].value)) && (parseInt(form.elements['newZone[MaxBlobs]'].value) > 0) ) {
errors[errors.length] = minBlobsLtMaxString;
}
2017-05-19 02:06:13 +08:00
}
}
2017-05-19 02:06:13 +08:00
}
if ( errors.length ) {
alert(errors.join("\n"));
return false;
2017-05-19 02:06:13 +08:00
}
return true;
}
function submitForm(form) {
2017-05-19 02:06:13 +08:00
form.elements['newZone[AlarmRGB]'].value = (form.newAlarmRgbR.value<<16)|(form.newAlarmRgbG.value<<8)|form.newAlarmRgbB.value;
form.elements['newZone[NumCoords]'].value = zone['Points'].length;
form.elements['newZone[Coords]'].value = getCoordString();
form.elements['newZone[Area]'].value = zone.Area;
2017-05-19 02:06:13 +08:00
form.submit();
}
2017-04-06 04:09:40 +08:00
function applyZoneType() {
2017-05-19 02:06:13 +08:00
var form = document.zoneForm;
if ( form.elements['newZone[Type]'].value == 'Inactive' || form.elements['newZone[Type]'].value == 'Privacy' ) {
form.presetSelector.disabled = true;
form.newAlarmRgbR.disabled = true;
form.newAlarmRgbG.disabled = true;
form.newAlarmRgbB.disabled = true;
form.elements['newZone[CheckMethod]'].disabled = true;
form.elements['newZone[MinPixelThreshold]'].disabled = true;
form.elements['newZone[MaxPixelThreshold]'].disabled = true;
form.elements['newZone[MinAlarmPixels]'].disabled = true;
form.elements['newZone[MaxAlarmPixels]'].disabled = true;
form.elements['newZone[FilterX]'].disabled = true;
form.elements['newZone[FilterY]'].disabled = true;
form.elements['newZone[MinFilterPixels]'].disabled = true;
form.elements['newZone[MaxFilterPixels]'].disabled = true;
form.elements['newZone[MinBlobPixels]'].disabled = true;
form.elements['newZone[MaxBlobPixels]'].disabled = true;
form.elements['newZone[MinBlobs]'].disabled = true;
form.elements['newZone[MaxBlobs]'].disabled = true;
form.elements['newZone[OverloadFrames]'].disabled = true;
form.elements['newZone[ExtendAlarmFrames]'].disabled = true;
} else if ( form.elements['newZone[Type]'].value == 'Preclusive' ) {
form.presetSelector.disabled = false;
form.newAlarmRgbR.disabled = true;
form.newAlarmRgbG.disabled = true;
form.newAlarmRgbB.disabled = true;
form.elements['newZone[CheckMethod]'].disabled = false;
form.elements['newZone[MinPixelThreshold]'].disabled = false;
form.elements['newZone[MaxPixelThreshold]'].disabled = false;
form.elements['newZone[MinAlarmPixels]'].disabled = false;
form.elements['newZone[MaxAlarmPixels]'].disabled = false;
form.elements['newZone[OverloadFrames]'].disabled = false;
form.elements['newZone[ExtendAlarmFrames]'].disabled = false;
applyCheckMethod();
} else {
form.presetSelector.disabled = false;
form.newAlarmRgbR.disabled = false;
form.newAlarmRgbG.disabled = false;
form.newAlarmRgbB.disabled = false;
form.elements['newZone[CheckMethod]'].disabled = false;
form.elements['newZone[MinPixelThreshold]'].disabled = false;
form.elements['newZone[MaxPixelThreshold]'].disabled = false;
form.elements['newZone[MinAlarmPixels]'].disabled = false;
form.elements['newZone[MaxAlarmPixels]'].disabled = false;
form.elements['newZone[OverloadFrames]'].disabled = false;
form.elements['newZone[ExtendAlarmFrames]'].disabled = true;
applyCheckMethod();
}
}
2017-04-06 04:09:40 +08:00
function applyCheckMethod() {
2017-05-19 02:06:13 +08:00
var form = document.zoneForm;
if ( form.elements['newZone[CheckMethod]'].value == 'AlarmedPixels' ) {
form.elements['newZone[FilterX]'].disabled = true;
form.elements['newZone[FilterY]'].disabled = true;
form.elements['newZone[MinFilterPixels]'].disabled = true;
form.elements['newZone[MaxFilterPixels]'].disabled = true;
form.elements['newZone[MinBlobPixels]'].disabled = true;
form.elements['newZone[MaxBlobPixels]'].disabled = true;
form.elements['newZone[MinBlobs]'].disabled = true;
form.elements['newZone[MaxBlobs]'].disabled = true;
} else if ( form.elements['newZone[CheckMethod]'].value == 'FilteredPixels' ) {
form.elements['newZone[FilterX]'].disabled = false;
form.elements['newZone[FilterY]'].disabled = false;
form.elements['newZone[MinFilterPixels]'].disabled = false;
form.elements['newZone[MaxFilterPixels]'].disabled = false;
form.elements['newZone[MinBlobPixels]'].disabled = true;
form.elements['newZone[MaxBlobPixels]'].disabled = true;
form.elements['newZone[MinBlobs]'].disabled = true;
form.elements['newZone[MaxBlobs]'].disabled = true;
} else {
form.elements['newZone[FilterX]'].disabled = false;
form.elements['newZone[FilterY]'].disabled = false;
form.elements['newZone[MinFilterPixels]'].disabled = false;
form.elements['newZone[MaxFilterPixels]'].disabled = false;
form.elements['newZone[MinBlobPixels]'].disabled = false;
form.elements['newZone[MaxBlobPixels]'].disabled = false;
form.elements['newZone[MinBlobs]'].disabled = false;
form.elements['newZone[MaxBlobs]'].disabled = false;
}
}
2017-04-06 04:09:40 +08:00
function applyPreset() {
2017-05-19 02:06:13 +08:00
var form = document.zoneForm;
var presetId = $('presetSelector').get('value');
if ( presets[presetId] ) {
var preset = presets[presetId];
form.elements['newZone[Units]'].selectedIndex = preset['UnitsIndex'];
form.elements['newZone[CheckMethod]'].selectedIndex = preset['CheckMethodIndex'];
form.elements['newZone[MinPixelThreshold]'].value = preset['MinPixelThreshold'];
form.elements['newZone[MaxPixelThreshold]'].value = preset['MaxPixelThreshold'];
form.elements['newZone[FilterX]'].value = preset['FilterX'];
form.elements['newZone[FilterY]'].value = preset['FilterY'];
form.elements['newZone[MinAlarmPixels]'].value = preset['MinAlarmPixels'];
form.elements['newZone[MaxAlarmPixels]'].value = preset['MaxAlarmPixels'];
form.elements['newZone[MinFilterPixels]'].value = preset['MinFilterPixels'];
form.elements['newZone[MaxFilterPixels]'].value = preset['MaxFilterPixels'];
form.elements['newZone[MinBlobPixels]'].value = preset['MinBlobPixels'];
form.elements['newZone[MaxBlobPixels]'].value = preset['MaxBlobPixels'];
form.elements['newZone[MinBlobs]'].value = preset['MinBlobs'];
form.elements['newZone[MaxBlobs]'].value = preset['MaxBlobs'];
form.elements['newZone[OverloadFrames]'].value = preset['OverloadFrames'];
form.elements['newZone[ExtendAlarmFrames]'].value = preset['ExtendAlarmFrames'];
applyCheckMethod();
form.elements['newZone[TempArea]'].value = 100;
}
}
function toPixels(field, maxValue) {
if ( field.value != '' ) {
2017-05-19 02:06:13 +08:00
field.value = Math.round((field.value*maxValue)/100);
if ( field.value > maxValue )
field.value = maxValue;
}
field.setAttribute('step', 1);
field.setAttribute('max', maxValue);
}
// maxValue is the max Pixels value which is normally the max area
function toPercent(field, maxValue) {
if ( field.value != '' ) {
2017-05-19 02:06:13 +08:00
field.value = Math.round((100*100*field.value)/maxValue)/100;
if ( field.value > 100 ) {
field.value = 100;
}
}
field.setAttribute('step', 0.01);
field.setAttribute('max', 100);
}
2017-04-06 04:09:40 +08:00
function applyZoneUnits() {
2017-05-19 02:06:13 +08:00
var area = zone.Area;
2017-05-19 02:06:13 +08:00
var form = document.zoneForm;
if ( form.elements['newZone[Units]'].value == 'Pixels' ) {
form.elements['newZone[TempArea]'].value = area;
toPixels(form.elements['newZone[MinAlarmPixels]'], area);
toPixels(form.elements['newZone[MaxAlarmPixels]'], area);
toPixels(form.elements['newZone[MinFilterPixels]'], area);
toPixels(form.elements['newZone[MaxFilterPixels]'], area);
toPixels(form.elements['newZone[MinBlobPixels]'], area);
toPixels(form.elements['newZone[MaxBlobPixels]'], area);
2017-05-19 02:06:13 +08:00
} else {
form.elements['newZone[TempArea]'].value = Math.round(area/monitorArea * 100);
toPercent(form.elements['newZone[MinAlarmPixels]'], area);
toPercent(form.elements['newZone[MaxAlarmPixels]'], area);
toPercent(form.elements['newZone[MinFilterPixels]'], area);
toPercent(form.elements['newZone[MaxFilterPixels]'], area);
toPercent(form.elements['newZone[MinBlobPixels]'], area);
toPercent(form.elements['newZone[MaxBlobPixels]'], area);
2017-05-19 02:06:13 +08:00
}
}
function limitRange(field, minValue, maxValue) {
if ( field.value != '' ) {
field.value = constrainValue(
2019-11-20 00:37:45 +08:00
parseInt(field.value),
parseInt(minValue),
parseInt(maxValue)
);
}
}
function limitRangeToUnsignedByte(field) {
if ( field.value != '' ) {
field.value = constrainValue(parseInt(field.value), 0, 255);
}
}
function limitFilter(field) {
2017-05-19 02:06:13 +08:00
field.value = (Math.floor((field.value-1)/2)*2) + 1;
field.value = constrainValue(parseInt(field.value), 3, 15);
}
function limitArea(field) {
2017-05-19 02:06:13 +08:00
var minValue = 0;
var maxValue = zone.Area;
if ( document.zoneForm.elements['newZone[Units]'].value == 'Percent' ) {
2017-05-19 02:06:13 +08:00
maxValue = 100;
}
limitRange(field, minValue, maxValue);
}
function highlightOn(index) {
$('row'+index).addClass('highlight');
$('point'+index).addClass('highlight');
}
function highlightOff(index) {
$('row'+index).removeClass('highlight');
$('point'+index).removeClass('highlight');
}
function setActivePoint(index) {
highlightOff(index);
$('row'+index).addClass('active');
$('point'+index).addClass('active');
}
function unsetActivePoint(index) {
$('row'+index).removeClass('active');
$('point'+index).removeClass('active');
}
2017-04-06 04:09:40 +08:00
function getCoordString() {
2017-05-19 02:06:13 +08:00
var coords = new Array();
for ( var i = 0; i < zone['Points'].length; i++ ) {
2017-05-19 02:06:13 +08:00
coords[coords.length] = zone['Points'][i].x+','+zone['Points'][i].y;
}
return coords.join(' ');
}
2017-04-06 04:09:40 +08:00
function updateZoneImage() {
2017-05-19 02:06:13 +08:00
var SVG = $('zoneSVG');
var Poly = $('zonePoly');
Poly.points.clear();
for ( var i = 0; i < zone['Points'].length; i++ ) {
var Point = SVG.createSVGPoint();
Point.x = zone['Points'][i].x;
Point.y = zone['Points'][i].y;
Poly.points.appendItem(Point);
2017-05-19 02:06:13 +08:00
}
}
function fixActivePoint(index) {
updateActivePoint(index);
unsetActivePoint(index);
2017-05-19 02:06:13 +08:00
updateZoneImage();
}
function constrainValue(value, loVal, hiVal) {
2017-05-19 02:06:13 +08:00
if ( value < loVal ) {
return loVal;
}
if ( value > hiVal ) {
return hiVal;
}
return value;
}
function updateActivePoint(index) {
2017-05-19 02:06:13 +08:00
var point = $('point'+index);
var x = constrainValue(point.getStyle('left').toInt(), 0, maxX);
var y = constrainValue(point.getStyle('top').toInt(), 0, maxY);
2017-05-19 02:06:13 +08:00
$('newZone[Points]['+index+'][x]').value = x;
$('newZone[Points]['+index+'][y]').value = y;
zone['Points'][index].x = x;
zone['Points'][index].y = y;
var Point = $('zonePoly').points.getItem(index);
2020-07-08 06:57:11 +08:00
Point.x = x;
Point.y = y;
2017-05-19 02:06:13 +08:00
updateArea();
}
function addPoint(index) {
2017-05-19 02:06:13 +08:00
var nextIndex = index+1;
if ( index >= (zone['Points'].length-1) ) {
2017-05-19 02:06:13 +08:00
nextIndex = 0;
}
2017-05-19 02:06:13 +08:00
var newX = parseInt(Math.round((zone['Points'][index]['x']+zone['Points'][nextIndex]['x'])/2));
var newY = parseInt(Math.round((zone['Points'][index]['y']+zone['Points'][nextIndex]['y'])/2));
if ( nextIndex == 0 ) {
zone['Points'][zone['Points'].length] = {'x': newX, 'y': newY};
} else {
zone['Points'].splice(nextIndex, 0, {'x': newX, 'y': newY});
}
2017-05-19 02:06:13 +08:00
drawZonePoints();
}
function delPoint(index) {
zone['Points'].splice(index, 1);
2017-05-19 02:06:13 +08:00
drawZonePoints();
}
function limitPointValue(point, loVal, hiVal) {
2017-05-19 02:09:46 +08:00
point.value = constrainValue(point.value, loVal, hiVal);
}
2017-05-19 02:09:46 +08:00
function updateArea( ) {
area = Polygon_calcArea(zone['Points']);
2017-05-19 02:09:46 +08:00
zone.Area = area;
var form = $('zoneForm');
form.elements['newZone[Area]'].value = area;
if ( form.elements['newZone[Units]'].value == 'Percent' ) {
form.elements['newZone[TempArea]'].value = Math.round( area/monitorArea*100 );
} else if ( form.elements['newZone[Units]'].value == 'Pixels' ) {
form.elements['newZone[TempArea]'].value = area;
} else {
2020-07-08 06:57:11 +08:00
alert('Unknown units: ' + form.elements['newZone[Units]'].value);
2016-06-07 23:40:53 +08:00
}
2017-05-19 02:09:46 +08:00
}
2016-06-07 23:40:53 +08:00
2020-07-08 06:57:11 +08:00
function updateX(input) {
index = input.getAttribute('data-point-index');
limitPointValue(input, 0, maxX);
2017-05-19 02:09:46 +08:00
var point = $('point'+index);
2020-07-08 06:57:11 +08:00
var x = input.value;
point.setStyle('left', x+'px');
2017-05-19 02:09:46 +08:00
zone['Points'][index].x = x;
var Point = $('zonePoly').points.getItem(index);
Point.x = x;
updateArea();
2017-05-19 02:09:46 +08:00
}
2020-07-08 06:57:11 +08:00
function updateY(input) {
index = input.getAttribute('data-point-index');
limitPointValue(input, 0, maxY);
2017-05-19 02:09:46 +08:00
var point = $('point'+index);
2020-07-08 06:57:11 +08:00
var y = input.value;
point.setStyle('top', y+'px');
2017-05-19 02:09:46 +08:00
zone['Points'][index].y = y;
var Point = $('zonePoly').points.getItem(index);
Point.y = y;
updateArea();
2017-05-19 02:09:46 +08:00
}
function saveChanges(element) {
2017-05-19 02:09:46 +08:00
var form = element.form;
if ( validateForm(form) ) {
submitForm(form);
2017-05-19 02:09:46 +08:00
if ( form.elements['newZone[Type]'].value == 'Privacy' ) {
alert('Capture process for this monitor will be restarted for the Privacy zone changes to take effect.');
}
return true;
2017-05-19 02:06:13 +08:00
}
return false;
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
function drawZonePoints() {
$('imageFrame').getElements('div.zonePoint').each(
2019-11-20 00:37:45 +08:00
function(element) {
element.destroy();
});
2017-05-19 02:09:46 +08:00
for ( var i = 0; i < zone['Points'].length; i++ ) {
var div = new Element('div', {
'id': 'point'+i,
2020-07-08 06:57:11 +08:00
'data-index': i,
'class': 'zonePoint',
'title': 'Point '+(i+1),
'styles': {
'left': zone['Points'][i].x,
'top': zone['Points'][i].y
}
});
div.addEvent('mouseover', highlightOn.pass(i));
div.addEvent('mouseout', highlightOff.pass(i));
div.inject($('imageFrame'));
div.makeDraggable( {
'container': $('imageFrame'),
'onStart': setActivePoint.pass(i),
'onComplete': fixActivePoint.pass(i),
'onDrag': updateActivePoint.pass(i)
} );
2017-05-19 02:06:13 +08:00
}
var tables = $('zonePoints').getElement('table').getElements('table');
tables.each( function(table) {
table.getElement('tbody').empty();
} );
2017-05-19 02:09:46 +08:00
for ( var i = 0; i < zone['Points'].length; i++ ) {
2019-11-20 00:37:45 +08:00
var row;
row = new Element('tr', {'id': 'row'+i});
row.addEvents({'mouseover': highlightOn.pass(i), 'mouseout': highlightOff.pass(i)});
var cell = new Element('td');
cell.set('text', i+1);
cell.inject(row);
cell = new Element('td');
var input = new Element('input', {
2019-11-20 00:37:45 +08:00
'id': 'newZone[Points]['+i+'][x]',
'name': 'newZone[Points]['+i+'][x]',
'value': zone['Points'][i].x,
2020-07-08 06:57:11 +08:00
'type': 'number',
'class': 'ZonePoint',
2020-07-31 00:15:05 +08:00
'min': '0',
'max': maxX,
'data-point-index': i
2019-11-20 00:37:45 +08:00
});
2020-07-08 06:57:11 +08:00
input.oninput = window['updateX'].bind(input, input);
input.inject(cell);
cell.inject(row);
cell = new Element('td');
input = new Element('input', {
2019-11-20 00:37:45 +08:00
'id': 'newZone[Points]['+i+'][y]',
'name': 'newZone[Points]['+i+'][y]',
'value': zone['Points'][i].y,
2020-07-08 06:57:11 +08:00
'type': 'number',
'class': 'ZonePoint',
2020-07-31 00:15:05 +08:00
'min': '0',
'max': maxY,
'data-point-index': i
2019-11-20 00:37:45 +08:00
} );
2020-07-08 06:57:11 +08:00
input.oninput = window['updateY'].bind(input, input);
input.inject(cell);
cell.inject(row);
cell = new Element('td');
new Element('button', {
2019-03-21 02:51:39 +08:00
'type': 'button',
'events': {'click': addPoint.pass(i)}
}).set('text', '+').inject(cell);
if ( zone['Points'].length > 3 ) {
cell.appendText(' ');
new Element('button', {
2019-03-21 02:51:39 +08:00
'id': 'delete'+i,
'type': 'button',
'events': {'click': delPoint.pass(i)}
}).set('text', '-').inject(cell);
}
cell.inject(row);
2017-05-19 02:09:46 +08:00
2019-11-20 00:37:45 +08:00
row.inject(tables[i%tables.length].getElement('tbody'));
} // end foreach point
2017-05-19 02:09:46 +08:00
// Sets up the SVG polygon
updateZoneImage();
}
2017-05-19 02:09:46 +08:00
//
// Imported from watch.js and modified for new zone edit view
//
var alarmState = STATE_IDLE;
var lastAlarmState = STATE_IDLE;
function setAlarmState( currentAlarmState ) {
alarmState = currentAlarmState;
var stateClass = '';
if ( alarmState == STATE_ALARM ) {
stateClass = 'alarm';
} else if ( alarmState == STATE_ALERT ) {
stateClass = 'alert';
}
$('stateValue').set('text', stateStrings[alarmState]);
if ( stateClass ) {
$('stateValue').setProperty('class', stateClass);
} else {
$('stateValue').removeProperty('class');
}
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
var streamCmdParms = "view=request&request=stream&connkey="+connKey;
if ( auth_hash ) {
streamCmdParms += '&auth='+auth_hash;
}
2018-10-21 02:32:58 +08:00
var streamCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getStreamCmdResponse
} );
2017-05-19 02:09:46 +08:00
var streamCmdTimer = null;
2017-05-19 02:09:46 +08:00
var streamStatus;
function getStreamCmdResponse(respObj, respText) {
2017-05-19 02:09:46 +08:00
watchdogOk("stream");
if ( streamCmdTimer ) {
streamCmdTimer = clearTimeout(streamCmdTimer);
}
2017-05-19 02:09:46 +08:00
if ( respObj.result == 'Ok' ) {
streamStatus = respObj.status;
$('fpsValue').set('text', streamStatus.fps);
setAlarmState(streamStatus.state);
2017-05-19 01:50:56 +08:00
if ( streamStatus.paused == true ) {
streamCmdPause(false);
2017-05-19 02:09:46 +08:00
} else if ( streamStatus.delayed == true && streamStatus.rate == 1 ) {
streamCmdPlay(false);
}
2017-05-19 02:09:46 +08:00
} else {
checkStreamForErrors('getStreamCmdResponse', respObj);//log them
2017-10-04 22:48:14 +08:00
if ( ! streamPause ) {
// Try to reload the image stream.
var streamImg = $('liveStream'+monitorId);
if ( streamImg ) {
streamImg.src = streamImg.src.replace(/rand=\d+/i, 'rand='+Math.floor(Math.random() * 1000000));
}
2017-10-04 22:48:14 +08:00
}
2017-05-19 02:06:13 +08:00
}
if ( !streamPause ) {
2017-10-04 22:48:14 +08:00
var streamCmdTimeout = statusRefreshTimeout;
if ( alarmState == STATE_ALARM || alarmState == STATE_ALERT ) {
2017-10-04 22:48:14 +08:00
streamCmdTimeout = streamCmdTimeout/5;
}
streamCmdTimer = streamCmdQuery.delay(streamCmdTimeout);
2017-10-04 22:48:14 +08:00
}
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
var streamPause = false;
2017-05-19 02:09:46 +08:00
function streamCmdPauseToggle() {
if ( streamPause == true ) {
streamCmdPlay(true);
2017-05-19 02:09:46 +08:00
streamPause = false;
document.getElementById('pauseBtn').innerHTML = pauseString;
2017-05-19 02:09:46 +08:00
} else {
streamCmdPause(true);
2017-05-19 02:09:46 +08:00
streamPause = true;
document.getElementById('pauseBtn').innerHTML = playString;
2017-05-19 02:06:13 +08:00
}
2017-05-19 02:09:46 +08:00
}
function streamCmdPause(action) {
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_PAUSE);
}
2017-05-19 02:09:46 +08:00
}
function streamCmdPlay(action) {
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_PLAY);
}
2017-05-19 02:09:46 +08:00
}
function streamCmdStop(action) {
if ( action ) {
streamCmdReq.send(streamCmdParms+"&command="+CMD_STOP);
}
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
function streamCmdQuery() {
streamCmdReq.send(streamCmdParms+"&command="+CMD_QUERY);
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
var statusCmdParms = "view=request&request=status&entity=monitor&id="+monitorId+"&element[]=Status&element[]=FrameRate";
if ( auth_hash ) {
statusCmdParms += '&auth='+auth_hash;
}
2018-10-21 02:32:58 +08:00
var statusCmdReq = new Request.JSON( {
url: monitorUrl,
method: 'get',
data: statusCmdParms,
timeout: AJAX_TIMEOUT,
link: 'cancel',
onSuccess: getStatusCmdResponse
} );
2017-05-19 02:09:46 +08:00
var statusCmdTimer = null;
function getStatusCmdResponse(respObj, respText) {
2017-05-19 02:09:46 +08:00
watchdogOk("status");
if ( statusCmdTimer ) {
statusCmdTimer = clearTimeout(statusCmdTimer);
}
2017-05-19 01:50:56 +08:00
if ( respObj.result == 'Ok' ) {
$('fpsValue').set('text', respObj.monitor.FrameRate);
setAlarmState(respObj.monitor.Status);
} else {
2017-05-19 02:09:46 +08:00
checkStreamForErrors("getStatusCmdResponse", respObj);
}
2017-10-04 22:48:14 +08:00
if ( ! streamPause ) {
var statusCmdTimeout = statusRefreshTimeout;
if ( alarmState == STATE_ALARM || alarmState == STATE_ALERT ) {
2017-10-04 22:48:14 +08:00
statusCmdTimeout = statusCmdTimeout/5;
}
statusCmdTimer = statusCmdQuery.delay(statusCmdTimeout);
2017-10-04 22:48:14 +08:00
}
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
function statusCmdQuery() {
statusCmdReq.send();
}
2017-05-19 02:09:46 +08:00
function fetchImage( streamImage ) {
streamImage.src = streamImage.src.replace(/rand=\d+/i, 'rand='+Math.floor((Math.random() * 1000000) ));
2017-05-19 02:09:46 +08:00
}
function appletRefresh() {
if ( streamStatus && (!streamStatus.paused && !streamStatus.delayed) ) {
var streamImg = $('liveStream');
var parent = streamImg.getParent();
streamImg.dispose();
streamImg.inject( parent );
if ( appletRefreshTime ) {
2017-05-19 02:09:46 +08:00
appletRefresh.delay( appletRefreshTime*1000 );
}
2017-05-19 02:09:46 +08:00
} else {
appletRefresh.delay( 15*1000 ); //if we are paused or delayed check every 15 seconds if we are live yet...
2017-05-19 02:06:13 +08:00
}
2017-05-19 02:09:46 +08:00
}
2017-05-19 02:09:46 +08:00
var watchdogInactive = {
'stream': false,
'status': false
};
var watchdogFunctions = {
'stream': streamCmdQuery,
'status': statusCmdQuery
};
//Make sure the various refreshes are still taking effect
function watchdogCheck(type) {
2017-05-19 02:09:46 +08:00
if ( watchdogInactive[type] ) {
watchdogFunctions[type]();
watchdogInactive[type] = false;
2017-05-19 02:09:46 +08:00
} else {
watchdogInactive[type] = true;
2017-05-19 02:06:13 +08:00
}
2017-05-19 02:09:46 +08:00
}
function watchdogOk(type) {
2017-05-19 02:09:46 +08:00
watchdogInactive[type] = false;
}
2017-05-19 02:09:46 +08:00
function initPage() {
var form = document.zoneForm;
2017-05-19 02:09:46 +08:00
//form.elements['newZone[Name]'].disabled = true;
//form.elements['newZone[Type]'].disabled = true;
form.presetSelector.disabled = true;
//form.elements['newZone[Units]'].disabled = true;
2019-11-20 00:37:45 +08:00
if ( CheckMethod = form.elements['newZone[CheckMethod]'] ) {
CheckMethod.disabled = true;
CheckMethod.onchange = window['applyCheckMethod'].bind(CheckMethod, CheckMethod);
}
[
'newZone[MinPixelThreshold]',
'newZone[MaxPixelThreshold]',
'newAlarmRgbR',
'newAlarmRgbG',
'newAlarmRgbB',
].forEach(
function(element_name, index) {
var el = form.elements[element_name];
if ( el ) {
el.oninput = window['limitRangeToUnsignedByte'].bind(el, el);
el.disabled = true;
} else {
console.error("Element " + element_name + " not found in zone edit form");
}
});
[
'newZone[FilterX]',
'newZone[FilterY]'
].forEach(
function(element_name, index) {
var el = form.elements[element_name];
if ( el ) {
el.oninput = window['limitFilter'].bind(el, el);
el.disabled = true;
} else {
console.error("Element " + element_name + " not found in zone edit form");
}
}
);
[
'newZone[MinAlarmPixels]',
'newZone[MaxAlarmPixels]',
'newZone[MinFilterPixels]',
'newZone[MaxFilterPixels]'
].forEach(function(element_name, index) {
var el = form.elements[element_name];
if ( el ) {
el.oninput = window['limitArea'].bind(el, el);
el.disabled = true;
} else {
console.error("Element " + element_name + " not found in zone edit form");
}
}
);
2017-05-19 02:09:46 +08:00
form.elements['newZone[MinBlobPixels]'].disabled = true;
form.elements['newZone[MaxBlobPixels]'].disabled = true;
form.elements['newZone[MinBlobs]'].disabled = true;
form.elements['newZone[MaxBlobs]'].disabled = true;
form.elements['newZone[OverloadFrames]'].disabled = true;
applyZoneType();
if ( form.elements['newZone[Units]'].value == 'Percent' ) {
applyZoneUnits();
}
2017-05-19 02:09:46 +08:00
applyCheckMethod();
drawZonePoints();
2019-11-20 00:37:45 +08:00
$('pauseBtn').onclick = function() {
streamCmdPauseToggle();
};
if ( el = $('saveBtn') ) {
el.onclick = window['saveChanges'].bind(el, el);
}
if ( el = $('cancelBtn') ) {
el.onclick = function() {
refreshParentWindow();
closeWindow();
};
2019-11-20 00:37:45 +08:00
}
2017-05-19 02:09:46 +08:00
//
// Imported from watch.js and modified for new zone edit view
//
var delay = (Math.random()+0.1) * statusRefreshTimeout;
2018-10-21 02:32:58 +08:00
//console.log("Delay for status updates is: " + delay );
if ( streamMode == 'single' ) {
statusCmdTimer = statusCmdQuery.delay(delay);
2017-05-19 02:09:46 +08:00
watchdogCheck.pass('status').periodical(statusRefreshTimeout*2);
} else {
streamCmdTimer = streamCmdQuery.delay(delay);
2017-05-19 02:09:46 +08:00
watchdogCheck.pass('stream').periodical(statusRefreshTimeout*2);
}
if ( canStreamNative || (streamMode == 'single') ) {
2017-05-19 02:09:46 +08:00
var streamImg = $('imageFrame').getElement('img');
if ( !streamImg ) {
2017-05-19 02:09:46 +08:00
streamImg = $('imageFrame').getElement('object');
}
if ( streamMode == 'single' ) {
streamImg.addEvent('click', fetchImage.pass(streamImg));
fetchImage.pass(streamImg).periodical(imageRefreshTimeout);
}
2017-05-19 02:06:13 +08:00
}
if ( refreshApplet && appletRefreshTime ) {
appletRefresh.delay(appletRefreshTime*1000);
}
2017-05-19 02:09:46 +08:00
}
2016-06-07 23:40:53 +08:00
function Polygon_calcArea(coords) {
2017-05-19 02:09:46 +08:00
var n_coords = coords.length;
var float_area = 0.0;
for ( i = 0; i < n_coords-1; i++ ) {
var trap_area = (coords[i].x*coords[i+1].y - coords[i+1].x*coords[i].y) / 2;
2017-05-19 02:09:46 +08:00
float_area += trap_area;
//printf( "%.2f (%.2f)\n", float_area, trap_area );
2016-06-07 23:40:53 +08:00
}
float_area += (coords[n_coords-1].x*coords[0].y - coords[0].x*coords[n_coords-1].y) / 2;
return Math.round(Math.abs(float_area));
2017-05-19 02:09:46 +08:00
}
2016-06-07 23:40:53 +08:00
window.addEventListener('DOMContentLoaded', initPage);