Esempio n. 1
0
 public Webcam(Messenger messenger, string name, string path)
     : base(messenger)
 {
     _folderPath = path;
     Name = name;
     RecordVideo = false;
     FirstAlarm = true;
 }
Esempio n. 2
0
        public void Connect(string address, int port)
        {
            if (_disposed)
                throw new ObjectDisposedException(GetType().FullName);

            try
            {
                _msgr = new Messenger(new TcpClient(address, port));
                _msgr.SendMessage("CL:" + ((IPEndPoint)_msgr.TcpClient.Client.LocalEndPoint).Address.ToString());
                _msgr.SendMessage("get-list");

                _view.Status = "Connected to server " + address;
                _view.IsConnected = true;

                Listen();
            }
            catch
            {
                Disconnect();
                _view.Status = "Failed to connect to server";
            }
        }
Esempio n. 3
0
 public Client(Messenger messenger, DevicePresenter presenter, string name)
     : base(messenger)
 {
     _dvPresenter = presenter;
     Name = name;
 }
Esempio n. 4
0
        private void StreamVideo(ImageCodecInfo codecInfo, EncoderParameters encoderParams)
        {
            MemoryStream m = new MemoryStream(20000);
            Bitmap image = null;
            IntPtr ip = IntPtr.Zero;
            Font fontOverlay = new Font("Times New Roman", 14, System.Drawing.FontStyle.Bold,
                System.Drawing.GraphicsUnit.Point);
            DateTime lastMotion = DateTime.Now;
            int interval = 5;

            _isRecording = false;
            _player = new SoundPlayer();
            _player.LoadCompleted += new System.ComponentModel.AsyncCompletedEventHandler((obj, arg) => _player.PlayLooping());

            Task.Factory.StartNew(() =>
                {
                    _device.Start();
                    while (_requested)
                    {
                        try
                        {
                            // capture image
                            ip = _device.GetBitMap();
                            image = new Bitmap(_device.Width, _device.Height, _device.Stride, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ip);
                            image.RotateFlip(RotateFlipType.RotateNoneFlipY);

                            _currentSensor.ProcessFrame(ref image);

                            if (_currentSensor.IsAlarming)
                            {
                                if (!_isRecording && (DateTime.Now.Second % interval == 0))
                                    _messenger.SendMessage("record");
                                lastMotion = DateTime.Now;
                            }
                            else
                            {
                                if (DateTime.Now.Subtract(lastMotion).Seconds > interval)
                                {
                                    if (_isRecording)
                                    {
                                        _messenger.SendMessage("stop-record");
                                        _isRecording = false;
                                    }
                                    if (_isAlarming)
                                    {
                                        _player.Stop();
                                        _isAlarming = false;
                                    }
                                }
                            }

                            // add text that displays date time to the image
                            image.AddText(fontOverlay, 10, 10, DateTime.Now.ToString());

                            // save it to jpeg using quality options
                            image.Save(m, codecInfo, encoderParams);

                            // send the jpeg image if server requests it
                            if (_messenger != null)
                                _messenger.SendMessage(m.GetBuffer(), MessageType.Image);
                        }
                        catch (SocketException)
                        {
                            _messenger.Dispose();
                            _messenger = null;
                            _requested = false;
                        }
                        catch { }
                        finally
                        {
                            // clean up
                            m.SetLength(0);
                            if (image != null)
                                image.Dispose();
                            if (ip != IntPtr.Zero)
                            {
                                Marshal.FreeCoTaskMem(ip);
                                ip = IntPtr.Zero;
                            }
                        }
                    }

                    _device.Pause();
                    _player.Stop();
                    _player.Dispose();

                    fontOverlay.Dispose();
                    disposeReady.Set();
                });
        }
Esempio n. 5
0
        private void Listen()
        {
            Task.Factory.StartNew(() =>
                {
                    _port = _view.Port;
                    UdpClient client = new UdpClient(_port);
                    IPEndPoint remoteEP = new IPEndPoint(IPAddress.Any, 0);

                    _done = false;
                    while (!_done)
                    {
                        try
                        {
                            string[] messages = Encoding.ASCII.GetString(client.Receive(ref remoteEP)).Split(' ');
                            if (messages[0] == "discovery")
                            {
                                _messenger = new Messenger(new TcpClient(remoteEP.Address.ToString(), Convert.ToInt32(messages[1])));
                                _messenger.SendMessage("WC:" + Dns.GetHostName());
                                _view.Status = "Connected to server " + remoteEP.Address.ToString() + " : " + messages[1];
                                _requested = true;

                                StreamVideo(_codecInfo, _encoderParams);

                                MessageType type;
                                while (true)
                                {
                                    byte[] buffer = _messenger.ReceiveMessage(out type);
                                    ProcessMessage(buffer, type);
                                }
                            }
                        }
                        catch
                        {
                            _requested = false;
                            _messenger.Dispose();
                            _messenger = null;
                            _view.Status = "Server has disconnected";
                        }
                    }
                    client.Close();
                });
        }
Esempio n. 6
0
 public Device(Messenger messenger)
 {
     _msgr = messenger;
 }
Esempio n. 7
0
 public Device(TcpClient client)
 {
     _msgr = new Messenger(client);
 }
Esempio n. 8
0
        private void IdentifyDevice(Messenger msgr)
        {
            try
            {
                MessageType type;
                msgr.TcpClient.ReceiveTimeout = 30000;
                byte[] buffer = msgr.ReceiveMessage(out type);
                msgr.TcpClient.ReceiveTimeout = 0;

                if (type == MessageType.Command)
                {
                    string id = Encoding.ASCII.GetString(buffer);
                    Device d = null;

                    if (id.StartsWith("WC:"))
                    {
                        Webcam w = new Webcam(msgr, id, _view.BasePath + @"\" + id.Substring(4));
                        w.Disconnected += OnDisconnect;
                        w.MotionDetected += OnMotionDetect;
                        w.Recorded += (source) => _view.Status = "Webcam " + source.Name + " started recording";
                        w.MotionStopped += (source) =>
                            {
                                ((Webcam)source).StopRecording();
                                _view.Status = "Webcam " + source.Name + " stopped recording";
                            };
                        w.AddClient(this);
                        _view.AddedWebcam = w.Name;
                        _view.Status = "Webcam " + w.Name + " is now connected";
                        d = w;
                    }
                    else if (id.StartsWith("CL:"))
                    {
                        Client c = new Client(msgr, this, id);
                        c.Disconnected += OnDisconnect;
                        c.WebcamChanged += (source) => _view.Status = ((Client)source).EventArg;
                        _view.AddedClient = c.Name;
                        _view.Status = "Client " + c.Name + " is now connected";
                        d = c;
                    }

                    if (d == null)
                        msgr.Dispose();
                    else
                    {
                        _devices.AddOrUpdate(id, d, (key, value) =>
                            {
                                value.Dispose();
                                return d;
                            });
                        d.Listen();
                    }
                }
            }
            catch { msgr.Dispose(); }
        }
Esempio n. 9
0
 private void AcceptConnections()
 {
     Task.Factory.StartNew(() =>
     {
         while (_listening)
         {
             try
             {
                 Messenger msgr = new Messenger(_listener.AcceptTcpClient());
                 Task.Factory.StartNew(() => IdentifyDevice(msgr));
             }
             catch (SocketException ex) { _view.Status = ex.Message; }
         }
     });
 }