Untitled
unknown
plain_text
a year ago
7.0 kB
12
Indexable
public async Task<bool> UpdateTelematicsDataCollectionStatusAsync(int manufacturerId, int siteId, string dataCollectionStatus, string region, int partnerId)
{
if (this.dataOptRedisCacheHandler != null && dataCollectionStatus == "2")
{
var cacheKey = siteId.ToString("G", NumberFormatInfo.CurrentInfo);
var cacheEntryOptions = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromDays(365)
};
await this.dataOptRedisCacheHandler.SetAsync(cacheKey, "2", "SO", cacheEntryOptions).ConfigureAwait(false);
}
if (this.dataOptRedisCacheHandler != null && dataCollectionStatus == "1")
{
var cacheKey = siteId.ToString("G", NumberFormatInfo.CurrentInfo);
await this.dataOptRedisCacheHandler.DeleteAsync(cacheKey, "SO").ConfigureAwait(false);
}
bool isUpdated = await this.manufacturerRepository.UpdateTelematicsDataCollectionStatusAsync(manufacturerId, siteId, dataCollectionStatus, region).ConfigureAwait(false);
if (isUpdated)
{
await NotifyMdmAsync("telematicsDataCollection", manufacturerId, siteId, dataCollectionStatus, partnerId, 0).ConfigureAwait(false);
}
return isUpdated;
}
/// <inheritdoc />
public async Task<bool> UpdatePrivacyModeStatusAsync(int manufacturerId, int siteId, string privacyModeStatus, int partnerId)
{
bool isUpdated = await this.manufacturerRepository.UpdatePrivacyModeStatusAsync(manufacturerId, siteId, privacyModeStatus).ConfigureAwait(false);
if (isUpdated)
{
await NotifyMdmAsync("privacyMode", manufacturerId, siteId, privacyModeStatus, partnerId, 0).ConfigureAwait(false);
}
return isUpdated;
}
public async Task<HttpResponseMessage> NotifyMdmAsync(string type, int manufacturerId, int siteId, string status, int partnerId, int retryCount)
{
var loggerProperties = new Dictionary<string, string>();
try
{
var payload = new
{
ManufacturerId = manufacturerId,
SiteId = siteId,
Status = status,
PartnerId = partnerId
};
this.logger.LogMessage(EventLevel.Informational, string.Empty, $"MDM Notification {type} started for ManufacturerId: {manufacturerId}, SiteId: {siteId}");
var mdmConfig = await this.configurationHandler.GetConfigurationOrDefaultAsync("MDMConfig", new MDMConfig()).ConfigureAwait(false);
if (mdmConfig.Disabled)
{
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
Uri apiUri;
if (type == "telematicsDataCollection")
{
// Telematics data collection API
apiUri = new Uri(mdmConfig.MdmBaseAddress + mdmConfig.TelematicsEndpoint);
}
else if (type == "privacyMode")
{
// Privacy mode API
apiUri = new Uri(mdmConfig.MdmBaseAddress + mdmConfig.PrivacyEndpoint);
}
else
{
throw new ArgumentException("Invalid type specified. Type must be either 'telematics' or 'privacy'.");
}
var authorizationUri = new Uri(mdmConfig.AuthorizationAddress);
var trackingData = new
{
ManufacturerId = manufacturerId,
SiteId = siteId,
Type = type,
Status = status,
InvokeDateTime = DateTime.UtcNow,
Payload = JsonConvert.SerializeObject(payload)
};
await this.mdmTrackingRepository.AddOrUpdateTrackingTable(trackingData).ConfigureAwait(false);
// Prepare the HTTP content
using (var content = new StringContent(JsonConvert.SerializeObject(payload), Encoding.UTF8))
{
var response = await SendToMdmAsync(content, apiUri, authorizationUri).ConfigureAwait(false);
string errorMessage = await GetResponseErrorMessage(response).ConfigureAwair(false);
string retry = null;
if (!response.IsSuccessStatusCode && (int)response.StatusCode != 400 && retryCount == 0)
{
retry = DateTime.UtcNow.AddMinutes(1).ToString("s", CultureInfo.InvariantCulture);
}
else if (!response.IsSuccessStatusCode && (int)response.StatusCode != 400 && retryCount == 1)
{
retry = DateTime.UtcNow.AddMinutes(10).ToString("s", CultureInfo.InvariantCulture);
}
else
{
retry = null;
}
var responseData = new
{
ManufucturerId = manufacturerId,
ApiResult = ((int)response.StatusCode).ToString(CultureInfo.InvariantCulture),
ApiRetryDateTime = retry,
ResponseErrorMessage = errorMessage
};
await this.mdmTrackingRepository.AddOrUpdateTrackingTable(responseData).ConfigureAwait(false);
if (!response.IsSuccessStatusCode && retryCount < 2 && (int)response.StatusCode != 400)
{
this.logger.LogMessage(EventLevel.Warning, string.Empty, $"MDM {type} Failed .Retrying...");
await Task.Delay(retryCount == 0 ? TimeSpan.FromMinutes(1) : TimeSpan.FromMinutes(10)).ConfigureAwait(false);
return await NotifyMdmAsync(type, manufacturerId, siteId, status, partnerId, retryCount + 1).ConfigureAwait(false);
}
this.logger.LogMessage(EventLevel.Informational,string.Empty, $"MDM {type} Completed with StatusCode {response.StatusCode}");
return response;
}
}
catch (Exception ex)
{
// Log any exception that occurs
this.logger.LogException("MDMError", $"MDM {type} failed for ManufacturerId: {manufacturerId}, SiteId: {siteId}", ex, loggerProperties);
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
}
}
public async Task<HttpResponseMessage> SendToMdmAsync(HttpContent httpContent, Uri apiUri, Uri authorizationUri, int retryCount)
{
var httpClient = new HttpClient();
if (this.executionContext.IsDemo)
{
return new HttpResponseMessage(HttpStatusCode.Accepted);
}
string bearerToken = await this.GetAuthorizationBearerToken(authorizationUri).ConfigureAwait(false);
if (httpContent != null)
{
httpContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
}
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", bearerToken);
var httpresponse = await httpClient.PutAsync(apiUri, httpContent).ConfigureAwait(false);
this.logger.LogMessage(
EventLevel.Informational,
string.Empty,
$"GTS mdm Integration - for the input {httpContent} responsecode {httpresponse.StatusCode}");
return httpresponse;
}Editor is loading...
Leave a Comment