public bool UpdateWorkDefinition(WorkDefinitionEvent workDefinitionEvent) { var workDefinition = GetDbModel(workDefinitionEvent); _loggingService.Info($"Update Work Definition: {JsonConvert.SerializeObject(workDefinition)}", "WorkDefinitionServices.UpdateWorkDefinition"); var messages = new List <KafkaMessage>(workdefintionTopicNames?.Select((topic) => new KafkaMessage { Key = workDefinition.AssetUID.ToString(), Message = new { UpdateWorkDefinitionEvent = new { AssetUID = workDefinition.AssetUID.ToString(), WorkDefinitionType = workDefinitionEvent.WorkDefinitionType, SensorNumber = workDefinition.SwitchNumber, StartIsOn = workDefinition.SwitchWorkStartState, ActionUTC = workDefinition.InsertUTC, ReceivedUTC = workDefinition.InsertUTC, } }, Topic = topic })); var actions = new List <Action>() { () => _transaction.Upsert(workDefinition), () => _transaction.Publish(messages) }; return(_transaction.Execute(actions)); }
private WorkDefinitionDto GetDbModel(WorkDefinitionEvent workDefinitionEvent) { DateTime utcNow = DateTime.UtcNow; return(new WorkDefinitionDto() { AssetUID = workDefinitionEvent.AssetUID, WorkDefinitionTypeID = GetWorkDefinitionTypeID(workDefinitionEvent.WorkDefinitionType), SwitchNumber = workDefinitionEvent.SensorNumber, SwitchWorkStartState = workDefinitionEvent.StartIsOn, StartDate = workDefinitionEvent.ReceivedUTC, InsertUTC = workDefinitionEvent.ReceivedUTC, UpdateUTC = workDefinitionEvent.ReceivedUTC }); }
public async Task <ActionResult> GetWorkDefinition(Guid assetUID, string deviceType = null) { try { if (assetUID == Guid.Empty) { _loggingService.Info($"AssetUID is mandatory AssetUID :{assetUID}", "WorkDefinitionV1Controller.GetWorkDefinition"); return(BadRequest("AssetUID is mandatory")); } var validationResult = await _requestValidator.Validate(new AssetSettingValidationRequestBase { AssetUIDs = new List <string> { assetUID.ToString() }, DeviceType = deviceType, GroupName = "WorkDefinitions" }); var workDefinitionDto = _wdRepo.GetWorkDefinition(assetUID); if (workDefinitionDto != null) { var workDefinitionEvent = new WorkDefinitionEvent { WorkDefinitionType = workDefinitionDto.WorkDefinitionType, AssetUID = workDefinitionDto.AssetUID, SensorNumber = workDefinitionDto.SwitchNumber, StartIsOn = workDefinitionDto.SwitchWorkStartState, ActionUTC = workDefinitionDto.InsertUTC, ReceivedUTC = workDefinitionDto.InsertUTC }; if (validationResult.Any(x => x.IsInvalid)) { _loggingService.Warn(validationResult.First().Message + " for AssetUID : " + assetUID.ToString(), "WorkDefinitionV1Controller.GetWorkDefinition"); workDefinitionEvent.ErrorMessage = validationResult.First().Message; } return(StatusCode((int)HttpStatusCode.OK, workDefinitionEvent)); } return(StatusCode((int)HttpStatusCode.NotFound)); } catch (Exception ex) { _loggingService.Error("An exception has occurred : ", "WorkDefinitionV1Controller.GetWorkDefinition", ex); return(StatusCode((int)HttpStatusCode.InternalServerError)); } }
public async Task <ActionResult> UpdateWorkDefinition(WorkDefinitionEvent workdefinition) { try { workdefinition = await base.ReadRequestContentAsync(workdefinition); workdefinition.ReceivedUTC = DateTime.UtcNow; #region Workaround to fix CustomJsonConverter save default value as -999999 var serializedWorkDefinition = JsonHelper.SerializeObjectToJson(workdefinition, new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver() }); workdefinition = JsonConvert.DeserializeObject <WorkDefinitionEvent>(serializedWorkDefinition); #endregion var result = await _requestValidator.Validate(new AssetSettingValidationRequestBase { AssetUIDs = new List <string> { workdefinition.AssetUID.ToString() }, DeviceType = workdefinition.DeviceType, GroupName = "WorkDefinitions" }); if (result.Any(x => x.IsInvalid)) { _loggingService.Warn(result.First().Message + " for AssetUID : " + workdefinition.AssetUID.ToString(), "WorkDefinitionV1Controller.UpdateWorkDefinition"); return(BadRequest(result.First().Message)); } _wdRepo.UpdateWorkDefinition(workdefinition); return(Ok()); } catch (Exception ex) { _loggingService.Error("An exception has occurred : ", "WorkDefinitionV1Controller.UpdateWorkDefinition", ex); return(StatusCode((int)HttpStatusCode.InternalServerError)); } }