配置 OPC 发布服务器
重要
当我们更新本文时,请参阅 Azure 工业 IoT 来了解最新内容。
你可以配置 OPC 发布服务器以指定:
- 要发布的 OPC UA 节点数据更改。
- 要发布的 OPC UA 事件。
- 遥测格式。
可以使用配置文件或方法调用来配置 OPC 发布服务器。
使用配置文件
本部分介绍使用配置文件配置 OPC UA 节点发布的选项。
使用配置文件配置发布数据更改
配置要发布的 OPC UA 节点的最简单方法是使用配置文件。 配置文件格式记录在存储库的 publishednodes.json 中。
配置文件语法随时间变化。 OPC 发布服务器仍然读取旧格式,但在继续配置时将其转换为最新格式。
以下示例显示了配置文件的格式:
[
{
"EndpointUrl": "opc.tcp://testserver:62541/Quickstarts/ReferenceServer",
"UseSecurity": true,
"OpcNodes": [
{
"Id": "i=2258",
"OpcSamplingInterval": 2000,
"OpcPublishingInterval": 5000,
"DisplayName": "Current time"
}
]
}
]
使用配置文件配置发布事件
若要发布 OPC UA 事件,请使用与数据更改相同的配置文件。
以下示例显示如何为 SimpleEvents 服务器生成的事件配置发布。 在 OPC Foundation 存储库中可以找到 SimpleEvents 服务器:
[
{
"EndpointUrl": "opc.tcp://testserver:62563/Quickstarts/SimpleEventsServer",
"OpcEvents": [
{
"Id": "i=2253",
"DisplayName": "SimpleEventServerEvents",
"SelectClauses": [
{
"TypeId": "i=2041",
"BrowsePaths": [
"EventId"
]
},
{
"TypeId": "i=2041",
"BrowsePaths": [
"Message"
]
},
{
"TypeId": "nsu=http://opcfoundation.org/Quickstarts/SimpleEvents;i=235",
"BrowsePaths": [
"/2:CycleId"
]
},
{
"TypeId": "nsu=http://opcfoundation.org/Quickstarts/SimpleEvents;i=235",
"BrowsePaths": [
"/2:CurrentStep"
]
}
],
"WhereClause": [
{
"Operator": "OfType",
"Operands": [
{
"Literal": "ns=2;i=235"
}
]
}
]
}
]
}
]
使用方法调用
本部分介绍可用于配置 OPC 发布服务器的方法调用。
使用 OPC UA 方法调用进行配置
OPC 发布服务器包括一个 OPC UA 服务器,在 62222 端口上可以访问该服务器。 如果主机名是“发布服务器”,则终结点 URI 是:opc.tcp://publisher:62222/UA/Publisher
。
此终结点公开以下四种方法:
- PublishNode
- UnpublishNode
- GetPublishedNodes
- IoT HubDirectMethod
使用 Azure IoT 中心直接方法调用进行配置
OPC 发布服务器实现以下 Azure IoT 中心直接方法调用:
- PublishNodes
- UnpublishNodes
- UnpublishAllNodes
- GetConfiguredEndpoints
- GetConfiguredNodesOnEndpoint
- GetDiagnosticInfo
- GetDiagnosticLog
- GetDiagnosticStartupLog
- ExitApplication
- GetInfo
方法请求和响应的 JSON 有效负载的格式在 opcpublisher/HubMethodModel.cs 中定义。
如果对模块调用一个未知的方法,它会用一个字符串来响应,表示该方法无法实现。 可以调用一种未知方法作为对模块执行 Ping 操作的方法。
配置用于身份验证的用户名和密码
通过 IoT 中心直接方法调用可以设置身份验证模式。 有效负载必须包含 OpcAuthenticationMode 属性以及用户名和密码:
{
"EndpointUrl": "<Url of the endpoint to set authentication settings>",
"OpcAuthenticationMode": "UsernamePassword",
"Username": "<Username>",
"Password": "<Password>"
...
}
密码由 IoT 中心工作负载客户端加密,并存储在发布服务器的配置中。 若要将身份验证更改回匿名,请使用具有以下有效负载的方法:
{
"EndpointUrl": "<Url of the endpoint to set authentication settings>",
"OpcAuthenticationMode": "Anonymous"
...
}
如果有效负载种未设置 OpcAuthenticationMode 属性,则配置中的身份验证设置保持不变。
配置遥测发布
OPC 发布服务器收到发布节点中的值更改通知后,会生成一条 JSON 格式的消息,并将其发送到 IoT 中心。
你可以使用配置文件配置此 JSON 格式消息的内容。 如果没有使用 --tc
选项指定配置文件,则使用与连接的工厂解决方案加速器兼容的默认配置。
如果将 OPC 发布服务器配置为批处理消息,那么它们将作为有效的 JSON 数组进行发送。
遥测数据的来源如下:
- 节点的 OPC 发布服务器节点配置
- OPC 发布服务器收到其通知的 OPC UA 堆栈的 MonitoredItem 对象。
- 传递给此通知的参数,其中提供有关数据值更改的详细信息。
输入到 JSON 格式消息中的遥测数据是这些对象的精选重要属性。 如果需要更多属性,则需要更改 OPC 发布服务器代码库。
此配置文件的语法如下所示:
// The configuration settings file consists of two objects:
// 1) The 'Defaults' object, which defines defaults for the telemetry configuration
// 2) An array 'EndpointSpecific' of endpoint specific configuration
// Both objects are optional and if they are not specified, then publisher uses
// its internal default configuration, which generates telemetry messages compatible
// with the Microsoft Connected factory Preconfigured Solution (https://github.com/Azure/azure-iot-connected-factory).
// A JSON telemetry message for Connected factory looks like:
// {
// "NodeId": "i=2058",
// "ApplicationUri": "urn:myopcserver",
// "DisplayName": "CurrentTime",
// "Value": {
// "Value": "10.11.2017 14:03:17",
// "SourceTimestamp": "2017-11-10T14:03:17Z"
// }
// }
// The 'Defaults' object in the sample below, are similar to what publisher is
// using as its internal default telemetry configuration.
{
"Defaults": {
// The first two properties ('EndpointUrl' and 'NodeId' are configuring data
// taken from the OpcPublisher node configuration.
"EndpointUrl": {
// The following three properties can be used to configure the 'EndpointUrl'
// property in the JSON message send by publisher to IoT Hub.
// Publish controls if the property should be part of the JSON message at all.
"Publish": false,
// Pattern is a regular expression, which is applied to the actual value of the
// property (here 'EndpointUrl').
// If this key is omitted (which is the default), then no regex matching is done
// at all, which improves performance.
// If the key is used you need to define groups in the regular expression.
// Publisher applies the regular expression and then concatenates all groups
// found and use the resulting string as the value in the JSON message to
//sent to IoT Hub.
// This example mimics the default behaviour and defines a group,
// which matches the conplete value:
"Pattern": "(.*)",
// Here some more exaples for 'Pattern' values and the generated result:
// "Pattern": "i=(.*)"
// defined for Defaults.NodeId.Pattern, will generate for the above sample
// a 'NodeId' value of '2058'to be sent by publisher
// "Pattern": "(i)=(.*)"
// defined for Defaults.NodeId.Pattern, will generate for the above sample
// a 'NodeId' value of 'i2058' to be sent by publisher
// Name allows you to use a shorter string as property name in the JSON message
// sent by publisher. By default the property name is unchanged and will be
// here 'EndpointUrl'.
// The 'Name' property can only be set in the 'Defaults' object to ensure
// all messages from publisher sent to IoT Hub have a similar layout.
"Name": "EndpointUrl"
},
"NodeId": {
"Publish": true,
// If you set Defaults.NodeId.Name to "ni", then the "NodeId" key/value pair
// (from the above example) will change to:
// "ni": "i=2058",
"Name": "NodeId"
},
// The MonitoredItem object is configuring the data taken from the MonitoredItem
// OPC UA object for published nodes.
"MonitoredItem": {
// If you set the Defaults.MonitoredItem.Flat to 'false', then a
// 'MonitoredItem' object will appear, which contains 'ApplicationUri'
// and 'DisplayNode' proerties:
// "NodeId": "i=2058",
// "MonitoredItem": {
// "ApplicationUri": "urn:myopcserver",
// "DisplayName": "CurrentTime",
// }
// The 'Flat' property can only be used in the 'MonitoredItem' and
// 'Value' objects of the 'Defaults' object and will be used
// for all JSON messages sent by publisher.
"Flat": true,
"ApplicationUri": {
"Publish": true,
"Name": "ApplicationUri"
},
"DisplayName": {
"Publish": true,
"Name": "DisplayName"
}
},
// The Value object is configuring the properties taken from the event object
// the OPC UA stack provided in the value change notification event.
"Value": {
// If you set the Defaults.Value.Flat to 'true', then the 'Value'
// object will disappear completely and the 'Value' and 'SourceTimestamp'
// members won't be nested:
// "DisplayName": "CurrentTime",
// "Value": "10.11.2017 14:03:17",
// "SourceTimestamp": "2017-11-10T14:03:17Z"
// The 'Flat' property can only be used for the 'MonitoredItem' and 'Value'
// objects of the 'Defaults' object and will be used for all
// messages sent by publisher.
"Flat": false,
"Value": {
"Publish": true,
"Name": "Value"
},
"SourceTimestamp": {
"Publish": true,
"Name": "SourceTimestamp"
},
// 'StatusCode' is the 32 bit OPC UA status code
"StatusCode": {
"Publish": false,
"Name": "StatusCode"
// 'Pattern' is ignored for the 'StatusCode' value
},
// 'Status' is the symbolic name of 'StatusCode'
"Status": {
"Publish": false,
"Name": "Status"
}
}
},
// The next object allows to configure 'Publish' and 'Pattern' for specific
// endpoint URLs. Those will overwrite the ones specified in the 'Defaults' object
// or the defaults used by publisher.
// It is not allowed to specify 'Name' and 'Flat' properties in this object.
"EndpointSpecific": [
// The following shows how a endpoint specific configuration can look like:
{
// 'ForEndpointUrl' allows to configure for which OPC UA server this
// object applies and is a required property for all objects in the
// 'EndpointSpecific' array.
// The value of 'ForEndpointUrl' must be an 'EndpointUrl' configured in
// the publishednodes.json confguration file.
"ForEndpointUrl": "opc.tcp://<your_opcua_server>:<your_opcua_server_port>/<your_opcua_server_path>",
"EndpointUrl": {
// We overwrite the default behaviour and publish the
// endpoint URL in this case.
"Publish": true,
// We are only interested in the URL part following the 'opc.tcp://' prefix
// and define a group matching this.
"Pattern": "opc.tcp://(.*)"
},
"NodeId": {
// We are not interested in the configured 'NodeId' value,
// so we do not publish it.
"Publish": false
// No 'Pattern' key is specified here, so the 'NodeId' value will be
// taken as specified in the publishednodes configuration file.
},
"MonitoredItem": {
"ApplicationUri": {
// We already publish the endpoint URL, so we do not want
// the ApplicationUri of the MonitoredItem to be published.
"Publish": false
},
"DisplayName": {
"Publish": true
}
},
"Value": {
"Value": {
// The value of the node is important for us, everything else we
// are not interested in to keep the data ingest as small as possible.
"Publish": true
},
"SourceTimestamp": {
"Publish": false
},
"StatusCode": {
"Publish": false
},
"Status": {
"Publish": false
}
}
}
]
}
后续步骤
现在你已了解如何配置 OPC 发布服务器,建议下一步是了解如何运行 OPC 发布服务器。