示例#1
0
        private async static Task <bool> ProcessEnergyMonitor(Guid thingId, byte[] message)
        {
            var _context  = _serviceScopeFactory.CreateScope().ServiceProvider.GetRequiredService <ApplicationDbContext>();
            var _measures = new MeasuresController(_context, _hubContext);

            var thing = _context.Thing.AsNoTracking().Where(thing => thing.ThingId.Equals(thingId)).FirstOrDefault();

            MeasureDTO measure;

            try
            {
                Stream measureStream = new MemoryStream(message);
                var    options       = new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                };
                options.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
                measure = await JsonSerializer.DeserializeAsync <MeasureDTO>(measureStream, options);

                measure.ThingId = thingId;
            }
            catch (JsonException ex)
            {
                _logger.LogError(ex.Message);
                return(false);
            }

            if (thing == null)
            {
                _logger.LogWarning($"Thing with Id {thingId} not found");
                return(false);
            }
            else if (!thing.Features.ToString().Split(',', StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToList().Contains(measure.Type.ToString().Trim()))
            {
                _logger.LogWarning($"Thing with Id {thingId} does not have the specified feature {measure.Type}");
                return(false);
            }

            try
            {
                await _measures.PostMeasure(measure);
            }
            catch (DbUpdateException ex)
            {
                _logger.LogError(ex.Message);
                return(false);
            }

            return(true);
        }
示例#2
0
 public BoschIoTSuiteController(ApiDbContext context)
 {
     _context  = context;
     _measures = new MeasuresController(_context);
 }