Пример #1
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(); }
        }