Skip to content

Commit

Permalink
fix issue with motion detection
Browse files Browse the repository at this point in the history
fix issue with plugin integration
  • Loading branch information
ispysoftware committed Jul 3, 2017
1 parent 1872865 commit 2f1963e
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 180 deletions.
4 changes: 2 additions & 2 deletions AssemblyInfo.cs
Expand Up @@ -25,7 +25,7 @@
// You can specify all the values or you can default the Revision and Build Numbers
// by using the '*' as shown below:

[assembly: AssemblyVersion("6.8.0.0")]
[assembly: AssemblyVersion("6.8.1.0")]

//
// In order to sign your assembly you must specify a key to use. Refer to the
Expand Down Expand Up @@ -55,4 +55,4 @@
[assembly: AssemblyDelaySign(false)]
[assembly: AssemblyKeyFile("")]
[assembly: AssemblyKeyName("")]
[assembly: AssemblyFileVersion("6.8.0.0")]
[assembly: AssemblyFileVersion("6.8.1.0")]
122 changes: 60 additions & 62 deletions Controls/CameraWindow.cs
Expand Up @@ -65,15 +65,14 @@ public sealed class CameraWindow : PictureBox, ISpyControl
private DateTime _reconnectTime = DateTime.MinValue;
private bool _firstFrame = true;
private Thread _recordingThread;
private int _calibrateTarget;
private Camera _camera;
private DateTime _lastFrameUploaded = Helper.Now;
private DateTime _lastFrameSaved = Helper.Now;
private DateTime _dtPTZLastCheck = DateTime.Now;
private long _lastRun = Helper.Now.Ticks;

private Int64 _lastMovementDetected = DateTime.MinValue.Ticks;
private Int64 _lastAlerted = DateTime.MinValue.Ticks;
private long _lastMovementDetected = DateTime.MinValue.Ticks;
private long _lastAlerted = DateTime.MinValue.Ticks;

public DateTime LastMovementDetected
{
Expand Down Expand Up @@ -172,29 +171,31 @@ public bool Listening
public XimeaVideoSource XimeaSource;
public bool Alerted;
public double MovementCount;
public double CalibrateCount;
public DateTime CalibrateTarget;
private DateTime _lastReconnect = DateTime.MinValue;
public Rectangle RestoreRect = Rectangle.Empty;


private bool _calibrating;
public bool Calibrating
{
get { return _calibrating; }
get
{
return DateTime.UtcNow < CalibrateTarget;
}
set
{
_calibrating = value;
if (value)
{
CalibrateCount = 0;
_calibrateTarget = Camobject.detector.calibrationdelay;
CalibrateTarget = DateTime.UtcNow.AddSeconds(Camobject.detector.calibrationdelay);
}
else
CalibrateTarget = DateTime.MinValue;
}
}
public Graphics CurrentFrame;
public PTZController PTZ;
public DateTime FlashCounter = DateTime.MinValue;
public double InactiveRecord;
public DateTime LastActivity = DateTime.MinValue;
public bool IsEdit;
public volatile bool MovementDetected;
public bool PTZNavigate;
Expand Down Expand Up @@ -1476,14 +1477,14 @@ public void Tick()
if (MovementDetected)
{
_autoofftimer = 0;
InactiveRecord = 0;
LastActivity = DateTime.UtcNow;
if (Camobject.alerts.mode != "nomovement" &&
(Camobject.detector.recordondetect || Camobject.detector.recordonalert))
{
var vc = VolumeControl;
if (vc != null)
{
vc.InactiveRecord = 0;
vc.LastActivity = DateTime.UtcNow;
}
}
}
Expand Down Expand Up @@ -1532,10 +1533,8 @@ public void Tick()
CheckPTZSchedule();
}

if (Recording && !MovementDetected && !ForcedRecording)
{
InactiveRecord += _tickThrottle;
}
if (MovementDetected)
LastActivity = DateTime.UtcNow;

if (Camera != null && GotImage)
{
Expand Down Expand Up @@ -1598,7 +1597,7 @@ private void CheckRecord()
var dur = (DateTime.UtcNow - _recordingStartTime).TotalSeconds;

if (dur > Camobject.recorder.maxrecordtime ||
((!MovementDetected && InactiveRecord > Camobject.recorder.inactiverecord) && !ForcedRecording && dur > Camobject.recorder.minrecordtime))
(!MovementDetected && (DateTime.UtcNow - LastActivity).TotalSeconds > Camobject.recorder.inactiverecord && !ForcedRecording && dur > Camobject.recorder.minrecordtime))
StopSaving();
}

