Beispiel #1
0
        /// <summary>
        /// Receive method to handle the camera responses.
        /// </summary>
        /// <param name="ar">The async result.</param>
        private void ListeningReceive(IAsyncResult ar)
        {
            IPEndPoint source = null;

            byte[] bytes = null;

            lock (listeningClientLock)
            {
                // check so client is still active
                if (listeningClient == null)
                {
                    return;
                }

                // receive the bytes from the source
                source = new IPEndPoint(IPAddress.Any, MULTICAST_PORT);
                bytes  = listeningClient.EndReceive(ar, ref source);

                // continue to listen for more packets
                listeningClient.BeginReceive(ListeningReceive, new object());
            }

            try
            {
                // convert the response to readable text
                string response = Encoding.UTF8.GetString(bytes);
                using (StringReader responseReader = new StringReader(response))
                {
                    // parse the xml soap message to a discovery response
                    XmlSerializer     serializer       = new XmlSerializer(typeof(DiscoveryResponse));
                    DiscoveryResponse discoveryMessage = (DiscoveryResponse)serializer.Deserialize(XmlReader.Create(responseReader));

                    // validate response guid in the response, so it corresponds to the guid we sent with the discovery request
                    Guid responseID;
                    if (Guid.TryParse(discoveryMessage.Header.RelatesTo.Substring(5), out responseID) && responseID == messageGuid)
                    {
                        // get the main soap service url for the camera
                        string deviceServiceURL = discoveryMessage.Body.ProbeMatches.ProbeMatch.XAddrs;

                        // get the properties about the camera
                        string[] properties = discoveryMessage.Body.ProbeMatches.ProbeMatch.Scopes.Split(' ');
                        string   name       = properties.First(p => p.StartsWith("onvif://www.onvif.org/name/")).Substring("onvif://www.onvif.org/name/".Length);
                        string   model      = properties.First(p => p.StartsWith("onvif://www.onvif.org/model/")).Substring("onvif://www.onvif.org/model/".Length);
                        string   location   = properties.First(p => p.StartsWith("onvif://www.onvif.org/location/")).Substring("onvif://www.onvif.org/location/".Length);

                        // create a new "camera" instance and trigger event that it is found
                        var camera = new DiscoveredCamera(name, model, source.Address, location, deviceServiceURL);
                        CameraDiscovered.Trigger(this, new CameraDiscoveredEventArgs(camera));

                        // add the camera to the local list
                        discoveredCameras.Add(camera);
                    }
                }
            }
            catch { /* Probably just an invalid response, nothing to do here */ }
        }
Beispiel #2
0
 protected void OnDiscovered(TargetDevice device)
 {
     CameraDiscovered?.Invoke(this, new CameraDeviceEventArgs {
         CameraDevice = device
     });
 }