Пример #1
0
        private void WriteTag_ValueChanged(object sender, OpcDataChangeEventArgs e)
        {
            var monitoredNode = (OpcMonitoredItem)sender;
            var nodeValue     = (bool)Client.ReadNode(monitoredNode.NodeId).Value;
            int stationNum    = int.Parse(monitoredNode.NodeId.ToString()
                                          .Remove(0, 42).Substring(
                                              monitoredNode.NodeId.ToString()
                                              .Remove(0, 42).IndexOf("_", 0, StringComparison.Ordinal) + 1, 1));

            if (!nodeValue) // False
            {
                return;
            }

            #region Write to OPC UA

            var tagNodeId = new OpcNodeId("2:RFID_Communications.RFID Readers.Station_" + stationNum + "_Tag");
            var reader    = Program.RfidReaders.FirstOrDefault(i => i.ReaderName == "Station" + stationNum);
            if (reader == null)
            {
                return;
            }
            var guid = Program.RfidReaders.First(i => i.ReaderName == "Station" + stationNum).WriteEpc();

            Client.WriteNode(monitoredNode.NodeId, false);
            Client.WriteNode(tagNodeId, guid);

            #endregion Write to OPC UA
        }
        public ObservedNode Add(OpcClient client, OpcNodeId parentNodeId, string name)
        {
            var item = new ObservedNode(parentNodeId, name);

            item.Initialize(client);

            this.Add(item);
            return(item);
        }
Пример #3
0
        //private BackgroundWorker backgroundWorker;


        public MainWindow()
        {
            //opcUaWorker.OPCNotify += OpcUaWorker_OPCNotify;
            InitializeComponent();
            Firstcheck = false;
            on         = new SolidColorBrush(Color.FromRgb(0, 255, 0));
            error      = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            neutral    = new SolidColorBrush(Color.FromRgb(221, 221, 221));
            Main_pressure.IsReadOnly    = true;
            Crio_pressure.IsReadOnly    = true;
            ForVac_pressure.IsReadOnly  = true;
            PnePressure.IsReadOnly      = true;
            CamTemperature.IsReadOnly   = true;
            Crio_temperature.IsReadOnly = true;


            UpdateTimerCallBack = new TimerCallback(delegate  {
                UpdateGUI(null);
            });
            UpdateTimer = new Timer(UpdateTimerCallBack, null, 0, 1000);



            var client = new OpcClient("opc.tcp://192.168.0.10:4840/");

            client.Connect();

            var node = client.BrowseNode("ns=3;s=\"Crio_DB\".\"Crio\".\"Input\"");

            if (node is OpcVariableNodeInfo variablenode)
            {
                OpcNodeId       datatypeid = variablenode.DataTypeId;
                OpcDataTypeInfo datatype   = client.GetDataTypeSystem().GetType(datatypeid);

                Console.WriteLine(datatype.TypeId);
                Console.WriteLine(datatype.Encoding);

                Console.WriteLine(datatype.Name);

                foreach (OpcDataFieldInfo field in datatype.GetFields())
                {
                    Console.WriteLine(".{0} : {1}", field.Name, field.FieldType);
                }

                Console.WriteLine();
                Console.WriteLine("data type attributes:");
                Console.WriteLine(
                    "\t[opcdatatype(\"{0}\")]",
                    datatype.TypeId.ToString(OpcNodeIdFormat.Foundation));
                Console.WriteLine(
                    "\t[opcdatatypeencoding(\"{0}\", namespaceuri = \"{1}\")]",
                    datatype.Encoding.Id.ToString(OpcNodeIdFormat.Foundation),
                    datatype.Encoding.Namespace.Value);
            }
            Console.WriteLine("А я из основного потока. Просто надо так сделать");
        }