Expand Down Expand Up @@ -1758,9 +1757,8 @@ private void CheckStopPTZTracking()
{
LastAutoTrackSent = DateTime.MinValue;
Calibrating = true;
_calibrateTarget = Camobject.settings.ptztimetohome;
if (string.IsNullOrEmpty(Camobject.settings.ptzautohomecommand) ||
Camobject.settings.ptzautohomecommand == "Center")
CalibrateTarget = DateTime.UtcNow.AddSeconds(Camobject.settings.ptztimetohome);
if (string.IsNullOrEmpty(Camobject.settings.ptzautohomecommand) || Camobject.settings.ptzautohomecommand == "Center")
PTZ.SendPTZCommand(Enums.PtzCommand.Center);
else
{
Expand Down Expand Up @@ -1959,13 +1957,6 @@ private void DoCalibrate(double since)
}
}
}

CalibrateCount += since;
if (CalibrateCount > _calibrateTarget)
{
Calibrating = false;
CalibrateCount = 0;
}
}
LastMovementDetected = Helper.Now;
}
Expand Down Expand Up @@ -2595,9 +2586,9 @@ protected override void OnPaint(PaintEventArgs pe)
{
if (Calibrating)
{
int remaining = _calibrateTarget - Convert.ToInt32(CalibrateCount);
if (remaining < 0) remaining = 0;

int remaining = Math.Max(0,
Convert.ToInt32((CalibrateTarget - DateTime.UtcNow).TotalSeconds));
txt += ": " + LocRm.GetString("Calibrating") + " (" + remaining + ")";
}
else
Expand Down Expand Up @@ -3440,15 +3431,12 @@ private void Record()
fa.Nullify();
}

public void Alarm(object sender, EventArgs e)

//Motion Detection
public void Detect(object sender, EventArgs e)
{
double d = 0.3;
if (Camera.Framerate > 3) //prevent division by zero
d = 1d/Camera.Framerate;

LastMovementDetected = Helper.Now.AddSeconds(d);


LastActivity = DateTime.UtcNow;

if (!Calibrating)
{
if (Camobject.detector.recordondetect)
Expand All @@ -3467,7 +3455,7 @@ public void Alarm(object sender, EventArgs e)
{
if ((Helper.Now - _lastFrameUploaded).TotalSeconds > Camobject.ftp.minimumdelay)
{
FtpFrame();
FtpFrame();

}
}
Expand All @@ -3482,16 +3470,14 @@ public void Alarm(object sender, EventArgs e)
}
}
}



}

if (sender is Camera)
{
FlashCounter = Helper.Now.AddSeconds(10);
MovementDetected = true;

if (Camera?.Plugin != null)
FlashCounter = Helper.Now.AddSeconds(10);
if (Camera?.Plugin != null && !Calibrating)
{
var o = Camera.Plugin.GetType();
var m = o.GetMethod("MotionDetect");
Expand All @@ -3503,9 +3489,13 @@ public void Alarm(object sender, EventArgs e)
}
return;
}
}

public void Alert(object sender, EventArgs e)
{
LastActivity = DateTime.UtcNow;
var c = sender as CameraWindow;
if (c!=null) //triggered from another camera
if (c != null) //triggered from another camera
{
FlashCounter = Helper.Now.AddSeconds(10);
DoAlert("alert");
Expand All @@ -3519,7 +3509,7 @@ public void Alarm(object sender, EventArgs e)
return;
}

if (sender is String || sender is IVideoSource)
if (sender is string || sender is IVideoSource)
{
if (Camobject.alerts.active && Camera != null)
{
Expand All @@ -3533,7 +3523,7 @@ public void Alarm(object sender, EventArgs e)
if (sender is KinectStream)
msg = "Trip Wire";
}
DoAlert("alert", msg);
DoAlert("alert", msg);
}
}
}
Expand Down Expand Up @@ -3898,13 +3888,13 @@ private void ProcessAlertEvent(string mode, byte[] rawgrab, string pluginmessage
case "1":
VolumeLevel vl =
MainForm.InstanceReference.GetVolumeLevel(Convert.ToInt32(tid[1]));
vl?.Alarm(this, EventArgs.Empty);
vl?.Alert(this, EventArgs.Empty);
break;
case "2":
CameraWindow cw =
MainForm.InstanceReference.GetCameraWindow(Convert.ToInt32(tid[1]));
if (cw != null && cw!=this) //prevent recursion
cw.Alarm(this, EventArgs.Empty);
cw.Alert(this, EventArgs.Empty);
break;
}
}
Expand Down Expand Up @@ -4118,7 +4108,8 @@ public void Disable(bool stopSource = true)
Calibrating = false;

Camera.NewFrame -= CameraNewFrame;
Camera.Alarm -= Alarm;
Camera.Detect -= Detect;
Camera.Alert -= Alert;
Camera.PlayingFinished -= VideoDeviceVideoFinished;
Camera.ErrorHandler -= CameraWindow_ErrorHandler;

Expand All @@ -4139,7 +4130,7 @@ public void Disable(bool stopSource = true)
var source = Camera.VideoSource as KinectStream;
if (source != null)
{
source.TripWire -= Alarm;
source.TripWire -= Alert;
}

