示例#1
0
        private Task DoPersistData(
            IDictionary <byte, ICollection <byte> > data, IDictionary <byte, byte> averages, DateTime currentTime, CancellationToken ct)
        {
            string fileName = _sensorDataUtil.GetDataFileName(currentTime, _options.PersistInterval);
            string filePath = Path.Combine(_options.PersistDirectory, fileName);

            Task persistTask = _writer.WriteData(data, averages, filePath, ct);

            return(persistTask);
        }
示例#2
0
        public async Task <ActionResult> GetSensorData([FromRoute] byte deviceID, [FromQuery] DateTime when)
        {
            // ASP.NET could convert date time parameters to local time, more here: https://stackoverflow.com/q/10293440/608971
            if (when.Kind == DateTimeKind.Local)
            {
                when = when.ToUniversalTime();
            }

            DateTime           normalizedWhen = _sensorDataUtil.Normalize(when, _options.PersistInterval);
            SensorDataCacheKey key            = new SensorDataCacheKey(deviceID, normalizedWhen);
            SensorDataResult   searchResult;

            if (_memoryCache.TryGetValue(key, out SensorDataResult cachedResult))
            {
                _logger.LogDebug("Use cached data for sensor {0} for {1}.", deviceID, when);
                searchResult = cachedResult;
            }
            else
            {
                string fileName = _sensorDataUtil.GetDataFileName(when, _options.PersistInterval);
                string filePath = Path.Combine(_options.DataDirectory, fileName);

                if (!System.IO.File.Exists(filePath))
                {
                    return(NotFound($"Data for sensor {deviceID} was not found for the specified date time {when}."));
                }

                searchResult = await _dataReader.FindSensorData(deviceID, filePath);

                _memoryCache.Set(key, searchResult);
            }

            ActionResult actionResult = ProcessSearchResult(deviceID, when, searchResult);

            return(actionResult);
        }