private async Task <Dictionary <long, IScadaModelPointItem> > CreatePointItemsFromNetworkModelMeasurements(Dictionary <byte, List <long> > modelChanges)
        {
            Dictionary <long, IScadaModelPointItem> pointItems = new Dictionary <long, IScadaModelPointItem>();

            INetworkModelGDAContract nmsGdaClient = NetworkModelGdaClient.CreateClient();

            int iteratorId;
            int resourcesLeft;
            int numberOfResources = 10000;

            List <ModelCode> props;

            //TOOD: change service contract IModelUpdateNotificationContract to receive types of all changed elements from NMS
            var changedTypesHashSet = new HashSet <ModelCode>();

            foreach (var gids in modelChanges.Values)
            {
                foreach (var gid in gids)
                {
                    ModelCode type = modelResourceDesc.GetModelCodeFromId(gid);
                    changedTypesHashSet.Add(type);
                }
            }

            foreach (ModelCode type in changedTypesHashSet)
            {
                if (type != ModelCode.ANALOG && type != ModelCode.DISCRETE)
                {
                    continue;
                }

                props = modelResourceDesc.GetAllPropertyIds(type);

                try
                {
                    iteratorId = await nmsGdaClient.GetExtentValues(type, props);

                    resourcesLeft = await nmsGdaClient.IteratorResourcesLeft(iteratorId);

                    while (resourcesLeft > 0)
                    {
                        List <ResourceDescription> resources = await nmsGdaClient.IteratorNext(numberOfResources, iteratorId);

                        foreach (ResourceDescription rd in resources)
                        {
                            if (pointItems.ContainsKey(rd.Id))
                            {
                                string message = $"{baseLogString} CreatePointItemsFromNetworkModelMeasurements => Trying to create point item for resource that already exists in model. Gid: 0x{rd.Id:X16}";
                                Logger.LogError(message);
                                throw new ArgumentException(message);
                            }

                            IScadaModelPointItem point;

                            //change service contract IModelUpdateNotificationContract => change List<long> to Hashset<long>
                            if (modelChanges[(byte)DeltaOpType.Update].Contains(rd.Id) || modelChanges[(byte)DeltaOpType.Insert].Contains(rd.Id))
                            {
                                point = CreatePointItemFromResource(rd);
                                pointItems.Add(rd.Id, point);
                            }
                        }

                        resourcesLeft = await nmsGdaClient.IteratorResourcesLeft(iteratorId);
                    }

                    await nmsGdaClient.IteratorClose(iteratorId);
                }
                catch (Exception ex)
                {
                    string errorMessage = $"{baseLogString} CreatePointItemsFromNetworkModelMeasurements => Failed with error: {ex.Message}";
                    Logger.LogError(errorMessage, ex);
                }
            }

            return(pointItems);
        }
Exemplo n.º 2
0
 public int GetExtentValues(ModelCode entityType, List <ModelCode> propIds)
 {
     return(Channel.GetExtentValues(entityType, propIds));
 }
