public async Task <InvokeResult> AddModelAsync(string orgId, string modelId, int revisionId, byte[] model) { var retryCount = 0; Exception ex = null; while (retryCount++ < 5) { try { var cloudClient = CreateBlobClient(); var primaryContainer = cloudClient.GetContainerReference(GetContainerName(orgId)); await primaryContainer.CreateIfNotExistsAsync(); var blob = primaryContainer.GetBlockBlobReference(GetBlobName(modelId, revisionId)); await blob.UploadFromByteArrayAsync(model, 0, model.Length); return(InvokeResult.Success); } catch (Exception exc) { ex = exc; _adminLogger.AddException("MLModelRepo_GetModelAsync", ex); Console.WriteLine("Exception deserializeing: " + ex.Message); await Task.Delay(retryCount * 250); } } return(InvokeResult.FromException("MLModelRepo_GetModelAsync", ex)); }
public async Task <InvokeResult> InitAsync(DataStream stream) { _stream = stream; _cloudBlobClient = CreateBlobClient(_stream); _container = _cloudBlobClient.GetContainerReference(_stream.AzureBlobStoragePath); try { Microsoft.WindowsAzure.Storage.NameValidator.ValidateContainerName(_stream.AzureBlobStoragePath); await _container.CreateIfNotExistsAsync(); return(InvokeResult.Success); } catch (ArgumentException ex) { _instanceLogger.AddException("AzureBlobConnector_InitAsync", ex); return(InvokeResult.FromException("AzureBlobConnector_InitAsync", ex)); } catch (StorageException ex) { _instanceLogger.AddException("AzureBlobConnector_InitAsync", ex); return(InvokeResult.FromException("AzureBlobConnector_InitAsync", ex)); } }
public async Task <InvokeResult <byte[]> > GetModelAsync(string orgId, string modelId, int revisionId) { var retryCount = 0; Exception ex = null; while (retryCount++ < 5) { try { var cloudClient = CreateBlobClient(); var primaryContainer = cloudClient.GetContainerReference(GetContainerName(orgId)); await primaryContainer.CreateIfNotExistsAsync(); var blobName = GetBlobName(modelId, revisionId); var blob = primaryContainer.GetBlobReference(blobName); using (var ms = new MemoryStream()) { await blob.DownloadToStreamAsync(ms); ms.Seek(0, SeekOrigin.Begin); return(InvokeResult <byte[]> .Create(ms.ToArray())); } } catch (Exception exc) { ex = exc; _adminLogger.AddException("MLModelRepo_GetModelAsync", ex); Console.WriteLine("Exception deserializeing: " + ex.Message); await Task.Delay(retryCount * 250); } } return(InvokeResult <byte[]> .FromException("MLModelRepo_GetModelAsync", ex)); }
private async Task <InvokeResult <CloudBlobContainer> > GetStorageContainerAsync(string containerName) { var client = CreateBlobClient(); var container = client.GetContainerReference(containerName); try { var options = new BlobRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(15) }; var opContext = new OperationContext(); await container.CreateIfNotExistsAsync(options, opContext); return(InvokeResult <CloudBlobContainer> .Create(container)); } catch (ArgumentException ex) { _logger.AddException("CloudFileStorage_GetStorageContainerAsync", ex, containerName.ToKVP(nameof(containerName))); return(InvokeResult <CloudBlobContainer> .FromException("CloudFileStorage_GetStorageContainerAsync_InitAsync", ex)); } catch (StorageException ex) { _logger.AddException("ReportsLibraryRepo_GetStorageContainerAsync", ex, containerName.ToKVP(nameof(containerName))); return(InvokeResult <CloudBlobContainer> .FromException("CloudFileStorage_GetStorageContainerAsync", ex)); } }
public async Task <InvokeResult> InitAsync(DataStream stream) { _stream = stream; var options = new CredentialProfileOptions { AccessKey = stream.AwsAccessKey, SecretKey = stream.AwsSecretKey }; var profile = new Amazon.Runtime.CredentialManagement.CredentialProfile($"awsprofile_{stream.Id}", options); var netSDKFile = new NetSDKCredentialsFile(); netSDKFile.RegisterProfile(profile); var creds = AWSCredentialsFactory.GetAWSCredentials(profile, netSDKFile); try { _s3Client = new AmazonS3Client(creds, AWSRegionMappings.MapRegion(stream.AwsRegion)); await _s3Client.EnsureBucketExistsAsync(stream.S3BucketName); } catch (AmazonS3Exception amazonS3Exception) { _logger.AddException("AWSS3Connector_InitAsync", amazonS3Exception); return(InvokeResult.FromException("AWSS3Connector_InitAsync", amazonS3Exception)); } return(InvokeResult.Success); }
private async Task <InvokeResult <CloudBlobContainer> > GetStorageContainerAsync(IConnectionSettings settings, string containerName) { var client = CreateBlobClient(settings); var container = client.GetContainerReference(containerName); try { var options = new BlobRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(15) }; var opContext = new OperationContext(); await container.CreateIfNotExistsAsync(options, opContext); return(InvokeResult <CloudBlobContainer> .Create(container)); } catch (ArgumentException ex) { _logger.AddException("DeviceMediaRepo_GetStorageContainerAsync", ex); return(InvokeResult <CloudBlobContainer> .FromException("DeviceMediaRepo_GetStorageContainerAsync_InitAsync", ex)); } catch (StorageException ex) { _logger.AddException("DeviceMediaRepo_GetStorageContainerAsync", ex); return(InvokeResult <CloudBlobContainer> .FromException("DeviceMediaRepo_GetStorageContainerAsync", ex)); } }
public async Task <InvokeResult> CreateInitialSampleAsync() { try { return(await this._orgInitializer.CreateExampleAppAsync(_env.EnvironmentName.ToLower(), OrgEntityHeader, UserEntityHeader)); } catch (RecordNotFoundException ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.RecordType + " " + ex.RecordId); Console.WriteLine(ex.StackTrace); Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Console.ResetColor(); return(InvokeResult.FromException("CreateInitialSampleAsync", ex)); } catch (ValidationException) { throw; } catch (Exception ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); Console.WriteLine("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); Console.ResetColor(); return(InvokeResult.FromException("CreateInitialSampleAsync", ex)); } }
public async Task <InvokeResult> UpdateSampleAsync(string orgId, string sampleId, byte[] sample) { var retryCount = 0; Exception ex = null; while (retryCount++ < 5) { try { var cloudClient = CreateBlobClient(); var primaryContainer = cloudClient.GetContainerReference(GetContainerName(orgId)); await primaryContainer.CreateIfNotExistsAsync(); var blob = primaryContainer.GetBlockBlobReference(sampleId); await blob.UploadFromByteArrayAsync(sample, 0, sample.Length); return(InvokeResult.Success); } catch (Exception exc) { ex = exc; _adminLogger.AddException("SampleMediaRepo_AddSampleAsync", ex); Console.WriteLine("Exception deserializeing: " + ex.Message); await Task.Delay(retryCount * 250); } } return(InvokeResult.FromException("SampleMediaRepo_AddSampleAsync", ex)); }
public async Task <InvokeResult> ValidateConnectionAsync(DataStream stream) { var result = await InitAsync(stream); if (!result.Successful) { return(result); } try { var existsResult = await _client.Indices.ExistsAsync("dontcare"); if (existsResult.IsValid) { return(InvokeResult.Success); } if (existsResult.OriginalException != null) { var failedResult = InvokeResult.FromError(existsResult.OriginalException.Message); return(failedResult); } else { return(InvokeResult.FromError("Could not validate AWS Elastic Search Connection, no data returned")); } } catch (Exception ex) { return(InvokeResult.FromException("AWSElasticSearchConnector_ValidateConnection", ex)); } }
protected async Task <InvokeResult> ExecWithRetry(TableOperation operation, int numberRetries = 5) { var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { var execResult = await _cloudTable.ExecuteAsync(operation); completed = (execResult.HttpStatusCode == 200 || execResult.HttpStatusCode == 204); if (!completed) { _instanceLogger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "PEMTableStorage_UpdateMessageAsync", "HTTP Error Adding PEM", execResult.HttpStatusCode.ToString().ToKVP("httpStatusCode"), retryCount.ToString().ToKVP("retryCount")); } } catch (Exception ex) { if (retryCount == numberRetries) { _instanceLogger.AddException("PEMTableStorage_UpdateMessageAsync", ex); return(InvokeResult.FromException("AzureTableStorageConnector_ExecWithRetyr", ex)); } else { _instanceLogger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "PEMTableStorage_UpdateMessageAsync", "Exception writing PEM, will retry", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult.Success); }
private async Task <InvokeResult <CloudBlobContainer> > GetStorageContainerAsync(string orgId) { var client = CreateBlobClient(_blobConnectionSettings); var containerName = $"dtresource-{orgId}".ToLower(); Console.WriteLine(containerName); var container = client.GetContainerReference(containerName); try { var options = new BlobRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(15) }; var opContext = new OperationContext(); await container.CreateIfNotExistsAsync(options, opContext); return(InvokeResult <CloudBlobContainer> .Create(container)); } catch (ArgumentException ex) { _logger.AddException("MediaServicesRepo_GetStorageContainerAsync", ex); return(InvokeResult <CloudBlobContainer> .FromException("MediaServicesRepo_GetStorageContainerAsync_InitAsync", ex)); } catch (StorageException ex) { _logger.AddException("MediaServicesRepo_GetStorageContainerAsync", ex); return(InvokeResult <CloudBlobContainer> .FromException("MediaServicesRepo_GetStorageContainerAsync", ex)); } }
public async Task <InvokeResult> ValidateConnectionAsync(DataStream stream) { try { if (_redis != null) { var db = _redis.GetDatabase(); var reuslt = await db.PingAsync(); return(InvokeResult.Success); } using (var redis = GetRedisConnection(stream)) { var db = redis.GetDatabase(); var reuslt = await db.PingAsync(); return(InvokeResult.Success); } } catch (Exception ex) { return(InvokeResult.FromException("RedisConnector__ValidateConnectionAsync", ex)); } }
public async Task <InvokeResult> InitAsync(DataStream stream) { var credentials = new StorageCredentials(stream.AzureStorageAccountName, stream.AzureAccessKey); var account = new CloudStorageAccount(credentials, true); _stream = stream; _tableClient = account.CreateCloudTableClient(); _cloudTable = _tableClient.GetTableReference(stream.AzureTableStorageName); try { var opContext = new OperationContext(); var options = new TableRequestOptions() { ServerTimeout = TimeSpan.FromSeconds(15) }; await _cloudTable.CreateIfNotExistsAsync(); return(InvokeResult.Success); } catch (StorageException ex) { _logger.AddException("AzureTableStorageConnector_InitAsync", ex); return(InvokeResult.FromException("AzureTableStorageConnector_InitAsync", ex)); } }
public async Task <InvokeResult> InitAsync(DataStream stream) { _stream = stream; _cloudBlobClient = CreateBlobClient(_stream); _container = _cloudBlobClient.GetContainerReference(_stream.AzureBlobStorageContainerName); try { Microsoft.WindowsAzure.Storage.NameValidator.ValidateContainerName(_stream.AzureBlobStorageContainerName); var options = new BlobRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(15) }; var opContext = new OperationContext(); await _container.CreateIfNotExistsAsync(options, opContext); return(InvokeResult.Success); } catch (ArgumentException ex) { _logger.AddException("AzureBlobConnector_InitAsync", ex); var result = InvokeResult.FromException("AzureBlobConnector_InitAsync", ex); return(result); } catch (StorageException ex) { _logger.AddException("AzureBlobConnector_InitAsync", ex); var result = InvokeResult.FromException("AzureBlobConnector_InitAsync", ex); return(result); } }
public async Task <InvokeResult <AuthResponse> > LoginAsync(AuthRequest loginInfo, CancellationTokenSource cancellationTokenSource = null) { var client = SLWIOC.Get <HttpClient>(); var json = JsonConvert.SerializeObject(loginInfo); var content = new StringContent(json, Encoding.UTF8, "application/json"); try { var response = await client.PostAsync("/api/v1/auth", content); if (response.IsSuccessStatusCode) { var resultContent = await response.Content.ReadAsStringAsync(); var serializerSettings = new JsonSerializerSettings(); serializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); var authResponse = JsonConvert.DeserializeObject <InvokeResult <AuthResponse> >(resultContent, serializerSettings); return(authResponse); } else { return(InvokeResult <AuthResponse> .FromErrors(new ErrorMessage() { Message = response.ReasonPhrase })); } } catch (Exception ex) { return(InvokeResult <AuthResponse> .FromException("AuthClient_LoginAsync", ex)); } }
public async Task <InvokeResult> AddBinaryMessageAsync(byte[] buffer, DateTime startTimeStamp, String deviceId = "", String topic = "") { try { var message = new PipelineExecutionMessage() { PayloadType = MessagePayloadTypes.Binary, BinaryPayload = buffer, CreationTimeStamp = startTimeStamp.ToJSONString() }; Metrics.MessagesProcessed++; if (buffer != null) { message.PayloadLength = buffer.Length; } Metrics.BytesProcessed = message.PayloadLength + (String.IsNullOrEmpty(topic) ? 0 : topic.Length); message.Envelope.DeviceId = deviceId; message.Envelope.Topic = topic; var listenerInstruction = new PipelineExecutionInstruction() { Name = _listenerConfiguration.Name, Type = GetType().Name, QueueId = "N/A", StartDateStamp = startTimeStamp.ToJSONString(), ProcessByHostId = PEMBus.Instance.PrimaryHost.Id, ExecutionTimeMS = (DateTime.UtcNow - startTimeStamp).TotalMilliseconds, }; message.Instructions.Add(listenerInstruction); var planner = PEMBus.Instance.Solution.Value.Planner.Value; var plannerInstruction = new PipelineExecutionInstruction() { Name = "Planner", Type = "Planner", QueueId = "N/A", }; message.CurrentInstruction = plannerInstruction; message.Instructions.Add(plannerInstruction); var plannerQueue = PEMBus.Queues.Where(queue => queue.ForModuleType == PipelineModuleType.Planner).FirstOrDefault(); await plannerQueue.EnqueueAsync(message); return(InvokeResult.Success); } catch (Exception ex) { PEMBus.InstanceLogger.AddException("ListenerModule_AddBinaryMessageAsync", ex); return(InvokeResult.FromException("ListenerModule_AddBinaryMessageAsync", ex)); } }
public async Task <InvokeResult <byte[]> > GetFileAsync(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } if (fileName.StartsWith("/")) { fileName = fileName.TrimStart('/'); } var result = await GetStorageContainerAsync(_containerName); if (!result.Successful) { return(InvokeResult <byte[]> .FromInvokeResult(result.ToInvokeResult())); } var container = result.Result; var blob = container.GetBlockBlobReference(fileName); var numberRetries = 5; var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { //TODO: We shouldn't likely return a byte array here probably return access to the response object and stream the bytes as they are downloaded, current architecture doesn't support... using (var ms = new MemoryStream()) { await blob.DownloadToStreamAsync(ms); return(InvokeResult <byte[]> .Create(ms.ToArray())); } } catch (Exception ex) { if (retryCount == numberRetries) { _logger.AddException("CloudFileStorage_GetFileAsync", ex, _containerName.ToKVP("containerName")); return(InvokeResult <byte[]> .FromException("CloudFileStorage_GetFileAsync", ex)); } else { _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "CloudFileStorage_GetFileAsync", "", fileName.ToKVP("fileName"), _containerName.ToKVP("containerName"), ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult <byte[]> .FromError("Could not retrieve Media Item")); }
public async Task <InvokeResult <string> > SendAsync(MessageTransmissionPlan plan) { IsBusy = true; _pointIndex = 0; var messageTemplate = plan.Message.Value; try { InvokeResult res = InvokeResult.FromError(""); switch (messageTemplate.Transport.Value) { case TransportTypes.TCP: res = await SendTCPMessage(plan); break; case TransportTypes.UDP: res = await SendUDPMessage(plan); break; case TransportTypes.AzureServiceBus: res = await SendServiceBusMessage(plan); break; case TransportTypes.AzureEventHub: res = await SendEventHubMessage(plan); break; case TransportTypes.AzureIoTHub: res = await SendIoTHubMessage(plan); break; case TransportTypes.MQTT: res = await SendMQTTMessage(plan); break; case TransportTypes.RestHttps: case TransportTypes.RestHttp: res = await SendRESTRequestAsync(plan); break; } if (res.Successful) { var msg = BuildRequestContent(plan); return(InvokeResult <string> .Create(msg)); } else { return(InvokeResult <string> .FromInvokeResult(res)); } } catch (Exception ex) { _adminLogger.AddException("Send", ex); ReceivedContent = ex.Message; await _notificationPublisher.PublishTextAsync(Targets.WebSocket, Channels.Simulator, InstanceId, $"Error sending {messageTemplate.Transport.Value} message {ex.Message}."); return(InvokeResult <string> .FromException("SendAsync", ex)); } finally { IsBusy = false; } }
public async Task <InvokeResult> DeleteFileAsync(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentNullException(nameof(fileName)); } if (fileName.StartsWith("/")) { fileName = fileName.TrimStart('/'); } var result = await GetStorageContainerAsync(_containerName); if (!result.Successful) { return(result.ToInvokeResult()); } var container = result.Result; var blob = container.GetBlockBlobReference(fileName); var numberRetries = 5; var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { await blob.DeleteAsync(); return(InvokeResult.Success); } catch (Exception ex) { if (retryCount == numberRetries) { _logger.AddException("CloudFileStorage_GetFileAsync", ex, _containerName.ToKVP("containerName")); return(InvokeResult.FromException("CloudFileStorage_GetFileAsync", ex)); } else { _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "CloudFileStorage_GetFileAsync", "", fileName.ToKVP("fileName"), _containerName.ToKVP("containerName"), ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult.FromError("Could not delete Media Item")); }
public async Task <InvokeResult> SendAsync(string email, string subject, string body) { try { body = $@"<body> <img src=""{_appConfig.AppLogo}"" /> <h1>{_appConfig.AppName}</h1> <h2>{subject}</h2> {body} <img src=""{_appConfig.CompanyLogo}"" /> <p>Please do not reply to this email as it is an unmonitored address</p> <a href=""mailto:[email protected]"">Contact Support</a> </body>"; var msg = new MimeMessage() { Subject = subject, Body = new TextPart("html", body), }; msg.To.Add(new MailboxAddress(email)); msg.From.Add(new MailboxAddress(_settings.SmtpFrom)); using (var client = new SmtpClient()) { await client.ConnectAsync(_settings.SmtpServer.Uri.ToString(), 587, false); await client.AuthenticateAsync(_settings.SmtpServer.UserName, _settings.SmtpServer.Password); await client.SendAsync(msg); await client.DisconnectAsync(true); } _adminLogger.AddCustomEvent(Core.PlatformSupport.LogLevel.Verbose, "SendGridEmailServices_SendAsync", "EmailSent", new System.Collections.Generic.KeyValuePair <string, string>("Subject", subject), new System.Collections.Generic.KeyValuePair <string, string>("to", email)); return(InvokeResult.Success); } catch (Exception ex) { _adminLogger.AddException("SendGridEmailServices_SendAsync", ex, new System.Collections.Generic.KeyValuePair <string, string>("Subject", subject), new System.Collections.Generic.KeyValuePair <string, string>("to", email)); return(InvokeResult.FromException("SendGridEmailServices_SendAsync", ex)); } }
public async Task <InvokeResult> OpenAsync(Uri uri) { try { _webSocket = new MessageWebSocket(); _webSocket.Closed += (sndr, args) => Closed?.Invoke(this, null); _webSocket.MessageReceived += _webSocket_MessageReceived; await _webSocket.ConnectAsync(uri); return(InvokeResult.Success); } catch (Exception ex) { return(InvokeResult.FromException("WebSocket_OpenAsync", ex)); } }
public Task <InvokeResult <TAsyncResult> > WaitOnAsync <TAsyncResult>(string correlationId, TimeSpan timeout) { try { RegisterAsyncRequest(correlationId); var asyncRequest = Wait(correlationId, timeout); return(GetAsyncResult <TAsyncResult>(asyncRequest)); } catch (Exception ex) { Logger.AddException("AsyncCoupler_WaitOnAsync", ex); UsageMetrics.ErrorCount++; return(Task.FromResult(InvokeResult <TAsyncResult> .FromException("AsyncCoupler_WaitOnAsync", ex))); } }
public async Task <InvokeResult> AddMediaAsync(byte[] data, string orgId, string fileName, string contentType) { var result = await GetStorageContainerAsync(orgId); if (!result.Successful) { return(result.ToInvokeResult()); } var container = result.Result; var blob = container.GetBlockBlobReference(fileName); blob.Properties.ContentType = contentType; //TODO: Should really encapsulate the idea of retry of an action w/ error reporting var numberRetries = 5; var retryCount = 0; var completed = false; var stream = new MemoryStream(data); while (retryCount++ < numberRetries && !completed) { try { stream.Seek(0, SeekOrigin.Begin); await blob.UploadFromStreamAsync(stream); return(InvokeResult.Success); } catch (Exception ex) { if (retryCount == numberRetries) { _logger.AddException("MediaServicesRepo_AddItemAsync", ex); return(InvokeResult.FromException("MediaServicesRepo_AddItemAsync", ex)); } else { _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "MediaServicesRepo_AddItemAsync", "", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult.Success); }
public async Task <InvokeResult <byte[]> > GetMediaAsync(string blobReferenceName, string org) { var result = await GetStorageContainerAsync(org); if (!result.Successful) { return(InvokeResult <byte[]> .FromInvokeResult(result.ToInvokeResult())); } var container = result.Result; var blob = container.GetBlockBlobReference(blobReferenceName); var numberRetries = 5; var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { using (var ms = new MemoryStream()) { await blob.DownloadToStreamAsync(ms); ms.Seek(0, SeekOrigin.Begin); return(InvokeResult <byte[]> .Create(ms.GetBuffer())); } } catch (Exception ex) { if (retryCount == numberRetries) { _logger.AddException("MediaServicesRepo_AddItemAsync", ex); return(InvokeResult <byte[]> .FromException("MediaServicesRepo_AddItemAsync", ex)); } else { _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "MediaServicesRepo_GetMediaAsync", "", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult <byte[]> .FromError("Could not load media.")); }
public async Task <InvokeResult <byte[]> > GetMediaAsync(DeviceRepository repo, string fileName) { var result = await GetStorageContainerAsync(repo.DeviceArchiveStorageSettings, repo.GetDeviceMediaStorageName()); if (!result.Successful) { return(InvokeResult <byte[]> .FromInvokeResult(result.ToInvokeResult())); } var container = result.Result; var blob = container.GetBlockBlobReference(fileName); var numberRetries = 5; var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { //TODO: We shouldn't likely return a byte array here probably return access to the response object and stream the bytes as they are downloaded, current architecture doesn't support... using (var ms = new MemoryStream()) { await blob.DownloadToStreamAsync(ms); return(InvokeResult <byte[]> .Create(ms.ToArray())); } } catch (Exception ex) { if (retryCount == numberRetries) { _logger.AddException("DeviceMediaRepo_GetMediaAsync", ex); return(InvokeResult <byte[]> .FromException("DeviceMediaRepo_GetMediaAsync", ex)); } else { _logger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "DeviceMediaRepo_GetMediAsync", "", fileName.ToKVP("fileName"), repo.Id.ToKVP("repoId"), ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult <byte[]> .FromError("Could not retrieve Media Item")); }
public static async Task <InvokeResult> ValidateDataStreamAsync(DataStream stream, IAdminLogger logger) { var result = DataStreamServices.GetConnector(stream.StreamType.Value, logger); if (!result.Successful) { return(result.ToInvokeResult()); } try { return(await result.Result.ValidateConnectionAsync(stream)); } catch (Exception ex) { return(InvokeResult.FromException("DataStreamValidator_ValidateDataStreamAsync", ex)); } }
public async Task <InvokeResult> AddItemAsync(DataStreamRecord item) { var recordId = DateTime.UtcNow.ToInverseTicksRowKey(); if (String.IsNullOrEmpty(item.Timestamp)) { switch (_stream.DateStorageFormat.Value) { case DateStorageFormats.Epoch: item.Data.Add(_stream.TimestampFieldName, DateTimeOffset.Now.ToUnixTimeSeconds()); break; case DateStorageFormats.ISO8601: item.Data.Add(_stream.TimestampFieldName, DateTime.UtcNow.ToJSONString()); break; } } item.Data.Add("id", recordId); item.Data.Add("dataStreamId", _stream.Id); item.Data.Add(_stream.DeviceIdFieldName, item.DeviceId); var obj = new PutObjectRequest() { BucketName = _stream.S3BucketName, Key = recordId, ContentBody = JsonConvert.SerializeObject(item.Data) }; obj.ContentType = "application/json"; obj.Metadata.Add("title", recordId); try { await _s3Client.PutObjectAsync(obj); return(InvokeResult.Success); } catch (AmazonS3Exception amazonS3Exception) { _logger.AddException("AWSS3Connector_AddItem", amazonS3Exception); return(InvokeResult.FromException("AWSS3Connector_AddItem", amazonS3Exception)); } }
public async Task <InvokeResult> AddItemAsync(DataStreamRecord item) { var recordId = DateTime.UtcNow.ToInverseTicksRowKey(); item.Data.Add(_stream.TimeStampFieldName, item.GetTimeStampValue(_stream)); item.Data.Add("sortOrder", item.GetTicks()); item.Data.Add("deviceId", item.DeviceId); item.Data.Add("id", recordId); item.Data.Add("dataStreamId", _stream.Id); var fileName = $"{recordId}.json"; var blob = _container.GetBlockBlobReference(fileName); blob.Properties.ContentType = "application/json"; var json = JsonConvert.SerializeObject(item.Data); var numberRetries = 5; var retryCount = 0; var completed = false; while (retryCount++ < numberRetries && !completed) { try { await blob.UploadTextAsync(json); } catch (Exception ex) { if (retryCount == numberRetries) { _instanceLogger.AddException("AzureBlobConnector_AddItemAsync", ex); return(InvokeResult.FromException("AzureBlobConnector_AddItemAsync", ex)); } else { _instanceLogger.AddCustomEvent(LagoVista.Core.PlatformSupport.LogLevel.Warning, "AzureBlobConnector_AddItemAsync", "", ex.Message.ToKVP("exceptionMessage"), ex.GetType().Name.ToKVP("exceptionType"), retryCount.ToString().ToKVP("retryCount")); } await Task.Delay(retryCount * 250); } } return(InvokeResult.Success); }
public async Task <InvokeResult <TMavlinkPacket> > WaitForMessageAsync <TMavlinkPacket>(UasMessages messageId, TimeSpan timeout) where TMavlinkPacket : class { try { var wor = new WaitOnRequest(messageId); Sessions[messageId] = wor; MavLinkPacket message = null; for (var idx = 0; (idx < timeout.TotalMilliseconds / 100) && message == null; ++idx) { if (wor.CompletionSource.Task.IsCompleted) { message = wor.CompletionSource.Task.Result; } await Task.Delay(100); } if (message == null) { return(InvokeResult <TMavlinkPacket> .FromError("Timeout waiting for message.")); } else { if (message.Message is TMavlinkPacket) { return(InvokeResult <TMavlinkPacket> .Create(message.Message as TMavlinkPacket)); } else { return(InvokeResult <TMavlinkPacket> .FromError("Invalid type.")); } } } catch (Exception ex) { return(InvokeResult <TMavlinkPacket> .FromException("AsyncCoupler_WaitOnAsync", ex)); } finally { Sessions.TryRemove(messageId, out WaitOnRequest obj); } }
public async Task <InvokeResult> OpenAsync(Uri uri) { try { _webSocket = new ClientWebSocket(); _cancelTokenSource = new System.Threading.CancellationTokenSource(); await _webSocket.ConnectAsync(uri, System.Threading.CancellationToken.None); _running = true; StartReceiveThread(); return(InvokeResult.Success); } catch (Exception ex) { return(InvokeResult.FromException("WebSocket_OpenAsync", ex)); } }