Пример #4
0
        private int ReadTagChange(OpcNodeId nodeId)
        {
            try
            {
                ServiceConfig.Reader readerConfig = null;
                foreach (var serviceConfig in RfidService.ServiceConfigs)
                {
                    readerConfig =
                        serviceConfig.Readers.FirstOrDefault(reader =>
                                                             reader.Tags.ReaderTag.ReadTag == nodeId.ToString());
                    if (readerConfig != null)
                    {
                        break;
                    }
                }

                if (readerConfig == null)
                {
                    _eventLog.WriteEntry("Unable to find matching config. Check system settings.", EventLogEntryType.Error, (int)EventIds.Ids.NullReaderError);
                    _client.WriteNode(nodeId, false);
                    return(1);
                }

                using (var reader = new RfidReader(readerConfig, ref _eventLog))
                {
                    if (!reader.GetStatus())
                    {
                        _client.WriteNode(readerConfig.Tags.ErrorTag.ErrorBool, true);
                        _client.WriteNode(readerConfig.Tags.ErrorTag.ErrorString, "Reader not online!");
                        _client.WriteNode(nodeId, false);
                        return(1);
                    }

                    var nodeVal = reader.Read_Epc();
                    if (string.IsNullOrEmpty(nodeVal))
                    {
                        _client.WriteNode(readerConfig.Tags.ErrorTag.ErrorBool, true);
                        _client.WriteNode(readerConfig.Tags.ErrorTag.ErrorString, "No Tags Detected!");
                        _client.WriteNode(readerConfig.Tags.UuidTag.UuidTag, string.Empty);
                    }
                    else
                    {
                        _client.WriteNode(readerConfig.Tags.UuidTag.UuidTag, nodeVal);
                    }
                    _client.WriteNode(nodeId, false);
                }
                return(0);
            }
            catch (Exception e)
            {
                _eventLog.WriteEntry($"Unknown Error\n{e.Message}\n{e.StackTrace}\n\n{e.InnerException}", EventLogEntryType.Error, (int)EventIds.Ids.UnknownError);
                return(1);
            }
        }
Пример #5
0
        /// <summary>
        /// Write the DataValue 'dv' to the CSV file.
        /// </summary>
        protected virtual void WriteDataValueToCsvFile(DataValue dv)
        {
            TMValue tmvalue =
                new TMValue(
                    OpcNodeId.ToString(),
                    dv.ServerTimestamp,
                    dv.WrappedValue.ToString(),
                    dv.StatusCode.ToString()
                    );

            tmvalue.WriteToCsvFile();
        }
Пример #6
0
        public MainViewModel()
            : base()
        {
            this.address       = new Uri("opc.tcp://localhost:4840");
            this.addressStatus = "Enter address of server.";

            this.nodeId     = "ns=2;s=MyNode";
            this.nodeStatus = "Enter a node identifier and click 'Subscribe'.";

            this.SubscribeCommand = new DelegateCommand(
                execute: (_) => this.Subscribe(),
                canExecute: (_) => this.AddressIsValid && this.NodeIdIsValid);
        }
Пример #7
0
        private static IEnumerable <OpcNodeId> BrowseNodes(OpcClient client, OpcNodeId rootNodeId)
        {
            var rootNode = client.BrowseNode(rootNodeId);

            foreach (var node in rootNode.Children())
            {
                var name = node.Name.Value;

                if (name.StartsWith("Var") || name == "Timestamp")
                {
                    yield return(node.NodeId);
                }
            }
        }
Пример #8
0
        public int SubscribeNode(OpcNodeId nodeId)
        {
            try
            {
                _client.SubscribeDataChange(nodeId, TagValueChanged);

                return(0);
            }
            catch (OpcException e)
            {
                _eventLog.WriteEntry($"Connection Failed. \n\tError Code:{e.Code}\n\tMessage:{e.Message}\n\tCause:{e.Result}", EventLogEntryType.Error, (int)EventIds.Ids.OpcConnectionFailure);
                return(1);
            }
        }