var source1 = Camera.VideoSource as KinectNetworkStream;
Expand Down Expand Up @@ -4199,7 +4190,7 @@ public void Disable(bool stopSource = true)
LastFrame = null;
}
Camobject.settings.active = false;
InactiveRecord = 0;
LastActivity = DateTime.MinValue;
_timeLapseTotal = _timeLapseFrameCount = 0;
ForcedRecording = false;

Expand Down Expand Up @@ -4604,7 +4595,7 @@ public void Enable()
UpdateFloorplans(false);

_timeLapseTotal = _timeLapseFrameCount = 0;
InactiveRecord = 0;
LastActivity = DateTime.MinValue;
MovementDetected = false;

Alerted = false;
Expand Down Expand Up @@ -4840,12 +4831,14 @@ private void OpenVideoSource(CameraWindow cw)

Camera.NewFrame -= CameraNewFrame;
Camera.PlayingFinished -= VideoDeviceVideoFinished;
Camera.Alarm -= Alarm;
Camera.Alert -= Alert;
Camera.Detect -= Detect;
Camera.ErrorHandler -= CameraWindow_ErrorHandler;

Camera.NewFrame += CameraNewFrame;
Camera.PlayingFinished += VideoDeviceVideoFinished;
Camera.Alarm += Alarm;
Camera.Alert += Alert;
Camera.Detect += Detect;
Camera.ErrorHandler += CameraWindow_ErrorHandler;

Calibrating = true;
Expand Down Expand Up @@ -4903,7 +4896,8 @@ void CWCameraReconnect(object sender, EventArgs e)
Camera.PlayingFinished -= VideoDeviceVideoFinished;
}
Camera.NewFrame -= CameraNewFrame;
Camera.Alarm -= Alarm;
Camera.Alert -= Alert;
Camera.Detect -= Detect;
}
}

Expand Down Expand Up @@ -4954,10 +4948,12 @@ void CWCameraReconnected(object sender, EventArgs e)
}

Camera.NewFrame -= CameraNewFrame;
Camera.Alarm -= Alarm;
Camera.Alert -= Alert;
Camera.Detect -= Detect;

Camera.NewFrame += CameraNewFrame;
Camera.Alarm += Alarm;
Camera.Alert += Alert;
Camera.Detect += Detect;

if (Camobject.settings.calibrateonreconnect)
{
Expand Down Expand Up @@ -4990,7 +4986,7 @@ private void OpenVideoSource(IVideoSource source, bool @override)
if (kinectStream != null)
{
kinectStream.InitTripWires(Camobject.alerts.pluginconfig);
kinectStream.TripWire += Alarm;
kinectStream.TripWire += Alert;
}

var kinectNetworkStream = source as KinectNetworkStream;
Expand All @@ -5011,13 +5007,15 @@ private void OpenVideoSource(IVideoSource source, bool @override)
Camera = new Camera(source);
Camera.NewFrame -= CameraNewFrame;
Camera.PlayingFinished -= VideoDeviceVideoFinished;
Camera.Alarm -= Alarm;
Camera.Alert -= Alert;
Camera.Detect -= Detect;
Camera.ErrorHandler -= CameraWindow_ErrorHandler;


Camera.NewFrame += CameraNewFrame;
Camera.PlayingFinished += VideoDeviceVideoFinished;
Camera.Alarm += Alarm;
Camera.Alert += Alert;
Camera.Detect += Detect;
Camera.ErrorHandler += CameraWindow_ErrorHandler;

Camera.PiPConfig = Camobject.settings.pip.config;
Expand Down Expand Up @@ -5145,7 +5143,7 @@ private void ProcessAlertFromPlugin(string a, string description)
switch (action)
{
case "alarm":
Alarm(description, EventArgs.Empty);
Alert(description, EventArgs.Empty);
break;
case "flash":
FlashCounter = Helper.Now.AddSeconds(10);
Expand Down
6 changes: 5 additions & 1 deletion Controls/FloorPlan.cs
Expand Up @@ -66,7 +66,11 @@ private Rectangle ButtonPanel
}
}

public void Alarm(object sender, EventArgs e)
public void Alert(object sender, EventArgs e)
{

}
public void Detect(object sender, EventArgs e)
{

}
Expand Down
3 changes: 2 additions & 1 deletion Controls/IspyControl.cs
Expand Up @@ -27,8 +27,9 @@ public interface ISpyControl
void Talk(IWin32Window f = null);
void Listen();
string SaveFrame(Bitmap bmp = null);
void Alert(object sender, EventArgs e);
void Detect(object sender, EventArgs e);

void Alarm(object sender, EventArgs e);
void Apply();
void ReloadSchedule();

Expand Down

0 comments on commit 2f1963e

Please sign in to comment.