Ejemplo n.º 1
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
 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());
     }
 }
Ejemplo n.º 3
0
 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 async Task <JArray> GetMany(string deviceGuids = null, DateTime?timestamp = null, string names = null, int?waitTimeout = null)
        {
            var deviceIds = deviceGuids == null ? null : ParseDeviceGuids(deviceGuids).Select(d => d.ID).ToArray();

            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.GetByDevices(deviceIds, filter);
                return(MapDeviceCommands(commands.Where(c => IsDeviceAccessible(c.Device))));
            }

            var config    = DeviceHiveConfiguration.RestEndpoint;
            var delayTask = Task.Delay(1000 * Math.Min(config.CommandPollMaxInterval, waitTimeout ?? config.CommandPollDefaultInterval));

            using (var waiterHandle = _commandByDeviceIdWaiter.BeginWait(
                       deviceIds == null ? new object[] { null } : deviceIds.Cast <object>().ToArray(),
                       commandNames == null ? null : commandNames.Cast <object>().ToArray()))
            {
                do
                {
                    var filter = new DeviceCommandFilter {
                        Start = start, IsDateInclusive = false, Commands = commandNames
                    };
                    var commands = DataContext.DeviceCommand.GetByDevices(deviceIds, filter)
                                   .Where(c => IsDeviceAccessible(c.Device)).ToArray();
                    if (commands != null && commands.Any())
                    {
                        return(MapDeviceCommands(commands));
                    }
                }while (await Task.WhenAny(waiterHandle.Wait(), delayTask) != delayTask);
            }

            return(new JArray());
        }
Ejemplo n.º 5
0
        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();
        }
        public List <DeviceCommand> GetByDevices(int[] deviceIds, DeviceCommandFilter filter = null)
        {
            var query = _mongo.DeviceCommands.AsQueryable();

            if (deviceIds != null)
            {
                query = query.Where(e => deviceIds.Contains(e.DeviceID));
            }
            var commands = query.Filter(filter).ToList();

            if (commands.Any())
            {
                var actualDeviceIds = commands.Select(e => e.DeviceID).Distinct().ToArray();
                var deviceLookup    = _mongo.Devices.Find(Query <Device> .In(e => e.ID, actualDeviceIds)).ToDictionary(e => e.ID);

                foreach (var command in commands)
                {
                    command.Device = deviceLookup[command.DeviceID];
                }
            }

            return(commands);
        }
 public List <DeviceCommand> GetByDevice(int deviceId, DeviceCommandFilter filter = null)
 {
     return(_mongo.DeviceCommands.AsQueryable().Where(e => e.DeviceID == deviceId).Filter(filter).ToList());
 }
        public static IQueryable <DeviceCommand> Filter(this IQueryable <DeviceCommand> query, DeviceCommandFilter filter)
        {
            if (filter == null)
            {
                return(query);
            }

            if (filter.Start != null)
            {
                var start = DateTime.SpecifyKind(filter.Start.Value, DateTimeKind.Utc);
                if (!filter.IsDateInclusive)
                {
                    start = start.AddTicks(10); // SQL Server has 7-digit precision, while JSON mapping 6-digit
                }
                query = filter.IsDateInclusive ? query.Where(e => e.Timestamp >= start) : query.Where(e => e.Timestamp > start);
            }

            if (filter.End != null)
            {
                var end = DateTime.SpecifyKind(filter.End.Value, DateTimeKind.Utc);
                if (!filter.IsDateInclusive)
                {
                    end = end.AddTicks(-10); // SQL Server has 7-digit precision, while JSON mapping 6-digit
                }
                query = filter.IsDateInclusive ? query.Where(e => e.Timestamp <= end) : query.Where(e => e.Timestamp < end);
            }

            if (filter.Command != null)
            {
                query = query.Where(e => e.Command == filter.Command);
            }

            if (filter.Commands != null)
            {
                query = query.Where(e => filter.Commands.Contains(e.Command));
            }

            if (filter.Status != null)
            {
                query = query.Where(e => e.Status == filter.Status);
            }

            if (filter.SortField != DeviceCommandSortField.None)
            {
                switch (filter.SortField)
                {
                case DeviceCommandSortField.Timestamp:
                    query = query.OrderBy(e => e.Timestamp, filter.SortOrder);
                    break;

                case DeviceCommandSortField.Command:
                    query = query.OrderBy(e => e.Command, filter.SortOrder)
                            .ThenBy(e => e.Timestamp, filter.SortOrder);
                    break;

                case DeviceCommandSortField.Status:
                    query = query.OrderBy(e => e.Status, filter.SortOrder)
                            .ThenBy(e => e.Timestamp, filter.SortOrder);
                    break;
                }
            }

            if (filter.Skip != null)
            {
                query = query.Skip(filter.Skip.Value);
            }

            if (filter.Take != null)
            {
                query = query.Take(filter.Take.Value);
            }

            return(query);
        }