Exemplo n.º 3
0
        public List <long> GetExtentValues(ModelCode modelCode)
        {
            string message = "Getting extent values method started.";

            Console.WriteLine(message);
            CommonTrace.WriteTrace(CommonTrace.TraceError, message);

            XmlTextWriter xmlWriter  = null;
            int           iteratorId = 0;
            List <long>   ids        = new List <long>();

            try
            {
                int numberOfResources = 50;
                int resourcesLeft     = 0;

                List <ModelCode> properties = modelResourcesDesc.GetAllPropertyIds(modelCode);

                iteratorId    = GdaQueryProxy.GetExtentValues(modelCode, properties);
                resourcesLeft = GdaQueryProxy.IteratorResourcesLeft(iteratorId);


                xmlWriter            = new XmlTextWriter(Config.Instance.ResultDirecotry + "\\GetExtentValues_Results.xml", Encoding.Unicode);
                xmlWriter.Formatting = Formatting.Indented;

                while (resourcesLeft > 0)
                {
                    List <ResourceDescription> rds = GdaQueryProxy.IteratorNext(numberOfResources, iteratorId);

                    for (int i = 0; i < rds.Count; i++)
                    {
                        ids.Add(rds[i].Id);
                        rds[i].ExportToXml(xmlWriter);
                        xmlWriter.Flush();
                    }

                    resourcesLeft = GdaQueryProxy.IteratorResourcesLeft(iteratorId);
                }

                GdaQueryProxy.IteratorClose(iteratorId);

                message = "Getting extent values method successfully finished.";
                Console.WriteLine(message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
            }
            catch (Exception e)
            {
                message = string.Format("Getting extent values method failed for {0}.\n\t{1}", modelCode, e.Message);
                Console.WriteLine(message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
            }
            finally
            {
                if (xmlWriter != null)
                {
                    xmlWriter.Close();
                }
            }

            return(ids);
        }
Exemplo n.º 4
0
 public Property(Property toCopy)
 {
     this.id    = toCopy.id;
     this.value = new PropertyValue(toCopy.PropertyValue);
 }
Exemplo n.º 5
0
 public Property(ModelCode id, PropertyValue value)
 {
     this.id    = id;
     this.value = value;
 }
Exemplo n.º 6
0
 public Property(ModelCode id, string stringValue)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(stringValue);
 }
Exemplo n.º 7
0
 public Property(ModelCode id, TimeSpan timeSpan)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(timeSpan);
 }
Exemplo n.º 8
0
 public void AddPropertyId(ModelCode propertyId, string propertyName)
 {
     propertyIds[propertyId] = propertyName;
 }
Exemplo n.º 9
0
        public List <long> GetExtentValues(ModelCode modelCodeType, List <ModelCode> properties, StringBuilder sb)
        {
            var message = "Getting extent values method started.";

            CommonTrace.WriteTrace(CommonTrace.TraceError, message);

            int iteratorId        = 0;
            int resourcesLeft     = 0;
            int numberOfResources = 300;
            var ids    = new List <long>();
            var tempSb = new StringBuilder();

            try
            {
                iteratorId    = GdaQueryProxy.GetExtentValues(modelCodeType, properties);
                resourcesLeft = GdaQueryProxy.IteratorResourcesLeft(iteratorId);

                while (resourcesLeft > 0)
                {
                    List <ResourceDescription> rds = GdaQueryProxy.IteratorNext(numberOfResources, iteratorId);

                    foreach (var rd in rds)
                    {
                        if (rd == null)
                        {
                            continue;
                        }
                        tempSb.Append($"\nEntity with gid: 0x{rd.Id:X16}" + Environment.NewLine);

                        foreach (var property in rd.Properties)
                        {
                            switch (property.Type)
                            {
                            case PropertyType.Int64:
                                StringAppender.AppendLong(tempSb, property);
                                break;

                            case PropertyType.Float:
                                StringAppender.AppendFloat(tempSb, property);
                                break;

                            case PropertyType.String:
                                StringAppender.AppendString(tempSb, property);
                                break;

                            case PropertyType.Reference:
                                StringAppender.AppendReference(tempSb, property);
                                break;

                            case PropertyType.ReferenceVector:
                                StringAppender.AppendReferenceVector(tempSb, property);
                                break;

                            case PropertyType.Enum:
                                StringAppender.AppendEnum(tempSb, property);
                                break;

                            case PropertyType.DateTime:
                                StringAppender.AppendDateTime(tempSb, property);
                                break;

                            default:
                                tempSb.Append($"\t{property.Id}: {property.PropertyValue.LongValue}{Environment.NewLine}");
                                break;
                            }
                        }

                        ids.Add(rd.Id);
                    }

                    resourcesLeft = GdaQueryProxy.IteratorResourcesLeft(iteratorId);
                }

                GdaQueryProxy.IteratorClose(iteratorId);

                message = "Getting extent values method successfully finished.";
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
            }
            catch (Exception e)
            {
                message = string.Format("Getting extent values method failed for {0}.\n\t{1}", modelCodeType, e.Message);
                CommonTrace.WriteTrace(CommonTrace.TraceError, message);
            }

            if (sb != null)
            {
                sb.Append(tempSb.ToString());
            }

            return(ids);
        }
