コード例 #1
0
        private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var deviceName = ConfigurationManager.AppSettings["DeviceName"];

            device = PTZDevice.GetDevice(deviceName, PTZType.Relative);

            url         = ConfigurationManager.AppSettings["relayServerUrl"];
            remoteGroup = Environment.MachineName; //They have to hardcode the group, but for us it's our machine name
            connection  = new HubConnection(url);
            proxy       = connection.CreateHubProxy("RelayHub");

            connection.TraceLevel  = TraceLevels.StateChanges | TraceLevels.Events;
            connection.TraceWriter = new PTZRemoteTraceWriter(Log);

            //Can't do this here because DirectShow has to be on the UI thread!
            // This would cause an obscure COM casting error with no clue what's up. So, um, ya.
            //proxy.On<int, int>("Move",(x,y) => device.Move(x, y));
            //proxy.On<int>("Zoom", (z) => device.Zoom(z));

            magic = SynchronizationContext.Current;

            proxy.On <int, int>("Move", (x, y) =>
            {
                //Toss this over the fence from this background thread to the UI thread
                magic.Post((_) => {
                    Log(String.Format("Move({0},{1})", x, y));
                    device.Move(x, y);
                }, null);
            });

            proxy.On <int>("Zoom", (z) =>
            {
                magic.Post((_) =>
                {
                    Log(String.Format("Zoom({0})", z));
                    device.Zoom(z);
                }, null);
            });


            try
            {
                await connection.Start();

                Log("After connection.Start()");
                await proxy.Invoke("JoinRelay", remoteGroup);

                Log("After JoinRelay");
            }
            catch (Exception pants)
            {
                Log(pants.GetError().ToString());
                throw;
            }
        }
コード例 #2
0
        protected override void OnStart(string[] args)
        {
            PTZDevice device = null;

            httpsv = new HttpServer(4649);

            httpsv.OnGet += (sender, e) =>
            {
                HttpListenerRequest  req = e.Request;
                HttpListenerResponse res = e.Response;

                string msg   = "";
                string query = req.QueryString.ToString();
                if (query.Contains("init"))
                {
                    Match  match      = Regex.Match(query, "init=(.+)");
                    string deviceName = match.Groups[1].Captures[0].ToString();
                    try
                    {
                        device         = PTZDevice.GetDevice(deviceName, PTZType.Relative);
                        msg            = "Device created";
                        res.StatusCode = (int)HttpStatusCode.OK;
                    }
                    catch (Exception ex)
                    {
                        msg            = ex.Message;
                        res.StatusCode = (int)HttpStatusCode.InternalServerError;
                    }
                }
                else if (query.Contains("action"))
                {
                    if (device == null)
                    {
                        msg            = "Device must be initialized before using";
                        res.StatusCode = (int)HttpStatusCode.BadRequest;
                    }
                    else
                    {
                        Match  match  = Regex.Match(query, "action=(.+)");
                        string action = match.Groups[1].Captures[0].ToString();
                        if (action.Equals("move_left"))
                        {
                            device.Move(-1, 0);
                        }
                        else if (action.Equals("move_right"))
                        {
                            device.Move(1, 0);
                        }
                        else if (action.Equals("move_up"))
                        {
                            device.Move(0, -1);
                        }
                        else if (action.Equals("move_down"))
                        {
                            device.Move(0, 1);
                        }
                        else if (action.Equals("zoom_in"))
                        {
                            device.Zoom(1);
                        }
                        else if (action.Equals("zoom_out"))
                        {
                            device.Zoom(-1);
                        }
                        else
                        {
                            msg            = "Unsupported action: " + action;
                            res.StatusCode = (int)HttpStatusCode.BadRequest;
                        }
                    }
                }
                else
                {
                    msg            = "Invalida request";
                    res.StatusCode = (int)HttpStatusCode.BadRequest;
                }

                string json = "{\"message\":\"" + msg + "\", \"query\":\"" + req.QueryString + "\"}";
                res.ContentType     = "application/json";
                res.ContentEncoding = Encoding.UTF8;

                res.WriteContent(Encoding.UTF8.GetBytes(json));
            };

            httpsv.Start();

            if (httpsv.IsListening)
            {
                Console.WriteLine("Listening on port {0}, and providing WebSocket services:", httpsv.Port);
                foreach (var path in httpsv.WebSocketServices.Paths)
                {
                    Console.WriteLine("- {0}", path);
                }
            }
        }