예제 #1
0
        /// <summary>
        /// Method definition for creating an IoT Alert in Dynamics.
        /// </summary>
        /// <param name="iconicsFault">Fault Data.</param>
        /// <param name="assetId">Customer Asset ID.</param>
        /// <param name="log">Log Content.</param>
        /// <returns>IoT Alert record GUID.</returns>
        public Guid CreateAlert(IconicsFault iconicsFault, Guid assetId, ILogger log)
        {
            Guid alertId = Guid.Empty;

            if (iconicsFault != null)
            {
                // create new alert from fault data
                Entity newAlert = new Entity(DynamicsEntities.IoTAlert);
                newAlert[IoTAlertProperties.MsdynAlertData]   = JsonConvert.SerializeObject(iconicsFault);
                newAlert[IoTAlertProperties.MsdynAlertTime]   = DateTimeOffset.Parse(iconicsFault.FaultActiveTime, CultureInfo.CurrentCulture);
                newAlert[IoTAlertProperties.MsdynAlertType]   = IotAlertAlertType.Anomaly;
                newAlert[IoTAlertProperties.MsdynDescription] = iconicsFault.FaultName;
                newAlert[IoTAlertProperties.StateCode]        = IotAlertStateCode.Active;
                newAlert[IoTAlertProperties.StatusCode]       = IotAlertStatusCode.Active;
                newAlert[DynamicsRelationships.IoTAlertToOneCustomerAsset] = new EntityReference(DynamicsEntities.CustomerAsset, assetId);
                newAlert[IoTAlertProperties.MsdynAlertToken] = DynamicsEntityService.ComputeSha256Hash(iconicsFault);

                alertId = GetExecutionPolicy(log).Execute(() =>
                {
                    return(this.cdsServiceClient.Create(newAlert));
                });

                log.LogInformation(
                    "Created Alert {alertId} from Fault (FaultName: {FaultName}, FaultActiveTime: {FaultActiveTime}, AssetName: {AssetName}, AssetPath: {AssetPath}).",
                    alertId,
                    iconicsFault.FaultName,
                    iconicsFault.FaultActiveTime,
                    iconicsFault.AssetName,
                    iconicsFault.AssetPath);

                log.LogInformation("Associated Alert {alertId} with Asset {assetId}.", alertId, assetId);
            }

            return(alertId);
        }
예제 #2
0
        /// <summary>
        /// Method declaration for getting previous IoT Alert based on Alert Token Hash Value.
        /// </summary>
        /// <param name="iconicsFault">Fault Data.</param>
        /// <param name="log">Log Content.</param>
        /// <returns>An existing IoT Alert record GUID.</returns>
        public Guid GetIoTAlert(IconicsFault iconicsFault, ILogger log)
        {
            Guid alertId = Guid.Empty;

            if (iconicsFault != null)
            {
                string alertToken = DynamicsEntityService.ComputeSha256Hash(iconicsFault);

                // build query expression that finds the Iot Alert with the Hash value supplied in the fault
                QueryExpression queryExpression = new QueryExpression()
                {
                    Distinct   = false,
                    EntityName = DynamicsEntities.IoTAlert,
                    ColumnSet  = new ColumnSet(IoTAlertProperties.MsdynAlertToken),
                    Criteria   =
                    {
                        Filters            =
                        {
                            new FilterExpression
                            {
                                Conditions =
                                {
                                    new ConditionExpression(IoTAlertProperties.MsdynAlertToken, ConditionOperator.Equal,    alertToken),
                                    new ConditionExpression(IoTAlertProperties.StateCode,       ConditionOperator.NotEqual, IotAlertStateCode.Inactive.ToString()),
                                },
                            },
                        },
                    },
                };

                // run the query to determine if the IoT Alert already exists in Dynamics
                EntityCollection queryExpressionResult = GetExecutionPolicy(log).Execute(() =>
                {
                    return(this.cdsServiceClient.RetrieveMultiple(queryExpression));
                });

                if (queryExpressionResult.Entities.Count == 0)
                {
                    // IoT does not exist, update log to Application Insights
                    log.LogWarning($"An Existing IoT Alert with Alert Token {alertToken} not found in Dynamics");
                    alertId = Guid.Empty;
                }
                else
                {
                    // IoT Alert does exist, Update IoT Alert Status to InActive and Alert Data with latest Iconic Fault Data
                    alertId = queryExpressionResult.Entities[0].Id;
                }
            }

            return(alertId);
        }