Exemple #1
0
        private bool PLCTagsChanged(IIncomingMessage Message)
        {
            bool RetValue = true;

            // get msg application data
            var MsgData = GeneralHelper.DeserializeObject(Message.MessageData) as PLCTagsData;
            var plctags = MsgData.Tags;

            Logger.InfoFormat("Ricevuto Messaggio da {0}", Message.SourceApplicationName);

            foreach (var plctag in plctags)
            {
                bool bOK = true;

                // trova il tag corrispondente (uno solo) all'indirizzo sottoscritto
                TagItem tag = ListTagItems.FirstOrDefault(item => item.PLCName == plctag.PLCName && item.Address == plctag.Address);

                if (tag != null)
                {
                    //
                    try
                    {
                        tag.Value = plctag.Value;
                        Logger.InfoFormat("Cambiato Tag {0} : {1}/{2}:{3} -> {4}", tag.Name, tag.PLCName, tag.Address, tag.Type, tag.Value);
                    }
                    catch (Exception exc)
                    {
                        Logger.WarnFormat("Errore in cambio Tag value {0} : {1}/{2}:{3} -> {4} {5}", tag.Name, tag.PLCName, tag.Address, tag.Type, tag.Value, exc.Message);
                    }

                    // recuperare la lista delle properties dell'impianto
                    List <MariniProperty> props = mariniImpiantoTree.MariniImpianto.GetObjectListByType(typeof(MariniProperty)).Cast <MariniProperty>().ToList();
                    // trova la property associata e cambia il valore
                    MariniProperty property = props.FirstOrDefault(prp => prp.bind == tag.Name);

                    if (property != null)
                    {
                        try
                        {
                            property.value = tag.Value;
                        }
                        catch (Exception exc)
                        {
                            Logger.WarnFormat("Errore in cambio valore property {0}:{1} {2}", property.name, tag.Value, exc.Message);
                            bOK = false;
                        }
                    }
                }
                else
                {
                    Logger.InfoFormat("Tag associato a : {0}/{1} non trovato", plctag.PLCName, plctag.Address);
                    bOK = false;
                }
                if (bOK == false)
                {
                    RetValue = bOK;
                }
            }
            return(RetValue);
        }
Exemple #2
0
        /// <summary>
        /// creo un tag plc completo (plcname/address:type da un tag name)
        /// </summary>
        /// <param name="PLCTagName"></param>
        /// <returns></returns>
        private TagItem GetPLCTagData(string PLCTagName)
        {
            bool bOK = true;

            string PLCTagFullAddress = null;
            string PLCTagPLCName     = null;
            string PLCTagAddress     = null;
            string PLCTagType        = null;

            if (PLCTagName == null || PLCTagName.Length == 0)
            {
                bOK = false;
            }

            try
            {
                PLCTagFullAddress = _hashTagsAddressByName[PLCTagName].ToString();
                bOK = true;
            }
            catch (Exception exc)
            {
                Logger.WarnFormat("Tag : {0} non presente {1}", PLCTagName, exc.Message);
                bOK = false;
            }

            if (bOK)
            {
                bOK = false;

                // split plcname e varname (es : plc4/db86.dbd58:Bool)
                string[] var1 = PLCTagFullAddress.Split('/');
                if (var1.Count() == 2)
                {
                    // controllo plc name
                    PLCTagPLCName = var1[0];
                    // split varname e var type (es : db86.dbd58:Bool)
                    string[] var2 = var1[1].Split(':');
                    if (var2.Count() == 2)
                    {
                        // controlla address
                        PLCTagAddress = var2[0];
                        // controlla tipo
                        PLCTagType = var2[1];
                        bOK        = true;
                    }
                }
            }

            TagItem tag = null;

            if (bOK)
            {
                tag = new TagItem(PLCTagName, PLCTagAddress, PLCTagPLCName, PLCTagType);
            }
            return(tag);
        }
