Skip to content

Commit 7662138

Browse files
committed
[UPD] rel v4.0.1 & AutoPublisher feature
1 parent c524da2 commit 7662138

10 files changed

Lines changed: 1470 additions & 42 deletions

File tree

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Changelog
22

3+
## [4.0.1] - 2026-04-09
4+
5+
### Added
6+
7+
- **Auto-publish.** New `auto_publish` config key in `session_manager` section. When enabled, the daemon automatically calls `publish()` for sessions with active subscriptions and dispatches PSR-14 events (`DataChangeReceived`, `EventNotificationReceived`, `AlarmActivated`, etc.) to Laravel's event system. No manual publish loop required — register listeners in your `EventServiceProvider` to handle notifications.
8+
- **Per-connection auto-connect.** New `auto_connect` config key per connection. When `true` (and `auto_publish` is enabled), the daemon auto-connects to the endpoint on startup and registers the subscriptions defined in the new `subscriptions` config key. Connections without `auto_connect` are managed imperatively as before.
9+
- **Declarative subscription config.** New `subscriptions` config key per connection. Define `monitored_items` and `event_monitored_items` directly in `config/opcua.php` — the daemon creates them automatically on startup. Each subscription supports `publishing_interval`, `max_keep_alive_count`, `lifetime_count`, `priority`, and per-item `sampling_interval`, `queue_size`, `client_handle`, `select_fields`.
10+
- **Event dispatcher injection into daemon.** `SessionCommand` resolves `EventDispatcherInterface` from the Laravel container and passes it to the daemon. All OPC UA client events (47 events) are dispatched through Laravel's event system when auto-publish is active.
11+
- `SessionCommand::buildAutoConnectConfig()` — reads connections with `auto_connect: true` and builds the daemon's auto-connect configuration.
12+
- `SessionCommand::mapToDaemonConfig()` — maps Laravel connection config keys (`security_policy`, `timeout`, `client_certificate`, etc.) to the daemon's internal format (`securityPolicy`, `opcuaTimeout`, `clientCertPath`, etc.).
13+
- `SessionCommand::resolveEventDispatcher()` — resolves `EventDispatcherInterface` from the Laravel container.
14+
- `SessionCommand::resolveSecurityPolicyUri()` and `resolveSecurityModeValue()` — helper methods for security config mapping.
15+
- New `.env` variable: `OPCUA_AUTO_PUBLISH`.
16+
- Auto-publish status displayed in the `opcua:session` startup table.
17+
- Auto-connect summary displayed before daemon startup when connections are configured.
18+
- Unit tests for `buildAutoConnectConfig`, `mapToDaemonConfig`, auto-publish flag passing, event dispatcher resolution, auto-connect filtering.
19+
- New documentation: [Auto-Publish & Monitoring](doc/10-auto-publish.md) with real-world industrial use case.
20+
21+
### Changed
22+
23+
- Updated dependency `php-opcua/opcua-session-manager` from `^4.0.0` to `^4.0.3` (required for auto-publish and auto-connect support).
24+
- `SessionCommand::createDaemon()` now accepts `?EventDispatcherInterface $clientEventDispatcher` and `bool $autoPublish` parameters.
25+
326
## [4.0.0] - 2026-04-7
427

528
### Changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Laravel integration for [OPC UA](https://opcfoundation.org/about/opc-technologie
2828
- **Laravel-native logging and caching** — your log channel and cache store are automatically injected into every OPC UA client
2929
- **All OPC UA operations** — browse, read, write, method calls, subscriptions, events, history, path resolution, type discovery
3030
- **PSR-14 events** — 47 dispatched events covering every OPC UA operation for observability and extensibility
31+
- **Auto-publish** — daemon monitors subscriptions automatically and dispatches PSR-14 events to your Laravel listeners — no manual publish loop
32+
- **Auto-connect** — define subscriptions in `config/opcua.php` per connection and the daemon sets them up at startup
3133
- **Trust store** — certificate trust management with configurable policies and auto-accept modes
3234
- **Write auto-detection** — omit the type parameter and let the client detect the correct OPC UA type automatically
3335

