Lua API guide ============= Load ``opcua.api`` to create clients, servers, and models. Examples use ``ua`` as the local name for the returned table. Start with the object that owns the operation: .. list-table:: :header-rows: 1 :widths: 31 69 * - Object - Entry point and reference * - Network client - :func:`ua.newClient` creates the client. See :doc:`client/client_api` for connection, session and service calls. * - Local server - :func:`ua.newServer` creates the server. See :doc:`server/server_api` for its lifecycle and local operations. * - Address space - :func:`ua.baseModel` loads the compiled standard model. :func:`ua.emptyModel` starts with the application namespace. See :doc:`model/model`, :doc:`model/browser` and :doc:`model/editor`. * - MQTT publisher or subscriber (full) - :func:`ua.newMqttClient` creates either. See :doc:`pubsub/mqtt` for broker connections, datasets and callbacks. Configuration and version ------------------------- .. function:: ua.clientConfig(config) ua.serverConfig(config) Validate a required Lua configuration table, fill its defaults **in place** and return the same table. Invalid settings raise a Lua error. Constructors call the matching function themselves. Call it directly to prepare a configuration for :func:`ua.baseModel` or :func:`ua.emptyModel`. See :ref:`Client configuration table` and :ref:`config_table`. .. function:: ua.versionValid(version) ua.assertVersion(version) Compare the required string with ``ua.Version.Version`` using exact equality. The first returns a boolean. The second raises an error on a mismatch and returns no value on success. ``ua.Version.BuildNumber`` is a separate string. See :doc:`profiles` to identify the build settings for that release. Identifiers, values and requests -------------------------------- * :ref:`node_id_type` covers ``ua.NodeId.fromString``, ``toString`` and ``isValid``. ``ua.NodeId.Null`` is the string ``"i=0"``. ``ua.NodeId.isNull(id)`` returns a boolean and accepts nil, a string or a parsed NodeId table. ``ua.createGuid()`` returns a new GUID string. * Use ``ua.AttributeId`` to select an attribute and ``ua.VariantType`` to identify the type of a value. :doc:`types` describes both the Lua value and its wire representation. ``ua.StatusCode.Good`` is numeric zero. * ``ua.newFolderParams(parentNodeId, name[, newNodeId])`` and ``ua.newVariableParams(parentNodeId, name, value[, newNodeId])`` return one AddNodes item table. Supply a parent NodeId string, a name string or a table with ``BrowseName`` (QualifiedName) and ``DisplayName`` (LocalizedText), and, for a variable, a DataValue table. Omit the new NodeId string to let the server assign one. See :doc:`client/adding_nodes`. * ``ua.newEventFilter(selectFields[, whereClause])`` builds a filter for an event monitored item. See :doc:`client/subscriptions` for field selection and predicates. Events require the full profile. * ``ua.parseUrl(url)`` accepts an endpoint URL string and returns a table with string ``scheme`` and ``host``, numeric ``port`` and string ``path``. An omitted port or path is nil. An invalid URL returns nil and a numeric status code. Identity token helpers ---------------------- Most clients can use the forms of :luamethod:`client:activateSession` described in :doc:`client/authentication`. When building a request by hand, the following functions return an ExtensionObject table with a ``TypeId`` string and a ``Body`` table. Each takes the server-advertised policy ID string first. .. list-table:: :header-rows: 1 :widths: 53 47 * - Function - Remaining arguments * - ``ua.createAnonymousToken(policyId)`` - None. * - ``ua.createUsernameToken(policyId, username, password, encryptionAlgorithm)`` - Username string, password bytes and optional encryption algorithm URI string. * - ``ua.createX509Token(policyId, cert)`` - User certificate bytes. * - ``ua.createIssuedToken(policyId, token, encryptionAlgorithm)`` - Issued token bytes and optional encryption algorithm URI string. These helpers wrap the supplied bytes. The higher-level session API handles token encryption and signatures according to the negotiated policy. Diagnostics and constants ------------------------- ``ua.printTable(name, value[, output])`` prints a labeled Lua value for inspection. Pass a string label and an optional function receiving one line at a time. See :doc:`logging` for runtime traces. The API exports numeric constants for node classes, attribute IDs, variant types, value ranks, browse masks and monitoring modes. It also exports NodeId strings in ``ua.DataTypeId`` and ``ua.ReferenceType``, security policy URIs in ``ua.SecurityPolicy``, and transport URIs in ``ua.TranportProfileUri`` (the spelling in Lua). :doc:`secure_policy` lists the policies implemented by each crypto backend. ``ua.crypto_engine`` names the backend initially chosen by the runtime (``"sharkssl"`` in BAS, ``"openssl"`` in stock Lua). ``ua.crypto`` holds its module table. ``ua.setCryptoEngine(name)`` accepts one of those strings, loads the named module and updates these two API fields, or raises an error. The channel policy implementation reads its backend directly from ``opcua.crypto``. Use the host's selected backend when configuring channels. Minimal builds omit the OpenSSL Lua module. ``ua.debug()`` starts the Lua debugger connection and requires the optional ``ldbgmon`` module. ``ua.ServerState`` contains numeric server states. ``ua.ServerProfile`` contains OPC UA profile URI constants. The build-time selectors for minimal and full are documented in :doc:`profiles`.