Пример #9
0
        /// <summary>
        /// Setup Opc Client
        /// </summary>
        public RunOpc()
        {
            #region Setup OPC UA

            Client.ServerAddress   = new Uri("opc.tcp://127.0.0.1:49320");
            Client.ApplicationName = AppDomain.CurrentDomain.FriendlyName;
            Client.Security.AutoAcceptUntrustedCertificates = true;
            Client.Connect();

            for (int i = 8; i <= 13; i++)
            {
                var readNodeId = new OpcNodeId("2:RFID_Communications.RFID Readers.Station_" + i + "_Read");
                OpcSubscriptions.Add(Client.SubscribeDataChange(readNodeId, ReadTag_ValueChanged));
                Console.WriteLine("Subscribed to node {0}\nCurrent Node Value {1}\n", readNodeId.Value, Client.ReadNode(readNodeId).Value);
            }

            var writeNodeId = new OpcNodeId("2:RFID_Communications.RFID Readers.Station_8_Write");
            OpcSubscriptions.Add(Client.SubscribeDataChange(writeNodeId, WriteTag_ValueChanged));

            writeNodeId = new OpcNodeId("2:RFID_Communications.RFID Readers.Station_9_Write");
            OpcSubscriptions.Add(Client.SubscribeDataChange(writeNodeId, WriteTag_ValueChanged));

            #endregion Setup OPC UA
        }
            public Repository(SQLiteConnection connection, OpcNodeId nodeId)
                : base()
            {
                this.connection = connection;
                this.syncRoot   = new object();

                var nodeIdValue = nodeId.ToString();

                //this.Create(value)
                {
                    this.createCommand = this.connection.CreateCommand();

                    var commandText
                        = "insert into HistoryData ("
                          + "            NodeId, "
                          + "            Timestamp, "
                          + "            TimestampValue, "
                          + "            Value, ";

                    if (!IsModifiedHistory)
                    {
                        commandText
                            += "        StatusCode)";
                    }
                    else
                    {
                        commandText
                            += "        StatusCode, "
                               + "         ModificationTime, "
                               + "         ModificationTimeValue, "
                               + "         ModificationType, "
                               + "         ModificationUserName)";
                    }

                    commandText
                        += "      values ("
                           + "            @nodeId, "
                           + "            @timestamp, "
                           + "            @timestampValue, "
                           + "            @value, ";

                    if (!IsModifiedHistory)
                    {
                        commandText
                            += "        @statusCode)";
                    }
                    else
                    {
                        commandText
                            += "        @statusCode, "
                               + "         @modificationTime, "
                               + "         @modificationTimeValue, "
                               + "         @modificationType, "
                               + "         @modificationUserName)";
                    }

                    this.createCommand.CommandText = commandText;

                    var parameters = this.createCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.createTimestamp      = parameters.Add("@timestamp", DbType.DateTime);
                    this.createTimestampValue = parameters.Add("@timestampValue", DbType.Int64);
                    this.createValue          = parameters.Add("@value", DbType.Object);
                    this.createStatusCode     = parameters.Add("@statusCode", DbType.Int64);

                    if (IsModifiedHistory)
                    {
                        this.createModificationTime      = parameters.Add("@modificationTime", DbType.DateTime);
                        this.createModificationTimeValue = parameters.Add("@modificationTimeValue", DbType.Int64);
                        this.createModificationType      = parameters.Add("@modificationType", DbType.Byte);
                        this.createModificationUserName  = parameters.Add("@modificationUserName", DbType.String);
                    }
                }

                //this.Delete(timestamp)
                {
                    this.deleteCommand = this.connection.CreateCommand();

                    this.deleteCommand.CommandText
                        = "delete from HistoryData "
                          + "      where NodeId = @nodeId "
                          + "        and TimestampValue = @timestampValue";

                    var parameters = this.deleteCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.deleteTimestampValue = parameters.Add("@timestampValue", DbType.Int64);
                }

                //this.Delete(startTime, endTime)
                {
                    this.deleteRangeCommand = this.connection.CreateCommand();

                    this.deleteRangeCommand.CommandText
                        = "delete from HistoryData "
                          + " where NodeId = @nodeId "
                          + "   and TimestampValue >= @startTimeValue "
                          + "   and TimeStampValue <= @endTimeValue";

                    var parameters = this.deleteRangeCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.deleteRangeStartTimeValue = parameters.Add("@startTimeValue", DbType.Int64);
                    this.deleteRangeEndTimeValue   = parameters.Add("@endTimeValue", DbType.Int64);
                }

                //this.Exists(timestamp)
                {
                    this.existsCommand = this.connection.CreateCommand();

                    this.existsCommand.CommandText
                        = "select count(1) "
                          + "  from HistoryData hd "
                          + " where hd.NodeId = @nodeId "
                          + "   and hd.TimestampValue = @timestampValue";

                    var parameters = this.existsCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.existsTimestampValue = parameters.Add("@timestampValue", DbType.Int64);
                }

                //this.Read(timestamp)
                {
                    this.readCommand = this.connection.CreateCommand();

                    var commandText
                        = "select hd.TimestampValue, "
                          + "       hd.Value, ";

                    if (!IsModifiedHistory)
                    {
                        commandText
                            += "      hd.StatusCode ";
                    }
                    else
                    {
                        commandText
                            += "      hd.StatusCode, "
                               + "       hd.ModificationTimeValue, "
                               + "       hd.ModificationType, "
                               + "       hd.ModificationUserName ";
                    }

                    commandText
                        += " from HistoryData hd "
                           + " where hd.NodeId = @nodeId "
                           + "   and hd.TimestampValue = @timestampValue";

                    this.readCommand.CommandText = commandText;

                    var parameters = this.readCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.readTimestampValue = parameters.Add("@timestampValue", DbType.Int64);
                }

                //this.Read(startTime, endTime)
                {
                    this.readRangeCommand = this.connection.CreateCommand();

                    var commandText
                        = "select hd.TimestampValue, "
                          + "       hd.Value, ";

                    if (!IsModifiedHistory)
                    {
                        commandText
                            += "      hd.StatusCode ";
                    }
                    else
                    {
                        commandText
                            += "      hd.StatusCode, "
                               + "       hd.ModificationTimeValue, "
                               + "       hd.ModificationType, "
                               + "       hd.ModificationUserName ";
                    }

                    commandText
                        += " from HistoryData hd "
                           + " where hd.NodeId = @nodeId "
                           + "   and hd.TimestampValue >= @startTimeValue "
                           + "   and hd.TimestampValue <= @endTimeValue";

                    this.readRangeCommand.CommandText = commandText;

                    var parameters = this.readRangeCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.readRangeStartTimeValue = parameters.Add("@startTimeValue", DbType.Int64);
                    this.readRangeEndTimeValue   = parameters.Add("@endTimeValue", DbType.Int64);
                }

                //this.Update(value)
                {
                    this.updateCommand = this.connection.CreateCommand();

                    var commandText
                        = "update HistoryData "
                          + "   set Value = @value, ";

                    if (!IsModifiedHistory)
                    {
                        commandText
                            += "   StatusCode = @statusCode ";
                    }
                    else
                    {
                        commandText
                            += "   StatusCode = @statusCode, "
                               + "    ModificationTime = @modificationTime, "
                               + "    ModificationTimeValue = @modificationTimeValue, "
                               + "    ModificationType = @modificationType, "
                               + "    ModificationUserName = @modificationUserName ";
                    }

                    commandText
                        += "where NodeId = @nodeId "
                           + "   and TimestampValue = @timestampValue";

                    this.updateCommand.CommandText = commandText;

                    var parameters = this.updateCommand.Parameters;
                    parameters.AddWithValue("@nodeId", nodeIdValue);

                    this.updateTimestampValue = parameters.Add("@timestampValue", DbType.Int64);
                    this.updateValue          = parameters.Add("@value", DbType.Object);
                    this.updateStatusCode     = parameters.Add("@statusCode", DbType.Int64);

                    if (IsModifiedHistory)
                    {
                        this.updateModificationTime      = parameters.Add("@modificationTime", DbType.DateTime);
                        this.updateModificationTimeValue = parameters.Add("@modificationTimeValue", DbType.Int64);
                        this.updateModificationType      = parameters.Add("@modificationType", DbType.Byte);
                        this.updateModificationUserName  = parameters.Add("@modificationUserName", DbType.String);
                    }
                }
            }
Пример #11
0
        private static IEnumerable <OpcMonitoredItem> CreateMonitoredItems(OpcClient client, OpcNodeId rootNodeId)
        {
            var node = client.BrowseNode(rootNodeId);

            foreach (var childNode in node.Children())
            {
                yield return(new OpcMonitoredItem(childNode.NodeId, OpcAttribute.Value));
            }
        }
Пример #12
0
        private static IEnumerable <OpcSubscribeDataChange> CreateCommands(OpcClient client, OpcNodeId rootNodeId)
        {
            var node = client.BrowseNode(rootNodeId);

            foreach (var childNode in node.Children())
            {
                yield return(new OpcSubscribeDataChange(childNode.NodeId, HandleDataChange));
            }
        }
Пример #13
0
 public ObservedNode(OpcNodeId parentNodeId, string name)
     : base()
 {
     this.Id    = OpcNodeId.Of(name, parentNodeId);
     this.Label = (name + ":").PadRight(16);
 }