public async Task<JArray> Get(string deviceGuid, DateTime? timestamp = null, string names = null, int? waitTimeout = null) 
        {
            var device = GetDeviceEnsureAccess(deviceGuid);

            var start = timestamp ?? _timestampRepository.GetCurrentTimestamp();
            var commandNames = names != null ? names.Split(',') : null;
            if (waitTimeout <= 0)
            {
                var filter = new DeviceCommandFilter { Start = start, IsDateInclusive = false, Commands = commandNames };
                var commands = DataContext.DeviceCommand.GetByDevice(device.ID, filter);
                return new JArray(commands.Select(c => MapDeviceCommand(c, device)));
            }

            var config = DeviceHiveConfiguration.RestEndpoint;
            var delayTask = Task.Delay(1000 * Math.Min(config.CommandPollMaxInterval, waitTimeout ?? config.CommandPollDefaultInterval));
            using (var waiterHandle = _commandByDeviceIdWaiter.BeginWait(new object[] { device.ID },
                commandNames == null ? null : commandNames.Cast<object>().ToArray()))
            {
                do
                {
                    var filter = new DeviceCommandFilter { Start = start, IsDateInclusive = false, Commands = commandNames };
                    var commands = DataContext.DeviceCommand.GetByDevice(device.ID, filter);
                    if (commands != null && commands.Any())
                        return new JArray(commands.Select(c => MapDeviceCommand(c, device)));
                }
                while (await Task.WhenAny(waiterHandle.Wait(), delayTask) != delayTask);
            }

            return new JArray();
        }
 public List<DeviceCommand> GetByDevice(int deviceId, DeviceCommandFilter filter = null)
 {
     using (var context = new DeviceHiveContext())
     {
         var query = context.DeviceCommands.Where(e => e.Device.ID == deviceId);
         return query.Filter(filter).ToList();
     }
 }
 public List<DeviceCommand> GetByDevices(int[] deviceIds, DeviceCommandFilter filter = null)
 {
     using (var context = new DeviceHiveContext())
     {
         var query = context.DeviceCommands.Include(e => e.Device);
         if (deviceIds != null)
             query = query.Where(e => deviceIds.Contains(e.Device.ID));
         return query.Filter(filter).ToList();
     }
 }
        public void SubsrcibeToDeviceCommands(DateTime? timestamp, string[] deviceGuids = null, string[] names = null)
        {
            var subscriptionId = Guid.NewGuid();
            var devices = GetDevices(deviceGuids, "GetDeviceCommand");
            var deviceIds = devices == null ? null : devices.Select(d => d.ID).ToArray();

            var initialCommandList = GetInitialCommandList(Connection, subscriptionId);
            lock (initialCommandList)
            {
                _deviceSubscriptionManagerForCommands.Subscribe(subscriptionId, Connection, deviceIds, names);
                SendResponse(new JProperty("subscriptionId", subscriptionId));

                if (timestamp != null)
                {
                    var filter = new DeviceCommandFilter { Start = timestamp, IsDateInclusive = false, Commands = names };
                    var initialCommands = DataContext.DeviceCommand.GetByDevices(deviceIds, filter)
                        .Where(n => IsDeviceAccessible(n.Device, "GetDeviceCommand")).ToArray();

                    foreach (var command in initialCommands)
                    {
                        initialCommandList.Add(command.ID);
                        Notify(Connection, subscriptionId, command, command.Device, isInitialCommand: true);
                    }
                }
            }
       }
 public void SubsrcibeToDeviceCommands(DateTime? timestamp)
 {
     var initialCommandList = GetInitialCommandList(Connection);
     lock (initialCommandList)
     {
         _subscriptionManager.Subscribe(Connection, CurrentDevice.ID);
         if (timestamp != null)
         {
             var filter = new DeviceCommandFilter { Start = timestamp, IsDateInclusive = false };
             var initialCommands = DataContext.DeviceCommand.GetByDevice(CurrentDevice.ID, filter);
             foreach (var command in initialCommands)
             {
                 initialCommandList.Add(command.ID);
                 Notify(Connection, CurrentDevice, command, isInitialCommand: true);
             }
         }
     }
     
     SendSuccessResponse();
 }