From 0d2e417f38765f064e04067d6ef208e00a10ba49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Ondreji=C4=8Dka?= <m.ondrejicka@gmail.com> Date: Thu, 10 Jun 2021 11:57:17 +0200 Subject: [PATCH] - partial classes - addsensor, getsensor, updatesensor - test sada --- Main/TestData.cs | 2 +- SDK.sln | 6 + SDK/{ => Connection}/ConnectionOptions.cs | 0 .../ConnectionOptionsBuilder.cs | 0 SDK/{ => Connection}/DevkitConnector.cs | 0 SDK/{ => Connection}/DevkitConnectorV2.cs | 0 SDK/{ => Connection}/DevkitFactory.cs | 0 SDK/Connection/V3/Areas.cs | 28 + SDK/Connection/V3/Authorization.cs | 85 +++ SDK/Connection/V3/Base.cs | 43 ++ SDK/Connection/V3/Beacons.cs | 62 ++ SDK/Connection/V3/Branches.cs | 28 + SDK/Connection/V3/Clients.cs | 21 + SDK/Connection/V3/Communication.cs | 121 ++++ SDK/Connection/V3/Configuration.cs | 52 ++ SDK/Connection/V3/Devices.cs | 80 +++ SDK/Connection/V3/Layers.cs | 29 + SDK/Connection/V3/Localization.cs | 62 ++ SDK/Connection/V3/Sectors.cs | 54 ++ SDK/Connection/V3/Sensors.cs | 114 ++++ SDK/Connection/V3/Users.cs | 22 + SDK/Connection/V3/Utils.cs | 55 ++ .../AddDeviceResponseContract.cs | 93 +++ .../AddSectorResponseContract.cs | 62 ++ .../AddSensorResponseContract.cs | 24 +- SDK/Contracts/Data/AccountContract.cs | 4 +- SDK/Contracts/Data/DeviceContract.cs | 47 +- SDK/DevkitConnectorV3.cs | 623 ------------------ SDK/{ => Helpers}/Address.cs | 0 SDK/SDK.csproj | 4 + Test/Test/Helper.cs | 43 ++ Test/Test/Test.csproj | 20 + Test/Test/TestData.cs | 245 +++++++ Test/Test/V3/Areas.cs | 39 ++ Test/Test/V3/Authorization.cs | 27 + Test/Test/V3/Base.cs | 16 + Test/Test/V3/Beacons.cs | 92 +++ Test/Test/V3/Branches.cs | 40 ++ Test/Test/V3/Clients.cs | 23 + Test/Test/V3/Configuration.cs | 31 + Test/Test/V3/Devices.cs | 114 ++++ Test/Test/V3/Layers.cs | 40 ++ Test/Test/V3/Localization.cs | 25 + Test/Test/V3/Sectors.cs | 73 ++ Test/Test/V3/Sensors.cs | 108 +++ Test/Test/V3/Users.cs | 19 + Test/Test/V3/Utils.cs | 43 ++ 47 files changed, 2079 insertions(+), 640 deletions(-) rename SDK/{ => Connection}/ConnectionOptions.cs (100%) rename SDK/{ => Connection}/ConnectionOptionsBuilder.cs (100%) rename SDK/{ => Connection}/DevkitConnector.cs (100%) rename SDK/{ => Connection}/DevkitConnectorV2.cs (100%) rename SDK/{ => Connection}/DevkitFactory.cs (100%) create mode 100644 SDK/Connection/V3/Areas.cs create mode 100644 SDK/Connection/V3/Authorization.cs create mode 100644 SDK/Connection/V3/Base.cs create mode 100644 SDK/Connection/V3/Beacons.cs create mode 100644 SDK/Connection/V3/Branches.cs create mode 100644 SDK/Connection/V3/Clients.cs create mode 100644 SDK/Connection/V3/Communication.cs create mode 100644 SDK/Connection/V3/Configuration.cs create mode 100644 SDK/Connection/V3/Devices.cs create mode 100644 SDK/Connection/V3/Layers.cs create mode 100644 SDK/Connection/V3/Localization.cs create mode 100644 SDK/Connection/V3/Sectors.cs create mode 100644 SDK/Connection/V3/Sensors.cs create mode 100644 SDK/Connection/V3/Users.cs create mode 100644 SDK/Connection/V3/Utils.cs create mode 100644 SDK/Contracts/Communication/AddDeviceResponseContract.cs create mode 100644 SDK/Contracts/Communication/AddSectorResponseContract.cs delete mode 100644 SDK/DevkitConnectorV3.cs rename SDK/{ => Helpers}/Address.cs (100%) create mode 100644 Test/Test/Helper.cs create mode 100644 Test/Test/Test.csproj create mode 100644 Test/Test/TestData.cs create mode 100644 Test/Test/V3/Areas.cs create mode 100644 Test/Test/V3/Authorization.cs create mode 100644 Test/Test/V3/Base.cs create mode 100644 Test/Test/V3/Beacons.cs create mode 100644 Test/Test/V3/Branches.cs create mode 100644 Test/Test/V3/Clients.cs create mode 100644 Test/Test/V3/Configuration.cs create mode 100644 Test/Test/V3/Devices.cs create mode 100644 Test/Test/V3/Layers.cs create mode 100644 Test/Test/V3/Localization.cs create mode 100644 Test/Test/V3/Sectors.cs create mode 100644 Test/Test/V3/Sensors.cs create mode 100644 Test/Test/V3/Users.cs create mode 100644 Test/Test/V3/Utils.cs diff --git a/Main/TestData.cs b/Main/TestData.cs index 5824f89..d9c1a47 100644 --- a/Main/TestData.cs +++ b/Main/TestData.cs @@ -119,7 +119,7 @@ namespace Main { Id = 7351, Login = "sdk-sensor2", - Password = "sdk", + //Password = "sdk", Title = "testtt", SectorId = 2, SensorData = sensorDataContracts, diff --git a/SDK.sln b/SDK.sln index 8d4f0c0..92bbac4 100644 --- a/SDK.sln +++ b/SDK.sln @@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SDK", "SDK\SDK.csproj", "{A EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Main", "Main\Main.csproj", "{8AB48636-5868-4A43-9B6A-7BAAB085FE77}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Test", "Test\Test\Test.csproj", "{488CE946-CD3E-49E5-A40D-AA2FD5A2744B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -21,6 +23,10 @@ Global {8AB48636-5868-4A43-9B6A-7BAAB085FE77}.Debug|Any CPU.Build.0 = Debug|Any CPU {8AB48636-5868-4A43-9B6A-7BAAB085FE77}.Release|Any CPU.ActiveCfg = Release|Any CPU {8AB48636-5868-4A43-9B6A-7BAAB085FE77}.Release|Any CPU.Build.0 = Release|Any CPU + {488CE946-CD3E-49E5-A40D-AA2FD5A2744B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {488CE946-CD3E-49E5-A40D-AA2FD5A2744B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {488CE946-CD3E-49E5-A40D-AA2FD5A2744B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {488CE946-CD3E-49E5-A40D-AA2FD5A2744B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/SDK/ConnectionOptions.cs b/SDK/Connection/ConnectionOptions.cs similarity index 100% rename from SDK/ConnectionOptions.cs rename to SDK/Connection/ConnectionOptions.cs diff --git a/SDK/ConnectionOptionsBuilder.cs b/SDK/Connection/ConnectionOptionsBuilder.cs similarity index 100% rename from SDK/ConnectionOptionsBuilder.cs rename to SDK/Connection/ConnectionOptionsBuilder.cs diff --git a/SDK/DevkitConnector.cs b/SDK/Connection/DevkitConnector.cs similarity index 100% rename from SDK/DevkitConnector.cs rename to SDK/Connection/DevkitConnector.cs diff --git a/SDK/DevkitConnectorV2.cs b/SDK/Connection/DevkitConnectorV2.cs similarity index 100% rename from SDK/DevkitConnectorV2.cs rename to SDK/Connection/DevkitConnectorV2.cs diff --git a/SDK/DevkitFactory.cs b/SDK/Connection/DevkitFactory.cs similarity index 100% rename from SDK/DevkitFactory.cs rename to SDK/Connection/DevkitFactory.cs diff --git a/SDK/Connection/V3/Areas.cs b/SDK/Connection/V3/Areas.cs new file mode 100644 index 0000000..1a361c6 --- /dev/null +++ b/SDK/Connection/V3/Areas.cs @@ -0,0 +1,28 @@ +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(2/2) + public partial class DevkitConnectorV3 + { + public async Task<AreaContract[]> GetAreas() + { + string subUrl = Address.Areas; + var response = await GetRequest<AreaContract[]>(subUrl); + + return response; + } + public async Task<AreaContract> GetArea(int id) + { + string subUrl = Address.Areas + id; + var response = await GetRequest<AreaContract>(subUrl); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Authorization.cs b/SDK/Connection/V3/Authorization.cs new file mode 100644 index 0000000..d65a9df --- /dev/null +++ b/SDK/Connection/V3/Authorization.cs @@ -0,0 +1,85 @@ +using SDK; +using SDK.Communication; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(3/3) + public partial class DevkitConnectorV3 + { + public async Task<HttpResponseMessage> DeleteCurrentToken() + { + string subUrl = Address.AuthorizationToken; + + httpClient.DefaultRequestHeaders.Clear(); + httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token); + + var response = await DeleteRequest(subUrl); + + connectionOptions.Token = null; + + resetHttpClientHeaders(); + + return response; + } + public async Task<AuthenticationResponseContract> GetToken() + { + string subUrl = Address.AuthorizationToken; + + var response = await GetRequest<AuthenticationResponseContract>(subUrl); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + connectionOptions.Token = response.Token; + if (response.Client != null) + connectionOptions.ClientGuid = response.Client; + if (response.Branch != null) + connectionOptions.BranchGuid = response.Branch; + + resetHttpClientHeaders(); + + return response; + } + public async Task<AuthenticationResponseContract> Authenticate(bool superUser) + { + return await Authenticate(connectionOptions.Login, connectionOptions.Password, superUser); + } + public async Task<AuthenticationResponseContract> Authenticate(string login, string password, bool superUser) + { + string subUrl = Address.AuthorizationAuthenticate; + + CredentialContract credentialContract = new CredentialContract + { + Login = login, + Password = password + }; + + + if (!superUser) + credentialContract.Client = connectionOptions.Client; + + var response = await PostRequest<AuthenticationResponseContract>(subUrl, credentialContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + connectionOptions.Token = response.Token; + if (response.Client != null) + connectionOptions.ClientGuid = response.Client; + if (response.Branch != null) + connectionOptions.BranchGuid = response.Branch; + + resetHttpClientHeaders(); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Base.cs b/SDK/Connection/V3/Base.cs new file mode 100644 index 0000000..c904e27 --- /dev/null +++ b/SDK/Connection/V3/Base.cs @@ -0,0 +1,43 @@ +using SDK; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using System.Net.Http.Headers; +using System.Text.Json; +using SDK.Communication; +using SDK.Exceptions; +using SDK.Contracts.Data; +using SDK.Contracts.Communication; +using System.IO; + +namespace tDevkit +{ + public partial class DevkitConnectorV3 : DevkitConnector + { + private ConnectionOptions connectionOptions; + protected HttpClient httpClient; + private string baseAddress; + + public DevkitConnectorV3(ConnectionOptions connectionOptions) : this(connectionOptions, new HttpClient()) + { + } + public DevkitConnectorV3(ConnectionOptions connectionOptions, HttpClient httpClient) + { + this.connectionOptions = connectionOptions; + this.httpClient = httpClient; + baseAddress = connectionOptions.Url + "/" + connectionOptions.Version + "/"; + + resetHttpClientHeaders(); + } + } + + #region AOS (0/13) + public partial class Base + { + } + #endregion +} diff --git a/SDK/Connection/V3/Beacons.cs b/SDK/Connection/V3/Beacons.cs new file mode 100644 index 0000000..d39fda4 --- /dev/null +++ b/SDK/Connection/V3/Beacons.cs @@ -0,0 +1,62 @@ +using SDK.Contracts.Communication; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(5/5) + public partial class DevkitConnectorV3 + { + public async Task<BeaconContract[]> GetBeacons() + { + string subUrl = Address.Beacons; + var response = await GetRequest<BeaconContract[]>(subUrl); + + return response; + } + public async Task<BeaconContract> AddBeacon(BeaconContract beaconContract) + { + string subUrl = Address.Beacons; + var response = await PostRequest<AddBeaconResponseContract>(subUrl, beaconContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return (BeaconContract)response; + } + public async Task<HttpResponseMessage> DeleteBeacon(int id) + { + string subUrl = Address.Beacons + id; + var response = await DeleteRequest(subUrl); + + return response; + } + public async Task<BeaconContract> GetBeacon(int id) + { + string subUrl = Address.Beacons + id; + var response = await GetRequest<BeaconContract>(subUrl); + + return response; + } + public async Task<PatchResponseContract> UpdateBeacon(BeaconContract beaconContract) + { + if (beaconContract.Id == 0) + { + throw new BadRequestException(NotFoundException.message + " Beacon object has no Id."); + } + string subUrl = Address.Beacons + beaconContract.Id; + var response = await PatchRequest(subUrl, beaconContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Branches.cs b/SDK/Connection/V3/Branches.cs new file mode 100644 index 0000000..8c1bb2b --- /dev/null +++ b/SDK/Connection/V3/Branches.cs @@ -0,0 +1,28 @@ +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(2/2) + public partial class DevkitConnectorV3 + { + public async Task<BranchContract[]> GetBranches(string queryString = "") + { + string subUrl = Address.Branches + queryString; + var response = await GetRequest<BranchContract[]>(subUrl); + + return response; + } + public async Task<BranchContract> GetBranch(int id) + { + string subUrl = Address.Branches + id; + var response = await GetRequest<BranchContract>(subUrl); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Clients.cs b/SDK/Connection/V3/Clients.cs new file mode 100644 index 0000000..c97f361 --- /dev/null +++ b/SDK/Connection/V3/Clients.cs @@ -0,0 +1,21 @@ +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(1/1) + public partial class DevkitConnectorV3 + { + public async Task<ClientContract[]> GetClients() + { + string subUrl = Address.Clients; + var response = await GetRequest<ClientContract[]>(subUrl); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Communication.cs b/SDK/Connection/V3/Communication.cs new file mode 100644 index 0000000..c13d8d4 --- /dev/null +++ b/SDK/Connection/V3/Communication.cs @@ -0,0 +1,121 @@ +using SDK.Contracts.Communication; +using SDK.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Text.Json; +using System.Threading.Tasks; +using tDevkit; + +namespace tDevkit +{ + public partial class DevkitConnectorV3 + { + private void resetHttpClientHeaders() + { + httpClient.DefaultRequestHeaders.Clear(); + httpClient.DefaultRequestHeaders.Add("Client", connectionOptions.ClientGuid); + httpClient.DefaultRequestHeaders.Add("Branch", connectionOptions.BranchGuid); + httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token); + httpClient.DefaultRequestHeaders.Add("Api-Key", connectionOptions.ApiKey); + } + + #region REQUESTS + private async Task<Type> GetRequest<Type>(string subUrl) + { + var task = await SendGetRequest(subUrl); + + string responseString = await ProcessResponse(task); + return JsonSerializer.Deserialize<Type>(responseString); + } + private async Task<Type> PostRequest<Type>(string subUrl, object body) + { + string bodyContent = JsonSerializer.Serialize(body); + var task = await SendPostRequest(subUrl, bodyContent); + + string responseString = await ProcessResponse(task); + return JsonSerializer.Deserialize<Type>(responseString); + } + private async Task<PatchResponseContract> PatchRequest(string subUrl, object body) + { + string bodyContent = JsonSerializer.Serialize(body); + var task = await SendPatchRequest(subUrl, bodyContent); + + string responseString = await ProcessResponse(task); + return new PatchResponseContract(); + } + private async Task<HttpResponseMessage> DeleteRequest(string subUrl) + { + var task = await SendDeleteRequest(subUrl); + + string responseString = await ProcessResponse(task); + + if (responseString == "") + { + return new HttpResponseMessage(); + } + return JsonSerializer.Deserialize<HttpResponseMessage>(responseString); + } + #endregion + + #region SEND REQUESTS + private async Task<HttpResponseMessage> SendGetRequest(string subUrl) + { + var task = await httpClient.GetAsync(baseAddress + subUrl); + + return task; + } + private async Task<HttpResponseMessage> SendPostRequest(string subUrl, string bodyContent) + { + HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json"); + var task = await httpClient.PostAsync(baseAddress + subUrl, httpContent); + + return task; + } + private async Task<HttpResponseMessage> SendPatchRequest(string subUrl, string bodyContent) + { + HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json"); + var task = await httpClient.PatchAsync(baseAddress + subUrl, httpContent); + + return task; + } + private async Task<HttpResponseMessage> SendDeleteRequest(string subUrl) + { + var task = await httpClient.DeleteAsync(baseAddress + subUrl); + + return task; + } + #endregion + + private async Task<string> ProcessResponse(HttpResponseMessage response) + { + var responseString = await response.Content.ReadAsStringAsync(); + + switch (response.StatusCode) + { + case (System.Net.HttpStatusCode)400: + var responseA = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString); + throw new BadRequestException(BadRequestException.message + " " + responseA.Message); + case (System.Net.HttpStatusCode)401: + throw new NotAuthorizedException(NotAuthorizedException.message); + case (System.Net.HttpStatusCode)404: + ServerErrorResponseContract responseB; + try + { + responseB = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString); + } + catch (JsonException) + { + throw new NotFoundException(NotFoundException.message); + } + throw new NotFoundException(NotFoundException.message + " " + responseB.Message); + case (System.Net.HttpStatusCode)500: + throw new InternalServerErrorException(InternalServerErrorException.message); + } + + return responseString; + } + } +} diff --git a/SDK/Connection/V3/Configuration.cs b/SDK/Connection/V3/Configuration.cs new file mode 100644 index 0000000..32bcea6 --- /dev/null +++ b/SDK/Connection/V3/Configuration.cs @@ -0,0 +1,52 @@ +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(3/3) + public partial class DevkitConnectorV3 + { + public async Task<ConfigurationContract> GetBranchConfiguration(string key) + { + string subUrl = Address.ConfigurationBranch + key; + + var task = await SendGetRequest(subUrl); + + var responseString = await ProcessResponse(task); + ConfigurationContract response = new ConfigurationContract + { + Value = responseString + }; + + return response; + } + public async Task<ConfigurationContract> GetAccountConfiguration(string key) + { + string subUrl = Address.ConfigurationAccount + key; + + var task = await SendGetRequest(subUrl); + + var responseString = await ProcessResponse(task); + ConfigurationContract response = new ConfigurationContract + { + Value = responseString + }; + + return response; + } + public async Task<long> GetConfigurationLastChange(string key) + { + string subUrl = Address.Configuration + key + "/last-change/"; + + var task = await SendGetRequest(subUrl); + + var responseString = await ProcessResponse(task); + + return Convert.ToInt64(responseString); + } + } +} diff --git a/SDK/Connection/V3/Devices.cs b/SDK/Connection/V3/Devices.cs new file mode 100644 index 0000000..6162c32 --- /dev/null +++ b/SDK/Connection/V3/Devices.cs @@ -0,0 +1,80 @@ +using SDK.Contracts.Communication; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(7/8) + public partial class DevkitConnectorV3 + { + public async Task<DeviceContract[]> GetDevices() + { + string subUrl = Address.Devices; + var response = await GetRequest<DeviceContract[]>(subUrl); + + return response; + } + public async Task<DeviceContract> AddDevice(DeviceContract deviceContract) + { + string subUrl = Address.Devices; + var response = await PostRequest<AddDeviceResponseContract>(subUrl, deviceContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return (DeviceContract)response; + } + public async Task<DeviceContract> GetDevice(int id) + { + string subUrl = Address.Devices + id; + var response = await GetRequest<DeviceContract>(subUrl); + + return response; + } + public async Task<PatchResponseContract> UpdateDevice(DeviceContract deviceContract) + { + if (deviceContract.Id == 0) + { + throw new BadRequestException(NotFoundException.message + " Device object has no Id."); + } + string subUrl = Address.Devices + deviceContract.Id; + var response = await PatchRequest(subUrl, deviceContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return response; + } + public async Task<DeviceContract> GetDevice(string login) + { + string subUrl = Address.DevicesLogin + login; + var response = await GetRequest<DeviceContract>(subUrl); + + return response; + } + public async Task<DynamicDeviceContract[]> GetDynamicDevices() + { + string subUrl = Address.DevicesDynamicLocations; + var response = await GetRequest<DynamicDeviceContract[]>(subUrl); + + return response; + } + public async Task<DynamicDeviceContract[]> GetDynamicDevicesShort() + { + string subUrl = Address.DevicesDynamicLocationsShort; + var response = await GetRequest<DynamicDeviceContract[]>(subUrl); + + return response; + } + public async Task<HttpResponseMessage> RegisterDevice() + { + return null; + } + } +} diff --git a/SDK/Connection/V3/Layers.cs b/SDK/Connection/V3/Layers.cs new file mode 100644 index 0000000..ff6dc3b --- /dev/null +++ b/SDK/Connection/V3/Layers.cs @@ -0,0 +1,29 @@ +using SDK.Contracts.Data; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(2/2) + public partial class DevkitConnectorV3 + { + public async Task<LayerContract[]> GetLayers() + { + string subUrl = Address.Layers; + var response = await GetRequest<LayerContract[]>(subUrl); + + return response; + } + public async Task<LayerContract> GetLayer(int id) + { + string subUrl = Address.Layers + id; + var response = await GetRequest<LayerContract>(subUrl); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Localization.cs b/SDK/Connection/V3/Localization.cs new file mode 100644 index 0000000..2d6fc91 --- /dev/null +++ b/SDK/Connection/V3/Localization.cs @@ -0,0 +1,62 @@ +using SDK.Communication; +using SDK.Contracts.Communication; +using SDK.Contracts.Data; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(2/2) + public partial class DevkitConnectorV3 + { + public async Task<PostResponseContract[]> AddLocalizationData(DeviceLocationContract[] deviceLocationContract) + { + string subUrl = Address.LocalizationAddDataBatch; + var response = await PostRequest<AddLocatizationDataResponseContract[]>(subUrl, deviceLocationContract); + + for (int i = 0; i < response.Length; i++) + { + if (response[i].Locations != null) + for (int j = 0; j < response[i].Locations.Count; j++) + { + var dataResult = response[i].Locations[j]; + if (dataResult.ErrorMessage != null) + { + throw new ServerResponseException(ServerResponseException.message + dataResult.ErrorMessage); + } + } + if (response[i].ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); + } + + return response; + } + public async Task<PostResponseContract[]> AddLocalizationData(LocationContract[] locationContract) + { + string subUrl = Address.LocalizationAddData; + var response = await PostRequest<AddLocatizationDataResponseContract[]>(subUrl, locationContract); + + for (int i = 0; i < response.Length; i++) + { + if (response[i].Locations != null) + for (int j = 0; j < response[i].Locations.Count; j++) + { + var dataResult = response[i].Locations[j]; + if (dataResult.ErrorMessage != null) + { + throw new ServerResponseException(ServerResponseException.message + dataResult.ErrorMessage); + } + } + if (response[i].ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); + } + + return response; + } + } +} diff --git a/SDK/Connection/V3/Sectors.cs b/SDK/Connection/V3/Sectors.cs new file mode 100644 index 0000000..0c948de --- /dev/null +++ b/SDK/Connection/V3/Sectors.cs @@ -0,0 +1,54 @@ +using SDK.Contracts.Communication; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(4/4) + public partial class DevkitConnectorV3 + { + public async Task<SectorContract[]> GetSectors() + { + string subUrl = Address.Sectors; + var response = await GetRequest<SectorContract[]>(subUrl); + + return response; + } + public async Task<SectorContract> AddSector(SectorContract sectorContract) + { + string subUrl = Address.Sectors; + var response = await PostRequest<AddSectorResponseContract>(subUrl, sectorContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return (SectorContract)response; + } + public async Task<SectorContract> GetSector(int id) + { + string subUrl = Address.Sectors + id; + var response = await GetRequest<SectorContract>(subUrl); + + return response; + } + public async Task<PatchResponseContract> UpdateSector(SectorContract sectorContract) + { + if (sectorContract.Id == 0) + { + throw new BadRequestException(NotFoundException.message + " Sector object has no Id."); + } + string subUrl = Address.Sectors + sectorContract.Id; + var response = await PatchRequest(subUrl, sectorContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Sensors.cs b/SDK/Connection/V3/Sensors.cs new file mode 100644 index 0000000..5bcf2a3 --- /dev/null +++ b/SDK/Connection/V3/Sensors.cs @@ -0,0 +1,114 @@ +using SDK.Communication; +using SDK.Contracts.Communication; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(7/8) + public partial class DevkitConnectorV3 + { + public async Task<SensorContract[]> GetSensors() + { + string subUrl = Address.Sensors; + var response = await GetRequest<SensorContract[]>(subUrl); + + return response; + } + public async Task<SensorContract> AddSensor(SensorContract sensorContract) + { + string subUrl = Address.Sensors; + var response = await PostRequest<AddSensorResponseContract>(subUrl, sensorContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return (SensorContract)response; + } + public async Task<SensorContract> GetSensor(int id) + { + string subUrl = Address.Sensors + id; + var response = await GetRequest<SensorContract>(subUrl); + + return response; + } + public async Task<PatchResponseContract> UpdateSensor(SensorContract sensorContract) + { + if (sensorContract.Id == 0) + { + throw new BadRequestException(NotFoundException.message + " Sensor object has no Id."); + } + string subUrl = Address.Sensors + sensorContract.Id; + var response = await PatchRequest(subUrl, sensorContract); + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return response; + } + public async void GetSensorAppFile() + { + //string subUrl = Address.SensorsAppFile; + //var response = await GetRequest<SensorAppInfoContract>(subUrl); + + //return response; + } + public async Task<SensorAppInfoContract> GetSensorAppInfo() + { + string subUrl = Address.SensorsAppInfo; + var response = await GetRequest<SensorAppInfoContract>(subUrl); + + return response; + } + public async Task<PostResponseContract[]> AddSensorData(SensorContract[] sensors) + { + string subUrl = Address.SensorsAddDataBatch; + var response = await PostRequest<AddSensorDataResponseContract[]>(subUrl, sensors); + + + for (int i = 0; i < response.Length; i++) + { + if (response[i].SensorData != null) + for (int j = 0; j < response[i].SensorData.Count; j++) + { + var sensorDataResult = response[i].SensorData[j]; + if (sensorDataResult.ErrorMessage != null) + { + throw new ServerResponseException(ServerResponseException.message + + " Quantity: " + sensorDataResult.Quantity + " - " + sensorDataResult.ErrorMessage); + } + } + if (response[i].ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); + } + + return response; + } + public async Task<PostResponseContract> AddSensorData(SensorDataContract[] sensorData) + { + string subUrl = Address.SensorsAddData; + var response = await PostRequest<AddSensorDataResponseContract>(subUrl, sensorData); + + if (response.SensorData != null) + for (int i = 0; i < response.SensorData.Count; i++) + { + var sensorDataResult = response.SensorData[i]; + if (sensorDataResult.ErrorMessage != null) + { + throw new ServerResponseException(ServerResponseException.message + + " Quantity: " + sensorDataResult.Quantity + " - " + sensorDataResult.ErrorMessage); + } + } + + if (response.ErrorMessage != null) + throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Users.cs b/SDK/Connection/V3/Users.cs new file mode 100644 index 0000000..086756c --- /dev/null +++ b/SDK/Connection/V3/Users.cs @@ -0,0 +1,22 @@ +using SDK.Contracts.Data; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(1/1) + public partial class DevkitConnectorV3 + { + public async Task<UserContract> GetUserInfo() + { + string subUrl = Address.Users; + var response = await GetRequest<UserContract>(subUrl); + + return response; + } + } +} diff --git a/SDK/Connection/V3/Utils.cs b/SDK/Connection/V3/Utils.cs new file mode 100644 index 0000000..81447c3 --- /dev/null +++ b/SDK/Connection/V3/Utils.cs @@ -0,0 +1,55 @@ +using SDK.Contracts.Data; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace tDevkit +{ + //(5/5) + public partial class DevkitConnectorV3 + { + public async Task<FileInfoContract[]> GetDemoFilesInfo() + { + string subUrl = Address.UtilsDemoFilesInfo; + var response = await GetRequest<FileInfoContract[]>(subUrl); + + return response; + } + public async Task<byte[]> GetFile(string fileName) + { + string subUrl = Address.UtilsFile + fileName; + + return await GetFile(fileName, subUrl); + } + public async Task<byte[]> GetDemoFile(string fileName) + { + string subUrl = Address.UtilsDemoFile + fileName; + //File.WriteAllBytes("C:\\Users\\mondr\\source\\repos\\twinzo-sdk\\bytes.jpg", bytes); + return await GetFile(fileName, subUrl); + } + private async Task<byte[]> GetFile(string fileName, string subUrl) + { + var task = await SendGetRequest(subUrl); + + string responseString = await ProcessResponse(task); + return Encoding.UTF8.GetBytes(responseString); + } + public async Task<string> GetUnityLastVersion(string platform) + { + string subUrl = Address.UtilsUnityLastVersion + platform; + var response = await GetRequest<UserContract>(subUrl); + + return null; + } + public async Task<FileInfoContract> GetUnityBundleInfo(string bundleName) + { + string subUrl = Address.UtilsUnityBundleInfo + bundleName; + var response = await GetRequest<FileInfoContract>(subUrl); + + return response; + } + } +} diff --git a/SDK/Contracts/Communication/AddDeviceResponseContract.cs b/SDK/Contracts/Communication/AddDeviceResponseContract.cs new file mode 100644 index 0000000..243434c --- /dev/null +++ b/SDK/Contracts/Communication/AddDeviceResponseContract.cs @@ -0,0 +1,93 @@ +using SDK.Communication; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SDK.Contracts.Communication +{ + class AddDeviceResponseContract : PostResponseContract + { + public int Id { get; set; } + + public string Mac { get; set; } + + public int BranchId { get; set; } + + public int SectorId { get; set; } + + public int ValidSectorId { get; set; } + + public string Title { get; set; } + + public string Note { get; set; } + + public long LastTimeOnline { get; set; } + + public long ValidLastTimeOnline { get; set; } + + public float X { get; set; } + + public float Y { get; set; } + + public float ValidX { get; set; } + + public float ValidY { get; set; } + + public string AppVersion { get; set; } + + public bool IsMoving { get; set; } + + public int FallStatus { get; set; } + + public float Battery { get; set; } + + public int DeviceTypeId { get; set; } + + public string DeviceType { get; set; } + + public bool Position { get; set; } + + public bool Geofence { get; set; } + + public float GeofenceRange { get; set; } + + public string Login { get; set; } + + //public string Password { get; set; } + + //public string Salt { get; set; } + + public static explicit operator DeviceContract(AddDeviceResponseContract addDeviceResponseContract) + { + return new DeviceContract + { + Id = addDeviceResponseContract.Id, + Mac = addDeviceResponseContract.Mac, + BranchId = addDeviceResponseContract.BranchId, + SectorId = addDeviceResponseContract.SectorId, + ValidSectorId = addDeviceResponseContract.ValidSectorId, + Title = addDeviceResponseContract.Title, + Note = addDeviceResponseContract.Note, + LastTimeOnline = addDeviceResponseContract.LastTimeOnline, + ValidLastTimeOnline = addDeviceResponseContract.ValidLastTimeOnline, + X = addDeviceResponseContract.X, + Y = addDeviceResponseContract.Y, + ValidX = addDeviceResponseContract.ValidX, + ValidY = addDeviceResponseContract.ValidY, + AppVersion = addDeviceResponseContract.AppVersion, + IsMoving = addDeviceResponseContract.IsMoving, + FallStatus = addDeviceResponseContract.FallStatus, + Battery = addDeviceResponseContract.Battery, + DeviceTypeId = addDeviceResponseContract.DeviceTypeId, + DeviceType = addDeviceResponseContract.DeviceType, + Position = addDeviceResponseContract.Position, + Geofence = addDeviceResponseContract.Geofence, + GeofenceRange = addDeviceResponseContract.GeofenceRange, + Login = addDeviceResponseContract.Login, + }; + } + } +} diff --git a/SDK/Contracts/Communication/AddSectorResponseContract.cs b/SDK/Contracts/Communication/AddSectorResponseContract.cs new file mode 100644 index 0000000..daa65e5 --- /dev/null +++ b/SDK/Contracts/Communication/AddSectorResponseContract.cs @@ -0,0 +1,62 @@ +using SDK.Communication; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SDK.Contracts.Communication +{ + class AddSectorResponseContract : PostResponseContract + { + public int Id { get; set; } + + public string Guid { get; set; } + + public int BranchId { get; set; } + + public string Title { get; set; } + + public double BarrierWidth { get; set; } + + public double BarrierHeight { get; set; } + + public double SectorWidth { get; set; } + + public double SectorHeight { get; set; } + + public long Modified { get; set; } + + public GpsItemContract[] GpsItems { get; set; } + + public AreaContract[] Areas { get; set; } + + public BarrierContract[] Barriers { get; set; } + + public BeaconContract[] Beacons { get; set; } + + public SensorContract[] Sensors { get; set; } + + public static explicit operator SectorContract(AddSectorResponseContract addSectorResponseContract) + { + return new SectorContract + { + Id = addSectorResponseContract.Id, + Guid = addSectorResponseContract.Guid, + BranchId = addSectorResponseContract.BranchId, + Title = addSectorResponseContract.Title, + BarrierHeight = addSectorResponseContract.BarrierHeight, + BarrierWidth = addSectorResponseContract.BarrierWidth, + SectorWidth = addSectorResponseContract.SectorWidth, + SectorHeight = addSectorResponseContract.SectorHeight, + Modified = addSectorResponseContract.Modified, + GpsItems = addSectorResponseContract.GpsItems, + Areas = addSectorResponseContract.Areas, + Barriers = addSectorResponseContract.Barriers, + Beacons = addSectorResponseContract.Beacons, + Sensors = addSectorResponseContract.Sensors + }; + } + } +} diff --git a/SDK/Contracts/Communication/AddSensorResponseContract.cs b/SDK/Contracts/Communication/AddSensorResponseContract.cs index 889049f..c17a51c 100644 --- a/SDK/Contracts/Communication/AddSensorResponseContract.cs +++ b/SDK/Contracts/Communication/AddSensorResponseContract.cs @@ -32,21 +32,21 @@ namespace SDK.Contracts.Communication public int AreaId { get; set; } - public static explicit operator SensorContract(AddSensorResponseContract addBeaconResponseContract) + public static explicit operator SensorContract(AddSensorResponseContract addSensorResponseContract) { return new SensorContract { - Id = addBeaconResponseContract.Id, - Login = addBeaconResponseContract.Login, - Password = addBeaconResponseContract.Password, - Salt = addBeaconResponseContract.Salt, - Title = addBeaconResponseContract.Title, - Mac = addBeaconResponseContract.Mac, - X = addBeaconResponseContract.X, - Y = addBeaconResponseContract.Y, - SectorId = addBeaconResponseContract.SectorId, - SensorData = addBeaconResponseContract.SensorData, - AreaId = addBeaconResponseContract.AreaId + Id = addSensorResponseContract.Id, + Login = addSensorResponseContract.Login, + //Password = addSensorResponseContract.Password, + //Salt = addSensorResponseContract.Salt, + Title = addSensorResponseContract.Title, + Mac = addSensorResponseContract.Mac, + X = addSensorResponseContract.X, + Y = addSensorResponseContract.Y, + SectorId = addSensorResponseContract.SectorId, + SensorData = addSensorResponseContract.SensorData, + AreaId = addSensorResponseContract.AreaId }; } } diff --git a/SDK/Contracts/Data/AccountContract.cs b/SDK/Contracts/Data/AccountContract.cs index 43f4604..2241534 100644 --- a/SDK/Contracts/Data/AccountContract.cs +++ b/SDK/Contracts/Data/AccountContract.cs @@ -10,8 +10,8 @@ namespace SDK.Contracts.Data { public string Login { get; set; } - public string Password { get; set; } + //public string Password { get; set; } - public string Salt { get; set; } + //public string Salt { get; set; } } } diff --git a/SDK/Contracts/Data/DeviceContract.cs b/SDK/Contracts/Data/DeviceContract.cs index cbc7907..803db1f 100644 --- a/SDK/Contracts/Data/DeviceContract.cs +++ b/SDK/Contracts/Data/DeviceContract.cs @@ -1,15 +1,58 @@ -using System; +using SDK.Contracts.Data; +using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; namespace SDK.Models { - public class DeviceContract + [DataContract] + public class DeviceContract : AccountContract { public int Id { get; set; } + public string Mac { get; set; } + + public int BranchId { get; set; } + + public int SectorId { get; set; } + + public int ValidSectorId { get; set; } + public string Title { get; set; } + + public string Note { get; set; } + + public long LastTimeOnline { get; set; } + + public long ValidLastTimeOnline { get; set; } + + public double X { get; set; } + + public double Y { get; set; } + + public float ValidX { get; set; } + + public float ValidY { get; set; } + + public string AppVersion { get; set; } + + public bool IsMoving { get; set; } + + public int FallStatus { get; set; } + + public float Battery { get; set; } + + public int DeviceTypeId { get; set; } + + public string DeviceType { get; set; } + + public bool Position { get; set; } + + public bool Geofence { get; set; } + + public float GeofenceRange { get; set; } } } diff --git a/SDK/DevkitConnectorV3.cs b/SDK/DevkitConnectorV3.cs deleted file mode 100644 index 13a95b7..0000000 --- a/SDK/DevkitConnectorV3.cs +++ /dev/null @@ -1,623 +0,0 @@ -using SDK; -using SDK.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using System.Text; -using System.Threading.Tasks; -using System.Net.Http.Headers; -using System.Text.Json; -using SDK.Communication; -using SDK.Exceptions; -using SDK.Contracts.Data; -using SDK.Contracts.Communication; -using System.IO; - -namespace tDevkit -{ - - public class DevkitConnectorV3 : DevkitConnector - { - private ConnectionOptions connectionOptions; - protected HttpClient httpClient; - private string baseAddress; - - public DevkitConnectorV3(ConnectionOptions connectionOptions) : this(connectionOptions, new HttpClient()) - { - } - public DevkitConnectorV3(ConnectionOptions connectionOptions, HttpClient httpClient) - { - this.connectionOptions = connectionOptions; - this.httpClient = httpClient; - baseAddress = connectionOptions.Url + "/" + connectionOptions.Version + "/"; - - resetHttpClientHeaders(); - } - - private void resetHttpClientHeaders() - { - httpClient.DefaultRequestHeaders.Clear(); - httpClient.DefaultRequestHeaders.Add("Client", connectionOptions.ClientGuid); - httpClient.DefaultRequestHeaders.Add("Branch", connectionOptions.BranchGuid); - httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token); - httpClient.DefaultRequestHeaders.Add("Api-Key", connectionOptions.ApiKey); - } - - #region REQUESTS - private async Task<Type> GetRequest<Type>(string subUrl) - { - var task = await SendGetRequest(subUrl); - - string responseString = await ProcessResponse(task); - return JsonSerializer.Deserialize<Type>(responseString); - } - private async Task<Type> PostRequest<Type>(string subUrl, object body) - { - string bodyContent = JsonSerializer.Serialize(body); - var task = await SendPostRequest(subUrl, bodyContent); - - string responseString = await ProcessResponse(task); - return JsonSerializer.Deserialize<Type>(responseString); - } - private async Task<PatchResponseContract> PatchRequest(string subUrl, object body) - { - string bodyContent = JsonSerializer.Serialize(body); - var task = await SendPatchRequest(subUrl, bodyContent); - - string responseString = await ProcessResponse(task); - return new PatchResponseContract(); - } - private async Task<HttpResponseMessage> DeleteRequest(string subUrl) - { - var task = await SendDeleteRequest(subUrl); - - string responseString = await ProcessResponse(task); - - if (responseString == "") - { - return new HttpResponseMessage(); - } - return JsonSerializer.Deserialize<HttpResponseMessage>(responseString); - } - #endregion - - #region SEND REQUESTS - private async Task<HttpResponseMessage> SendGetRequest(string subUrl) - { - var task = await httpClient.GetAsync(baseAddress + subUrl); - - return task; - } - private async Task<HttpResponseMessage> SendPostRequest(string subUrl, string bodyContent) - { - HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json"); - var task = await httpClient.PostAsync(baseAddress + subUrl, httpContent); - - return task; - } - private async Task<HttpResponseMessage> SendPatchRequest(string subUrl, string bodyContent) - { - HttpContent httpContent = new StringContent(bodyContent, Encoding.UTF8, "application/json"); - var task = await httpClient.PatchAsync(baseAddress + subUrl, httpContent); - - return task; - } - private async Task<HttpResponseMessage> SendDeleteRequest(string subUrl) - { - var task = await httpClient.DeleteAsync(baseAddress + subUrl); - - return task; - } - #endregion - - private async Task<string> ProcessResponse(HttpResponseMessage response) - { - var responseString = await response.Content.ReadAsStringAsync(); - - switch (response.StatusCode) - { - case (System.Net.HttpStatusCode)400: - var responseA = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString); - throw new BadRequestException(BadRequestException.message + " " + responseA.Message); - case (System.Net.HttpStatusCode)401: - throw new NotAuthorizedException(NotAuthorizedException.message); - case (System.Net.HttpStatusCode)404: - ServerErrorResponseContract responseB; - try { - responseB = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString); - } - catch (JsonException) - { - throw new NotFoundException(NotFoundException.message); - } - throw new NotFoundException(NotFoundException.message + " " + responseB.Message); - case (System.Net.HttpStatusCode)500: - throw new InternalServerErrorException(InternalServerErrorException.message); - } - - return responseString; - } - - #region AOS (0/13) - #endregion - - #region AREAS (2/2) - public async Task<AreaContract[]> GetAreas() - { - string subUrl = Address.Areas; - var response = await GetRequest<AreaContract[]>(subUrl); - - return response; - } - public async Task<AreaContract> GetArea(int id) - { - string subUrl = Address.Areas + id; - var response = await GetRequest<AreaContract>(subUrl); - - return response; - } - #endregion - - #region AUTHORIZATION (3/3) - public async Task<HttpResponseMessage> DeleteCurrentToken() - { - string subUrl = Address.AuthorizationToken; - - httpClient.DefaultRequestHeaders.Clear(); - httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token); - - var response = await DeleteRequest(subUrl); - - resetHttpClientHeaders(); - - return response; - } - public async Task<AuthenticationResponseContract> GetToken() - { - string subUrl = Address.AuthorizationToken; - - var response = await GetRequest<AuthenticationResponseContract>(subUrl); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - connectionOptions.Token = response.Token; - if (response.Client != null) - connectionOptions.ClientGuid = response.Client; - if (response.Branch != null) - connectionOptions.BranchGuid = response.Branch; - - resetHttpClientHeaders(); - - return response; - } - public async Task<AuthenticationResponseContract> Authenticate(bool superUser) - { - return await Authenticate(connectionOptions.Login, connectionOptions.Password, superUser); - } - public async Task<AuthenticationResponseContract> Authenticate(string login, string password, bool superUser) - { - string subUrl = Address.AuthorizationAuthenticate; - - CredentialContract credentialContract = new CredentialContract - { - Login = login, - Password = password - }; - - - if (!superUser) - credentialContract.Client = connectionOptions.Client; - - var response = await PostRequest<AuthenticationResponseContract>(subUrl, credentialContract); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - connectionOptions.Token = response.Token; - if (response.Client != null) - connectionOptions.ClientGuid = response.Client; - if (response.Branch != null) - connectionOptions.BranchGuid = response.Branch; - - resetHttpClientHeaders(); - - return response; - } - #endregion - - #region BEACONS (5/5) - - public async Task<BeaconContract[]> GetBeacons() - { - string subUrl = Address.Beacons; - var response = await GetRequest<BeaconContract[]>(subUrl); - - return response; - } - public async Task<BeaconContract> AddBeacon(BeaconContract beaconContract) - { - string subUrl = Address.Beacons; - var response = await PostRequest<AddBeaconResponseContract>(subUrl, beaconContract); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - return (BeaconContract)response; - } - public async Task<HttpResponseMessage> DeleteBeacon(int id) - { - string subUrl = Address.Beacons + id; - var response = await DeleteRequest(subUrl); - - return response; - } - public async Task<BeaconContract> GetBeacon(int id) - { - string subUrl = Address.Beacons + id; - var response = await GetRequest<BeaconContract>(subUrl); - - return response; - } - public async Task<PatchResponseContract> UpdateBeacon(BeaconContract beaconContract) - { - if (beaconContract.Id == 0) - { - throw new BadRequestException(NotFoundException.message + " Beacon object has no Id."); - } - string subUrl = Address.Beacons + beaconContract.Id; - var response = await PatchRequest(subUrl, beaconContract); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - return response; - } - #endregion - - #region BRANCHES (2/2) - public async Task<BranchContract[]> GetBranches(string queryString = "") - { - string subUrl = Address.Branches + queryString; - var response = await GetRequest<BranchContract[]>(subUrl); - - return response; - } - public async Task<BranchContract> GetBranch(int id) - { - string subUrl = Address.Branches + id; - var response = await GetRequest<BranchContract>(subUrl); - - return response; - } - #endregion - - #region CLIENTS (1/1) - public async Task<ClientContract[]> GetClients() - { - string subUrl = Address.Clients; - var response = await GetRequest<ClientContract[]>(subUrl); - - return response; - } - #endregion - - #region CONFIGURATION (3/3) - public async Task<ConfigurationContract> GetBranchConfiguration(string key) - { - string subUrl = Address.ConfigurationBranch + key; - - var task = await SendGetRequest(subUrl); - - var responseString = await ProcessResponse(task); - ConfigurationContract response = new ConfigurationContract - { - Value = responseString - }; - - return response; - } - public async Task<ConfigurationContract> GetAccountConfiguration(string key) - { - string subUrl = Address.ConfigurationAccount + key; - - var task = await SendGetRequest(subUrl); - - var responseString = await ProcessResponse(task); - ConfigurationContract response = new ConfigurationContract - { - Value = responseString - }; - - return response; - } - public async Task<long> GetConfigurationLastChange(string key) - { - string subUrl = Address.Configuration + key + "/last-change/"; - - var task = await SendGetRequest(subUrl); - - var responseString = await ProcessResponse(task); - - return Convert.ToInt64(responseString); - } - #endregion - - #region DEVICES (5/6) - public async Task<DeviceContract[]> GetDevices() - { - string subUrl = Address.Devices; - var response = await GetRequest<DeviceContract[]>(subUrl); - - return response; - } - public async Task<DeviceContract> GetDevice(int id) - { - string subUrl = Address.Devices + id; - var response = await GetRequest<DeviceContract>(subUrl); - - return response; - } - public async Task<DeviceContract> GetDevice(string login) - { - string subUrl = Address.DevicesLogin + login; - var response = await GetRequest<DeviceContract>(subUrl); - - return response; - } - public async Task<DynamicDeviceContract[]> GetDynamicDevices() - { - string subUrl = Address.DevicesDynamicLocations; - var response = await GetRequest<DynamicDeviceContract[]>(subUrl); - - return response; - } - public async Task<DynamicDeviceContract[]> GetDynamicDevicesShort() - { - string subUrl = Address.DevicesDynamicLocationsShort; - var response = await GetRequest<DynamicDeviceContract[]>(subUrl); - - return response; - } - public async Task<HttpResponseMessage> AddDevice() - { - return null; - } - #endregion - - #region LAYERS (2/2) - public async Task<LayerContract[]> GetLayers() - { - string subUrl = Address.Layers; - var response = await GetRequest<LayerContract[]>(subUrl); - - return response; - } - public async Task<LayerContract> GetLayer(int id) - { - string subUrl = Address.Layers + id; - var response = await GetRequest<LayerContract>(subUrl); - - return response; - } - #endregion - - #region LOCALIZATION (2/2) - public async Task<PostResponseContract[]> AddLocalizationData(DeviceLocationContract[] deviceLocationContract) - { - string subUrl = Address.LocalizationAddDataBatch; - var response = await PostRequest<AddLocatizationDataResponseContract[]>(subUrl, deviceLocationContract); - - for (int i = 0; i < response.Length; i++) - { - if (response[i].Locations != null) - for (int j = 0; j < response[i].Locations.Count; j++) - { - var dataResult = response[i].Locations[j]; - if (dataResult.ErrorMessage != null) - { - throw new ServerResponseException(ServerResponseException.message + dataResult.ErrorMessage); - } - } - if (response[i].ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); - } - - return response; - } - public async Task<PostResponseContract[]> AddLocalizationData(LocationContract[] locationContract) - { - string subUrl = Address.LocalizationAddData; - var response = await PostRequest<AddLocatizationDataResponseContract[]>(subUrl, locationContract); - - for (int i = 0; i < response.Length; i++) - { - if (response[i].Locations != null) - for (int j = 0; j < response[i].Locations.Count; j++) - { - var dataResult = response[i].Locations[j]; - if (dataResult.ErrorMessage != null) - { - throw new ServerResponseException(ServerResponseException.message + dataResult.ErrorMessage); - } - } - if (response[i].ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); - } - - return response; - } - #endregion - - #region SECTORS (2/2) - public async Task<SectorContract[]> GetSectors() - { - string subUrl = Address.Sectors; - var response = await GetRequest<SectorContract[]>(subUrl); - - return response; - } - public async Task<SectorContract> GetSector(int id) - { - string subUrl = Address.Sectors + id; - var response = await GetRequest<SectorContract>(subUrl); - - return response; - } - #endregion - - #region SENSORS (7/8) - public async Task<SensorContract[]> GetSensors() - { - string subUrl = Address.Sensors; - var response = await GetRequest<SensorContract[]>(subUrl); - - return response; - } - public async Task<SensorContract> AddSensor(SensorContract sensorContract) - { - string subUrl = Address.Sensors; - var response = await PostRequest<AddSensorResponseContract>(subUrl, sensorContract); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - return (SensorContract)response; - } - public async Task<SensorContract> GetSensor(int id) - { - string subUrl = Address.Sensors + id; - var response = await GetRequest<SensorContract>(subUrl); - - return response; - } - public async Task<PatchResponseContract> UpdateSensor(SensorContract sensorContract) - { - if (sensorContract.Id == 0) - { - throw new BadRequestException(NotFoundException.message + " Sensor object has no Id."); - } - string subUrl = Address.Sensors + sensorContract.Id; - var response = await PatchRequest(subUrl, sensorContract); - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - return response; - } - public async void GetSensorAppFile() - { - //string subUrl = Address.SensorsAppFile; - //var response = await GetRequest<SensorAppInfoContract>(subUrl); - - //return response; - } - public async Task<SensorAppInfoContract> GetSensorAppInfo() - { - string subUrl = Address.SensorsAppInfo; - var response = await GetRequest<SensorAppInfoContract>(subUrl); - - return response; - } - public async Task<PostResponseContract[]> AddSensorData(SensorContract[] sensors) - { - string subUrl = Address.SensorsAddDataBatch; - var response = await PostRequest<AddSensorDataResponseContract[]>(subUrl, sensors); - - - for (int i = 0; i < response.Length; i++) - { - if (response[i].SensorData != null) - for (int j = 0; j < response[i].SensorData.Count; j++) - { - var sensorDataResult = response[i].SensorData[j]; - if (sensorDataResult.ErrorMessage != null) - { - throw new ServerResponseException(ServerResponseException.message + - " Quantity: " + sensorDataResult.Quantity + " - " + sensorDataResult.ErrorMessage); - } - } - if (response[i].ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response[i].ErrorMessage); - } - - return response; - } - public async Task<PostResponseContract> AddSensorData(SensorDataContract[] sensorData) - { - string subUrl = Address.SensorsAddData; - var response = await PostRequest<AddSensorDataResponseContract>(subUrl, sensorData); - - if (response.SensorData != null) - for (int i = 0; i < response.SensorData.Count; i++) - { - var sensorDataResult = response.SensorData[i]; - if (sensorDataResult.ErrorMessage != null) - { - throw new ServerResponseException(ServerResponseException.message + - " Quantity: " + sensorDataResult.Quantity + " - " + sensorDataResult.ErrorMessage); - } - } - - if (response.ErrorMessage != null) - throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage); - - return response; - } - #endregion - - #region USERS (1/1) - public async Task<UserContract> GetUserInfo() - { - string subUrl = Address.Users; - var response = await GetRequest<UserContract>(subUrl); - - return response; - } - #endregion - - #region UTILS (5/5) - public async Task<FileInfoContract[]> GetDemoFilesInfo() - { - string subUrl = Address.UtilsDemoFilesInfo; - var response = await GetRequest<FileInfoContract[]>(subUrl); - - return response; - } - public async Task<byte[]> GetFile(string fileName) - { - string subUrl = Address.UtilsFile + fileName; - - return await GetFile(fileName, subUrl); - } - public async Task<byte[]> GetDemoFile(string fileName) - { - string subUrl = Address.UtilsDemoFile + fileName; - //File.WriteAllBytes("C:\\Users\\mondr\\source\\repos\\twinzo-sdk\\bytes.jpg", bytes); - return await GetFile(fileName, subUrl); - } - private async Task<byte[]> GetFile(string fileName, string subUrl) - { - var task = await SendGetRequest(subUrl); - - string responseString = await ProcessResponse(task); - return Encoding.UTF8.GetBytes(responseString); - } - public async Task<string> GetUnityLastVersion(string platform) - { - string subUrl = Address.UtilsUnityLastVersion + platform; - var response = await GetRequest<UserContract>(subUrl); - - return null; - } - public async Task<FileInfoContract> GetUnityBundleInfo(string bundleName) - { - string subUrl = Address.UtilsUnityBundleInfo + bundleName; - var response = await GetRequest<FileInfoContract>(subUrl); - - return response; - } - #endregion - } -} diff --git a/SDK/Address.cs b/SDK/Helpers/Address.cs similarity index 100% rename from SDK/Address.cs rename to SDK/Helpers/Address.cs diff --git a/SDK/SDK.csproj b/SDK/SDK.csproj index f208d30..9b39370 100644 --- a/SDK/SDK.csproj +++ b/SDK/SDK.csproj @@ -4,4 +4,8 @@ <TargetFramework>net5.0</TargetFramework> </PropertyGroup> + <ItemGroup> + <Folder Include="Managers\" /> + </ItemGroup> + </Project> diff --git a/Test/Test/Helper.cs b/Test/Test/Helper.cs new file mode 100644 index 0000000..2333f06 --- /dev/null +++ b/Test/Test/Helper.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using tDevkit; + +namespace Test +{ + class Helper + { + public static DevkitConnectorV3 GetConnector() + { + Console.WriteLine("Hello World!"); + ConnectionOptionsBuilder optionsBuilder = new ConnectionOptionsBuilder(); + ConnectionOptions connectionOptions = optionsBuilder + .Url("http://twin.develop.rtls.solutions:8080/api/") + //.Token("K169z9mIQ2Ln3UG34jOobsytFGGqYRwzCundpeM67G4=") + .Client("Infotech") + .ClientGuid("00000000-0000-0000-0000-000000000001") + .BranchGuid("00000000-0000-0000-0000-000000000003") + .Timeout(1000) + .ApiKey("X1fprPtlkvolW1Bl47UQV4SoW8siY3n8QDQkDsGJ") + .Version(ConnectionOptions.VERSION_3) + .Login("ondrejicka") + .Password("zJz1sJRUMNr4b4M") + .Build(); + //FeF/lTE9ZBPYMvKkQZnRMRK7loOXdfeD1qOcVMr00uQ= + //Console.WriteLine("Url: " + connectionOptions.Url); + Console.WriteLine("Token: " + connectionOptions.Token); + Console.WriteLine("Client: " + connectionOptions.Client); + Console.WriteLine("ClientGuid: " + connectionOptions.ClientGuid); + Console.WriteLine("BranchGuid: " + connectionOptions.BranchGuid); + Console.WriteLine("Timeout: " + connectionOptions.Timeout); + Console.WriteLine("ApiKey: " + connectionOptions.ApiKey); + Console.WriteLine("Version: " + connectionOptions.Version); + Console.WriteLine("Login: " + connectionOptions.Login); + Console.WriteLine("Password: " + connectionOptions.Password); + + return (DevkitConnectorV3)DevkitFactory.CreateDevkitConnector(connectionOptions); + } + } +} diff --git a/Test/Test/Test.csproj b/Test/Test/Test.csproj new file mode 100644 index 0000000..bc16512 --- /dev/null +++ b/Test/Test/Test.csproj @@ -0,0 +1,20 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net5.0</TargetFramework> + + <IsPackable>false</IsPackable> + </PropertyGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" /> + <PackageReference Include="MSTest.TestAdapter" Version="2.1.1" /> + <PackageReference Include="MSTest.TestFramework" Version="2.1.1" /> + <PackageReference Include="coverlet.collector" Version="1.3.0" /> + </ItemGroup> + + <ItemGroup> + <ProjectReference Include="..\..\SDK\SDK.csproj" /> + </ItemGroup> + +</Project> diff --git a/Test/Test/TestData.cs b/Test/Test/TestData.cs new file mode 100644 index 0000000..fe12b4f --- /dev/null +++ b/Test/Test/TestData.cs @@ -0,0 +1,245 @@ +using SDK.Contracts.Data; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Main +{ + public static class TestData + { + public static BeaconContract GetBeacon() + { + return new BeaconContract + { + SectorId = 1, + BranchId = 1, + Mac = "aa:aa:aa:aa:aa:aa", + X = 0, + Y = 0, + Z = 0, + Title = "sdk-beacon", + Active = true, + TypeId = 23, + Position = true, + Geofence = true, + GeofenceRange = 20, + Cluster = "c1", + UseGps = false, + }; + } + public static BeaconContract GetBeaconPatch() + { + return new BeaconContract + { + Id = 148, + SectorId = 1, + BranchId = 1, + Mac = "aa:aa:aa:aa:aa:ac", + X = 0, + Y = 0, + Z = 0, + Title = "sdk-test", + Active = true, + TypeId = 24, + Position = true, + Geofence = true, + GeofenceRange = 20, + Cluster = "c1", + UseGps = false, + }; + } + + public static DeviceContract GetDevice() + { + DeviceContract device = new DeviceContract + { + Mac = "00:00:00:00:00:00", + BranchId = 1, + SectorId = 1, + Login = "sdk-device", + Title = "sdk-device", + X = 10.0, + Y = 10.0, + //AppVersion = "1.0", + IsMoving = false, + //FallStatus = 0, + //Battery = 46f, + DeviceTypeId = 8, + Position = false, + Geofence = false + }; + return device; + } + + public static DeviceLocationContract[] GetLocalizationDataBatch() + { + DistanceContract[] distanceContract1 = new DistanceContract[] + { + new DistanceContract {BeaconId = 34, RSSI= -56}, + new DistanceContract {BeaconId = 34, RSSI= -56}, + new DistanceContract {BeaconId = 34, RSSI= -56}, + }; + + LocationContract[] locationContract = new LocationContract[] + { + new LocationContract {SectorId = 1, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 2, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 3, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 4, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + }; + + return new DeviceLocationContract[] { new DeviceLocationContract { Login = "sdk-device", Locations = locationContract } }; + } + public static LocationContract[] GetLocalizationData() + { + DistanceContract[] distanceContract1 = new DistanceContract[] + { + new DistanceContract {BeaconId = 34, RSSI= -56}, + new DistanceContract {BeaconId = 34, RSSI= -56}, + new DistanceContract {BeaconId = 34, RSSI= -56}, + }; + + return new LocationContract[] + { + new LocationContract {SectorId = 1, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 2, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 3, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + new LocationContract {SectorId = 4, Battery = 100, IsMoving = true, Timestamp = 1599644652178, + X = 0, Y = 0, Z = 0, Interval = 300, Distances = distanceContract1 }, + }; + } + + public static SectorContract GetSector() + { + return new() + { + BranchId = 1, + Title = "sector-sdk", + BarrierWidth = 10, + BarrierHeight = 10, + SectorWidth = 10000, + SectorHeight = 10000 + }; + } + + public static SensorContract GetSensor() + { + SensorDataContract data1 = new SensorDataContract + { + Quantity = "Temperature", + Value = "16", + Unit = "°C", + DataType = "Single" + }; + SensorDataContract data2 = new SensorDataContract + { + Quantity = "Humidity", + Value = "31", + Unit = "%", + DataType = "Single" + }; + SensorDataContract[] sensorDataContracts = new SensorDataContract[] { data1, data2 }; + + SensorContract sensor = new SensorContract + { + Id = 7351, + Login = "sdk-sensor", + //Password = "sdk", + Title = "sdk-sensor", + SectorId = 2, + SensorData = sensorDataContracts, + AreaId = 19 + }; + + return sensor; + } + public static SensorContract GetSensorUpdate() + { + SensorContract sensor = new SensorContract + { + Id = 7351, + Login = "sdk-sensor2", + Title = "tests", + SectorId = 2, + AreaId = 19 + }; + + return sensor; + } + public static SensorContract[] GetSensorDataBatch() + { + SensorDataContract data1 = new SensorDataContract + { + Quantity = "Temperature", + Value = "16", + Unit = "°C", + DataType = "Single" + }; + SensorDataContract data2 = new SensorDataContract + { + Quantity = "Humidity", + Value = "31", + Unit = "%", + DataType = "Single" + }; + SensorDataContract data3 = new SensorDataContract + { + Quantity = "CO2", + Timestamp = 1614599484673, + Value = "800", + DataType = "Int32", + Unit = "unit" + }; + SensorDataContract[] sensorDataContracts = new SensorDataContract[] { data1, data2, data3 }; + + SensorContract sensor = new SensorContract + { + Login = "sdk-sensor", + SectorId = 2, + SensorData = sensorDataContracts, + AreaId = 24 + }; + SensorContract[] sensorContracts = new SensorContract[] { sensor }; + + return sensorContracts; + } + public static SensorDataContract[] GetSensorData() + { + SensorDataContract data1 = new SensorDataContract + { + Quantity = "Temperature", + Value = "16", + Unit = "°C", + DataType = "Single" + }; + SensorDataContract data2 = new SensorDataContract + { + Quantity = "Humidity", + Value = "31", + Unit = "%", + DataType = "Single" + }; + SensorDataContract data3 = new SensorDataContract + { + Quantity = "CO2", + Timestamp = 1614599484673, + Value = "800", + DataType = "Int32", + Unit = "unit" + }; + SensorDataContract[] sensorDataContracts = new SensorDataContract[] { data1, data2, data3 }; + + return sensorDataContracts; + } + } +} diff --git a/Test/Test/V3/Areas.cs b/Test/Test/V3/Areas.cs new file mode 100644 index 0000000..6aa00ae --- /dev/null +++ b/Test/Test/V3/Areas.cs @@ -0,0 +1,39 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Communication; +using SDK.Exceptions; +using SDK.Models; +using System.Collections.Generic; +using System.Threading.Tasks; +using tDevkit; + +namespace Test +{ + //(2/2) + public partial class TestClass + { + [TestMethod] + public async Task Areas1() + { + await A_Authenticate(); + AreaContract[] area = await devkitConnector.GetAreas(); + Assert.IsNotNull(area[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Areas2() + { + await A_Authenticate(); + AreaContract area1 = await devkitConnector.GetArea(1); + AreaContract area2 = null; + try + { + area2 = await devkitConnector.GetArea(3); + } + catch (NotFoundException) {} + Assert.IsNotNull(area1); + Assert.IsNull(area2); + await A_DeleteToken(); + } + } +} diff --git a/Test/Test/V3/Authorization.cs b/Test/Test/V3/Authorization.cs new file mode 100644 index 0000000..fdfac57 --- /dev/null +++ b/Test/Test/V3/Authorization.cs @@ -0,0 +1,27 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Communication; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + public partial class TestClass + { + //[TestMethod] + public async Task A_Authenticate() + { + AuthenticationResponseContract auth = await devkitConnector.Authenticate(true); + //Assert.IsNotNull(auth.Token); + } + + //[TestMethod] + public async Task A_DeleteToken() + { + HttpResponseMessage auth = await devkitConnector.DeleteCurrentToken(); + } + } +} diff --git a/Test/Test/V3/Base.cs b/Test/Test/V3/Base.cs new file mode 100644 index 0000000..6e4a7c0 --- /dev/null +++ b/Test/Test/V3/Base.cs @@ -0,0 +1,16 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using tDevkit; + +namespace Test +{ + [TestClass] + public partial class TestClass + { + private DevkitConnectorV3 devkitConnector = Helper.GetConnector(); + } +} diff --git a/Test/Test/V3/Beacons.cs b/Test/Test/V3/Beacons.cs new file mode 100644 index 0000000..b137704 --- /dev/null +++ b/Test/Test/V3/Beacons.cs @@ -0,0 +1,92 @@ +using Main; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(5/5) + public partial class TestClass + { + [TestMethod] + public async Task Beacons1() + { + await A_Authenticate(); + BeaconContract[] beacon = await devkitConnector.GetBeacons(); + Assert.IsNotNull(beacon[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Beacons2() + { + await A_Authenticate(); + BeaconContract beacon1 = await devkitConnector.GetBeacon(34); + BeaconContract beacon2 = null; + try + { + beacon2 = await devkitConnector.GetBeacon(1); + } + catch (NotFoundException) { } + Assert.IsNotNull(beacon1); + Assert.IsNull(beacon2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Beacons3() + { + await A_Authenticate(); + BeaconContract beacon = await devkitConnector.AddBeacon(TestData.GetBeacon()); + BeaconContract beacon2 = null; + try + { + beacon2 = await devkitConnector.AddBeacon(TestData.GetBeacon()); + } + catch (BadRequestException) + { + Assert.IsNull(null); + } + Assert.IsNotNull(beacon); + Assert.IsNull(beacon2); + await devkitConnector.DeleteBeacon(beacon.Id); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Beacons4() + { + await A_Authenticate(); + + BeaconContract beaconData = TestData.GetBeacon(); + + BeaconContract beacon = await devkitConnector.AddBeacon(beaconData); + beacon.X = 10; + beacon.Position = false; + beacon.Geofence = false; + try + { + var message = await devkitConnector.UpdateBeacon(beacon); + var f = 0; + } + catch (BadRequestException) + { + Assert.IsNotNull(null); + } + Assert.IsNotNull(beacon); + await devkitConnector.DeleteBeacon(beacon.Id); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Beacons5() + { + //Delete je otestovany v add a update + } + } +} diff --git a/Test/Test/V3/Branches.cs b/Test/Test/V3/Branches.cs new file mode 100644 index 0000000..312ddb4 --- /dev/null +++ b/Test/Test/V3/Branches.cs @@ -0,0 +1,40 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(2/2) + public partial class TestClass + { + [TestMethod] + public async Task Branches1() + { + await A_Authenticate(); + BranchContract[] branch = await devkitConnector.GetBranches(); + Assert.IsNotNull(branch[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Branches2() + { + await A_Authenticate(); + BranchContract branch1 = await devkitConnector.GetBranch(1); + BranchContract branch2 = null; + try + { + branch2 = await devkitConnector.GetBranch(3); + } + catch (NotFoundException) { } + Assert.IsNotNull(branch1); + Assert.IsNull(branch2); + await A_DeleteToken(); + } + } +} diff --git a/Test/Test/V3/Clients.cs b/Test/Test/V3/Clients.cs new file mode 100644 index 0000000..dc9c810 --- /dev/null +++ b/Test/Test/V3/Clients.cs @@ -0,0 +1,23 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(1/1) + public partial class TestClass + { + [TestMethod] + public async Task Clients1() + { + await A_Authenticate(); + ClientContract[] client = await devkitConnector.GetClients(); + Assert.IsNotNull(client[0]); + await A_DeleteToken(); + } + } +} diff --git a/Test/Test/V3/Configuration.cs b/Test/Test/V3/Configuration.cs new file mode 100644 index 0000000..8e23a11 --- /dev/null +++ b/Test/Test/V3/Configuration.cs @@ -0,0 +1,31 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(0/3) + public partial class TestClass + { + [TestMethod] + public async Task Configuration1() + { + + } + + [TestMethod] + public async Task Configuration2() + { + + } + + [TestMethod] + public async Task Configuration3() + { + + } + } +} diff --git a/Test/Test/V3/Devices.cs b/Test/Test/V3/Devices.cs new file mode 100644 index 0000000..4189bd5 --- /dev/null +++ b/Test/Test/V3/Devices.cs @@ -0,0 +1,114 @@ +using Main; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(7/8) + public partial class TestClass + { + [TestMethod] + public async Task Devices1() + { + await A_Authenticate(); + DeviceContract[] device = await devkitConnector.GetDevices(); + Assert.IsNotNull(device[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices2() + { + await A_Authenticate(); + DeviceContract device1 = await devkitConnector.GetDevice(1); + DeviceContract device2 = null; + try + { + device2 = await devkitConnector.GetDevice(3); + } + catch (NotFoundException) { } + Assert.IsNotNull(device1); + Assert.IsNull(device2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices3() + { + await A_Authenticate(); + DeviceContract device1 = await devkitConnector.GetDevice("sdk-device"); + DeviceContract device2 = null; + try + { + device2 = await devkitConnector.GetDevice("sdk-device2"); + } + catch (NotFoundException) { } + Assert.IsNotNull(device1); + Assert.IsNull(device2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices4() + { + await A_Authenticate(); + DynamicDeviceContract[] device = await devkitConnector.GetDynamicDevices(); + Assert.IsNotNull(device[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices5() + { + await A_Authenticate(); + DynamicDeviceContract[] device = await devkitConnector.GetDynamicDevicesShort(); + Assert.IsNotNull(device[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices6() + { + await A_Authenticate(); + DeviceContract device = await devkitConnector.AddDevice(TestData.GetDevice()); + Assert.IsNotNull(device); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices7() + { + await A_Authenticate(); + + DeviceContract deviceData = TestData.GetDevice(); + + DeviceContract device = await devkitConnector.AddDevice(deviceData); + device.X = 20; + device.Position = true; + device.Note = "aaa"; + try + { + var message = await devkitConnector.UpdateDevice(device); + var f = 0; + } + catch (BadRequestException b) + { + Assert.IsNotNull(null); + } + Assert.IsNotNull(device); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Devices8() + { + + } + } +} diff --git a/Test/Test/V3/Layers.cs b/Test/Test/V3/Layers.cs new file mode 100644 index 0000000..25375ee --- /dev/null +++ b/Test/Test/V3/Layers.cs @@ -0,0 +1,40 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Contracts.Data; +using SDK.Exceptions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(2/2) + public partial class TestClass + { + [TestMethod] + public async Task Layers1() + { + await A_Authenticate(); + LayerContract[] layer = await devkitConnector.GetLayers(); + Assert.IsNotNull(layer[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Layers2() + { + await A_Authenticate(); + LayerContract layer1 = await devkitConnector.GetLayer(1); + LayerContract layer2 = null; + try + { + layer2 = await devkitConnector.GetLayer(14); + } + catch (NotFoundException) { } + Assert.IsNotNull(layer1); + Assert.IsNull(layer2); + await A_DeleteToken(); + } + } +} diff --git a/Test/Test/V3/Localization.cs b/Test/Test/V3/Localization.cs new file mode 100644 index 0000000..c321500 --- /dev/null +++ b/Test/Test/V3/Localization.cs @@ -0,0 +1,25 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(0/2) + public partial class TestClass + { + [TestMethod] + public async Task Localization1() + { + + } + + [TestMethod] + public async Task Localization2() + { + + } + } +} diff --git a/Test/Test/V3/Sectors.cs b/Test/Test/V3/Sectors.cs new file mode 100644 index 0000000..9115901 --- /dev/null +++ b/Test/Test/V3/Sectors.cs @@ -0,0 +1,73 @@ +using Main; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(4/4) + public partial class TestClass + { + [TestMethod] + public async Task Sectors1() + { + await A_Authenticate(); + SectorContract[] sector = await devkitConnector.GetSectors(); + Assert.IsNotNull(sector[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sectors2() + { + await A_Authenticate(); + SectorContract sector1 = await devkitConnector.GetSector(1); + SectorContract sector2 = null; + try + { + sector2 = await devkitConnector.GetSector(5); + } + catch (NotFoundException) { } + Assert.IsNotNull(sector1); + Assert.IsNull(sector2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sectors3() + { + await A_Authenticate(); + SectorContract sector = await devkitConnector.AddSector(TestData.GetSector()); + Assert.IsNotNull(sector); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sectors4() + { + await A_Authenticate(); + + SectorContract sectorData = TestData.GetSector(); + + SectorContract sector = await devkitConnector.AddSector(sectorData); + sector.Title = "aaa"; + sector.BarrierWidth = 20; + try + { + var message = await devkitConnector.UpdateSector(sector); + var f = 0; + } + catch (BadRequestException b) + { + Assert.IsNotNull(null); + } + Assert.IsNotNull(sector); + await A_DeleteToken(); + } + } +} diff --git a/Test/Test/V3/Sensors.cs b/Test/Test/V3/Sensors.cs new file mode 100644 index 0000000..10f0597 --- /dev/null +++ b/Test/Test/V3/Sensors.cs @@ -0,0 +1,108 @@ +using Main; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using SDK.Exceptions; +using SDK.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(4/8) + public partial class TestClass + { + [TestMethod] + public async Task Sensors1() + { + await A_Authenticate(); + SensorContract[] sensor = await devkitConnector.GetSensors(); + Assert.IsNotNull(sensor[0]); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sensors2() + { + await A_Authenticate(); + SensorContract sensor1 = await devkitConnector.GetSensor(37); + SensorContract sensor2 = null; + try + { + sensor2 = await devkitConnector.GetSensor(1); + } + catch (NotFoundException) { } + Assert.IsNotNull(sensor1); + Assert.IsNull(sensor2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sensors3() + { + await A_Authenticate(); + SensorContract sensor = await devkitConnector.AddSensor(TestData.GetSensor()); + SensorContract sensor2 = null; + try + { + sensor2 = await devkitConnector.AddSensor(TestData.GetSensor()); + } + catch (BadRequestException) + { + Assert.IsNull(null); + } + Assert.IsNotNull(sensor); + Assert.IsNull(sensor2); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sensors4() + { + await A_Authenticate(); + + SensorContract sensorData = TestData.GetSensor(); + + SensorContract sensor = await devkitConnector.AddSensor(sensorData); + sensor.X = 10; + sensor.Title = "aaaaa sdk"; + sensor.SensorData = null; + try + { + var message = await devkitConnector.UpdateSensor(sensor); + var f = 0; + } + catch (BadRequestException b) + { + Assert.IsNotNull(null); + } + Assert.IsNotNull(sensor); + await A_DeleteToken(); + } + + [TestMethod] + public async Task Sensors5() + { + + } + + [TestMethod] + public async Task Sensors6() + { + + } + + [TestMethod] + public async Task Sensors7() + { + + } + + [TestMethod] + public async Task Sensors8() + { + + } + } +} diff --git a/Test/Test/V3/Users.cs b/Test/Test/V3/Users.cs new file mode 100644 index 0000000..51b77f2 --- /dev/null +++ b/Test/Test/V3/Users.cs @@ -0,0 +1,19 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(0/1) + public partial class TestClass + { + [TestMethod] + public async Task Users1() + { + + } + } +} diff --git a/Test/Test/V3/Utils.cs b/Test/Test/V3/Utils.cs new file mode 100644 index 0000000..475cca8 --- /dev/null +++ b/Test/Test/V3/Utils.cs @@ -0,0 +1,43 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Test +{ + //(0/5) + public partial class TestClass + { + [TestMethod] + public async Task Utils1() + { + + } + + [TestMethod] + public async Task Utils2() + { + + } + + [TestMethod] + public async Task Utils3() + { + + } + + [TestMethod] + public async Task Utils4() + { + + } + + [TestMethod] + public async Task Utils5() + { + + } + } +} -- GitLab