예제 #1
0
        public override Task <ListAlarmChangesResponse> ListAlarmChanges(
            ListAlarmChangesRequest request,
            ServerCallContext context)
        {
            string id = BitConverter.ToString(request.DeviceId.ToByteArray());

            DateTimeOffset endDateTime;

            if (string.IsNullOrEmpty(request.PageToken))
            {
                endDateTime = request.EndTime?.ToDateTimeOffset() ?? DateTimeOffset.UtcNow;
            }
            else
            {
                endDateTime = DateTimeOffset.Parse(request.PageToken, CultureInfo.InvariantCulture);
            }

            endDateTime = endDateTime.ToUniversalTime();
            DateTimeOffset?startDateTime = request.StartTime?.ToDateTimeOffset().ToUniversalTime();

            var response = new ListAlarmChangesResponse();

            using (BjdireContext db = this.serviceProvider.GetRequiredService <BjdireContext>())
            {
                var alarmChanges = (from m in db.AlarmChanges
                                    where m.DeviceId == id &&
                                    (startDateTime == null || startDateTime <= m.Timestamp) &&
                                    m.Timestamp <= endDateTime
                                    orderby m.Timestamp descending
                                    select m)
                                   .Take(request.PageSize)
                                   .ToList();
                response.AlarmChanges.AddRange(alarmChanges.Select(alarmChange =>
                {
                    var m = new GrpcAlarmChange();
                    m.AssignFrom(alarmChange);
                    return(m);
                }));

                if (alarmChanges.Count == request.PageSize && alarmChanges.Last().Timestamp > startDateTime)
                {
                    response.NextPageToken = alarmChanges.Last().Timestamp
                                             .ToUniversalTime()
                                             .ToString(CultureInfo.InvariantCulture);
                }
            }

            return(Task.FromResult(response));
        }
예제 #2
0
        public override AsyncUnaryCall <ListAlarmChangesResponse> ListAlarmChangesAsync(ListAlarmChangesRequest request, Metadata headers = null, DateTime?deadline = null, CancellationToken cancellationToken = default)
        {
            DateTimeOffset endDateTime;

            if (string.IsNullOrEmpty(request.PageToken))
            {
                endDateTime = request.EndTime?.ToDateTimeOffset() ?? DateTimeOffset.UtcNow;
            }
            else
            {
                endDateTime = DateTimeOffset.Parse(request.PageToken, CultureInfo.InvariantCulture);
            }

            endDateTime = endDateTime.ToUniversalTime();
            var startDateTime = request.StartTime?.ToDateTime();

            var alarmChanges = new List <AlarmChange>(request.PageSize);

            for (int i = 0; i < request.PageSize; i++)
            {
                endDateTime = endDateTime.Subtract(TimeSpan.FromSeconds(5));
                if (startDateTime.HasValue && startDateTime > endDateTime)
                {
                    break;
                }

                alarmChanges.Add(new AlarmChange
                {
                    CreateTime           = Timestamp.FromDateTimeOffset(endDateTime),
                    AlarmType            = (AlarmType)RandomUtils.Next(1, 7),
                    AlarmChangeDirection = (AlarmChangeDirection)RandomUtils.Next(1, 3),
                });
            }

            var response = new ListAlarmChangesResponse
            {
                NextPageToken = endDateTime.ToUniversalTime().ToString(CultureInfo.InvariantCulture),
            };

            response.AlarmChanges.AddRange(alarmChanges);

            return(TestCalls.AsyncUnaryCall(
                       Task.FromResult(response),
                       Task.FromResult(new Metadata()),
                       () => Status.DefaultSuccess,
                       () => new Metadata(),
                       () => { }));
        }