@@ -155,6 +157,46 @@ $client->deleteSubscription($sub->subscriptionId);
155157
$client->disconnect();
156158
```
157159

160+
### Auto-publish — no manual publish loop
161+
162+
With `auto_publish` enabled, the daemon handles subscriptions automatically. Just register Laravel event listeners:
163+
164+
```php
165+
// config/opcua.php
166+
'session_manager' => ['auto_publish' => true],
167+
168+
'connections' => [
169+
'plc-1' => [
170+
'endpoint' => 'opc.tcp://192.168.1.10:4840',
171+
'auto_connect' => true,
172+
'subscriptions' => [
173+
[
174+
'publishing_interval' => 500.0,
175+
'monitored_items' => [
176+
['node_id' => 'ns=2;s=Temperature', 'client_handle' => 1],
177+
],
178+
],
179+
],
180+
],
181+
],
182+
```
183+
184+
```php
185+
// EventServiceProvider
186+
use PhpOpcua\Client\Event\DataChangeReceived;
187+
188+
Event::listen(DataChangeReceived::class, function (DataChangeReceived $e) {
189+
DB::table('sensor_readings')->insert([
190+
'value' => $e->dataValue->getValue(),
191+
'client_handle' => $e->clientHandle,
192+
]);
193+
});
194+
```
195+
196+
```bash
197+
php artisan opcua:session # connects, subscribes, publishes — all automatic
198+
```
199+
158200
### Switch connections
159201

160202
```php
@@ -205,6 +247,8 @@ echo $mock->callCount('read'); // 1
205247
| **Read Metadata Cache** | Cached node metadata avoids redundant reads; refresh on demand with `read($nodeId, refresh: true)` |
206248
| **Trust Store** | `FileTrustStore` with configurable `TrustPolicy`, auto-accept modes, and certificate management |
207249
| **Type Discovery** | `discoverDataTypes()` auto-detects custom server structures |
250+
| **Auto-Publish** | Daemon auto-publishes for sessions with subscriptions, dispatches PSR-14 events to Laravel listeners |
251+
| **Auto-Connect** | Per-connection `auto_connect` with declarative `subscriptions` config — daemon sets up monitoring on startup |
208252
| **Subscription Management** | `createMonitoredItems()`, `modifyMonitoredItems()`, `setTriggering()`, `transferSubscriptions()` |
209253
| **MockClient** | Test without a server — register handlers, assert calls |
210254
| **Timeout & Retry** | Per-connection `timeout`, `auto_retry` via config or fluent API |
@@ -228,6 +272,7 @@ echo $mock->callCount('read'); // 1
228272
| 07 | [Security](doc/07-security.md) | Policies, modes, certificates, authentication |
229273
| 08 | [Testing](doc/08-testing.md) | MockClient, DataValue factories, unit and integration tests |
230274
| 09 | [Examples](doc/09-examples.md) | Complete code examples for all features |
275+
| 10 | [Auto-Publish & Monitoring](doc/10-auto-publish.md) | Auto-publish, auto-connect, event listeners, real-world use case |
231276

232277
## Testing
233278

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"require": {
1313
"php": "^8.2",
1414
"php-opcua/opcua-client": "^4.0.0",
15-
"php-opcua/opcua-session-manager": "^4.0.0",
15+
"php-opcua/opcua-session-manager": "^4.0.3",
1616
"psr/event-dispatcher": "^1.0",
1717
"illuminate/support": "^11.0|^12.0|^13.0",
1818
"illuminate/console": "^11.0|^12.0|^13.0",

config/opcua.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343
// Daemon client cache — uses a Laravel cache store.
4444
// Falls back to Laravel's default cache store when not specified.
4545
'cache_store' => env('OPCUA_CACHE_STORE', env('CACHE_STORE', 'file')),
46+
47+
// Auto-publish: when enabled, the daemon automatically calls publish()
48+
// for sessions with active subscriptions. Notifications are delivered
49+
// via PSR-14 events (DataChangeReceived, EventNotificationReceived,
50+
// AlarmActivated, etc.). Register listeners in your EventServiceProvider.
51+
'auto_publish' => env('OPCUA_AUTO_PUBLISH', false),
4652
],
4753

4854
/*
@@ -95,6 +101,30 @@
95101

96102
// Read metadata cache (v4.0+ — caches non-Value attribute reads)
97103
'read_metadata_cache' => env('OPCUA_READ_METADATA_CACHE',false),
104+
105+
// Auto-connect (optional) — when true and auto_publish is enabled,
106+
// the daemon connects to this endpoint on startup and registers
107+
// the subscriptions defined below.
108+
// 'auto_connect' => false,
109+
110+
// Subscriptions (optional) — auto-registered when auto_connect is true.
111+
// Each subscription can have monitored_items and/or event_monitored_items.
112+
// 'subscriptions' => [
113+
// [
114+
// 'publishing_interval' => 500.0,
115+
// 'max_keep_alive_count' => 5,
116+
// 'monitored_items' => [
117+
// ['node_id' => 'ns=2;s=Temperature', 'client_handle' => 1],
118+
// ],
119+
// 'event_monitored_items' => [
120+
// [
121+
// 'node_id' => 'i=2253',
122+
// 'client_handle' => 10,
123+
// 'select_fields' => ['EventId', 'EventType', 'SourceName', 'Time', 'Message', 'Severity'],
124+
// ],
125+
// ],
126+
// ],
127+
// ],
98128
],
99129

100130
],

0 commit comments

Comments
 (0)