forked from PrzemekFortuna/iTram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorReadingService.cs
139 lines (118 loc) · 4.9 KB
/
SensorReadingService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using AutoMapper;
using DBConnection;
using DBConnection.DTO;
using DBConnection.Entities;
using DBConnection.Entities.Sensors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.EntityFrameworkCore;
using Services.Handlers;
using Services.Handlers.Gyroscope;
using Services.Helpers;
namespace Services
{
public class SensorReadingService
{
private readonly IMapper _mapper;
private readonly AccelerometerHandler _accelerometerHandler;
private readonly GyroscopeHandler _gyroscopeHandler;
private readonly LocationHandler _locationHandler;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ModelsManager.ModelsManager _modelsManager;
public TramContext Context { get; set; }
public ModelStateDictionary ModelState;
public SensorReadingService(TramContext context, IMapper mapper, AccelerometerHandler accelerometerHandler,
GyroscopeHandler gyroscopeHandler, LocationHandler locationHandler, IHttpContextAccessor httpContextAccessor,
ModelsManager.ModelsManager modelsManager)
{
_mapper = mapper;
_accelerometerHandler = accelerometerHandler;
_gyroscopeHandler = gyroscopeHandler;
_locationHandler = locationHandler;
_httpContextAccessor = httpContextAccessor;
_modelsManager = modelsManager;
Context = context;
}
public async Task<IEnumerable<SensorsReadingDTO>> GetAllAsync()
{
var sensorReadings = await Context.SensorsReadings
.Include(x => x.Gyroscope)
.Include(x => x.Accelerometer)
.Include(x => x.Location)
.ToListAsync();
return _mapper.Map<IEnumerable<SensorsReadingDTO>>(sensorReadings);
}
public async Task<bool> AddAsync(SensorsReadingUnitsDTO sensorsReading)
{
var reading = _mapper.Map<SensorsReading>(sensorsReading);
if (!TryToHandleSensorsReading(reading))
return false;
var sr = await Context.SensorsReadings.AddAsync(reading);
await Context.SaveChangesAsync();
return true;
}
public async Task<bool> AddAllAsync(IEnumerable<SensorsReadingUnitsDTO> sensorsReadings)
{
var readings = _mapper.Map<IEnumerable<SensorsReading>>(sensorsReadings).ToList();
foreach (var sensorsReading in readings)
{
if (!TryToHandleSensorsReading(sensorsReading))
return false;
}
await Context.AddRangeAsync(readings);
await Context.SaveChangesAsync();
return true;
}
public async Task<bool?> AmIInTram(IEnumerable<SensorsReadingUnitsDTO> sensorsReadings)
{
var readings = _mapper.Map<IEnumerable<SensorsReading>>(sensorsReadings).ToList();
foreach (var sensorsReading in readings)
{
if (!TryToHandleSensorsReading(sensorsReading))
return null;
}
return await _modelsManager.IsInTram(readings);
}
private bool TryToHandleSensorsReading(SensorsReading sensorsReading)
{
if (!String.IsNullOrWhiteSpace(sensorsReading.Accelerometer.AccelerometerUnit))
{
var result = _accelerometerHandler.Handle(sensorsReading.Accelerometer);
if (result == null)
return false;
}
if (!String.IsNullOrWhiteSpace(sensorsReading.Gyroscope.GyroscopeUnit))
{
var result = _gyroscopeHandler.Handle(sensorsReading.Gyroscope);
if (result == null)
return false;
}
if (!String.IsNullOrWhiteSpace(sensorsReading.Location.LocationUnit))
{
var result = _locationHandler.Handle(sensorsReading.Location);
if (result == null)
return false;
}
sensorsReading.UserId = Int32.Parse(_httpContextAccessor.HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value);
return true;
}
public async Task<IEnumerable<SensorsReadingDTO>> GetAllNewAsync()
{
var sensorReadings = await Context.SensorsReadings
.Where(x=>x.IsNew)
.Include(x => x.Gyroscope)
.Include(x => x.Accelerometer)
.Include(x => x.Location)
.ToListAsync();
sensorReadings.ForEach(x=>x.IsNew = false);
await Context.SaveChangesAsync();
return _mapper.Map<IEnumerable<SensorsReadingDTO>>(sensorReadings);
}
}
}