Exemplo n.º 10
0
 public ResourcePropertiesDesc(ModelCode resourceId, string resourceName)
 {
     this.resourceId   = resourceId;
     this.resourceName = resourceName;
 }
Exemplo n.º 11
0
 /// <summary>
 /// Adds new property code to class type.
 /// </summary>
 /// <param name="propertyId">Property code</param>
 public void AddPropertyId(ModelCode propertyId)
 {
     propertyIds[propertyId] = null;
 }
Exemplo n.º 12
0
 /// <summary>
 /// Initializes a new instance of the ResourcePropertiesDesc class.
 /// </summary>
 /// <param name="resourceId">Model type code</param>
 public ResourcePropertiesDesc(ModelCode resourceId)
 {
     this.resourceId   = resourceId;
     this.resourceName = resourceId.ToString();
 }
Exemplo n.º 13
0
 /// <summary>
 /// Checks if property with specified code exists in class type.
 /// </summary>
 /// <param name="propertyId">Code of the property</param>
 /// <returns>TRUE if property exists in class type, FALSE otherwise</returns>
 public bool PropertyExists(ModelCode propertyId)
 {
     return(propertyIds.ContainsKey(propertyId));
 }
Exemplo n.º 14
0
 public override bool HasProperty(ModelCode property)
 {
     return(base.HasProperty(property));
 }
Exemplo n.º 15
0
 public Property(ModelCode id, long int64Value)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(int64Value);
 }
        public void AddProperty(ModelCode pid, PropertyValue value)
        {
            Property property = new Property(pid, value);

            this.AddProperty(property);
        }
Exemplo n.º 17
0
 public Property(ModelCode id, float floatValue)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(floatValue);
 }
        private void RelationalPropertiesDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectedProperty == null)
            {
                return;
            }

            RelatedEntityDmsTypes.Clear();
            SelectedDmsType = null;
            PropertiesInRelated.Children.Clear();
            RelatedValues.Document.Blocks.Clear();

            List <DMSType> dmsTypes = new List <DMSType>();

            if (RelationalPropertiesHelper.Relations.ContainsKey(SelectedProperty.Property))
            {
                ModelCode relatedEntity = RelationalPropertiesHelper.Relations[SelectedProperty.Property];
                dmsTypes.AddRange(ModelResourcesDesc.GetLeavesForCoreEntities(relatedEntity));

                if (dmsTypes.Count == 0)
                {
                    dmsTypes.Add(ModelCodeHelper.GetTypeFromModelCode(relatedEntity));
                }
            }

            foreach (DMSType type in dmsTypes)
            {
                RelatedEntityDmsTypes.Add(new DmsTypeViewModel()
                {
                    DmsType = type
                });
            }

            HashSet <ModelCode> referencedTypeProperties = new HashSet <ModelCode>();

            if (RelatedEntityDmsTypes.Count > 0)
            {
                foreach (DmsTypeViewModel referencedDmsType in RelatedEntityDmsTypes)
                {
                    foreach (ModelCode propInReferencedType in modelResourcesDesc.GetAllPropertyIds(referencedDmsType.DmsType))
                    {
                        if (!referencedTypeProperties.Contains(propInReferencedType))
                        {
                            referencedTypeProperties.Add(propInReferencedType);
                        }
                    }
                }
            }

            Label label = new Label()
            {
                FontWeight = FontWeights.UltraBold,
                Content    = "Properties (for classes in selected relation)",
            };

            PropertiesInRelated.Children.Add(label);

            propertiesDesc.Clear();

            if (referencedTypeProperties.Count > 0)
            {
                foreach (ModelCode property in referencedTypeProperties)
                {
                    if (propertiesDesc.ContainsKey(property))
                    {
                        continue;
                    }

                    propertiesDesc.Add(property, property.ToString());

                    CheckBox checkBox = new CheckBox()
                    {
                        Content = property.ToString(),
                    };
                    checkBox.Unchecked += CheckBox_Unchecked;
                    PropertiesInRelated.Children.Add(checkBox);
                }
                CheckAllBtn.IsEnabled = true;
            }
        }
Exemplo n.º 19
0
 public Property(ModelCode id, DateTime dateTime)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(dateTime);
 }
        public void InitializeAnalogPointItem(AnalogPointItem pointItem, List <Property> props, ModelCode type, EnumDescs enumDescs)
        {
            InitializeScadaModelPointItem(pointItem as ScadaModelPointItem, props, type, enumDescs);

            foreach (var item in props)
            {
                switch (item.Id)
                {
                case ModelCode.ANALOG_CURRENTVALUE:
                    pointItem.CurrentEguValue = item.AsFloat();
                    break;

                case ModelCode.ANALOG_MAXVALUE:
                    pointItem.EGU_Max = item.AsFloat();
                    break;

                case ModelCode.ANALOG_MINVALUE:
                    pointItem.EGU_Min = item.AsFloat();
                    break;

                case ModelCode.ANALOG_NORMALVALUE:
                    pointItem.NormalValue = item.AsFloat();
                    break;

                case ModelCode.ANALOG_SCALINGFACTOR:
                    pointItem.ScalingFactor = item.AsFloat();
                    break;

                case ModelCode.ANALOG_DEVIATION:
                    pointItem.Deviation = item.AsFloat();
                    break;

                case ModelCode.ANALOG_SIGNALTYPE:
                    pointItem.AnalogType = (AnalogMeasurementType)(enumDescs.GetEnumValueFromString(ModelCode.ANALOG_SIGNALTYPE, item.AsEnum().ToString()));
                    break;

                default:
                    break;
                }
            }

            if (pointItem.ScalingFactor == 0)
            {
                string warnMessage = $"{baseLogString} InitializeAnalogPointItem => Analog measurement is of type: {pointItem.AnalogType} which is not supported for alarming. Gid: 0x{pointItem.Gid:X16}, Addres: {pointItem.Address}, Name: {pointItem.Name}, RegisterType: {pointItem.RegisterType}, Initialized: {pointItem.Initialized}";
                Logger.LogWarning(warnMessage);

                pointItem.ScalingFactor = 1;
            }

            pointItem.Initialized = true;
        }
Exemplo n.º 21
0
 public static PropertyType GetPropertyType(ModelCode propId)
 {
     return((PropertyType)((long)propId & (long)ModelCodeMask.MASK_ATTRIBUTE_TYPE));
 }
        public void InitializeScadaModelPointItem(ScadaModelPointItem pointItem, List <Property> props, ModelCode type, EnumDescs enumDescs)
        {
            foreach (var item in props)
            {
                switch (item.Id)
                {
                case ModelCode.IDOBJ_GID:
                    pointItem.Gid = item.AsLong();
                    break;

                case ModelCode.IDOBJ_NAME:
                    pointItem.Name = item.AsString();
                    break;

                case ModelCode.MEASUREMENT_ADDRESS:
                    if (ushort.TryParse(item.AsString(), out ushort address))
                    {
                        pointItem.Address = address;
                    }
                    else
                    {
                        string errorMessage = $"{baseLogString} InitializeScadaModelPointItem => Address ('{item.AsString()}') is either not defined or is invalid.";
                        Logger.LogError(errorMessage);
                        throw new ArgumentException(errorMessage);
                    }
                    break;

                case ModelCode.MEASUREMENT_ISINPUT:
                    if (type == ModelCode.ANALOG)
                    {
                        pointItem.RegisterType = (item.AsBool() == true) ? PointType.ANALOG_INPUT : PointType.ANALOG_OUTPUT;
                    }
                    else if (type == ModelCode.DISCRETE)
                    {
                        pointItem.RegisterType = (item.AsBool() == true) ? PointType.DIGITAL_INPUT : PointType.DIGITAL_OUTPUT;
                    }
                    else
                    {
                        string errorMessage = $"{baseLogString} InitializeScadaModelPointItem => ModelCode type is neither ANALOG nor DISCRETE.";
                        Logger.LogError(errorMessage);
                        throw new ArgumentException(errorMessage);
                    }
                    break;

                default:
                    break;
                }
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// Initializes a new instance of the Property class
 /// </summary>
 /// <param name="id">property ID</param>
 public Property(ModelCode id)
 {
     this.id    = id;
     this.value = new PropertyValue();
 }
        public void InitializeDiscretePointItem(DiscretePointItem pointItem, List <Property> props, ModelCode type, EnumDescs enumDescs)
        {
            InitializeScadaModelPointItem(pointItem as ScadaModelPointItem, props, type, enumDescs);

            foreach (var item in props)
            {
                switch (item.Id)
                {
                case ModelCode.DISCRETE_CURRENTOPEN:
                    pointItem.CurrentValue = (ushort)((item.AsBool() == true) ? 1 : 0);
                    break;

                case ModelCode.DISCRETE_MAXVALUE:
                    pointItem.MaxValue = (ushort)item.AsInt();
                    break;

                case ModelCode.DISCRETE_MINVALUE:
                    pointItem.MinValue = (ushort)item.AsInt();
                    break;

                case ModelCode.DISCRETE_NORMALVALUE:
                    pointItem.NormalValue = (ushort)item.AsInt();
                    break;

                case ModelCode.DISCRETE_MEASUREMENTTYPE:
                    pointItem.DiscreteType = (DiscreteMeasurementType)(enumDescs.GetEnumValueFromString(ModelCode.ANALOG_SIGNALTYPE, item.AsEnum().ToString()));
                    break;

                default:
                    break;
                }
            }

            pointItem.Initialized = true;
        }
Exemplo n.º 25
0
 public Property(ModelCode id, bool boolValue)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(boolValue);
 }
Exemplo n.º 26
0
 public Property(ModelCode id, short int16Value)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(int16Value);
 }
Exemplo n.º 27
0
 public PropertyView(ModelCode modelCode, string value)
 {
     this.modelCode = modelCode.ToString();
     this.value     = value;
 }
Exemplo n.º 28
0
 public Property(ModelCode id, int int32Value)
 {
     this.id    = id;
     this.value = new PropertyValue();
     SetValue(int32Value);
 }
Exemplo n.º 29
0
 public override void SettableReferencePropertiesTest(ModelCode propertyId)
 {
     base.SettableReferencePropertiesTest(propertyId);
 }
        public async Task <bool> Prepare()
        {
            bool success;

            while (!ReliableDictionariesInitialized)
            {
                await Task.Delay(1000);
            }

            try
            {
                //INIT INCOMING SCADA MODEL with current model values
                await InitializeIncomingScadaModel();

                //IMPORT ALL measurements from NMS and create PointItems for them
                var enumerableModelChanges = await ModelChanges.GetEnumerableDictionaryAsync();

                Dictionary <long, IScadaModelPointItem> incomingPointItems = await CreatePointItemsFromNetworkModelMeasurements(enumerableModelChanges);

                //ORDER IS IMPORTANT due to IncomingAddressToGidMap validity: DELETE => UPDATE => INSERT
                var orderOfOperations = new List <DeltaOpType>()
                {
                    DeltaOpType.Delete, DeltaOpType.Update, DeltaOpType.Insert
                };

                foreach (var operation in orderOfOperations)
                {
                    foreach (long gid in enumerableModelChanges[(byte)operation])
                    {
                        ModelCode type = modelResourceDesc.GetModelCodeFromId(gid);
                        if (type != ModelCode.ANALOG && type != ModelCode.DISCRETE)
                        {
                            continue;
                        }

                        if (operation == DeltaOpType.Delete)
                        {
                            await HandleDeleteOperation(gid);
                        }
                        else if (operation == DeltaOpType.Update)
                        {
                            IScadaModelPointItem incomingPointItem = incomingPointItems[gid];
                            await HandleUpdateOperation(incomingPointItem, gid);
                        }
                        else if (operation == DeltaOpType.Insert)
                        {
                            IScadaModelPointItem incomingPointItem = incomingPointItems[gid];
                            await HandleInsertOperation(incomingPointItem, gid);
                        }
                    }
                }

                success = await CheckSuccessiveAddressCondition();

                ;
            }
            catch (Exception e)
            {
                string errorMessage = $"{baseLogString} Prepare => Exception: {e.Message}";
                Logger.LogError(errorMessage, e);
                success = false;
            }

            return(success);
        }