Exemple #3
0
        /// <summary>
        /// Sottoscrizione tag
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        private bool PLCAddTag(TagItem tag)
        {
            bool RetValue = true;

            // se già presente non lo agiungo
            if (ListTagItems.Contains(tag))
            {
                Logger.InfoFormat("tag {0}/{1} già presente", tag.PLCName, tag.Address);
                return(false);
            }

            //Create a DotNetMQ Message to send
            var message = mdsClient.CreateMessage();

            //Set destination application name
            message.DestinationApplicationName = PLCServerApplicationName;

            //Create a message
            var MsgData = new PLCTagData
            {
                MsgCode = MsgCodes.SubscribePLCTag,
                Tag     = new PLCTag()
                {
                    PLCName = tag.PLCName, Address = tag.Address
                }
            };

            //Set message data
            message.MessageData = GeneralHelper.SerializeObject(MsgData);

            // message.MessageData = Encoding.UTF8.GetBytes(messageText);
            message.TransmitRule = MessageTransmitRules.NonPersistent;

            try
            {
                //Send message
                message.Send();
            }
            catch
            {
                // non sono riuscito a inviare il messaggio
                Logger.InfoFormat("Messaggio non inviato");
                RetValue = false;
            }

            if (RetValue)
            {
                Logger.InfoFormat("Aggiunto {0}/{1}:{2}", tag.PLCName, tag.Address, tag.Type);

                /* verifica il nome del plc tag */
                ListTagItems.Add(tag);
            }

            return(RetValue);
        }
Exemple #4
0
        /// <summary>
        /// Rimozione sottoscrizione tag
        /// </summary>
        /// <param name="tag"></param>
        /// <returns></returns>
        private bool PLCRemoveTag(TagItem tag)
        {
            bool RetValue = true;

            //Create a DotNetMQ Message to send
            var message = mdsClient.CreateMessage();

            //Set destination application name
            message.DestinationApplicationName = PLCServerApplicationName;

            //Create a message
            var MsgData = new PLCTagData
            {
                MsgCode = MsgCodes.RemovePLCTag,
                Tag     = new PLCTag()
                {
                    PLCName = tag.PLCName, Address = tag.Address
                }
            };

            //Set message data
            message.MessageData = GeneralHelper.SerializeObject(MsgData);

            // message.MessageData = Encoding.UTF8.GetBytes(messageText);
            message.TransmitRule = MessageTransmitRules.NonPersistent;

            try
            {
                //Send message
                message.Send();
            }
            catch
            {
                // non sono riuscito a inviare il messaggio
                Logger.InfoFormat("Messaggio non inviato");
                RetValue = false;
            }

            if (RetValue)
            {
                ListTagItems.Remove(tag);
            }

            return(RetValue);
        }
Exemple #5
0
        private bool PLCTagsChanged(IIncomingMessage Message)
        {
            bool RetValue = true;

            // get msg application data
            var MsgData = GeneralHelper.DeserializeObject(Message.MessageData) as PLCTagsData;
            var plctags = MsgData.Tags;

            Logger.InfoFormat(Texts._MSG_RECEIVED_FROM_ + " {0}", Message.SourceApplicationName);

            foreach (var plctag in plctags)
            {
                bool bOK = true;

                // trova il tag corrispondente (uno solo) all'indirizzo sottoscritto
                TagItem tag = ListTagItems.FirstOrDefault(item => item.PLCName == plctag.PLCName && item.Address == plctag.Address);

                if (tag != null)
                {
                    //
                    try
                    {
                        tag.Value = plctag.Value;
                        Logger.InfoFormat("Cambiato Tag {0} : {1}/{2}:{3} -> {4}", tag.Name, tag.PLCName, tag.Address, tag.Type, tag.Value);
                    }
                    catch (Exception exc)
                    {
                        Logger.WarnFormat("Errore in cambio Tag value {0} : {1}/{2}:{3} -> {4} {5}", tag.Name, tag.PLCName, tag.Address, tag.Type, tag.Value, exc.Message);
                    }

                    // recuperare la lista delle properties dell'impianto
                    // LG: Ma la uso anche in SubscribePLCTags e PLCTagChanged!!! Devo fare tutte le volte sta roba?
                    List <PropertyObject> props = dataManager.PathObjectsDictionary.Values.Where(item => item.GetType() == typeof(PropertyObject)).Cast <PropertyObject>().ToList();
                    // trova la property associata e cambia il valore
                    PropertyObject property = props.FirstOrDefault(prp => prp.bind == tag.Name);

                    if (property != null)
                    {
                        try
                        {
                            property.value = tag.Value;
                        }
                        catch (Exception exc)
                        {
                            Logger.WarnFormat("Errore in cambio valore property {0}:{1} {2}", property.path, tag.Value, exc.Message);
                            bOK = false;
                        }
                    }
                }
                else
                {
                    Logger.InfoFormat("Tag associato a : {0}/{1} non trovato", plctag.PLCName, plctag.Address);
                    bOK = false;
                }
                if (bOK == false)
                {
                    RetValue = bOK;
                }
            }
            return(RetValue);
        }