Skip to content
Snippets Groups Projects
Commit 365412f9 authored by Michal Ondrejička's avatar Michal Ondrejička
Browse files

Merge branch 'develop' into 'master'

Develop

See merge request digital-twin/twinzo/tdevkit!5
parents dc795810 b387746d
No related branches found
No related tags found
No related merge requests found
......@@ -34,10 +34,10 @@ namespace Main
{
return new BeaconContract
{
Id = 139,
Id = 148,
SectorId = 1,
BranchId = 1,
Mac = "aa:aa:aa:aa:aa:aa",
Mac = "aa:aa:aa:aa:aa:ac",
X = 0,
Y = 0,
Z = 0,
......@@ -97,6 +97,50 @@ namespace Main
};
}
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-sensor2",
Password = "sdk",
Title = "testtt",
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
......
......@@ -43,5 +43,11 @@ namespace SDK.Models
public const string SensorsAddData = "sensors/sensor-data/";
public const string Users = "users/detail";
public const string UtilsDemoFilesInfo = "utils/demo-files-info/";
public const string UtilsFile = "utils/file/";
public const string UtilsDemoFile = "utils/demo-file/";
public const string UtilsUnityLastVersion = "utils/unity-last-version/";
public const string UtilsUnityBundleInfo = "utils/unity-bundle-info/";
}
}
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 AddSensorResponseContract : PostResponseContract
{
public int Id { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
public string Title { get; set; }
public string Mac { get; set; }
public double X { get; set; }
public double Y { get; set; }
public int SectorId { get; set; }
public SensorDataContract[] SensorData { get; set; }
public int AreaId { get; set; }
public static explicit operator SensorContract(AddSensorResponseContract addBeaconResponseContract)
{
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
};
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SDK.Contracts.Communication
{
public class PatchResponseContract
{
public bool Success { get; set; } = true;
public string ErrorMessage { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SDK.Contracts.Data
{
public class AccountContract
{
public string Login { get; set; }
public string Password { get; set; }
public string Salt { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace SDK.Contracts.Data
{
[DataContract]
public class FileInfoContract
{
[DataMember(Order = 1)]
public string Version { get; set; }
[DataMember(Order = 2)]
public long Size { get; set; }
[DataMember(Order = 3)]
public string Name { get; set; }
}
}
using System;
using SDK.Contracts.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
......@@ -6,12 +7,10 @@ using System.Threading.Tasks;
namespace SDK.Models
{
public class SensorContract
public class SensorContract : AccountContract
{
public int Id { get; set; }
public string Login { get; set; }
public string Title { get; set; }
public string Mac { get; set; }
......
......@@ -12,6 +12,7 @@ using SDK.Communication;
using SDK.Exceptions;
using SDK.Contracts.Data;
using SDK.Contracts.Communication;
using System.IO;
namespace tDevkit
{
......@@ -59,13 +60,13 @@ namespace tDevkit
string responseString = await ProcessResponse(task);
return JsonSerializer.Deserialize<Type>(responseString);
}
private async Task<Type> PatchRequest<Type>(string subUrl, object body)
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 JsonSerializer.Deserialize<Type>(responseString);
return new PatchResponseContract();
}
private async Task<HttpResponseMessage> DeleteRequest(string subUrl)
{
......@@ -122,7 +123,14 @@ namespace tDevkit
case (System.Net.HttpStatusCode)401:
throw new NotAuthorizedException(NotAuthorizedException.message);
case (System.Net.HttpStatusCode)404:
var responseB = JsonSerializer.Deserialize<ServerErrorResponseContract>(responseString);
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);
......@@ -151,7 +159,7 @@ namespace tDevkit
}
#endregion
#region AUTHORIZATION (2/3)
#region AUTHORIZATION (3/3)
public async Task<HttpResponseMessage> DeleteCurrentToken()
{
string subUrl = Address.AuthorizationToken;
......@@ -165,24 +173,24 @@ namespace tDevkit
return response;
}
public async Task<HttpResponseMessage> GetToken()
public async Task<AuthenticationResponseContract> GetToken()
{
string url = Address.AuthorizationToken;
var task = await SendGetRequest(url);
string subUrl = Address.AuthorizationToken;
if (task.IsSuccessStatusCode)
{
var responseString = await task.Content.ReadAsStringAsync();
var response = await GetRequest<AuthenticationResponseContract>(subUrl);
AuthenticationResponseContract response = JsonSerializer.Deserialize<AuthenticationResponseContract>(responseString);
if (response.ErrorMessage != null)
throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage);
connectionOptions.Token = response.Token;
httpClient.DefaultRequestHeaders.Add("Token", connectionOptions.Token);
}
connectionOptions.Token = response.Token;
if (response.Client != null)
connectionOptions.ClientGuid = response.Client;
if (response.Branch != null)
connectionOptions.BranchGuid = response.Branch;
return task;
resetHttpClientHeaders();
return response;
}
public async Task<AuthenticationResponseContract> Authenticate(bool superUser)
{
......@@ -252,14 +260,14 @@ namespace tDevkit
return response;
}
public async Task<AddBeaconResponseContract> UpdateBeacon(BeaconContract beaconContract)
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<AddBeaconResponseContract>(subUrl, beaconContract);
var response = await PatchRequest(subUrl, beaconContract);
if (response.ErrorMessage != null)
throw new ServerResponseException(ServerResponseException.message + " " + response.ErrorMessage);
......@@ -459,7 +467,7 @@ namespace tDevkit
}
#endregion
#region SENSORS (4/5)
#region SENSORS (7/8)
public async Task<SensorContract[]> GetSensors()
{
string subUrl = Address.Sensors;
......@@ -467,6 +475,37 @@ namespace tDevkit
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;
......@@ -537,5 +576,48 @@ namespace tDevkit
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
}
}
bytes.jpg 0 → 100644
bytes.jpg

117 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment