-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathDevicesController.cs
More file actions
225 lines (185 loc) · 6.86 KB
/
DevicesController.cs
File metadata and controls
225 lines (185 loc) · 6.86 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Resgrid.Model.Services;
using System.Threading.Tasks;
using Resgrid.Model;
using System;
using System.Web;
using Resgrid.Model.Providers;
using Resgrid.Web.Services.Models.v4.Device;
using Resgrid.Web.Services.Helpers;
using Resgrid.Model.Events;
using Resgrid.Framework;
using Resgrid.Web.Services.Controllers.Version3.Models.Devices;
using System.Threading;
namespace Resgrid.Web.Services.Controllers.v4
{
/// <summary>
/// Mobile or Tablet Device specific operations
/// </summary>
[Route("api/v{VersionId:apiVersion}/[controller]")]
[ApiVersion("4.0")]
[ApiExplorerSettings(GroupName = "v4")]
public class DevicesController : V4AuthenticatedApiControllerbase
{
#region Members and Constructors
private readonly IPushService _pushService;
private readonly ICqrsProvider _cqrsProvider;
public DevicesController(IPushService pushService, ICqrsProvider cqrsProvider)
{
_pushService = pushService;
_cqrsProvider = cqrsProvider;
}
#endregion Members and Constructors
/// <summary>
/// Register a unit device to receive push notification from the Resgrid system
/// </summary>
/// <param name="registrationInput">Input to create the registration for</param>
/// <returns>Result for the registration</returns>
[HttpPost("RegisterUnitDevice")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<PushRegistrationResult>> RegisterUnitDevice([FromBody] PushRegistrationInput registrationInput)
{
var result = new PushRegistrationResult();
if (this.ModelState.IsValid)
{
try
{
if (registrationInput == null)
return BadRequest();
PushRegisterionEvent pushRegisterionEvent = new PushRegisterionEvent();
pushRegisterionEvent.PushUriId = 0;
pushRegisterionEvent.UserId = UserId;
pushRegisterionEvent.PlatformType = registrationInput.Platform;
if (!string.IsNullOrWhiteSpace(registrationInput.Prefix))
pushRegisterionEvent.PushLocation = registrationInput.Prefix;
else
pushRegisterionEvent.PushLocation = "";
pushRegisterionEvent.DepartmentId = DepartmentId;
pushRegisterionEvent.DeviceId = registrationInput.Token;
pushRegisterionEvent.Uuid = registrationInput.DeviceUuid;
if (!String.IsNullOrWhiteSpace(registrationInput.UnitId) && registrationInput.UnitId != "0")
pushRegisterionEvent.UnitId = int.Parse(registrationInput.UnitId);
CqrsEvent registerUnitPushEvent = new CqrsEvent();
registerUnitPushEvent.Type = (int)CqrsEventTypes.UnitPushRegistration;
registerUnitPushEvent.Data = ObjectSerialization.Serialize(pushRegisterionEvent);
await _cqrsProvider.EnqueueCqrsEventAsync(registerUnitPushEvent);
result.Status = ResponseHelper.Queued;
}
catch (Exception ex)
{
result.Status = ResponseHelper.Failure;
Framework.Logging.LogException(ex);
return result;
}
}
result.Id = "";
result.PageSize = 0;
ResponseHelper.PopulateV4ResponseData(result);
return result;
}
/// <summary>
/// Removed a Unit Push Notification support by PushUriId.
/// </summary>
/// <param name="deviceUuid">Input to deregister the device for</param>
/// <returns>Result for the unregistration</returns>
[HttpDelete("UnRegisterUnitDevice")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<PushRegistrationResult>> UnRegisterUnitDevice(string deviceUuid)
{
if (String.IsNullOrWhiteSpace(deviceUuid))
return BadRequest();
var result = new PushRegistrationResult();
try
{
var deviceId = HttpUtility.UrlDecode(deviceUuid);
await _pushService.UnRegisterUnit(new PushUri() { UserId = UserId, DeviceId = deviceId });
result.Status = ResponseHelper.Success;
}
catch (Exception ex)
{
Framework.Logging.LogException(ex);
result.Status = ResponseHelper.Failure;
}
result.Id = "";
result.PageSize = 0;
ResponseHelper.PopulateV4ResponseData(result);
return result;
}
/// <summary>
/// Register a device to receive push notification from the Resgrid system
/// </summary>
/// <param name="registrationInput">Input to create the registration for</param>
/// <returns>Result for the registration</returns>
[HttpPost("RegisterDevice")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<PushRegistrationResult>> RegisterDevice([FromBody] PushRegistrationInput registrationInput)
{
if (this.ModelState.IsValid)
{
var result = new PushRegistrationResult();
try
{
if (registrationInput == null)
return BadRequest();
var push = new PushUri();
push.UserId = UserId;
push.PlatformType = registrationInput.Platform;
if (registrationInput.Platform == (int)Platforms.Android)
push.PushLocation = "Android";
else if (registrationInput.Platform == (int)Platforms.iPhone || registrationInput.Platform == (int)Platforms.iPad)
push.PushLocation = "Apple";
push.DeviceId = registrationInput.Token;
push.Uuid = registrationInput.DeviceUuid;
push.DepartmentId = DepartmentId;
CqrsEvent registerUnitPushEvent = new CqrsEvent();
registerUnitPushEvent.Type = (int)CqrsEventTypes.PushRegistration;
registerUnitPushEvent.Data = ObjectSerialization.Serialize(push);
await _cqrsProvider.EnqueueCqrsEventAsync(registerUnitPushEvent);
result.Status = ResponseHelper.Queued;
}
catch (Exception ex)
{
result.Status = ResponseHelper.Failure;
Framework.Logging.LogException(ex);
return result;
}
return result;
}
return BadRequest();
}
/// <summary>
/// Removed a Push Notification support by PushUriId.
/// </summary>
/// <param name="deviceUuid">Input to deregister the device for</param>
/// <returns>Result for the unregistration</returns>
[HttpDelete("UnRegisterDevice")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult<PushRegistrationResult>> UnRegisterDevice(string deviceUuid)
{
if (String.IsNullOrWhiteSpace(deviceUuid))
return BadRequest();
var result = new PushRegistrationResult();
try
{
var deviceId = HttpUtility.UrlDecode(deviceUuid);
//await _pushService.UnRegister(new PushUri() { UserId = UserId, DeviceId = deviceId, PushUriId = input.Pid });
await _pushService.UnRegister(new PushUri() { UserId = UserId, DeviceId = deviceId });
result.Status = ResponseHelper.Success;
}
catch (Exception ex)
{
Framework.Logging.LogException(ex);
result.Status = ResponseHelper.Failure;
}
result.Id = "";
result.PageSize = 0;
ResponseHelper.PopulateV4ResponseData(result);
return result;
}
}
}