Exemplo n.º 1
0
        public A10APIServer(TargetDeviceEventDispatcher dispatcher)
        {
            this.EventDispatcher = dispatcher;

            urlMap = new Dictionary <string, System.Func <HttpListenerContext, HttpListenerResponse> >()
            {
                { $"{apiUrlPrefix}/addQueue", (context) =>
                  {
                      Logger.log("/addQueue");

                      string intervalStr = context.Request.QueryString.Get("interval");
                      float  interval    = float.Parse(intervalStr);

                      string directionStr = context.Request.QueryString.Get("direction");
                      int    direction    = int.Parse(directionStr);
                      direction = direction < 0 ? -1 : 1;

                      Logger.log($"/addQueue interval : {interval}, direction : {direction}");
                      DeviceCommand command = new DeviceCommand();
                      command.direction = direction;
                      command.interval  = interval;
                      EventDispatcher.AddQueue(command);
                      return(context.Response);
                  } },
                { $"{apiUrlPrefix}/clearQueue", (context) =>
                  {
                      Logger.log("/clearQueue");
                      EventDispatcher.ClearQueue();
                      return(context.Response);
                  } },
            };
        }
        public void PublishCommand(DeviceCommand command)
        {
            List <Task> tasks = new List <Task>();

            foreach (var device in searcher.ResolvedDevices)
            {
                tasks.Add(Task.Run(() => device.PublishCommand(command)));
            }

            if (tasks != null && tasks.Count > 0)
            {
                Task.WaitAll(tasks.ToArray());
            }
        }