100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Introduced AttributeEvent multithreaded processing BREAKING (#1205)
Rich Turner531c73ab8efe
diff --git a/.ci_cd/aws/cloudformation-create-ec2.yml b/.ci_cd/aws/cloudformation-create-ec2.ymlindex 8e5e18cee..083d79527 100644--- a/.ci_cd/aws/cloudformation-create-ec2.yml+++ b/.ci_cd/aws/cloudformation-create-ec2.yml@@ -43,6 +43,11 @@ Parameters:EFSDNS:Description: EFS DNS entry (for maps - IP address of EFS mount target in the same AZ ID)Type: String+# Old string based image ID+# ImageId:+# Description: AMI ID+# Type: String+# Default: "ami-062cd09187a31b743"ImageId:Description: AMI IDType: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'@@ -83,7 +88,7 @@ Parameters:DiskSize:Description: Root EBS volume size GBType: Number- Default: 16+ Default: 30HealthChecks:Description: Create health checksType: String@@ -246,7 +251,7 @@ Resources:"measurement": ["mem_used_percent"],- "metrics_collection_interval": 300+ "metrics_collection_interval": 600},"disk": {"drop_device": true,@@ -256,7 +261,7 @@ Resources:"resources": ["/"],- "metrics_collection_interval": 600+ "metrics_collection_interval": 1800}}},@@ -274,12 +279,12 @@ Resources:"or_rules_firing_count": "Count","or_camel_route_queue_size": "Count","or_camel_route_failed_total": "Count",+ "or_camel_route_failed_queue_full_total": "Count","or_camel_route_total": "Count","or_camel_route_seconds_sum": "Seconds","or_camel_route_seconds_count": "Count","executor_seconds_count": "Count","executor_seconds_sum": "Seconds",- "executor_queued_tasks": "Count","haproxy_server_current_sessions": "Count","haproxy_server_bytes_in_total": "Bytes","haproxy_server_bytes_out_total": "Bytes",@@ -293,7 +298,7 @@ Resources:"source_labels": [ "job" ],"label_matcher": "^manager$","dimensions": [- [ "instance_name" ]+ [ "instance_name","id","type" ]],"metric_selectors": ["^or_rules.*$"@@ -309,6 +314,16 @@ Resources:"^or_camel_route.*$"]},+ {+ "source_labels": [ "job" ],+ "label_matcher": "^manager$",+ "dimensions": [+ [ "instance_name" ]+ ],+ "metric_selectors": [+ "^or_camel_route_failed_queue_full_total$"+ ]+ },{"source_labels": [ "job" ],"label_matcher": "^manager$",@@ -316,8 +331,7 @@ Resources:[ "instance_name","name" ]],"metric_selectors": [- "^executor_seconds.*$",- "^executor_queued.*$"+ "^executor_seconds.*$"]},{@@ -364,7 +378,7 @@ Resources:content: !Sub |# Prometheus scrape config fileglobal:- scrape_interval: 1m+ scrape_interval: 5mscrape_timeout: 10sscrape_configs:- job_name: proxy@@ -393,13 +407,8 @@ Resources:instance_name: '${Host}' # DOESN'T WORK https://github.com/aws/amazon-cloudwatch-agent/issues/751metric_relabel_configs:- action: keep- source_labels: [__name__]- regex: '(executor_queued_tasks|or_camel_route_queue_size|or_camel_route_failed_total|or_camel_route_total|or_camel_route_seconds.*|or_rules_.*)'- - action: replace- source_labels: [name]- regex: '.+:\/\/(.+)\?.*'- replacement: '${!1}'- target_label: name+ source_labels: [__name__,routeId,name]+ regex: '(or_camel_route_failed_queue_full_total;;|or_camel_route_total;AttributeEvent-Router;|or_camel_route_seconds_count;AttributeEvent-Processor1;|or_camel_route_seconds_sum;AttributeEvent-Processor1;|or_camel_route_total;NotificationQueue;|or_camel_route_failed_handled_total;NotificationQueue;|or_rules_.*|executor_seconds_count;;RulesService|executor_seconds_sum;;RulesService)'- action: replace # THIS IS A WORKAROUND FOR BROKEN LABELS ABOVEsource_labels: [instance_name]regex: '(.*)'@@ -791,11 +800,6 @@ Resources:#!/bin/bash -xe# Install latest cfn scriptsyum -y install aws-cfn-bootstrap- #sed -i '/update_cmd/s/= .*/= security/' /etc/yum/yum-cron-hourly.conf- #sed -i '/update_messages/s/= .*/= no/' /etc/yum/yum-cron-hourly.conf- #sed -i '/apply_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf- #sed -i '/download_updates/s/= .*/= yes/' /etc/yum/yum-cron-hourly.conf- #sed -i '/download_updates/s/= .*/= no/' /etc/yum/yum-cron.conf# Install cloudwatch agentyum -y install amazon-cloudwatch-agent@@ -803,7 +807,7 @@ Resources:# Execute required config set/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource EC2Instance --configsets default --region ${AWS::Region}- # All done so signal success+ # All done so signal result/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackId} --resource EC2Instance --region ${AWS::Region}IPAddress:@@ -899,8 +903,8 @@ Resources:Namespace: CWAgentMetricName: disk_used_percentStatistic: Average- Period: 600- EvaluationPeriods: 3+ Period: 3600+ EvaluationPeriods: 1ComparisonOperator: GreaterThanThresholdThreshold: 90AlarmActions:@@ -947,229 +951,249 @@ Resources:DashboardName: !Join ["-", !Split [ '.', !Ref Host] ]DashboardBody: !Sub |{- "widgets": [- {- "height": 6,- "width": 6,- "y": 0,- "x": 0,- "type": "metric",- "properties": {- "metrics": [- [ "CWAgent-Prometheus", "executor_queued_tasks", "instance_name", "${Host}", "name", "ContainerExecutorService", { "label": "ContainerExecutorService" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "period": 300,- "title": "Thread Pool Queue Size",- "stat": "Average"- }- },- {- "height": 6,- "width": 6,- "y": 0,- "x": 6,- "type": "metric",- "properties": {- "metrics": [- [ { "expression": "m1 / m2", "label": "Avg Processing Time", "id": "e1" } ],- [ "CWAgent-Prometheus", "or_camel_route_queue_size", "routeId", "AttributeEventProcessor", "instance_name", "${Host}", { "id": "e2", "label": "Queued Events", "yAxis": "right" } ],- [ ".", "or_camel_route_seconds_sum", ".", ".", ".", ".", { "id": "m1", "visible": false, "stat": "Average" } ],- [ ".", "or_camel_route_seconds_count", ".", ".", ".", ".", { "id": "m2", "visible": false, "stat": "Average" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "stat": "Maximum",- "period": 300,- "title": "Attr Event Processing Time"- }- },- {- "height": 6,- "width": 6,- "y": 6,- "x": 6,- "type": "metric",- "properties": {- "metrics": [- [ { "expression": "RUNNING_SUM(m1)", "label": "Total", "id": "e1" } ],- [ { "expression": "RUNNING_SUM(m2)", "label": "Failed", "id": "e2" } ],- [ "CWAgent-Prometheus", "or_camel_route_total", "routeId", "AttributeEventProcessor", "instance_name", "${Host}", { "id": "m1", "visible": false } ],- [ "CWAgent-Prometheus", "or_camel_route_failed_total", "routeId", "AttributeEventProcessor", "instance_name", "${Host}", { "id": "m2", "visible": false } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "Attr Event Count",- "stat": "Maximum",- "period": 300- }- },- {- "height": 6,- "width": 6,- "y": 6,- "x": 0,- "type": "metric",- "properties": {- "metrics": [- [ { "expression": "m2 / m1", "label": "All", "id": "e1" } ],- [ "CWAgent-Prometheus", "or_rules_firing_count", "instance_name", "${Host}", { "id": "m1", "visible": false } ],- [ "CWAgent-Prometheus", "or_rules_firing_sum", "instance_name", "${Host}", { "id": "m2", "visible": false } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "stat": "Average",- "period": 300,- "title": "Rule Engine Firing Duration (Seconds)"- }- },- {- "height": 6,- "width": 6,- "y": 6,- "x": 12,- "type": "metric",- "properties": {- "metrics": [- [ "CWAgent-Prometheus", "haproxy_server_current_sessions", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT" } ],- [ "...", "manager_backend", ".", ".", { "label": "HTTP/WS" } ],- [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "Proxy Active Sessions",- "stat": "Maximum",- "period": 300- }- },- {- "height": 6,- "width": 6,- "y": 0,- "x": 12,- "type": "metric",- "properties": {- "metrics": [- [ "CWAgent-Prometheus", "haproxy_server_max_session_rate", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT" } ],- [ "...", "manager_backend", ".", ".", { "label": "HTTP/WS" } ],- [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "Proxy Session Rate",- "stat": "Average",- "period": 300- }- },- {- "height": 6,- "width": 6,- "y": 12,- "x": 12,- "type": "metric",- "properties": {- "metrics": [- [ "CWAgent-Prometheus", "haproxy_server_bytes_in_total", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT Bytes In" } ],- [ ".", "haproxy_server_bytes_out_total", ".", ".", ".", ".", ".", ".", { "label": "MQTT Bytes Out" } ],- [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak Bytes Out" } ],- [ ".", "haproxy_server_bytes_in_total", ".", ".", ".", ".", ".", ".", { "label": "Keycloak Bytes In" } ],- [ "...", "manager", ".", "manager_backend", ".", ".", { "label": "HTTP/WS Bytes In" } ],- [ ".", "haproxy_server_bytes_out_total", ".", ".", ".", ".", ".", ".", { "label": "HTTP/WS Bytes Out" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "Proxy I/O",- "stat": "Average",- "period": 300- }- },- {- "height": 6,- "width": 6,- "y": 12,- "x": 6,- "type": "metric",- "properties": {- "metrics": [- [ "CWAgent-Prometheus", "haproxy_server_http_responses_total", "server", "keycloak", "proxy", "keycloak_backend", "instance_name", "${Host}", "code", "5xx", { "label": "Keycloak 5xx" } ],- [ "...", "manager", ".", "manager_backend", ".", ".", ".", ".", { "label": "Manager 5xx" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "HTTP Error Responses",- "stat": "Maximum",- "period": 300+ "widgets": [+ {+ "height": 6,+ "width": 6,+ "y": 0,+ "x": 6,+ "type": "metric",+ "properties": {+ "metrics": [+ [ { "expression": "m11 / m12", "label": "Avg Processor 1 (s)", "id": "e1" } ],+ [ "CWAgent-Prometheus", "or_camel_route_seconds_sum", "routeId", "AttributeEvent-Processor1", "instance_name", "${Host}", { "id": "m11", "visible": false } ],+ [ ".", "or_camel_route_seconds_count", ".", ".", ".", ".", { "id": "m12", "visible": false, "stat": "Maximum" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "stat": "Average",+ "period": 300,+ "title": "Attr Event Processing Time",+ "yAxis": {+ "left": {+ "showUnits": false+ }}- },- {- "height": 6,- "width": 6,- "y": 6,- "x": 18,- "type": "metric",- "properties": {- "metrics": [- [ "AWS/EBS", "VolumeReadOps", "VolumeId", "vol-08af38ccf9207da16", { "label": "Reads" } ],- [ ".", "VolumeWriteOps", ".", ".", { "label": "Writes" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "EBS Operations",- "stat": "Average",- "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 0,+ "x": 0,+ "type": "metric",+ "properties": {+ "metrics": [+ [ { "expression": "m1/PERIOD(m1)", "label": "Success [/s]", "id": "e1", "region": "eu-west-1" } ],+ [ { "expression": "m2/PERIOD(m2)", "label": "Fail (Queue Full) [/s]", "id": "e2", "region": "eu-west-1" } ],+ [ "CWAgent-Prometheus", "or_camel_route_total", "routeId", "AttributeEvent-Router", "instance_name", "${Host}", { "id": "m1", "label": "Success", "visible": false } ],+ [ ".", "or_camel_route_failed_queue_full_total", "instance_name", "${Host}", { "id": "m2", "label": "Fail (Queue Full)", "visible": false } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "Attr Events Processed",+ "stat": "Sum",+ "period": 300,+ "yAxis": {+ "left": {+ "showUnits": false+ }}- },- {- "height": 6,- "width": 6,- "y": 12,- "x": 18,- "type": "metric",- "properties": {- "metrics": [- [ "AWS/EBS", "VolumeReadBytes", "VolumeId", "vol-08af38ccf9207da16", { "label": "Read" } ],- [ ".", "VolumeWriteBytes", ".", ".", { "label": "Write" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "title": "EBS Bytes",- "stat": "Average",- "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 6,+ "x": 0,+ "type": "metric",+ "properties": {+ "metrics": [+ [ { "expression": "m1 / m2", "label": "Max (s)", "id": "e1" } ],+ [ "CWAgent-Prometheus", "executor_seconds_sum", "instance_name", "${Host}", "name", "RulesService", { "id": "m1", "visible": false } ],+ [ ".", "executor_seconds_count", ".", ".", ".", ".", { "id": "m2", "visible": false } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "stat": "Maximum",+ "period": 300,+ "title": "Rule Engine Execution Time",+ "yAxis": {+ "left": {+ "showUnits": false+ }}- },- {- "height": 6,- "width": 6,- "y": 0,- "x": 18,- "type": "metric",- "properties": {- "metrics": [- [ "AWS/EC2", "EBSIOBalance%", "InstanceId", "${EC2Instance}", { "label": "EBS IO Balance %" } ],- [ ".", "EBSByteBalance%", ".", ".", { "label": "EBS Byte Balance %" } ],- [ ".", "CPUCreditBalance", ".", ".", { "yAxis": "right", "label": "CPU Credit Balance" } ],- [ ".", "CPUUtilization", ".", ".", { "label": "CPU Utilization %", "stat": "Maximum" } ],- [ "CWAgent", "mem_used_percent", ".", ".", "ImageId", "${ImageId}", "InstanceType", "${InstanceType}", { "label": "Memory Usage %", "stat": "Average" } ],- [ ".", "disk_used_percent", "path", "/", "InstanceId", "i-0c9e821c9865e70ea", "ImageId", "${ImageId}", "InstanceType", "${InstanceType}", "fstype", "xfs", { "label": "Disk Usage %", "stat": "Average" } ]- ],- "view": "timeSeries",- "stacked": false,- "region": "${AWS::Region}",- "stat": "Minimum",- "period": 300,- "title": "EC2 Stats"+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 6,+ "x": 12,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "CWAgent-Prometheus", "haproxy_server_current_sessions", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT" } ],+ [ "...", "manager_backend", ".", ".", { "label": "HTTP/WS" } ],+ [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "Proxy Active Sessions",+ "stat": "Sum",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 0,+ "x": 12,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "CWAgent-Prometheus", "haproxy_server_max_session_rate", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT" } ],+ [ "...", "manager_backend", ".", ".", { "label": "HTTP/WS" } ],+ [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "Proxy Session Rate",+ "stat": "Average",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 12,+ "x": 12,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "CWAgent-Prometheus", "haproxy_server_bytes_in_total", "server", "manager", "proxy", "mqtt", "instance_name", "${Host}", { "label": "MQTT Bytes In" } ],+ [ ".", "haproxy_server_bytes_out_total", ".", ".", ".", ".", ".", ".", { "label": "MQTT Bytes Out" } ],+ [ "...", "keycloak", ".", "keycloak_backend", ".", ".", { "label": "Keycloak Bytes Out" } ],+ [ ".", "haproxy_server_bytes_in_total", ".", ".", ".", ".", ".", ".", { "label": "Keycloak Bytes In" } ],+ [ "...", "manager", ".", "manager_backend", ".", ".", { "label": "HTTP/WS Bytes In" } ],+ [ ".", "haproxy_server_bytes_out_total", ".", ".", ".", ".", ".", ".", { "label": "HTTP/WS Bytes Out" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "Proxy I/O",+ "stat": "Sum",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 12,+ "x": 6,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "CWAgent-Prometheus", "haproxy_server_http_responses_total", "server", "keycloak", "proxy", "keycloak_backend", "instance_name", "${Host}", "code", "5xx", { "label": "Keycloak 5xx" } ],+ [ "...", "manager", ".", "manager_backend", ".", ".", ".", ".", { "label": "Manager 5xx" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "HTTP Error Responses",+ "stat": "Sum",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 6,+ "x": 18,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "AWS/EBS", "VolumeReadOps", "VolumeId", "vol-08af38ccf9207da16", { "label": "Reads" } ],+ [ ".", "VolumeWriteOps", ".", ".", { "label": "Writes" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "EBS Operations",+ "stat": "Sum",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 12,+ "x": 18,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "AWS/EBS", "VolumeReadBytes", "VolumeId", "vol-08af38ccf9207da16", { "label": "Read" } ],+ [ ".", "VolumeWriteBytes", ".", ".", { "label": "Write" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "title": "EBS Bytes",+ "stat": "Sum",+ "period": 300+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 0,+ "x": 18,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "AWS/EC2", "EBSIOBalance%", "InstanceId", "i-0c9e821c9865e70ea", { "label": "EBS IO Balance %" } ],+ [ ".", "EBSByteBalance%", ".", ".", { "label": "EBS Byte Balance %" } ],+ [ ".", "CPUCreditBalance", ".", ".", { "yAxis": "right", "label": "CPU Credit Balance" } ],+ [ ".", "CPUUtilization", ".", ".", { "label": "CPU Utilization %", "stat": "Maximum" } ],+ [ "CWAgent", "mem_used_percent", ".", ".", "ImageId", "ami-062cd09187a31b743", "InstanceType", "t4g.small", { "label": "Memory Usage %", "period": 900 } ],+ [ ".", "disk_used_percent", "path", "/", "InstanceId", "i-0c9e821c9865e70ea", "ImageId", "ami-062cd09187a31b743", "InstanceType", "t4g.small", "fstype", "xfs", { "label": "Disk Usage %", "period": 1800 } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "region": "${AWS::Region}",+ "stat": "Average",+ "period": 300,+ "title": "EC2 Stats"+ }+ },+ {+ "height": 6,+ "width": 6,+ "y": 6,+ "x": 6,+ "type": "metric",+ "properties": {+ "metrics": [+ [ "CWAgent-Prometheus", "or_camel_route_total", "instance_name", "${Host}", "routeId", "NotificationQueue", { "label": "Success" } ],+ [ ".", "or_camel_route_failed_total", ".", ".", ".", ".", { "label": "Fail" } ]+ ],+ "view": "timeSeries",+ "stacked": false,+ "title": "Notifications",+ "region": "${AWS::Region}",+ "stat": "Sum",+ "period": 300,+ "yAxis": {+ "left": {+ "showUnits": false+ }}}- ]+ }+ ]}diff --git a/.ci_cd/env/.env b/.ci_cd/env/.envindex d8b4e0148..75c2a17e4 100644--- a/.ci_cd/env/.env+++ b/.ci_cd/env/.env@@ -4,3 +4,5 @@ DAILY_BACKUP=falseENV_COMPOSE_FILE=profile/demo.ymlOR_SETUP_TYPE=demoOR_EMAIL_TO="[email protected]"+OR_SETUP_RUN_ON_RESTART=true+DAILY_RESTART=truediff --git a/.ci_cd/env/demo.env b/.ci_cd/env/demo.envindex 8d31c4ef0..badc232c4 100644--- a/.ci_cd/env/demo.env+++ b/.ci_cd/env/demo.env@@ -1,4 +1,2 @@OR_HOSTNAME=demo.openremote.appOR_ADDITIONAL_HOSTNAMES=demo.openremote.io-OR_SETUP_RUN_ON_RESTART=true-DAILY_RESTART=truediff --git a/.ci_cd/env/load1.env b/.ci_cd/env/load1.envnew file mode 100644index 000000000..0163a8aa2--- /dev/null+++ b/.ci_cd/env/load1.env@@ -0,0 +1,2 @@+DAILY_RESTART=false+OR_SETUP_TYPE=load1diff --git a/.ci_cd/env/staging.env b/.ci_cd/env/staging.envindex f6ccf71e9..ae6171c49 100644--- a/.ci_cd/env/staging.env+++ b/.ci_cd/env/staging.env@@ -1,4 +1,2 @@OR_HOSTNAME=staging.demo.openremote.appOR_ADDITIONAL_HOSTNAMES=staging.demo.openremote.io-OR_SETUP_RUN_ON_RESTART=true-DAILY_RESTART=truediff --git a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.javaindex bb882fac4..ec104ca31 100644--- a/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/AbstractProtocol.java@@ -20,13 +20,10 @@package org.openremote.agent.protocol;import org.apache.camel.ProducerTemplate;-import org.apache.camel.builder.RouteBuilder;import org.apache.camel.impl.DefaultCamelContext;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.timer.TimerService;import org.openremote.model.Container;-import org.openremote.model.PersistenceEvent;-import org.openremote.model.asset.Asset;import org.openremote.model.asset.agent.Agent;import org.openremote.model.asset.agent.AgentLink;import org.openremote.model.asset.agent.ConnectionStatus;@@ -35,16 +32,17 @@ import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.AttributeState;+import org.openremote.model.protocol.ProtocolAssetService;import org.openremote.model.protocol.ProtocolUtil;-import org.openremote.model.query.AssetQuery;import org.openremote.model.util.Pair;-import org.openremote.model.util.TextUtil;-import java.util.*;+import java.util.Collections;+import java.util.HashSet;+import java.util.Map;+import java.util.Set;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;-import java.util.function.Consumer;import static org.openremote.model.protocol.ProtocolUtil.hasDynamicWriteValue;import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;@@ -62,53 +60,26 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agentprotected ProtocolPredictedDatapointService predictedDatapointService;protected ProtocolDatapointService datapointService;protected T agent;+ protected final Object processorLock = new Object();public AbstractProtocol(T agent) {this.agent = agent;}+ @Override+ public void setAssetService(ProtocolAssetService assetService) {+ this.assetService = assetService;+ }+@Overridepublic void start(Container container) throws Exception {timerService = container.getService(TimerService.class);executorService = container.getExecutorService();- assetService = proxyAssetService(container.getService(ProtocolAssetService.class));predictedDatapointService = container.getService(ProtocolPredictedDatapointService.class);datapointService = container.getService(ProtocolDatapointService.class);messageBrokerContext = container.getService(MessageBrokerService.class).getContext();-- // TODO: Priority should each protocol instance have its' own camel route- try {- messageBrokerContext.addRoutes(new RouteBuilder() {- @Override- public void configure() throws Exception {- from(ACTUATOR_TOPIC)- .routeId("ProtocolInbound-" + getProtocolName() + getAgent().getId())- .process(exchange -> {- final Protocol<?> protocolInstance = exchange.getIn().getHeader(ACTUATOR_TOPIC_TARGET_PROTOCOL, Protocol.class);- if (protocolInstance != AbstractProtocol.this) {- return;- }- synchronized (this) {- AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);- Attribute<?> linkedAttribute = getLinkedAttributes().get(event.getAttributeRef());-- if (linkedAttribute == null) {- LOG.log(System.Logger.Level.INFO, () -> "Attempt to write to attribute that is not actually linked to this protocol '" + AbstractProtocol.this + "': " + event);- return;- }-- processLinkedAttributeWrite(linkedAttribute, event);- }- });- }- });-- doStart(container);-- } catch (Exception ex) {- throw new RuntimeException(ex);- }this.producerTemplate = container.getService(MessageBrokerService.class).getProducerTemplate();+ doStart(container);}@Override@@ -170,26 +141,29 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agentreturn linkedAttributes;}- final protected void processLinkedAttributeWrite(Attribute<?> attribute, AttributeEvent event) {- LOG.log(System.Logger.Level.TRACE, () -> "Processing linked attribute write on protocol '" + this + "': " + event);- AgentLink<?> agentLink = agent.getAgentLink(attribute);-- Pair<Boolean, Object> ignoreAndConverted = ProtocolUtil.doOutboundValueProcessing(- event.getAssetId(),- attribute,- agentLink,- event.getValue().orElse(null),- dynamicAttributes.contains(event.getAttributeRef()));-- if (ignoreAndConverted.key) {- LOG.log(System.Logger.Level.DEBUG, "Value conversion returned ignore so attribute will not write to protocol: " + event.getAttributeRef());- return;- }+ @Override+ public void processLinkedAttributeWrite(AttributeEvent event) {+ synchronized (processorLock) {+ LOG.log(System.Logger.Level.TRACE, () -> "Processing linked attribute write on protocol '" + this + "': " + event);+ AgentLink<?> agentLink = agent.getAgentLink(event);++ Pair<Boolean, Object> ignoreAndConverted = ProtocolUtil.doOutboundValueProcessing(+ event.getId(),+ event,+ agentLink,+ event.getValue().orElse(null),+ dynamicAttributes.contains(event.getRef()));++ if (ignoreAndConverted.key) {+ LOG.log(System.Logger.Level.DEBUG, "Value conversion returned ignore so attribute will not write to protocol: " + event.getRef());+ return;+ }- doLinkedAttributeWrite(attribute, agent.getAgentLink(attribute), event, ignoreAndConverted.value);+ doLinkedAttributeWrite(agent.getAgentLink(event), event, ignoreAndConverted.value);- if (agent.isUpdateOnWrite().orElse(false) || agentLink.getUpdateOnWrite().orElse(false)) {- updateLinkedAttribute(new AttributeState(event.getAttributeRef(), ignoreAndConverted.value));+ if (agent.isUpdateOnWrite().orElse(false) || agentLink.getUpdateOnWrite().orElse(false)) {+ updateLinkedAttribute(new AttributeState(event.getRef(), ignoreAndConverted.value));+ }}}@@ -208,18 +182,16 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent*/final protected void sendAttributeEvent(AttributeEvent event) {// Don't allow updating linked attributes with this mechanism as it could cause an infinite loop- if (linkedAttributes.containsKey(event.getAttributeRef())) {+ if (linkedAttributes.containsKey(event.getRef())) {LOG.log(System.Logger.Level.WARNING, () -> "Cannot update an attribute linked to the same protocol; use updateLinkedAttribute for that: " + event);return;}assetService.sendAttributeEvent(event);}- /**- * Update the value of a linked attribute. Call this to publish new sensor values. This will call- * {@link ProtocolUtil#doInboundValueProcessing} before sending on the sensor queue.- */- final protected void updateLinkedAttribute(final AttributeState state, long timestamp) {++ @Override+ final public void updateLinkedAttribute(final AttributeState state, long timestamp) {Attribute<?> attribute = linkedAttributes.get(state.getRef());if (attribute == null) {@@ -236,30 +208,21 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends AgentAttributeEvent attributeEvent = new AttributeEvent(new AttributeState(state.getRef(), ignoreAndConverted.value), timestamp);LOG.log(System.Logger.Level.TRACE, () -> "Sending linked attribute update: " + attributeEvent);- producerTemplate.sendBodyAndHeader(SENSOR_QUEUE, attributeEvent, Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, getProtocolName());- }-- /**- * Update the value of one of this {@link Protocol}s linked {@link Agent}'s {@link Attribute}s.- */- final protected void updateAgentAttribute(final AttributeState state) {- if (!agent.getAttributes().has(state.getRef().getName()) || !agent.getId().equals(state.getRef().getId())) {- LOG.log(System.Logger.Level.WARNING, () -> "Attempt to update non existent agent attribute or agent ID is incorrect: " + state);- return;- }- AttributeEvent attributeEvent = new AttributeEvent(state, timerService.getCurrentTimeMillis());- LOG.log(System.Logger.Level.TRACE, () -> "Sending protocol agent attribute update: " + attributeEvent);assetService.sendAttributeEvent(attributeEvent);}- /**- * Update the value of a linked attribute, with the current system time as event time see- * {@link #updateLinkedAttribute(AttributeState, long)} for more details.- */- final protected void updateLinkedAttribute(AttributeState state) {+ @Override+ final public void updateLinkedAttribute(final AttributeState state) {updateLinkedAttribute(state, timerService.getCurrentTimeMillis());}+ @Override+ public boolean onAgentAttributeChanged(AttributeEvent event) {+ // If event is for an agent attribute then we can try and handle it here in a generic way+ Agent<?,?,?> agent = getAgent();+ return agent.isConfigurationAttribute(event.getName());+ }+/*** Start this protocol instance*/@@ -291,107 +254,5 @@ public abstract class AbstractProtocol<T extends Agent<T, ?, U>, U extends Agent* (see {@link ProtocolUtil#doOutboundValueProcessing}). Protocol implementations should generally use the* processedValue but may also choose to use the original value for some purpose if required.*/- abstract protected void doLinkedAttributeWrite(Attribute<?> attribute, U agentLink, AttributeEvent event, Object processedValue);-- private ProtocolAssetService proxyAssetService(ProtocolAssetService protocolAssetService) {- return new ProtocolAssetService() {-- @Override- public <T extends Asset<?>> T mergeAsset(T asset) {- if (TextUtil.isNullOrEmpty(asset.getRealm())) {- asset.setRealm(getAgent().getRealm());- } else if (!Objects.equals(asset.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to merge asset into another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to merge asset into another realm");- }- return protocolAssetService.mergeAsset(asset);- }-- @Override- public boolean deleteAssets(String... assetIds) {- for (String assetId: assetIds) {- Asset<?> asset = protocolAssetService.findAsset(assetId);- if (asset != null) {- if (!Objects.equals(asset.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to delete asset from another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to delete asset from another realm");- }- }- }- return protocolAssetService.deleteAssets(assetIds);- }-- @Override- public <T extends Asset<?>> T findAsset(String assetId, Class<T> assetType) {- T asset = protocolAssetService.findAsset(assetId, assetType);- if (asset != null) {- if (!Objects.equals(asset.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to find asset from another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to find asset from another realm");- }- }- return asset;- }-- @Override- public <T extends Asset<?>> T findAsset(String assetId) {- T asset = protocolAssetService.findAsset(assetId);- if (asset != null) {- if (!Objects.equals(asset.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to find asset from another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to find asset from another realm");- }- }- return asset;- }-- @Override- public List<Asset<?>> findAssets(String assetId, AssetQuery assetQuery) {- List<Asset<?>> assets = protocolAssetService.findAssets(assetId, assetQuery);- for (Asset<?> asset : assets) {- if (!Objects.equals(asset.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to find asset from another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to find asset from another realm");- }- }- return assets;- }-- @Override- public void sendAttributeEvent(AttributeEvent attributeEvent) {- if (TextUtil.isNullOrEmpty(attributeEvent.getRealm())) {- attributeEvent.setRealm(getAgent().getRealm());- } else if (!Objects.equals(attributeEvent.getRealm(), getAgent().getRealm())) {- Protocol.LOG.warning("Protocol attempting to send attribute event to another realm: " + agent);- throw new IllegalArgumentException("Protocol attempting to send attribute event to another realm");- }- protocolAssetService.sendAttributeEvent(attributeEvent);- }-- @Override- public void subscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {- protocolAssetService.subscribeChildAssetChange(agentId, assetChangeConsumer);- }-- @Override- public void unsubscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {- protocolAssetService.unsubscribeChildAssetChange(agentId, assetChangeConsumer);- }-- @Override- public void init(Container container) throws Exception {- protocolAssetService.init(container);- }-- @Override- public void start(Container container) throws Exception {- protocolAssetService.start(container);- }-- @Override- public void stop(Container container) throws Exception {- protocolAssetService.stop(container);- }- };- }+ abstract protected void doLinkedAttributeWrite(U agentLink, AttributeEvent event, Object processedValue);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java b/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.javadeleted file mode 100644index 0ee800528..000000000--- a/agent/src/main/java/org/openremote/agent/protocol/ProtocolAssetService.java+++ /dev/null@@ -1,158 +0,0 @@-/*- * Copyright 2017, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <http://www.gnu.org/licenses/>.- */-package org.openremote.agent.protocol;--import org.openremote.model.PersistenceEvent;-import org.openremote.model.ContainerService;-import org.openremote.model.asset.Asset;-import org.openremote.model.asset.agent.Agent;-import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.query.AssetQuery;--import java.util.List;-import java.util.function.Consumer;-import java.util.function.Predicate;--/**- * Interface for protocols to perform limited asset related operations.- */-public interface ProtocolAssetService extends ContainerService {-- /**- * Options when merging and storing assets from protocols.- */- class MergeOptions {-- final protected String assignToUserName;- final protected Predicate<String> attributeNamesToEvaluate;- final protected Predicate<String> ignoredAttributeNames;- final protected Predicate<String> ignoredAttributeKeys;-- public MergeOptions(String assignToUserName) {- this(assignToUserName, null, null, null);- }-- public MergeOptions(Predicate<String> attributeNamesToEvaluate) {- this(null, attributeNamesToEvaluate, null, null);- }-- public MergeOptions(Predicate<String> ignoredAttributeNames, Predicate<String> ignoredAttributeKeys) {- this(null, null, ignoredAttributeNames, ignoredAttributeKeys);- }-- public MergeOptions(String assignToUserName, Predicate<String> ignoredAttributeKeys) {- this(assignToUserName, null, null, ignoredAttributeKeys);- }-- public MergeOptions(String assignToUserName, Predicate<String> attributeNamesToEvaluate, Predicate<String> ignoredAttributeNames, Predicate<String> ignoredAttributeKeys) {- this.assignToUserName = assignToUserName;- this.attributeNamesToEvaluate = attributeNamesToEvaluate;- this.ignoredAttributeNames = ignoredAttributeNames;- this.ignoredAttributeKeys = ignoredAttributeKeys;- }-- /**- * Assigns the merged asset to the given user, can be <code>null</code> to not assign the asset to a user. The- * {@link #mergeAsset} call returns <code>null</code> if the user doesn't exist or the asset couldn't be- * assigned.- */- public String getAssignToUserName() {- return assignToUserName;- }-- /**- * Compare existing and merged asset state before doing the actual storage merge. If only the attributes in this- * predicate have change, then perform the merge on storage. Ignores what {#link getIgnoredAttributeNames} has- * stored if attributeNamesToEvaluate is not null.- */- public Predicate<String> getAttributeNamesToEvaluate() {- return attributeNamesToEvaluate;- }-- /**- * Compare existing and merged asset state before doing the actual storage merge. If only the ignored attributes- * have changed, don't perform the merge on storage.- */- public Predicate<String> getIgnoredAttributeNames() {- return ignoredAttributeNames;- }-- /**- * Compare existing and merged asset state before doing the actual storage merge. If only the ignored keys of- * any attributes have changed, don't perform the merge on storage. If {#link getAttributeNamesToEvaluate} is- * not null, then ignoredAttributeKeys is ignored.- */- public Predicate<String> getIgnoredAttributeKeys() {- return ignoredAttributeKeys;- }- }-- /**- * Protocols may store assets in the context or update existing assets. A unique identifier must be set by the- * protocol implementor, and the parent must be the {@link Agent} associated with the requesting protocol instance.- * This operation stores transient or detached state and returns the current state. It will override any existing- * stored asset data, ignoring versions.- */- <T extends Asset<?>> T mergeAsset(T asset);-- /**- * Protocols may remove assets from the context store.- *- * @return <code>false</code> if the delete could not be performed (asset may have children?)- */- boolean deleteAssets(String...assetIds);-- /**- * Get asset of specified type from the store by ID.- */- <T extends Asset<?>> T findAsset(String assetId, Class<T> assetType);-- /**- * Get asset from the store by ID.- */- <T extends Asset<?>> T findAsset(String assetId);-- /**- * Get assets by an {@link org.openremote.model.query.AssetQuery}; can only access {@link Asset}s that are- * descendants of the specified {@link Asset}- */- List<Asset<?>> findAssets(String assetId, AssetQuery assetQuery);-- /**- * Protocols can send arbitrary attribute change events for regular processing.- */- void sendAttributeEvent(AttributeEvent attributeEvent);-- /**- * Subscribe to changes of {@link Asset}s that are descendants of the specified agent.- * <p>- * When an agent is unlinked from a protocol then all subscriptions will be automatically removed also; it is safe- * to call this method multiple times for the same agentId and assetChangeConsumer and only a single subscription- * would actually be created.- */- void subscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer);-- /**- * Unsubscribe from asset changes for the specified agent.- * <p>- * When an agent is unlinked from a protocol then all subscriptions will be automatically removed also.- */- void unsubscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer);-}diff --git a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.javaindex c43ea67c9..cc3040fda 100644--- a/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/artnet/ArtnetProtocol.java@@ -129,7 +129,7 @@ public class ArtnetProtocol extends AbstractNettyIOClientProtocol<ArtnetProtocol}@Override- protected ArtnetPacket createWriteMessage(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected ArtnetPacket createWriteMessage(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {// // TODO check for group later here// AttributeRef attributeRef = event.getAttributeRef();//@@ -144,7 +144,7 @@ public class ArtnetProtocol extends AbstractNettyIOClientProtocol<ArtnetProtocol// if(updatedLight != null) {// ArtnetLightState oldLightState = updatedLight.getLightState();// //UPDATE LIGHT VALUES (R,G,B FOR EXAMPLE)-// if(event.getAttributeRef().getAttributeName().equalsIgnoreCase("Values")) {+// if(event.getAttributeRef().getName().equalsIgnoreCase("Values")) {// Map<String, Integer> valuesToUpdate = new HashMap<>();// for(String requiredKey : updatedLight.getRequiredValues()) {// try {@@ -160,7 +160,7 @@ public class ArtnetProtocol extends AbstractNettyIOClientProtocol<ArtnetProtocol// updateLightStateInMemory(lightId, new ArtnetLightState(lightId, valuesToUpdate, oldLightState.getDim(), oldLightState.isEnabled()));// }// //UPDATE DIM-// else if(event.getAttributeRef().getAttributeName().equalsIgnoreCase("Dim")) {+// else if(event.getAttributeRef().getName().equalsIgnoreCase("Dim")) {// try {// JsonNode node = ValueUtil.JSON.readTree(processedValue.toJson());// int dimValue = node.asInt();@@ -170,7 +170,7 @@ public class ArtnetProtocol extends AbstractNettyIOClientProtocol<ArtnetProtocol// }// }// //UPDATE ENABLED/DISABLED-// else if(event.getAttributeRef().getAttributeName().equalsIgnoreCase("Switch")) {+// else if(event.getAttributeRef().getName().equalsIgnoreCase("Switch")) {// try{// JsonNode node = ValueUtil.JSON.readTree(processedValue.toJson());// boolean enabled = node.asBoolean();diff --git a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.javaindex 24cc7ccf6..dd8476be1 100644--- a/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/bluetooth/mesh/BluetoothMeshProtocol.java@@ -323,7 +323,7 @@ public class BluetoothMeshProtocol extends AbstractProtocol<BluetoothMeshAgent,}@Override- protected synchronized void doLinkedAttributeWrite(Attribute<?> attribute, BluetoothMeshAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected synchronized void doLinkedAttributeWrite(BluetoothMeshAgentLink agentLink, AttributeEvent event, Object processedValue) {if (meshNetwork == null) {return;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/HTTPProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/http/HTTPProtocol.javaindex a89261396..442ec868e 100644--- a/agent/src/main/java/org/openremote/agent/protocol/http/HTTPProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/http/HTTPProtocol.java@@ -36,17 +36,16 @@ import org.jboss.resteasy.specimpl.ResponseBuilderImpl;import org.openremote.agent.protocol.AbstractProtocol;import org.openremote.container.web.QueryParameterInjectorFilter;import org.openremote.container.web.WebTargetBuilder;+import org.openremote.model.Constants;import org.openremote.model.Container;import org.openremote.model.asset.agent.AgentLink;import org.openremote.model.asset.agent.ConnectionStatus;-import org.openremote.model.asset.agent.Protocol;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.AttributeState;import org.openremote.model.auth.OAuthGrant;import org.openremote.model.auth.UsernamePassword;-import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;@@ -83,7 +82,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;* <h1>Dynamic value injection</h1>* This allows the {@link HTTPAgentLink#getPath()}} and/or {@link AgentLink#getWriteValue()} to contain the linked* {@link Attribute} value when sending requests. To dynamically inject the attribute value use {@value- * Protocol#DYNAMIC_VALUE_PLACEHOLDER} as a placeholder and this will be dynamically replaced at request time.+ * Constants#DYNAMIC_VALUE_PLACEHOLDER} as a placeholder and this will be dynamically replaced at request time.* <h2>Path example</h2>* {@link HTTPAgentLink#getPath()} = "volume/set/{$value}" and request received to set attribute value to 100. Actual* path used for the request = "volume/set/100"@@ -161,9 +160,9 @@ public class HTTPProtocol extends AbstractProtocol<HTTPAgent, HTTPAgentLink> {paramNameAndValues.getValue() != null&& paramNameAndValues.getValue().stream()- .anyMatch(val -> val.contains(DYNAMIC_VALUE_PLACEHOLDER)));+ .anyMatch(val -> val.contains(Constants.DYNAMIC_VALUE_PLACEHOLDER)));- boolean dynamicPath = !TextUtil.isNullOrEmpty(path) && path.contains(DYNAMIC_VALUE_PLACEHOLDER);+ boolean dynamicPath = !TextUtil.isNullOrEmpty(path) && path.contains(Constants.DYNAMIC_VALUE_PLACEHOLDER);if (!dynamicPath) {requestTarget = createRequestTarget(path);}@@ -190,7 +189,7 @@ public class HTTPProtocol extends AbstractProtocol<HTTPAgent, HTTPAgentLink> {requestBuilder = requestTarget.request();} else {// This means that the path is dynamic- String path = this.path.replaceAll(DYNAMIC_VALUE_PLACEHOLDER_REGEXP, value);+ String path = this.path.replaceAll(Constants.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, value);requestBuilder = createRequestTarget(path).request();}@@ -426,9 +425,9 @@ public class HTTPProtocol extends AbstractProtocol<HTTPAgent, HTTPAgentLink> {}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, HTTPAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(HTTPAgentLink agentLink, AttributeEvent event, Object processedValue) {- HttpClientRequest request = requestMap.get(event.getAttributeRef());+ HttpClientRequest request = requestMap.get(event.getRef());if (request != null) {@@ -562,7 +561,7 @@ public class HTTPProtocol extends AbstractProtocol<HTTPAgent, HTTPAgentLink> {if (hexMode || binaryMode) {byte[] bytes = response.readEntity(byte[].class);- value = hexMode ? ProtocolUtil.bytesToHexString(bytes) : ProtocolUtil.bytesToBinaryString(bytes);+ value = hexMode ? ValueUtil.bytesToHexString(bytes) : ValueUtil.bytesToBinaryString(bytes);} else {value = response.readEntity(String.class);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.javaindex 9adb105dc..a9ce9f186 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractIOClientProtocol.java@@ -36,8 +36,8 @@ import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.asset.agent.Protocol;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.ValueUtil;import java.nio.charset.Charset;import java.util.ArrayList;@@ -75,14 +75,14 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtocoString.class,client,(msg, out) -> {- byte[] bytes = hexMode ? ProtocolUtil.bytesFromHexString(msg) : ProtocolUtil.bytesFromBinaryString(msg);+ byte[] bytes = hexMode ? ValueUtil.bytesFromHexString(msg) : ValueUtil.bytesFromBinaryString(msg);out.writeBytes(bytes);}));if (delimiters.length > 0) {ByteBuf[] byteDelimiters = Arrays.stream(delimiters)- .map(delim -> Unpooled.wrappedBuffer(hexMode ? ProtocolUtil.bytesFromHexString(delim) : ProtocolUtil.bytesFromBinaryString(delim)))+ .map(delim -> Unpooled.wrappedBuffer(hexMode ? ValueUtil.bytesFromHexString(delim) : ValueUtil.bytesFromBinaryString(delim))).toArray(ByteBuf[]::new);encodersDecoders.add(new DelimiterBasedFrameDecoder(maxLength, stripDelimiter, byteDelimiters));} else {@@ -96,7 +96,7 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco(byteBuf, messages) -> {byte[] bytes = new byte[byteBuf.readableBytes()];byteBuf.readBytes(bytes);- String msg = hexMode ? ProtocolUtil.bytesToHexString(bytes) : ProtocolUtil.bytesToBinaryString(bytes);+ String msg = hexMode ? ValueUtil.bytesToHexString(bytes) : ValueUtil.bytesToBinaryString(bytes);messages.add(msg);})@@ -161,13 +161,13 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, X agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(X agentLink, AttributeEvent event, Object processedValue) {- if (client == null || attribute == null) {+ if (client == null) {return;}- V message = createWriteMessage(attribute, agent.getAgentLink(attribute), event, processedValue);+ V message = createWriteMessage(agent.getAgentLink(event), event, processedValue);if (message == null) {LOG.fine("No message produced for attribute event so not sending to IO client '" + client.getClientUri() + "': " + event);@@ -211,5 +211,5 @@ public abstract class AbstractIOClientProtocol<T extends AbstractIOClientProtoco/*** Generate the actual message to send to the {@link IOClient} for this {@link AttributeEvent}*/- protected abstract V createWriteMessage(Attribute<?> attribute, X agentLink, AttributeEvent event, Object processedValue);+ protected abstract V createWriteMessage(X agentLink, AttributeEvent event, Object processedValue);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.javaindex 26a2715c5..8e36955f5 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIOClient.java@@ -473,16 +473,14 @@ public abstract class AbstractNettyIOClient<T, U extends SocketAddress> implemenif (connectionStatusConsumers.size() > 0) {LOG.finest("Notifying connection status consumers: count=" + connectionStatusConsumers.size());}- executorService.submit(() -> {- connectionStatusConsumers.forEach(- consumer -> {- try {- consumer.accept(connectionStatus);- } catch (Exception e) {- LOG.log(Level.INFO, "Connection status change handler threw an exception: " + getClientUri(), e);- }- });- });+ connectionStatusConsumers.forEach(+ consumer -> {+ try {+ consumer.accept(connectionStatus);+ } catch (Exception e) {+ LOG.log(Level.INFO, "Connection status change handler threw an exception: " + getClientUri(), e);+ }+ });}protected void setPermanentError(String message) {diff --git a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.javaindex 7e96e0bf4..75bf23184 100644--- a/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/knx/KNXProtocol.java@@ -137,10 +137,10 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implem}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, KNXAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(KNXAgentLink agentLink, AttributeEvent event, Object processedValue) {synchronized (attributeActionMap) {- Datapoint datapoint = attributeActionMap.get(event.getAttributeRef());+ Datapoint datapoint = attributeActionMap.get(event.getRef());if (datapoint == null) {LOG.fine("Attribute isn't linked to a KNX datapoint so cannot process write: " + event);@@ -150,7 +150,7 @@ public class KNXProtocol extends AbstractProtocol<KNXAgent, KNXAgentLink> implemconnection.sendCommand(datapoint, event.getValue());// We assume KNX actuator will send new status on relevant status group address which will be picked up by listener and updates the state again later- updateLinkedAttribute(event.getAttributeState());+ updateLinkedAttribute(event.getState());}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.javaindex 27e795dcf..1028572c5 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java@@ -121,7 +121,7 @@ public abstract class AbstractMailProtocol<T extends AbstractMailAgent<T, U, V>,}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, V agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(V agentLink, AttributeEvent event, Object processedValue) {// Not supported}diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.javaindex bcc0e662b..be3da6f84 100644--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTProtocol.java@@ -117,7 +117,7 @@ public class MQTTProtocol extends AbstractMQTTClientProtocol<MQTTProtocol, MQTTA}@Override- protected MQTTMessage<String> createWriteMessage(Attribute<?> attribute, MQTTAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected MQTTMessage<String> createWriteMessage(MQTTAgentLink agentLink, AttributeEvent event, Object processedValue) {Optional<String> topic = agentLink.getPublishTopic();if (!topic.isPresent()) {diff --git a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialProtocol.javaindex c079828e0..1cad3f350 100644--- a/agent/src/main/java/org/openremote/agent/protocol/serial/SerialProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/serial/SerialProtocol.java@@ -23,13 +23,11 @@ import io.netty.channel.ChannelHandler;import org.openremote.model.asset.agent.DefaultAgentLink;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.attribute.AttributeExecuteStatus;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.Pair;import org.openremote.model.util.ValueUtil;-import org.openremote.model.value.ValueType;import java.util.ArrayList;import java.util.List;@@ -94,20 +92,7 @@ public class SerialProtocol extends AbstractSerialProtocol<SerialProtocol, Seria}@Override- protected String createWriteMessage(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {-- if (ValueType.EXECUTION_STATUS.equals(attribute.getType())) {- AttributeExecuteStatus status = event.getValue()- .flatMap(ValueUtil::getString)- .flatMap(AttributeExecuteStatus::fromString)- .orElse(null);-- if (status != null && status != AttributeExecuteStatus.REQUEST_START) {- LOG.fine("Unsupported execution status: " + status);- return null;- }- }-+ protected String createWriteMessage(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {return ValueUtil.convert(processedValue, String.class);}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.javaindex 8ee1e030d..b881d8fb8 100644--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/SimulatorProtocol.java@@ -106,22 +106,22 @@ public class SimulatorProtocol extends AbstractProtocol<SimulatorAgent, Simulato}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, SimulatorAgentLink agentLink, AttributeEvent event, Object processedValue) {- if (replayMap.containsKey(event.getAttributeRef())) {- LOG.info("Attempt to write to linked attribute that is configured for value replay so ignoring: " + attribute);+ protected void doLinkedAttributeWrite(SimulatorAgentLink agentLink, AttributeEvent event, Object processedValue) {+ if (replayMap.containsKey(event.getRef())) {+ LOG.info("Attempt to write to linked attribute that is configured for value replay so ignoring: " + event.getRef());return;}// Assume write through and endpoint returned the value we sentLOG.finest("Write to linked attribute so simulating that the endpoint returned the written value");- updateSensor(event.getAttributeRef(), processedValue);+ updateSensor(event.getRef(), processedValue);}/*** Call this to simulate a sensor update*/public void updateSensor(AttributeEvent attributeEvent) {- updateSensor(attributeEvent.getAttributeRef(), attributeEvent.getValue().orElse(null), attributeEvent.getTimestamp());+ updateSensor(attributeEvent.getRef(), attributeEvent.getValue().orElse(null), attributeEvent.getTimestamp());}public void updateSensor(AttributeRef attributeRef, Object value) {@@ -132,9 +132,7 @@ public class SimulatorProtocol extends AbstractProtocol<SimulatorAgent, Simulato* Call this to simulate a sensor update using the specified timestamp*/public void updateSensor(AttributeRef attributeRef, Object value, long timestamp) {- AttributeState state = new AttributeState(attributeRef, value);-- updateLinkedAttribute(state, timestamp);+ updateLinkedAttribute(new AttributeState(attributeRef, value), timestamp);}public Map<AttributeRef, ScheduledFuture<?>> getReplayMap() {diff --git a/agent/src/main/java/org/openremote/agent/protocol/simulator/storage/StorageSimulatorProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/simulator/storage/StorageSimulatorProtocol.javaindex e1f524f94..5b69fd50e 100644--- a/agent/src/main/java/org/openremote/agent/protocol/simulator/storage/StorageSimulatorProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/simulator/storage/StorageSimulatorProtocol.java@@ -83,16 +83,15 @@ public class StorageSimulatorProtocol extends AbstractProtocol<StorageSimulatorAscheduleInit(assetId);}- @SuppressWarnings("unchecked")@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, StorageSimulatorAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(StorageSimulatorAgentLink agentLink, AttributeEvent event, Object processedValue) {// Power attribute is updated only by this protocol not by clients- if (attribute.getName().equals(POWER.getName())) {+ if (event.getName().equals(POWER.getName())) {return;}- updateLinkedAttribute(new AttributeState(event.getAttributeRef(), processedValue));+ updateLinkedAttribute(new AttributeState(event.getRef(), processedValue));}protected void updateStorageAsset(ElectricityStorageAsset storageAsset) {@@ -186,7 +185,7 @@ public class StorageSimulatorProtocol extends AbstractProtocol<StorageSimulatorAinitFuture.cancel(false);}initFutureMap.put(assetId, executorService.schedule(() -> {- updateStorageAsset(assetService.findAsset(assetId, ElectricityStorageAsset.class));+ updateStorageAsset(assetService.findAsset(assetId));initFutureMap.remove(assetId);}, 1, TimeUnit.SECONDS));}@@ -194,7 +193,7 @@ public class StorageSimulatorProtocol extends AbstractProtocol<StorageSimulatorAprotected ScheduledFuture<?> scheduleUpdate(String assetId) {return executorService.schedule(() -> {try {- updateStorageAsset(assetService.findAsset(assetId, ElectricityStorageAsset.class));+ updateStorageAsset(assetService.findAsset(assetId));} catch (Exception e) {LOG.log(Level.SEVERE, "Exception in " + getProtocolName(), e);setConnectionStatus(ConnectionStatus.ERROR);diff --git a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPProtocol.javaindex 05d1b4c2e..535f034b4 100644--- a/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/snmp/SNMPProtocol.java@@ -120,7 +120,7 @@ public class SNMPProtocol extends AbstractProtocol<SNMPAgent, SNMPAgentLink> {}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, SNMPAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(SNMPAgentLink agentLink, AttributeEvent event, Object processedValue) {// Nothing to do here}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/tcp/TCPProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tcp/TCPProtocol.javaindex 077fa19e0..e617a8f63 100644--- a/agent/src/main/java/org/openremote/agent/protocol/tcp/TCPProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/tcp/TCPProtocol.java@@ -94,7 +94,7 @@ public class TCPProtocol extends AbstractTCPClientProtocol<TCPProtocol, TCPAgent}@Override- protected String createWriteMessage(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected String createWriteMessage(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {return ValueUtil.convert(processedValue, String.class);}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriConnection.java b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriConnection.javaindex 3858a61b3..715411695 100644--- a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriConnection.java+++ b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriConnection.java@@ -158,20 +158,20 @@ public class TradfriConnection {if (device.isLight()){Light light = device.toLight();- if (event.getAttributeName().equals(LightAsset.BRIGHTNESS.getName())) {+ if (event.getName().equals(LightAsset.BRIGHTNESS.getName())) {int value = ValueUtil.getInteger(event.getValue()).orElse(0);light.setBrightness(convertBrightness(value, false));- } else if (event.getAttributeName().equals(LightAsset.ON_OFF.getName())) {+ } else if (event.getName().equals(LightAsset.ON_OFF.getName())) {light.setOn(ValueUtil.getBooleanCoerced(event.getValue()).orElse(false));- } else if (event.getAttributeName().equals(LightAsset.COLOUR_RGB.getName())) {+ } else if (event.getName().equals(LightAsset.COLOUR_RGB.getName())) {light.setColour(ValueUtil.convert(event.getValue(), ColourRGB.class));- } else if (event.getAttributeName().equals(LightAsset.COLOUR_TEMPERATURE.getName())) {+ } else if (event.getName().equals(LightAsset.COLOUR_TEMPERATURE.getName())) {light.setColourTemperature(ValueUtil.getInteger(event.getValue()).orElse(0));}}else if (device.isPlug()) {Plug plug = device.toPlug();- if (event.getAttributeName().equals(PlugAsset.ON_OFF.getName())) {+ if (event.getName().equals(PlugAsset.ON_OFF.getName())) {plug.setOn(ValueUtil.getBooleanCoerced(event.getValue()).orElse(false));}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.javaindex d573b6e7b..6e83628e8 100644--- a/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/tradfri/TradfriProtocol.java@@ -132,9 +132,9 @@ public class TradfriProtocol extends AbstractProtocol<TradfriAgent, DefaultAgent}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {- Device device = tradfriDevices.get(event.getAttributeRef().getId());+ Device device = tradfriDevices.get(event.getRef().getId());if (device != null) {tradfriConnection.controlDevice(device, event);@@ -150,7 +150,6 @@ public class TradfriProtocol extends AbstractProtocol<TradfriAgent, DefaultAgent// Find all existing child assets of this agent that have a deviceId attributeList<Asset<?>> childAssets = assetService.findAssets(- agent.getId(),new AssetQuery().attributeName(TradfriAsset.DEVICE_ID.getName()));List<String> obsoleteAssetIds = childAssets.stream()diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/UDPProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UDPProtocol.javaindex 537b4b19e..8058d877b 100644--- a/agent/src/main/java/org/openremote/agent/protocol/udp/UDPProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UDPProtocol.java@@ -91,7 +91,7 @@ public class UDPProtocol extends AbstractUDPProtocol<UDPProtocol, UDPAgent, Defa}@Override- protected String createWriteMessage(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected String createWriteMessage(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {return ValueUtil.convert(processedValue, String.class);}}diff --git a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.javaindex 34352401e..cda3dd63b 100644--- a/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/velbus/AbstractVelbusProtocol.java@@ -132,7 +132,7 @@ public abstract class AbstractVelbusProtocol<S extends AbstractVelbusProtocol<S,}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, VelbusAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(VelbusAgentLink agentLink, AttributeEvent event, Object processedValue) {// Get the device that this attribute is linked toint deviceAddress = getOrThrowAgentLinkProperty(agentLink.getDeviceAddress(), "device address");diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.javaindex e8a9e7ab8..dcd9f6e73 100644--- a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketAgentProtocol.java@@ -20,6 +20,10 @@package org.openremote.agent.protocol.websocket;import io.netty.channel.ChannelHandler;+import jakarta.ws.rs.client.Entity;+import jakarta.ws.rs.client.Invocation;+import jakarta.ws.rs.core.MultivaluedHashMap;+import jakarta.ws.rs.core.Response;import org.apache.http.HttpHeaders;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;@@ -30,7 +34,6 @@ import org.openremote.model.Container;import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.attribute.AttributeExecuteStatus;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.auth.OAuthGrant;import org.openremote.model.auth.UsernamePassword;@@ -41,10 +44,6 @@ import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.ValueType;-import jakarta.ws.rs.client.Entity;-import jakarta.ws.rs.client.Invocation;-import jakarta.ws.rs.core.MultivaluedHashMap;-import jakarta.ws.rs.core.Response;import java.net.URI;import java.net.URISyntaxException;import java.util.*;@@ -116,19 +115,7 @@ public class WebsocketAgentProtocol extends AbstractNettyIOClientProtocol<Websoc}@Override- protected String createWriteMessage(Attribute<?> attribute, WebsocketAgentLink agentLink, AttributeEvent event, Object processedValue) {-- if (ValueType.EXECUTION_STATUS.equals(attribute.getType())) {- boolean isRequestStart = event.getValue()- .flatMap(v -> ValueUtil.getValue(v, AttributeExecuteStatus.class))- .map(status -> status == AttributeExecuteStatus.REQUEST_START)- .orElse(false);- if (!isRequestStart) {- LOG.fine("Unsupported execution status: " + event);- return null;- }- }-+ protected String createWriteMessage(WebsocketAgentLink agentLink, AttributeEvent event, Object processedValue) {return ValueUtil.convert(processedValue, String.class);}diff --git a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.javaindex 7646907b0..a326c4e6f 100644--- a/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/zwave/ZWaveProtocol.java@@ -120,7 +120,7 @@ public class ZWaveProtocol extends AbstractProtocol<ZWaveAgent, ZWaveAgentLink>}@Override- protected synchronized void doLinkedAttributeWrite(Attribute<?> attribute, ZWaveAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected synchronized void doLinkedAttributeWrite(ZWaveAgentLink agentLink, AttributeEvent event, Object processedValue) {if (network == null) {return;}diff --git a/container/src/main/java/org/openremote/container/Container.java b/container/src/main/java/org/openremote/container/Container.javaindex 6fa8a879f..f369c287d 100644--- a/container/src/main/java/org/openremote/container/Container.java+++ b/container/src/main/java/org/openremote/container/Container.java@@ -27,13 +27,13 @@ import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;import io.micrometer.prometheus.PrometheusConfig;import io.prometheus.client.CollectorRegistry;import org.openremote.container.concurrent.ContainerScheduledExecutor;-import org.openremote.container.concurrent.ContainerThreads;import org.openremote.container.util.LogUtil;import org.openremote.model.ContainerService;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;import java.util.*;+import java.util.concurrent.CountDownLatch;import java.util.concurrent.ScheduledExecutorService;import java.util.logging.Handler;import java.util.logging.Logger;@@ -209,7 +209,23 @@ public class Container implements org.openremote.model.Container {*/public void startBackground() throws Exception {start();- waitingThread = ContainerThreads.startWaitingThread();+ waitingThread = startWaitingThread();+ }++ static Thread startWaitingThread() {+ Thread thread = new Thread("Container Waiting") {+ @Override+ public void run() {+ try {+ new CountDownLatch(1).await();+ } catch (InterruptedException ex) {+ // Ignore, thrown on shutdown+ }+ }+ };+ thread.setDaemon(false);+ thread.start();+ return thread;}@Overridediff --git a/container/src/main/java/org/openremote/container/concurrent/ContainerExecutor.java b/container/src/main/java/org/openremote/container/concurrent/ContainerExecutor.javaindex b89ebca41..0af7a23cf 100644--- a/container/src/main/java/org/openremote/container/concurrent/ContainerExecutor.java+++ b/container/src/main/java/org/openremote/container/concurrent/ContainerExecutor.java@@ -20,15 +20,15 @@package org.openremote.container.concurrent;import java.util.concurrent.*;--import static org.openremote.container.concurrent.ContainerThreads.DEFAULT_REJECTED_EXECUTION_HANDLER;-import static org.openremote.container.concurrent.ContainerThreads.logExceptionCause;+import java.util.logging.Level;+import java.util.logging.Logger;/*** Thread pool that adds logging for tasks that throw exceptions*/public class ContainerExecutor extends ThreadPoolExecutor {+ protected static final Logger LOG = Logger.getLogger(ContainerExecutor.class.getName());protected String name;/**@@ -38,7 +38,8 @@ public class ContainerExecutor extends ThreadPoolExecutor {int corePoolSize,int maximumPoolSize,long keepAliveSeconds,- int blockingQueueCapacity) {+ int blockingQueueCapacity,+ RejectedExecutionHandler rejectedExecutionHandler) {super(corePoolSize,maximumPoolSize,@@ -46,7 +47,12 @@ public class ContainerExecutor extends ThreadPoolExecutor {TimeUnit.SECONDS,blockingQueueCapacity == -1 ? new SynchronousQueue<>() : new ArrayBlockingQueue<>(blockingQueueCapacity),new ContainerThreadFactory(name),- DEFAULT_REJECTED_EXECUTION_HANDLER+ // Wrap rejected handler to add logging+ (r, executor) -> {+ // Log and discard+ LOG.info("Container thread pool '" + executor + "' rejected execution of " + r);+ rejectedExecutionHandler.rejectedExecution(r, executor);+ });this.name = name;@@ -58,6 +64,28 @@ public class ContainerExecutor extends ThreadPoolExecutor {logExceptionCause(runnable, throwable);}+ protected static void logExceptionCause(Runnable runnable, Throwable throwable) {+ if (throwable != null) {+ Throwable cause = unwrap(throwable);+ if (cause instanceof InterruptedException) {+ // Ignore this, might happen when we shutdownNow() the executor. We can't+ // log at this point as the logging system might be stopped already.+ return;+ }+ LOG.log(Level.WARNING, "Thread terminated unexpectedly executing: " +runnable.getClass(), throwable);+ }+ }++ protected static Throwable unwrap(Throwable throwable) throws IllegalArgumentException {+ if (throwable == null) {+ throw new IllegalArgumentException("Cannot unwrap null throwable");+ }+ for (Throwable current = throwable; current != null; current = current.getCause()) {+ throwable = current;+ }+ return throwable;+ }+@Overridepublic String toString() {return name + ":" + super.toString();diff --git a/container/src/main/java/org/openremote/container/concurrent/ContainerScheduledExecutor.java b/container/src/main/java/org/openremote/container/concurrent/ContainerScheduledExecutor.javaindex 43733691f..bf76f29ec 100644--- a/container/src/main/java/org/openremote/container/concurrent/ContainerScheduledExecutor.java+++ b/container/src/main/java/org/openremote/container/concurrent/ContainerScheduledExecutor.java@@ -21,22 +21,30 @@ package org.openremote.container.concurrent;import java.util.concurrent.RejectedExecutionHandler;import java.util.concurrent.ScheduledThreadPoolExecutor;-import java.util.concurrent.ThreadFactory;--import static org.openremote.container.concurrent.ContainerThreads.DEFAULT_REJECTED_EXECUTION_HANDLER;-import static org.openremote.container.concurrent.ContainerThreads.logExceptionCause;+import java.util.logging.Level;+import java.util.logging.Logger;/*** Thread pool that adds logging for tasks that throw exceptions*/public class ContainerScheduledExecutor extends ScheduledThreadPoolExecutor {+ protected static final Logger LOG = Logger.getLogger(ContainerScheduledExecutor.class.getName());+public ContainerScheduledExecutor(String name, int corePoolSize) {- this(new ContainerThreadFactory(name), DEFAULT_REJECTED_EXECUTION_HANDLER, corePoolSize);+ this(name, corePoolSize, new CallerRunsPolicy());}- public ContainerScheduledExecutor(ThreadFactory threadFactory, RejectedExecutionHandler rejectedHandler, int corePoolSize) {- super(corePoolSize, threadFactory, rejectedHandler);+ public ContainerScheduledExecutor(String name, int corePoolSize, RejectedExecutionHandler rejectedHandler) {+ super(+ corePoolSize,+ new ContainerThreadFactory(name),+ // Wrap rejected handler to add logging+ (r, executor) -> {+ // Log and discard+ LOG.info("Container scheduled thread pool '" + executor + "' rejected execution of " + r);+ rejectedHandler.rejectedExecution(r, executor);+ });}@Override@@ -45,4 +53,25 @@ public class ContainerScheduledExecutor extends ScheduledThreadPoolExecutor {logExceptionCause(runnable, throwable);}+ protected static void logExceptionCause(Runnable runnable, Throwable throwable) {+ if (throwable != null) {+ Throwable cause = unwrap(throwable);+ if (cause instanceof InterruptedException) {+ // Ignore this, might happen when we shutdownNow() the executor. We can't+ // log at this point as the logging system might be stopped already.+ return;+ }+ LOG.log(Level.WARNING, "Thread terminated unexpectedly executing: " +runnable.getClass(), throwable);+ }+ }++ protected static Throwable unwrap(Throwable throwable) throws IllegalArgumentException {+ if (throwable == null) {+ throw new IllegalArgumentException("Cannot unwrap null throwable");+ }+ for (Throwable current = throwable; current != null; current = current.getCause()) {+ throwable = current;+ }+ return throwable;+ }}diff --git a/container/src/main/java/org/openremote/container/concurrent/ContainerThreads.java b/container/src/main/java/org/openremote/container/concurrent/ContainerThreads.javadeleted file mode 100644index 1c3cdb83d..000000000--- a/container/src/main/java/org/openremote/container/concurrent/ContainerThreads.java+++ /dev/null@@ -1,78 +0,0 @@-/*- * Copyright 2017, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <http://www.gnu.org/licenses/>.- */-package org.openremote.container.concurrent;--import java.util.concurrent.CountDownLatch;-import java.util.concurrent.RejectedExecutionHandler;-import java.util.concurrent.ThreadPoolExecutor;-import java.util.logging.Level;-import java.util.logging.Logger;--public interface ContainerThreads {-- Logger LOG = Logger.getLogger(ContainerThreads.class.getName());-- RejectedExecutionHandler DEFAULT_REJECTED_EXECUTION_HANDLER = new ThreadPoolExecutor.DiscardPolicy() {- @Override- public void rejectedExecution(Runnable runnable, ThreadPoolExecutor threadPoolExecutor) {- // Log and discard- LOG.info("Container thread pool '" + threadPoolExecutor + "' rejected execution of " + runnable);- super.rejectedExecution(runnable, threadPoolExecutor);- }- };-- static Thread startWaitingThread() {- Thread thread = new Thread("Container Waiting") {- @Override- public void run() {- try {- new CountDownLatch(1).await();- } catch (InterruptedException ex) {- // Ignore, thrown on shutdown- }- }- };- thread.setDaemon(false);- thread.start();- return thread;- }-- static void logExceptionCause(Runnable runnable, Throwable throwable) {- if (throwable != null) {- Throwable cause = unwrap(throwable);- if (cause instanceof InterruptedException) {- // Ignore this, might happen when we shutdownNow() the executor. We can't- // log at this point as the logging system might be stopped already.- return;- }- LOG.log(Level.WARNING, "Thread terminated unexpectedly executing: " +runnable.getClass(), throwable);- }- }-- static Throwable unwrap(Throwable throwable) throws IllegalArgumentException {- if (throwable == null) {- throw new IllegalArgumentException("Cannot unwrap null throwable");- }- for (Throwable current = throwable; current != null; current = current.getCause()) {- throwable = current;- }- return throwable;- }-}diff --git a/container/src/main/java/org/openremote/container/message/MessageBrokerService.java b/container/src/main/java/org/openremote/container/message/MessageBrokerService.javaindex bce027618..b9ce12033 100644--- a/container/src/main/java/org/openremote/container/message/MessageBrokerService.java+++ b/container/src/main/java/org/openremote/container/message/MessageBrokerService.java@@ -20,6 +20,7 @@package org.openremote.container.message;import io.micrometer.core.instrument.MeterRegistry;+import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;import org.apache.camel.FluentProducerTemplate;import org.apache.camel.ProducerTemplate;import org.apache.camel.builder.DefaultErrorHandlerBuilder;@@ -48,6 +49,7 @@ import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.ThreadFactory;+import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.atomic.AtomicLong;import java.util.logging.Logger;@@ -84,8 +86,10 @@ public class MessageBrokerService implements ContainerService {1,Integer.MAX_VALUE,10,- -1);+ -1,+ new ThreadPoolExecutor.CallerRunsPolicy());+// Disabled as not very useful for SEDA components// if (meterRegistry != null) {// executorService = ExecutorServiceMetrics.monitor(meterRegistry, executorService, name("instrumented-delegate-"));// }@@ -101,15 +105,16 @@ public class MessageBrokerService implements ContainerService {profile.getPoolSize(),profile.getMaxPoolSize(),profile.getKeepAliveTime(),- profile.getMaxQueueSize()+ profile.getMaxQueueSize(),+ profile.getRejectedExecutionHandler());-// Disabled as not very useful for SEDA components-// if (meterRegistry != null) {-// String name = getExecutorName("Pool", threadFactory);-// name = "Pool".equals(name) ? profile.getId() : name;-// executorService = ExecutorServiceMetrics.monitor(meterRegistry, executorService, name(name));-// }+ // Want to instrument pools that use defaultThreadPool profile (ProducerTemplate and multiple consumer SEDA endpoints)+ if (meterRegistry != null && profile.isDefaultProfile() != null && profile.isDefaultProfile()) {+ String name = getExecutorName("Pool", threadFactory);+ name = "Pool".equals(name) ? profile.getId() : name;+ executorService = ExecutorServiceMetrics.monitor(meterRegistry, executorService, name(name));+ }return executorService;}@@ -118,7 +123,7 @@ public class MessageBrokerService implements ContainerService {public ScheduledExecutorService newScheduledThreadPool(ThreadPoolProfile profile, ThreadFactory threadFactory) {ScheduledExecutorService scheduledExecutorService = new ContainerScheduledExecutor(getExecutorName("ScheduledPool", threadFactory),- profile.getPoolSize()+ profile.getPoolSize(), profile.getRejectedExecutionHandler());// Disabled as not very useful for SEDA components@@ -132,8 +137,7 @@ public class MessageBrokerService implements ContainerService {}protected String getExecutorName(String name, ThreadFactory threadFactory) {- if (threadFactory instanceof CamelThreadFactory) {- CamelThreadFactory factory = (CamelThreadFactory) threadFactory;+ if (threadFactory instanceof CamelThreadFactory factory) {String camelName = factory.getName();camelName = camelName.contains("://") ? StringHelper.after(camelName, "://") : camelName;camelName = camelName.contains("?") ? StringHelper.before(camelName, "?") : camelName;@@ -176,6 +180,11 @@ public class MessageBrokerService implements ContainerService {context.getCamelContextExtension().setErrorHandlerFactory(new DefaultErrorHandlerBuilder());+ if (container.isDevMode()) {+ context.setMessageHistory(true);+ context.setSourceLocationEnabled(true);+ }+((SimpleRegistry)((DefaultRegistry)context.getRegistry()).getFallbackRegistry()).put("OpenRemote", Map.of(Container.class, container));context.addComponent("snmp", new SnmpComponent());diff --git a/container/src/main/java/org/openremote/container/web/QueryParameterInjectorFilter.java b/container/src/main/java/org/openremote/container/web/QueryParameterInjectorFilter.javaindex fda37b6a2..206292776 100644--- a/container/src/main/java/org/openremote/container/web/QueryParameterInjectorFilter.java+++ b/container/src/main/java/org/openremote/container/web/QueryParameterInjectorFilter.java@@ -19,7 +19,7 @@*/package org.openremote.container.web;-import org.openremote.model.asset.agent.Protocol;+import org.openremote.model.Constants;import jakarta.ws.rs.client.ClientRequestContext;import jakarta.ws.rs.client.ClientRequestFilter;@@ -35,9 +35,9 @@ import java.util.regex.Pattern;/*** A filter for injecting query parameters into the request URI. The query parameters are extracted from the request- * property {@link #QUERY_PARAMETERS_PROPERTY}. Any {@link Protocol#DYNAMIC_VALUE_PLACEHOLDER} in the query parameters+ * property {@link #QUERY_PARAMETERS_PROPERTY}. Any {@link Constants#DYNAMIC_VALUE_PLACEHOLDER} in the query parameters* will be replaced with the {@link #DYNAMIC_VALUE_PROPERTY} from the request. Any- * {@link Protocol#DYNAMIC_TIME_PLACEHOLDER_REGEXP} in the query parameters will be replaced with the current system+ * {@link Constants#DYNAMIC_TIME_PLACEHOLDER_REGEXP} in the query parameters will be replaced with the current system* time in the specified format with optional offset.*/@Provider@@ -54,7 +54,7 @@ public class QueryParameterInjectorFilter implements ClientRequestFilter {*/public static final String QUERY_PARAMETERS_PROPERTY = QueryParameterInjectorFilter.class.getName() + ".params";- public static final Pattern DYNAMIC_TIME_PATTERN = Pattern.compile(Protocol.DYNAMIC_TIME_PLACEHOLDER_REGEXP);+ public static final Pattern DYNAMIC_TIME_PATTERN = Pattern.compile(Constants.DYNAMIC_TIME_PLACEHOLDER_REGEXP);@SuppressWarnings("unchecked")@Override@@ -72,7 +72,7 @@ public class QueryParameterInjectorFilter implements ClientRequestFilter {queryParameters.forEach((name, values) -> {Object[] formattedValues = values.stream().map(v -> {- v = v.replaceAll(Protocol.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, dynamicValue);+ v = v.replaceAll(Constants.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, dynamicValue);Matcher matcher = DYNAMIC_TIME_PATTERN.matcher(v);@@ -87,7 +87,7 @@ public class QueryParameterInjectorFilter implements ClientRequestFilter {millisToAdd = Long.parseLong(matcher.group(2));}- v = v.replaceAll(Protocol.DYNAMIC_TIME_PLACEHOLDER_REGEXP, dateTimeFormatter.format(Instant.now().plusMillis(millisToAdd).atZone(ZoneId.systemDefault())));+ v = v.replaceAll(Constants.DYNAMIC_TIME_PLACEHOLDER_REGEXP, dateTimeFormatter.format(Instant.now().plusMillis(millisToAdd).atZone(ZoneId.systemDefault())));}return v;diff --git a/docker-compose.yml b/docker-compose.ymlindex 0d0b642fb..eedfea3f1 100644--- a/docker-compose.yml+++ b/docker-compose.yml@@ -24,7 +24,7 @@ services:- "80:80" # Needed for SSL generation using letsencrypt- "${OR_SSL_PORT:-443}:443"- "8883:8883"- #- "127.0.0.1:8404:8404" # Localhost metrics access+ - "127.0.0.1:8404:8404" # Localhost metrics accessvolumes:- proxy-data:/deploymentenvironment:@@ -62,8 +62,8 @@ services:depends_on:keycloak:condition: service_healthy- #ports:- # - "127.0.0.1:8405:8404" # Localhost metrics access+ ports:+ - "127.0.0.1:8405:8404" # Localhost metrics accessenvironment:OR_SETUP_TYPE:OR_ADMIN_PASSWORD:@@ -74,6 +74,7 @@ services:OR_EMAIL_X_HEADERS:OR_EMAIL_FROM:OR_EMAIL_ADMIN:+ OR_METRICS_ENABLED: ${OR_METRICS_ENABLED:-true}OR_HOSTNAME: ${OR_HOSTNAME:-localhost}OR_ADDITIONAL_HOSTNAMES: ${OR_ADDITIONAL_HOSTNAMES:-}OR_SSL_PORT: ${OR_SSL_PORT:--1}diff --git a/gradle.properties b/gradle.propertiesindex 5d178ec40..9b082a195 100644--- a/gradle.properties+++ b/gradle.properties@@ -8,11 +8,11 @@ javaxELVersion = 4.0.2slf4jVersion = 2.0.6guavaVersion = 31.1-jreundertowVersion = 2.3.4.Final-resteasyVersion = 6.2.6.Final+resteasyVersion = 6.2.7.FinaljbossLoggingAnnotationsVersion = 2.0.2.FinalkeycloakVersion = 18.0.2classgraphVersion=4.8.149-jacksonVersion = 2.15.2+jacksonVersion = 2.16.0groovyVersion = 3.0.10groovySandboxVersion = 4.0.0sqliteVersion = 3.34.0@@ -33,7 +33,7 @@ uuidGeneratorVersion = 4.0.1jSerialCommVersion = 2.7.0saxonHEVersion = 11.3jmxtermVersion = 1.0.0-nettyVersion = 4.1.79.Final+nettyVersion = 4.1.101.FinalflywaydbVersion = 9.15.1ical4jVersion = 3.2.6friendlyIdVersion = 1.0.1diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.propertiesindex 070cb702f..db9a6b825 100644--- a/gradle/wrapper/gradle-wrapper.properties+++ b/gradle/wrapper/gradle-wrapper.properties@@ -1,5 +1,5 @@distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/dists-distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip+distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zipzipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdiff --git a/manager/Dockerfile b/manager/Dockerfileindex a175ba940..a2563227e 100644--- a/manager/Dockerfile+++ b/manager/Dockerfile@@ -55,6 +55,7 @@ ENV OR_RULE_EVENT_EXPIRES ${OR_RULE_EVENT_EXPIRES:-PT1H}ENV OR_IDENTITY_PROVIDER ${OR_IDENTITY_PROVIDER:-keycloak}ENV OR_IDENTITY_SESSION_MAX_MINUTES ${OR_IDENTITY_SESSION_MAX_MINUTES:-1440}ENV OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES ${OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES:-2628000}+ENV OR_ATTRIBUTE_EVENT_THREADS ${OR_ATTRIBUTE_EVENT_THREADS}ENV OR_STORAGE_DIR ${OR_STORAGE_DIR:-/storage}ENV OR_JAVA_OPTS ${OR_JAVA_OPTS:--Xms500m -Xmx2g \-XX:NativeMemoryTracking=summary \diff --git a/manager/build.gradle b/manager/build.gradleindex 8d0cd542a..0867656e3 100644--- a/manager/build.gradle+++ b/manager/build.gradle@@ -25,13 +25,17 @@ dependencies {implementation "org.openjdk.nashorn:nashorn-core:$nashornVersion"implementation "org.apache.camel:camel-undertow:$camelVersion"- implementation ("io.netty:netty-codec-http:$nettyVersion") {- force = true+ implementation ("io.netty:netty-codec-http") {+ version {+ strictly "$nettyVersion"+ }}// Exposed via MQTT API- api ("io.netty:netty-codec-mqtt:$nettyVersion") {- force = true+ api ("io.netty:netty-codec-mqtt") {+ version {+ strictly "$nettyVersion"+ }}implementation("org.apache.activemq:artemis-mqtt-protocol:$activeMQVersion") {diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex a371d537b..63e0b52cf 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -21,13 +21,11 @@ package org.openremote.manager.agent;import jakarta.persistence.EntityManager;import org.apache.camel.builder.RouteBuilder;-import org.openremote.agent.protocol.ProtocolAssetService;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetProcessingException;import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.asset.AssetUpdateProcessor;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.security.ManagerIdentityService;@@ -36,20 +34,18 @@ import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetFilter;import org.openremote.model.asset.AssetTreeNode;-import org.openremote.model.asset.agent.Agent;-import org.openremote.model.asset.agent.AgentLink;-import org.openremote.model.asset.agent.ConnectionStatus;-import org.openremote.model.asset.agent.Protocol;+import org.openremote.model.asset.agent.*;import org.openremote.model.attribute.*;-import org.openremote.model.attribute.AttributeEvent.Source;import org.openremote.model.protocol.ProtocolAssetDiscovery;import org.openremote.model.protocol.ProtocolAssetImport;+import org.openremote.model.protocol.ProtocolAssetService;import org.openremote.model.protocol.ProtocolInstanceDiscovery;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.AttributePredicate;import org.openremote.model.query.filter.NameValuePredicate;-import org.openremote.model.query.filter.PathPredicate;+import org.openremote.model.query.filter.RealmPredicate;import org.openremote.model.query.filter.StringPredicate;import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;@@ -69,29 +65,142 @@ import static java.util.stream.Collectors.mapping;import static java.util.stream.Collectors.toList;import static org.openremote.container.persistence.PersistenceService.PERSISTENCE_TOPIC;import static org.openremote.container.persistence.PersistenceService.isPersistenceEventForEntityType;-import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_QUEUE;import static org.openremote.manager.gateway.GatewayService.isNotForGateway;-import static org.openremote.model.asset.agent.Protocol.ACTUATOR_TOPIC;-import static org.openremote.model.asset.agent.Protocol.SENSOR_QUEUE;import static org.openremote.model.attribute.Attribute.getAddedOrModifiedAttributes;-import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;-import static org.openremote.model.attribute.AttributeEvent.Source.*;import static org.openremote.model.value.MetaItemType.AGENT_LINK;/**- * Handles life cycle and communication with {@link Protocol}s.+ * This service's role is to communicate asset attribute writes to actuators, through protocol instances. It also+ * handles redeploying {@link Protocol} instances when an {@link Attribute} of the associated {@link Agent} is modified+ * either via Asset CRUD or just via an {@link AttributeEvent}.* <p>- * Finds all {@link Agent} assets and manages their {@link Protocol} instances.+ * Only an {@link AttributeEvent} for an {@link Attribute} containing a+ * {@link org.openremote.model.value.MetaItemType#AGENT_LINK} {@link MetaItem} will be intercepted here and passed to+ * the associated {@link Protocol} instance for processing; the event will not be committed to the DB and it is up to+ * the {@link Protocol} to generate a new {@link AttributeEvent} to signal that the action has been successfully handled.+ * <p>+ * Any {@link AttributeEvent} that originates from an {@link Agent} {@link Protocol} will not be consumed by the source+ * {@link Protocol} when it passes back through this service; this is to prevent infinite loops.*/-public class AgentService extends RouteBuilder implements ContainerService, AssetUpdateProcessor, ProtocolAssetService {+public class AgentService extends RouteBuilder implements ContainerService {++ /**+ * This is used as source of events generated by this service+ */+ protected static class AgentSourceRef extends AttributeRef {+ public AgentSourceRef(String id, String name) {+ super(id, name);+ }++ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (!(o instanceof AttributeRef that)) return false;+ return id.equals(that.getId()) && name.equals(that.getName());+ }+ }++ protected class AgentProtocolAssetService implements ProtocolAssetService {+ protected Agent<?,?,?> agent;++ public AgentProtocolAssetService(Agent<?,?,?> agent) {+ this.agent = agent;+ }++ @Override+ public <T extends Asset<?>> T mergeAsset(T asset) {+ Objects.requireNonNull(asset.getId());++ if (TextUtil.isNullOrEmpty(asset.getRealm())) {+ asset.setRealm(agent.getRealm());+ } else if (!Objects.equals(asset.getRealm(), agent.getRealm())) {+ String msg = "Protocol attempting to merge asset into another realm: " + agent;+ Protocol.LOG.warning(msg);+ throw new IllegalArgumentException(msg);+ }++ // TODO: Define access permissions for merged asset (user asset links inherit from parent agent?)+ LOG.fine("Merging asset with protocol-provided: " + asset);+ return assetStorageService.merge(asset, true);+ }++ @Override+ public boolean deleteAssets(String... assetIds) {+ for (String assetId: assetIds) {+ Asset<?> asset = findAsset(assetId);+ if (asset != null) {+ if (!Objects.equals(asset.getRealm(), agent.getRealm())) {+ Protocol.LOG.warning("Protocol attempting to delete asset from another realm: " + agent);+ throw new IllegalArgumentException("Protocol attempting to delete asset from another realm");+ }+ }+ }+ LOG.fine("Deleting protocol-provided: " + Arrays.toString(assetIds));+ return assetStorageService.delete(Arrays.asList(assetIds), false);+ }++ @SuppressWarnings("unchecked")+ @Override+ public <T extends Asset<?>> T findAsset(String assetId) {+ LOG.fine("Getting protocol-provided: " + assetId);+ T asset = (T)assetStorageService.find(assetId);+ if (asset != null) {+ if (!Objects.equals(asset.getRealm(), agent.getRealm())) {+ Protocol.LOG.warning("Protocol attempting to find asset from another realm: " + agent);+ throw new IllegalArgumentException("Protocol attempting to find asset from another realm");+ }+ }+ return asset;+ }++ @Override+ public List<Asset<?>> findAssets(AssetQuery assetQuery) {+ List<Asset<?>> assets = assetStorageService.findAll(assetQuery.realm(new RealmPredicate(agent.getRealm())));+ for (Asset<?> asset : assets) {+ if (!Objects.equals(asset.getRealm(), agent.getRealm())) {+ Protocol.LOG.warning("Protocol attempting to find asset from another realm: " + agent);+ throw new IllegalArgumentException("Protocol attempting to find asset from another realm");+ }+ }+ return assets;+ }++ @Override+ public void sendAttributeEvent(AttributeEvent attributeEvent) {+ if (TextUtil.isNullOrEmpty(attributeEvent.getRealm())) {+ attributeEvent.setRealm(agent.getRealm());+ } else if (!Objects.equals(attributeEvent.getRealm(), agent.getRealm())) {+ Protocol.LOG.warning("Protocol attempting to send attribute event to another realm: " + agent);+ throw new IllegalArgumentException("Protocol attempting to send attribute event to another realm");+ }+ AgentService.this.sendAttributeEvent(attributeEvent);+ }++ @Override+ public void subscribeChildAssetChange(Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {+ if (!getAgents().containsKey(agent.getId())) {+ LOG.fine("Attempt to subscribe to child asset changes with an invalid agent ID: " + agent.getId());+ return;+ }++ Set<Consumer<PersistenceEvent<Asset<?>>>> consumers = childAssetSubscriptions+ .computeIfAbsent(agent.getId(), (id) -> Collections.synchronizedSet(new HashSet<>()));+ consumers.add(assetChangeConsumer);+ }++ @Override+ public void unsubscribeChildAssetChange(Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {+ childAssetSubscriptions.computeIfPresent(agent.getId(), (id, consumers) -> {+ consumers.remove(assetChangeConsumer);+ return consumers.isEmpty() ? null : consumers;+ });+ }+ }private static final Logger LOG = Logger.getLogger(AgentService.class.getName());public static final int PRIORITY = MessageBrokerService.PRIORITY + 100; // Start quite late to ensure asset model etc. are initialised- protected TimerService timerService;- protected ManagerIdentityService identityService;protected AssetProcessingService assetProcessingService;protected AssetStorageService assetStorageService;- protected MessageBrokerService messageBrokerService;protected ClientEventService clientEventService;protected GatewayService gatewayService;protected ScheduledExecutorService executorService;@@ -101,20 +210,19 @@ public class AgentService extends RouteBuilder implements ContainerService, Asseprotected final Map<String, Set<Consumer<PersistenceEvent<Asset<?>>>>> childAssetSubscriptions = new ConcurrentHashMap<>();protected boolean initDone;protected Container container;+ protected final Object agentLock = new Object();@Overridepublic int getPriority() {return PRIORITY;}+ @SuppressWarnings("unchecked")@Overridepublic void init(Container container) throws Exception {this.container = container;- timerService = container.getService(TimerService.class);- identityService = container.getService(ManagerIdentityService.class);assetProcessingService = container.getService(AssetProcessingService.class);assetStorageService = container.getService(AssetStorageService.class);- messageBrokerService = container.getService(MessageBrokerService.class);clientEventService = container.getService(ClientEventService.class);gatewayService = container.getService(GatewayService.class);executorService = container.getExecutorService();@@ -132,6 +240,10 @@ public class AgentService extends RouteBuilder implements ContainerService, Assecontainer.getExecutorService()));+ assetProcessingService.addEventInterceptor(this::onAttributeEventIntercepted);++ clientEventService.addInternalSubscription(AttributeEvent.class, new AssetFilter<AttributeEvent>().setAssetClasses(Collections.singletonList(Agent.class)), this::onAgentAttributeEvent);+initDone = true;}@@ -151,7 +263,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Assepublic void stop(Container container) throws Exception {if (agentMap != null) {List<Agent<?, ?, ?>> agents = new ArrayList<>(agentMap.values());- agents.forEach(agent -> this.stopAgent(agent.getId()));+ agents.forEach(agent -> this.undeployAgent(agent.getId()));agentMap.clear();}protocolInstanceMap.clear();@@ -174,109 +286,36 @@ public class AgentService extends RouteBuilder implements ContainerService, AsseprocessAssetChange(persistenceEvent);}});-- // A protocol wants to write a new sensor value- from(SENSOR_QUEUE)- .routeId("ProtocolOutbound")- .filter(body().isInstanceOf(AttributeEvent.class))- .setHeader(HEADER_SOURCE, () -> SENSOR)- .to(ATTRIBUTE_EVENT_QUEUE);- }-- @Override- public <T extends Asset<?>> T mergeAsset(T asset) {- Objects.requireNonNull(asset.getId());-- // TODO: Define access permissions for merged asset (user asset links inherit from parent agent?)- LOG.fine("Merging asset with protocol-provided: " + asset);- return assetStorageService.merge(asset, true);- }-- @Override- public boolean deleteAssets(String...assetIds) {- LOG.fine("Deleting protocol-provided: " + Arrays.toString(assetIds));- return assetStorageService.delete(Arrays.asList(assetIds), false);- }-- @SuppressWarnings("unchecked")- @Override- public <T extends Asset<?>> T findAsset(String assetId) {- LOG.fine("Getting protocol-provided: " + assetId);- return (T)assetStorageService.find(assetId);- }-- @Override- public <T extends Asset<?>> T findAsset(String assetId, Class<T> assetType) {- LOG.fine("Getting protocol-provided: " + assetId);- return assetStorageService.find(assetId, assetType);- }-- @Override- public List<Asset<?>> findAssets(String assetId, AssetQuery assetQuery) {- if (TextUtil.isNullOrEmpty(assetId)) {- return Collections.emptyList();- }-- if (assetQuery == null) {- assetQuery = new AssetQuery();- }-- // Ensure agent ID is injected into each path predicate- if (assetQuery.paths != null) {- for (PathPredicate pathPredicate : assetQuery.paths) {- int len = pathPredicate.path.length;- pathPredicate.path = Arrays.copyOf(pathPredicate.path, len+1);- pathPredicate.path[len] = assetId;- }- } else {- assetQuery.paths(new PathPredicate(assetId));- }-- return assetStorageService.findAll(assetQuery);- }-- @Override- public void sendAttributeEvent(AttributeEvent attributeEvent) {- assetProcessingService.sendAttributeEvent(attributeEvent);}+ /**+ * Called when an {@link Agent} is modified in the DB+ */protected void processAgentChange(PersistenceEvent<Agent<?, ?, ?>> persistenceEvent) {LOG.finest("Processing agent persistence event: " + persistenceEvent.getCause());Agent<?, ?, ?> agent = persistenceEvent.getEntity();switch (persistenceEvent.getCause()) {- case CREATE -> {- agent = addReplaceAgent(agent);- doAgentInit(agent);- }- case UPDATE -> onAgentUpdated(agent);- case DELETE -> {- if (!removeAgent(agent.getId())) {- return;- }- // Unlink any attributes that have an agent link to this agent- stopAgent(agent.getId());- }+ case CREATE, UPDATE -> deployAgent(agent);+ case DELETE -> undeployAgent(agent.getId());}}- protected Agent<?,?,?> onAgentUpdated(Agent<?,?,?> agent) {-- // Old agent can be null if an update happens straight after the creation- if (!removeAgent(agent.getId())) {- return agent;- }+ /**+ * Deploy the {@link Agent} by creating a protocol instance, starting it and linking all attributes+ */+ protected void deployAgent(Agent<?,?,?> agent) {+ synchronized (agentLock) {+ undeployAgent(agent.getId());+ agent = addAgent(agent);- stopAgent(agent.getId());- agent = addReplaceAgent(agent);+ if (agent == null) {+ return;+ }- if (agent == null) {- return null;+ doAgentInit(agent);}-- doAgentInit(agent);- return agent;}/**@@ -310,7 +349,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse.collect(Collectors.toList());// Unlink obsolete or modified linked attributes- List<Attribute<?>> obsoleteOrModified = getAddedOrModifiedAttributes(newLinkedAttributes, oldLinkedAttributes).collect(toList());+ List<Attribute<?>> obsoleteOrModified = getAddedOrModifiedAttributes(newLinkedAttributes, oldLinkedAttributes).toList();getGroupedAgentLinkAttributes(obsoleteOrModified.stream(),attribute -> true@@ -323,12 +362,9 @@ public class AgentService extends RouteBuilder implements ContainerService, Asseattribute -> true).forEach((agent, attributes) -> linkAttributes(agent, asset.getId(), attributes));}- case DELETE -> {-- // Unlink any AGENT_LINK attributes from the referenced protocol+ case DELETE -> // Unlink any AGENT_LINK attributes from the referenced protocolgetGroupedAgentLinkAttributes(asset.getAttributes().stream(), attribute -> true).forEach((agent, attributes) -> unlinkAttributes(agent.getId(), asset.getId(), attributes));- }}notifyAgentAncestor(asset, persistenceEvent);@@ -363,6 +399,11 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse}}+ protected void sendAttributeEvent(AttributeEvent event) {+ // Set the source so we can ignore the intercept when the event comes back through the chain+ assetProcessingService.sendAttributeEvent(event, new AgentSourceRef(event.getId(), event.getName()));+ }+protected void doAgentInit(Agent<?,?,?> agent) {boolean isDisabled = agent.isDisabled().orElse(false);if (isDisabled) {@@ -374,75 +415,83 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse}protected void startAgent(Agent<?,?,?> agent) {- Protocol<?> protocol = null;- try {- protocol = agent.getProtocolInstance();+ synchronized (agentLock) {- LOG.fine("Starting protocol instance: " + protocol);- protocol.start(container);- protocolInstanceMap.put(agent.getId(), protocol);- LOG.fine("Started protocol instance:" + protocol);+ Protocol<?> protocol = null;- LOG.finest("Linking attributes to protocol instance: " + protocol);-- // Get all assets that have attributes with agent link meta for this agent- List<Asset<?>> assets = assetStorageService.findAll(- new AssetQuery()- .attributes(- new AttributePredicate().meta(- new NameValuePredicate(AGENT_LINK, new StringPredicate(agent.getId()), false, new NameValuePredicate.Path("id"))+ try {+ protocol = agent.getProtocolInstance();+ protocol.setAssetService(new AgentProtocolAssetService(agent));++ LOG.fine("Starting protocol instance: " + protocol);+ protocol.start(container);+ protocolInstanceMap.put(agent.getId(), protocol);+ LOG.fine("Started protocol instance:" + protocol);++ LOG.finest("Linking attributes to protocol instance: " + protocol);++ // Get all assets that have attributes with agent link meta for this agent+ List<Asset<?>> assets = assetStorageService.findAll(+ new AssetQuery()+ .attributes(+ new AttributePredicate().meta(+ new NameValuePredicate(AGENT_LINK, new StringPredicate(agent.getId()), false, new NameValuePredicate.Path("id"))+ ))- )- );-- LOG.finest("Found '" + assets.size() + "' asset(s) with attributes linked to this protocol instance: " + protocol);-- assets.forEach(- asset ->- getGroupedAgentLinkAttributes(- asset.getAttributes().stream(),- assetAttribute -> assetAttribute.getMetaValue(AGENT_LINK)- .map(agentLink -> agentLink.getId().equals(agent.getId()))- .orElse(false)- ).forEach((agnt, attributes) -> linkAttributes(agnt, asset.getId(), attributes))- );- } catch (Exception e) {- if (protocol != null) {- try {- protocol.stop(container);- } catch (Exception ignored) {+ );++ LOG.finest("Found '" + assets.size() + "' asset(s) with attributes linked to this protocol instance: " + protocol);++ assets.forEach(+ asset ->+ getGroupedAgentLinkAttributes(+ asset.getAttributes().stream(),+ assetAttribute -> assetAttribute.getMetaValue(AGENT_LINK)+ .map(agentLink -> agentLink.getId().equals(agent.getId()))+ .orElse(false)+ ).forEach((agnt, attributes) -> linkAttributes(agnt, asset.getId(), attributes))+ );+ } catch (Exception e) {+ if (protocol != null) {+ try {+ protocol.stop(container);+ } catch (Exception ignored) {+ }}+ protocolInstanceMap.remove(agent.getId());+ LOG.log(Level.SEVERE, "Failed to start protocol: " + protocol, e);+ sendAttributeEvent(new AttributeEvent(agent.getId(), Agent.STATUS.getName(), ConnectionStatus.ERROR));}- protocolInstanceMap.remove(agent.getId());- LOG.log(Level.SEVERE, "Failed to start protocol instance for agent: " + agent, e);- sendAttributeEvent(new AttributeEvent(agent.getId(), Agent.STATUS.getName(), ConnectionStatus.ERROR));}}- protected void stopAgent(String agentId) {- Protocol<?> protocol = protocolInstanceMap.get(agentId);+ protected void undeployAgent(String agentId) {+ synchronized (agentLock) {+ removeAgent(agentId);+ Protocol<?> protocol = protocolInstanceMap.get(agentId);- if (protocol == null) {- return;- }+ if (protocol == null) {+ return;+ }- Map<String, List<Attribute<?>>> groupedAttributes = protocol.getLinkedAttributes().entrySet().stream().collect(- Collectors.groupingBy(entry -> entry.getKey().getId(), mapping(Map.Entry::getValue, toList()))- );+ Map<String, List<Attribute<?>>> groupedAttributes = protocol.getLinkedAttributes().entrySet().stream().collect(+ Collectors.groupingBy(entry -> entry.getKey().getId(), mapping(Map.Entry::getValue, toList()))+ );- groupedAttributes.forEach((assetId, linkedAttributes) -> unlinkAttributes(agentId, assetId, linkedAttributes));+ groupedAttributes.forEach((assetId, linkedAttributes) -> unlinkAttributes(agentId, assetId, linkedAttributes));- // Stop the protocol instance- try {- protocol.stop(container);- } catch (Exception e) {- LOG.log(Level.SEVERE, "Protocol instance threw an exception whilst being stopped", e);- }+ // Stop the protocol instance+ try {+ protocol.stop(container);+ } catch (Exception e) {+ LOG.log(Level.SEVERE, "Protocol instance threw an exception whilst being stopped", e);+ }- // Remove child asset subscriptions for this agent- childAssetSubscriptions.remove(agentId);- protocolInstanceMap.remove(agentId);+ // Remove child asset subscriptions for this agent+ childAssetSubscriptions.remove(agentId);+ protocolInstanceMap.remove(agentId);+ }}protected void linkAttributes(Agent<?,?,?> agent, String assetId, Collection<Attribute<?>> attributes) {@@ -494,58 +543,103 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse}/**- * If this is an update from a sensor, or if the changed attribute is not linked to an agent, it's ignored.+ * Intercepts any {@link AttributeEvent} for an {@link Attribute} that has an+ * {@link org.openremote.model.value.MetaItemType#AGENT_LINK} {@link MetaItem} and passes it to the {@link Agent}'s+ * {@link Protocol#processLinkedAttributeWrite} method for handling.* <p>- * Otherwise push the update to the attributes' linked protocol to handle and prevent any further- * processing of this event by the processing chain. The protocol should raise sensor updates as- * required (i.e. the protocol is responsible for synchronising state with the database).+ * If the {@link AttributeEvent} originated from the {@link Agent} that the {@link Attribute} is linked to then it+ * is not intercepted.*/- @Override- public boolean processAssetUpdate(EntityManager entityManager,- Asset<?> asset,- Attribute<?> attribute,- Source source) throws AssetProcessingException {+ protected boolean onAttributeEventIntercepted(EntityManager em,+ AttributeEvent event) throws AssetProcessingException {- if (source == SENSOR || source == GATEWAY) {+ AgentSourceRef sourceRef = getSourceRef(event);++ // Don't intercept an event that was generated by this service for the same attribute+ if (sourceRef != null && sourceRef.equals(event.getRef())) {return false;}- AttributeEvent attributeEvent = new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().orElseGet(timerService::getCurrentTimeMillis));+ Optional<AgentLink> agentLinkOptional = event.getMetaValue(AGENT_LINK);- if (asset instanceof Agent) {+ // Never intercept events with no agent link+ return agentLinkOptional.map(agentLink -> {+ LOG.finest("Attribute event for agent linked attribute: agent=" + agentLink.getId() + ", ref=" + event.getRef());- // This is how we update the agent when an attribute event occurs on an agent's attribute by a client, this- // is not ideal as value is not committed to the DB and could in theory be consumed by another processor- // but we need to know the source which isn't available from the client event service.- // TODO: Expose event source to client event subscriptions- Agent<?, ?, ?> agent = getAgent(attributeEvent.getAssetId());+ if (event.isOutdated()) {+ // Don't process outdated events but still intercept them+ return true;+ }- if (agent != null) {+ Protocol<?> protocolInstance = getProtocolInstance(agentLink.getId());+ if (protocolInstance == null) {+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, "Agent protocol instance not found, agent may be disabled or has been deleted: attributeRef=" + event.getRef() + ", agentLink=" + agentLink);+ }+ try {+ protocolInstance.processLinkedAttributeWrite(event);+ } catch (Exception e) {+ AttributeWriteFailure failure = AttributeWriteFailure.UNKNOWN;+ String msg = e.getMessage();+ if (e instanceof AssetProcessingException assetProcessingException) {+ failure = assetProcessingException.getReason();+ }+ throw new AssetProcessingException(failure, "An exception occurred whilst the protocol was trying to process the attribute write request: agentLink=" + agentLink + ", msg=" + msg);+ }+ return true; // Processing complete, skip other processors+ }).orElse(false); // This is a regular attribute so allow the processing to continue+ }++ /**+ * Called when an {@link AttributeEvent} for an {@link Agent} is broadcast on the client event bus (i.e. the+ * attribute has been updated in the DB).+ * <p>+ * We use this to try and react to agent changes in a generic way by re-initialising the agent to simplify each+ * agent implementation.+ */+ // TODO: Queue up agent attribute events and do a single agent replacement+ protected void onAgentAttributeEvent(AttributeEvent event) {- // Update in memory agent- agent.addOrReplaceAttributes(attribute);+ synchronized (agentLock) {+ Agent<?, ?, ?> agent = getAgent(event.getId());- if (source == CLIENT && agent.isConfigurationAttribute(attribute.getName())) {- LOG.finest("Agent attribute event occurred from a client for an agent config attribute so updating: agent=" + agent.getId() + ", event=" + attributeEvent);- onAgentUpdated(agent);- }+ if (agent == null) {+ return;}- // Don't consume the event as we want the agent attribute to be updated in the DB- return false;+ // Check that the event has a newer timestamp than the existing agent attribute - if the attribute doesn't+ // exist on the agent either then assume the agent has been modified and attribute removed+ boolean eventOutdated = agent.getAttribute(event.getName()).flatMap(Attribute::getTimestamp)+ .map(timestamp -> event.getTimestamp() <= timestamp)+ .orElse(true);++ if (eventOutdated) {+ return;+ }++ // Update in memory agent+ agent.getAttribute(event.getName()).ifPresent(attr -> attr.setValue(event.getValue().orElse(null), event.getTimestamp()));++ Protocol<?> protocolInstance = getProtocolInstance(agent.getId());++ if (protocolInstance == null) {+ // Maybe agent was disabled and now isn't - use standard mechanism+ deployAgent(agent);+ return;+ }++ LOG.finer("Notifying protocol instance of an event for one of its agent attributes: " + event.getRef());+ if (protocolInstance.onAgentAttributeChanged(event)) {+ LOG.info("Protocol has requested recreation following agent attribute event: " + event.getRef());+ deployAgent(agent);+ }}+ }- return attribute.getMetaValue(AGENT_LINK)- .map(agentLink -> {- LOG.finest("Attribute write for agent linked attribute: agent=" + agentLink.getId() + ", asset=" + asset.getId() + ", attribute=" + attribute.getName());- // TODO: Priority remove actuator topic and call protocol instance directly- messageBrokerService.getFluentProducerTemplate()- .withBody(attributeEvent)- .withHeader(Protocol.ACTUATOR_TOPIC_TARGET_PROTOCOL, getProtocolInstance(agentLink.getId()))- .to(ACTUATOR_TOPIC)- .asyncSend();- return true; // Processing complete, skip other processors- }).orElse(false); // This is a regular attribute so allow the processing to continue+ protected static AgentSourceRef getSourceRef(AttributeEvent event) {+ if (event.getSource() instanceof AgentSourceRef agentSourceRef) {+ return agentSourceRef;+ }+ return null;}/**@@ -578,7 +672,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn getClass().getSimpleName() + "{" + "}";}- protected Agent<?, ?, ?> addReplaceAgent(Agent<?, ?, ?> agent) {+ protected Agent<?, ?, ?> addAgent(Agent<?, ?, ?> agent) {// Fully load agent asset if path and parent info not loadedif (agent.getPath() == null || (agent.getPath().length > 1 && agent.getParentId() == null)) {@@ -604,17 +698,18 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn getAgents().get(agentId);}- protected synchronized Map<String, Agent<?, ?, ?>> getAgents() {-- if (agentMap == null) {- agentMap = assetStorageService.findAll(- new AssetQuery().types(Agent.class)- )- .stream()- .filter(asset -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) == null)- .collect(Collectors.toConcurrentMap(Asset::getId, agent -> (Agent<?, ?, ?>) agent));+ protected Map<String, Agent<?, ?, ?>> getAgents() {+ synchronized (agentLock) {+ if (agentMap == null) {+ agentMap = assetStorageService.findAll(+ new AssetQuery().types(Agent.class)+ )+ .stream()+ .filter(asset -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) == null)+ .collect(Collectors.toConcurrentMap(Asset::getId, agent -> (Agent<?, ?, ?>) agent));+ }+ return agentMap;}- return agentMap;}public Protocol<?> getProtocolInstance(Agent<?, ?, ?> agent) {@@ -625,26 +720,6 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn protocolInstanceMap.get(agentId);}- @Override- public void subscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {- if (!getAgents().containsKey(agentId)) {- LOG.fine("Attempt to subscribe to child asset changes with an invalid agent ID: " +agentId);- return;- }-- Set<Consumer<PersistenceEvent<Asset<?>>>> consumers = childAssetSubscriptions- .computeIfAbsent(agentId, (id) -> Collections.synchronizedSet(new HashSet<>()));- consumers.add(assetChangeConsumer);- }-- @Override- public void unsubscribeChildAssetChange(String agentId, Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer) {- childAssetSubscriptions.computeIfPresent(agentId, (id, consumers) -> {- consumers.remove(assetChangeConsumer);- return consumers.isEmpty() ? null : consumers;- });- }-protected void notifyChildAssetChange(String agentId, PersistenceEvent<Asset<?>> assetPersistenceEvent) {childAssetSubscriptions.computeIfPresent(agentId, (id, consumers) -> {LOG.finest("Notifying child asset change consumers of change to agent child asset: Agent ID=" + id + ", Asset<?> ID=" + assetPersistenceEvent.getEntity().getId());diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.javaindex 7d30ec0ff..e38df9b9b 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetProcessingService.java@@ -19,16 +19,20 @@*/package org.openremote.manager.asset;-import jakarta.persistence.EntityManager;+import io.micrometer.core.instrument.Counter;+import io.micrometer.core.instrument.MeterRegistry;+import io.micrometer.core.instrument.Tags;+import jakarta.validation.ConstraintViolation;import org.apache.camel.Exchange;-import org.apache.camel.Processor;import org.apache.camel.builder.RouteBuilder;+import org.apache.camel.builder.RouteConfigurationBuilder;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.persistence.PersistenceService;-import org.openremote.container.security.AuthContext;import org.openremote.container.timer.TimerService;+import org.openremote.container.util.MapAccess;import org.openremote.manager.agent.AgentService;import org.openremote.manager.datapoint.AssetDatapointService;+import org.openremote.manager.event.AttributeEventInterceptor;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.event.EventSubscriptionAuthorizer;import org.openremote.manager.gateway.GatewayService;@@ -38,66 +42,29 @@ import org.openremote.model.Constants;import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.asset.Asset;-import org.openremote.model.asset.AssetResource;-import org.openremote.model.asset.agent.Protocol;-import org.openremote.model.attribute.*;-import org.openremote.model.attribute.AttributeEvent.Source;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.MetaItem;import org.openremote.model.security.ClientRole;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaItemType;-import org.openremote.model.value.ValueType;+import java.time.Instant;import java.util.*;-import java.util.function.Supplier;-import java.util.logging.Level;-import java.util.logging.Logger;+import java.util.concurrent.RejectedExecutionException;+import java.util.stream.IntStream;-import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;-import static org.openremote.model.attribute.AttributeEvent.Source.*;+import static org.openremote.manager.system.HealthService.OR_CAMEL_ROUTE_METRIC_PREFIX;import static org.openremote.model.attribute.AttributeWriteFailure.*;/**- * Receives {@link AttributeEvent} from {@link Source} and processes them.- * <dl>- * <dt>{@link Source#CLIENT}</dt>- * <dd><p>Client events published through event bus or sent by web service. These exchanges must contain an {@link AuthContext}- * header named {@link Constants#AUTH_CONTEXT}.</dd>- * <dt>{@link Source#INTERNAL}</dt>- * <dd><p>Events sent to {@link #ATTRIBUTE_EVENT_QUEUE} or through {@link #sendAttributeEvent} convenience method by processors.</dd>- * <dt>{@link Source#SENSOR}</dt>- * <dd><p>Protocol sensor updates sent to {@link Protocol#SENSOR_QUEUE}.</dd>- * </dl>- * NOTE: An attribute value can be changed during Asset<?> CRUD but this does not come through- * this route but is handled separately, see {@link AssetResource}. Any attribute values- * assigned during Asset<?> CRUD can be thought of as the attributes initial value.+ * Receives {@link AttributeEvent} from various sources (clients and services) and processes them.+ * An {@link AttributeEventInterceptor} can register to intercept the events using {@link #addEventInterceptor} at which+ * point they can decide whether or not to allow the event to continue being processed or not.* <p>- * The {@link AttributeEvent}s are first validated depending on their source, and if validation fails- * at any point then an {@link AssetProcessingException} will be logged as a warning with an- * {@link AttributeWriteFailure}.+ * Any {@link AttributeEvent}s that fail to be processed will generate an {@link AssetProcessingException} which will be+ * logged; there is currently no dead letter queue or retry processing.* <p>- * Once successfully validated a chain of {@link AssetUpdateProcessor}s is handling the update message:- * <ul>- * <li>{@link AgentService}</li>- * <li>{@link RulesService}</li>- * <li>{@link AssetStorageService}</li>- * <li>{@link AssetDatapointService}</li>- * </ul>- * <h2>Agent service processing logic</h2>- * <p>- * The agent service's role is to communicate asset attribute writes to actuators, through protocols.- * When the update messages' source is {@link Source#SENSOR}, the agent service ignores the message.- * The message will also be ignored if the updated attribute is not linked to an agent.- * <p>- * If the updated attribute has a valid agent link, an {@link AttributeEvent} is sent on the {@link Protocol#ACTUATOR_TOPIC},- * for execution on an actual device or service 'things'. The update is then considered complete, and no further processing- * is necessary. The update will not reach the rules engine or the database.- * <p>- * This means that a protocol implementation is responsible for producing a new {@link AttributeEvent} to- * indicate to the rules and database memory that the attribute value has/has not changed. The protocol should know- * best when to do this and it will vary from protocol to protocol; some 'things' might respond to an actuator write- * immediately with a new sensor read, or they might send a later "sensor value changed" message or both or neither- * (the actuator is "fire and forget"). The protocol must decide what the best course of action is based on the- * 'things' it communicates with and the transport layer it uses etc.* <h2>Rules Service processing logic</h2>* <p>* Checks if attribute has {@link MetaItemType#RULE_STATE} and/or {@link MetaItemType#RULE_EVENT} {@link MetaItem}s,@@ -114,14 +81,20 @@ import static org.openremote.model.attribute.AttributeWriteFailure.*;* is not stored in a time series DB of historical data, otherwise the value is stored. Then allows the message to* continue if the commit was successful.*/-@SuppressWarnings("unchecked")public class AssetProcessingService extends RouteBuilder implements ContainerService {+ public static final String ATTRIBUTE_EVENT_ROUTE_CONFIG_ID = "attributeEvent";public static final int PRIORITY = AssetStorageService.PRIORITY + 1000;- // Single threaded attribute event queue (event order has to be maintained)- public static final String ATTRIBUTE_EVENT_QUEUE = "seda://AttributeEventQueue?waitForTaskToComplete=IfReplyExpected&timeout=10000&purgeWhenStopping=true&discardIfNoConsumers=false&size=25000";- private static final Logger LOG = Logger.getLogger(AssetProcessingService.class.getName());- final protected List<AssetUpdateProcessor> processors = new ArrayList<>();+ public static final String ATTRIBUTE_EVENT_ROUTER_QUEUE = "seda://AttributeEventRouter?waitForTaskToComplete=IfReplyExpected&timeout=10000&purgeWhenStopping=false&discardIfNoConsumers=false&size=10000";+ public static final String OR_ATTRIBUTE_EVENT_THREADS = "OR_ATTRIBUTE_EVENT_THREADS";+ public static final int OR_ATTRIBUTE_EVENT_THREADS_DEFAULT = Runtime.getRuntime().availableProcessors();+ protected static final String EVENT_ROUTE_COUNT_HEADER = "EVENT_ROUTE_COUNT_HEADER";+ protected static final String EVENT_PROCESSOR_URI_PREFIX = "seda://AttributeEventProcessor";+ protected static final String EVENT_PROCESSOR_URI_SUFFIX = "?size=3000&timeout=10000";+ public static final String EVENT_PROCESSOR_ROUTE_ID_PREFIX = "AttributeEvent-Processor";+ private static final System.Logger LOG = System.getLogger(AssetProcessingService.class.getName());++ final protected List<AttributeEventInterceptor> eventInterceptors = new ArrayList<>();protected TimerService timerService;protected ManagerIdentityService identityService;protected PersistenceService persistenceService;@@ -135,38 +108,8 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerprotected ClientEventService clientEventService;// Used in testing to detect if initial/startup processing has completedprotected long lastProcessedEventTimestamp = System.currentTimeMillis();-- protected static Processor handleAssetProcessingException(Logger logger) {- return exchange -> {- AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);- Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);-- StringBuilder error = new StringBuilder();-- Source source = exchange.getIn().getHeader(HEADER_SOURCE, CLIENT, Source.class);- if (source != null) {- error.append("Error processing from ").append(source);- }-- String protocolName = exchange.getIn().getHeader(Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, String.class);- if (protocolName != null) {- error.append(" (protocol: ").append(protocolName).append(")");- }-- // TODO Better exception handling - dead letter queue?- if (exception instanceof AssetProcessingException processingException) {- error.append(" - ").append(processingException.getMessage());- error.append(": ").append(event.toString());- logger.warning(error.toString());- } else {- error.append(": ").append(event.toString());- logger.log(Level.WARNING, error.toString(), exception);- }-- // Make the exception available if MEP is InOut- exchange.getMessage().setBody(exception);- };- }+ protected int eventProcessingThreadCount;+ protected Counter queueFullCounter;@Overridepublic int getPriority() {@@ -186,8 +129,13 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerassetAttributeLinkingService = container.getService(AttributeLinkingService.class);messageBrokerService = container.getService(MessageBrokerService.class);clientEventService = container.getService(ClientEventService.class);+ MeterRegistry meterRegistry = container.getMeterRegistry();EventSubscriptionAuthorizer assetEventAuthorizer = AssetStorageService.assetInfoAuthorizer(identityService, assetStorageService);+ if (meterRegistry != null) {+ queueFullCounter = meterRegistry.counter(OR_CAMEL_ROUTE_METRIC_PREFIX + "_failed_queue_full", Tags.empty());+ }+clientEventService.addSubscriptionAuthorizer((requestedRealm, auth, subscription) -> {if (!subscription.isEventType(AttributeEvent.class)) {return false;@@ -209,28 +157,28 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSer// Check realm against userif (!identityService.getIdentityProvider().isRealmActiveAndAccessible(authContext,requestedRealm)) {- LOG.info("Realm is inactive, inaccessible or nonexistent: " + requestedRealm);+ LOG.log(System.Logger.Level.INFO, "Realm is inactive, inaccessible or nonexistent: " + requestedRealm);return false;}// Users must have write attributes roleif (authContext != null && !authContext.hasResourceRoleOrIsSuperUser(ClientRole.WRITE_ATTRIBUTES.getValue(),Constants.KEYCLOAK_CLIENT_ID)) {- LOG.fine("User doesn't have required role '" + ClientRole.WRITE_ATTRIBUTES + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ LOG.log(System.Logger.Level.DEBUG, "User doesn't have required role '" + ClientRole.WRITE_ATTRIBUTES + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());return false;}// Have to load the asset and attribute to perform additional checks - should permissions be moved out of the// asset model (possibly if the performance is determined to be not good enough)// TODO: Use a targeted query to retrieve just the info we need- Asset<?> asset = assetStorageService.find(attributeEvent.getAssetId());- Attribute<?> attribute = asset != null ? asset.getAttribute(attributeEvent.getAttributeName()).orElse(null) : null;+ Asset<?> asset = assetStorageService.find(attributeEvent.getId());+ Attribute<?> attribute = asset != null ? asset.getAttribute(attributeEvent.getName()).orElse(null) : null;- if (asset == null || !asset.hasAttribute(attributeEvent.getAttributeName())) {- LOG.info("Cannot authorize asset event as asset and/or attribute doesn't exist: " + attributeEvent.getAssetId());+ if (asset == null || !asset.hasAttribute(attributeEvent.getName())) {+ LOG.log(System.Logger.Level.INFO, () -> "Cannot authorize asset event as asset and/or attribute doesn't exist: " + attributeEvent.getRef());return false;} else if (!Objects.equals(requestedRealm, asset.getRealm())) {- LOG.info("Asset is not in the requested realm: requestedRealm=" + requestedRealm + ", ref=" + attributeEvent.getAttributeRef());+ LOG.log(System.Logger.Level.INFO, () -> "Asset is not in the requested realm: requestedRealm=" + requestedRealm + ", ref=" + attributeEvent.getRef());return false;}@@ -239,20 +187,20 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerif (identityService.getIdentityProvider().isRestrictedUser(authContext)) {// Must be asset linked to userif (!assetStorageService.isUserAsset(authContext.getUserId(),- attributeEvent.getAssetId())) {- LOG.fine("Restricted user is not linked to asset '" + attributeEvent.getAssetId() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ attributeEvent.getId())) {+ LOG.log(System.Logger.Level.DEBUG, () -> "Restricted user is not linked to asset '" + attributeEvent.getId() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());return false;}if (attribute == null || !attribute.getMetaValue(MetaItemType.ACCESS_RESTRICTED_WRITE).orElse(false)) {- LOG.fine("Asset attribute doesn't support restricted write on '" + attributeEvent.getAttributeRef() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());+ LOG.log(System.Logger.Level.DEBUG, () -> "Asset attribute doesn't support restricted write on '" + attributeEvent.getRef() + "': username=" + authContext.getUsername() + ", userRealm=" + authContext.getAuthenticatedRealmName());return false;}}} else {// Check attribute has public write flag for anonymous writeif (attribute == null || !attribute.hasMeta(MetaItemType.ACCESS_PUBLIC_WRITE)) {- LOG.fine("Asset doesn't support public write on '" + attributeEvent.getAttributeRef() + "': username=null");+ LOG.log(System.Logger.Level.DEBUG, () -> "Asset doesn't support public write on '" + attributeEvent.getRef() + "': username=null");return false;}}@@ -260,13 +208,69 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerreturn true;});- processors.add(gatewayService);- processors.add(agentService);- processors.add(rulesService);- processors.add(assetDatapointService);- processors.add(assetAttributeLinkingService);+ // Get dynamic route count for event processing (multithreaded event processing but guaranteeing events for the same asset end up in the same route)+ eventProcessingThreadCount = MapAccess.getInteger(container.getConfig(), OR_ATTRIBUTE_EVENT_THREADS, OR_ATTRIBUTE_EVENT_THREADS_DEFAULT);+ if (eventProcessingThreadCount < 1) {+ LOG.log(System.Logger.Level.WARNING, OR_ATTRIBUTE_EVENT_THREADS + " value " + eventProcessingThreadCount + " is less than 1; forcing to 1");+ eventProcessingThreadCount = 1;+ } else if (eventProcessingThreadCount > 20) {+ LOG.log(System.Logger.Level.WARNING, OR_ATTRIBUTE_EVENT_THREADS + " value " + eventProcessingThreadCount + " is greater than max value of 20; forcing to 20");+ eventProcessingThreadCount = 20;+ }++ // Add exception handling for attribute event processing that logs queue full exceptions and counts them+ messageBrokerService.getContext().addRoutesConfigurations(new RouteConfigurationBuilder() {+ @SuppressWarnings("unchecked")+ @Override+ public void configuration() throws Exception {+ routeConfiguration(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID)+ .onException(IllegalStateException.class, RejectedExecutionException.class, AssetProcessingException.class)+ .handled(true)+ .logExhausted(false)+ .logStackTrace(false)+ .process(exchange -> {+ AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);+ Exception exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);++ if (exception instanceof RejectedExecutionException || (exception instanceof IllegalStateException illegalStateException && "Queue full".equals(illegalStateException.getMessage()))) {+ exception = new AssetProcessingException(QUEUE_FULL, "Queue for this event is full");+ if (queueFullCounter != null) {+ queueFullCounter.increment();+ }+ }++ // Make the exception available if MEP is InOut+ exchange.getMessage().setBody(exception);++ if (!LOG.isLoggable(System.Logger.Level.WARNING)) {+ return;+ }++ StringBuilder error = new StringBuilder("Error processing from ");- container.getService(MessageBrokerService.class).getContext().addRoutes(this);+ if (event.getSource() != null) {+ error.append(event.getSource());+ } else {+ error.append("N/A");+ }++ error.append(": ").append(event.toStringWithValueType());++ if (exception instanceof AssetProcessingException processingException) {+ error.append(" ").append(" - ").append(processingException.getMessage());+ if (processingException.getReason() == ASSET_NOT_FOUND) {+ LOG.log(System.Logger.Level.DEBUG, error::toString);+ } else {+ LOG.log(System.Logger.Level.WARNING, error::toString);+ }+ } else {+ LOG.log(System.Logger.Level.WARNING, error::toString, exception);+ }+ });+ }+ });++ messageBrokerService.getContext().addRoutes(this);}@Override@@ -277,226 +281,183 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerpublic void stop(Container container) throws Exception {}- @SuppressWarnings("rawtypes")+ @SuppressWarnings({"rawtypes", "unchecked"})@Overridepublic void configure() throws Exception {- // Process attribute events- // TODO: Make SENDER much more granular (switch to microservices with RBAC)- // TODO: Unify logging of attribute updates- /* TODO This message consumer should be transactionally consistent with the database, this is currently not the case+ // All user authorisation checks MUST have been carried out before events reach this queue- Our "if I have not processed this message before" duplicate detection:-- - discard events with source time greater than server processing time (future events)- - discard events with source time less than last applied/stored event source time- - allow the rest (also events with same source time, order of application undefined)-- Possible improvements moving towards at-least-once:-- - Make AssetUpdateProcessor transactional with a two-phase commit API- - Replace at-most-once ClientEventService with at-least-once capable, embeddable message broker/protocol- - See pseudocode here: http://activemq.apache.org/should-i-use-xa.html- */- from(ATTRIBUTE_EVENT_QUEUE)- .routeId("AttributeEventProcessor")- .doTry()+ // Router is responsible for routing events to the same processor for a given asset ID, this allows for+ // multithreaded processing across assets+ from(ATTRIBUTE_EVENT_ROUTER_QUEUE)+ .routeId("AttributeEvent-Router")+ .routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID).process(exchange -> {AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);- // Set timestamp if not set+ if (event.getId() == null || event.getId().isEmpty())+ return; // Ignore events with no asset ID+ if (event.getName() == null || event.getName().isEmpty())+ return; // Ignore events with no attribute name+if (event.getTimestamp() <= 0) {+ // Set timestamp if not set+ event.setTimestamp(timerService.getCurrentTimeMillis());+ } else if (event.getTimestamp() > timerService.getCurrentTimeMillis()) {+ // Use system time if event time is in the future (clock issue)event.setTimestamp(timerService.getCurrentTimeMillis());}- if (LOG.isLoggable(Level.FINEST)) {- LOG.finest("Processing: " + event);- }- if (event.getAssetId() == null || event.getAssetId().isEmpty())- return;- if (event.getAttributeName() == null || event.getAttributeName().isEmpty())- return;- Source source = exchange.getIn().getHeader(HEADER_SOURCE, () -> null, Source.class);- if (source == null) {- throw new AssetProcessingException(MISSING_SOURCE);- }-- // Process the asset update in a database transaction, this ensures that processors- // will see consistent database state and we only commit if no processor failed. This- // still won't make this procedure consistent with the message queue from which we consume!- persistenceService.doTransaction(em -> {- Asset<?> asset = assetStorageService.find(em, event.getAssetId(), true);-- if (asset == null) {- if (source == SENSOR) {- // Fail silently as a protocol may have queued updates before the asset was deleted- return;- }-- throw new AssetProcessingException(ASSET_NOT_FOUND);- }-- Attribute<?> oldAttribute = asset.getAttribute(event.getAttributeName()).orElse(null);- if (oldAttribute == null) {- if (source == SENSOR) {- // Fail silently as a protocol may have queued updates before the attribute was modified/deleted- return;- }-- throw new AssetProcessingException(ATTRIBUTE_NOT_FOUND);- }+ exchange.getIn().setHeader(EVENT_ROUTE_COUNT_HEADER, getEventProcessingRouteNumber(event.getId()));+ })+ .toD(EVENT_PROCESSOR_URI_PREFIX + "${header." + EVENT_ROUTE_COUNT_HEADER + "}");++ // Create the event processor routes+ IntStream.rangeClosed(1, eventProcessingThreadCount).forEach(processorCount -> {+ String camelRouteURI = getEventProcessingRouteURI(processorCount);++ from(camelRouteURI)+ .routeId(EVENT_PROCESSOR_ROUTE_ID_PREFIX + processorCount)+ .routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID)+ .process(exchange -> {+ AttributeEvent event = exchange.getIn().getBody(AttributeEvent.class);+ LOG.log(System.Logger.Level.TRACE, () -> ">>> Attribute event processing start: processor=" + processorCount + ", event=" + event);+ long startMillis = System.currentTimeMillis();+ boolean processed = processAttributeEvent(event);++ // Need to record time here otherwise an infinite loop generated inside one of the interceptors means the timestamp+ // is not updated so tests can't then detect the problem.+ lastProcessedEventTimestamp = startMillis;- // Either use the timestamp of the event or set event time to processing time or (old event time + 1)- // We need a different timestamp for Attribute.equals() check- long oldEventTime = oldAttribute.getTimestamp().orElse(0L);- long eventTime = event.getTimestamp();- long processingTime = timerService.getCurrentTimeMillis();+ long processingMillis = System.currentTimeMillis() - startMillis;- if (eventTime > 0) {- // If it's less than previous event but within 10ms then just bump the time to old+1- if (oldEventTime - eventTime > 0 && oldEventTime - eventTime < 10) {- eventTime = oldEventTime + 1;- }+ if (processingMillis > 50) {+ LOG.log(System.Logger.Level.INFO, () -> "<<< Attribute event processing took a long time " + processingMillis + "ms: processor=" + processorCount + ", event=" + event);} else {- eventTime = Math.max(oldEventTime + 1, processingTime);- }-- // Check the last update timestamp of the attribute, ignoring any event that is older than last update- long finalEventTime = eventTime;- oldAttribute.getTimestamp().filter(t -> t >= 0 && finalEventTime < t).ifPresent(- lastStateTime -> {- throw new AssetProcessingException(- EVENT_OUTDATED,- "last asset state time: " + new Date(lastStateTime) + "/" + lastStateTime- + ", event time: " + new Date(finalEventTime) + "/" + finalEventTime);- }- );-- // For executable attributes, non-sensor sources can set a writable attribute execute status- if (oldAttribute.getType() == ValueType.EXECUTION_STATUS && source != SENSOR) {- Optional<AttributeExecuteStatus> status = event.getValue()- .flatMap(ValueUtil::getString)- .flatMap(AttributeExecuteStatus::fromString);-- // TODO: Make this mechanism more generic with an interface- if (status.isPresent() && !status.get().isWrite()) {- throw new AssetProcessingException(INVALID_ATTRIBUTE_EXECUTE_STATUS);- }+ LOG.log(System.Logger.Level.DEBUG, () -> "<<< Attribute event processed in " + processingMillis + "ms: processor=" + processorCount + ", event=" + event);}- // Type coercion- Object value = event.getValue().map(eventValue -> {- Class<?> attributeValueType = oldAttribute.getTypeClass();- return ValueUtil.getValueCoerced(eventValue, attributeValueType).orElseThrow(() -> {- LOG.info("Failed to coerce attribute event value into the correct value type: realm=" + event.getRealm() + ", attribute=" + event.getAttributeRef() + ", event value type=" + eventValue.getClass() + ", attribute value type=" + attributeValueType);- return new AssetProcessingException(INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE);- });-- }).orElse(null);-- // Create a copy of the attribute and set the new value and timestamp- Attribute updatedAttribute = oldAttribute.shallowClone();- updatedAttribute.setValue(value, eventTime);-- // Need to record time here otherwise an infinite loop generated inside one of the processors means the timestamp- // is not updated so tests can't then detect the problem.- lastProcessedEventTimestamp = System.currentTimeMillis();-- // Push through all processors- processAssetUpdate(em, asset, updatedAttribute, source);+ exchange.getIn().setBody(processed);});- })- .endDoTry()- .doCatch(AssetProcessingException.class)- .process(handleAssetProcessingException(LOG));+ });+ }++ public void addEventInterceptor(AttributeEventInterceptor eventInterceptor) {+ eventInterceptors.add(eventInterceptor);+ eventInterceptors.sort(Comparator.comparingInt(AttributeEventInterceptor::getPriority));}/**- * Send internal attribute change events into the {@link #ATTRIBUTE_EVENT_QUEUE}.+ * Send internal attribute change events into the {@link #ATTRIBUTE_EVENT_ROUTER_QUEUE}.*/public void sendAttributeEvent(AttributeEvent attributeEvent) {- sendAttributeEvent(attributeEvent, INTERNAL);+ sendAttributeEvent(attributeEvent, null);}- public void sendAttributeEvent(AttributeEvent attributeEvent, Source source) {+ /**+ * Send internal attribute change events into the {@link #ATTRIBUTE_EVENT_ROUTER_QUEUE}.+ */+ public void sendAttributeEvent(AttributeEvent attributeEvent, Object source) {+ attributeEvent.setSource(source);+// Set event source time if not already setif (attributeEvent.getTimestamp() <= 0) {attributeEvent.setTimestamp(timerService.getCurrentTimeMillis());}messageBrokerService.getFluentProducerTemplate()- .withHeader(HEADER_SOURCE, source).withBody(attributeEvent)- .to(ATTRIBUTE_EVENT_QUEUE)+ .to(ATTRIBUTE_EVENT_ROUTER_QUEUE).asyncSend();}/**- * This deals with single {@link Attribute} updates and pushes them through the chain where each processor is given- * the opportunity to completely consume the update or allow its progress to the next processor, see {@link- * AssetUpdateProcessor#processAssetUpdate}. If no processor completely consumed the update, the attribute will be- * stored in the database.+ * The {@link AttributeEvent} is passed to each registered {@link AttributeEventInterceptor} and if no interceptor+ * handles the event then the {@link Attribute} value is updated in the DB with the new event value and timestamp.*/- protected boolean processAssetUpdate(EntityManager em,- Asset<?> asset,- Attribute<?> attribute,- Source source) throws AssetProcessingException {+ protected boolean processAttributeEvent(AttributeEvent event) throws AssetProcessingException {- Supplier<String> attributeStrSupplier = () -> "Asset ID=" + asset.getId() + ", Asset name=" + asset.getName() + ", " + attribute;+ // TODO: Get asset lock so it cannot be modified during event processing+ persistenceService.doTransaction(em -> {- if (LOG.isLoggable(Level.FINEST)) {- LOG.finest(">>> Attribute event processing start: " + attributeStrSupplier.get());- }+ // TODO: Retrieve optimised DTO rather than whole asset+ Asset<?> asset = assetStorageService.find(em, event.getId(), true);- String consumerProcessor = null;- boolean complete = false;- long startMillis = System.currentTimeMillis();- StringBuilder processorTimings = new StringBuilder();+ if (asset == null) {+ throw new AssetProcessingException(ASSET_NOT_FOUND, "Asset may have been deleted before event could be processed or it never existed");+ }- for (AssetUpdateProcessor processor : processors) {- long processorStartMillis = System.currentTimeMillis();+ Attribute<Object> attribute = asset.getAttribute(event.getName()).orElseThrow(() ->+ new AssetProcessingException(ATTRIBUTE_NOT_FOUND, "Attribute may have been deleted before event could be processed or it never existed"));- if (LOG.isLoggable(Level.FINEST)) {- LOG.finest("==> Processor " + processor + " accepts: " + attributeStrSupplier.get());- }+ // Type coercion+ Object value = event.getValue().map(eventValue -> {+ Class<?> attributeValueType = attribute.getTypeClass();+ return ValueUtil.getValueCoerced(eventValue, attributeValueType).orElseThrow(() -> {+ String msg = "Event processing failed unable to coerce value into the correct value type: realm=" + event.getRealm() + ", attribute=" + event.getRef() + ", event value type=" + eventValue.getClass() + ", attribute value type=" + attributeValueType;+ return new AssetProcessingException(INVALID_VALUE, msg);+ });+ }).orElse(null);+ event.setValue(value);- try {- complete = processor.processAssetUpdate(em, asset, attribute, source);- processorTimings.append(processor).append("=").append(System.currentTimeMillis() - processorStartMillis).append("ms ");- } catch (AssetProcessingException ex) {- throw ex;- } catch (Throwable t) {- throw new AssetProcessingException(- PROCESSOR_FAILURE,- "processor '" + processor + "' threw an exception",- t- );- }- if (complete) {- consumerProcessor = processor.toString();- break;- }- }+ AttributeEvent enrichedEvent = new AttributeEvent(asset, attribute, event.getSource(), event.getValue().orElse(null), event.getTimestamp(), attribute.getValue().orElse(null), attribute.getTimestamp().orElse(0L));- if (!complete) {- if (!assetStorageService.updateAttributeValue(em, asset, attribute)) {- throw new AssetProcessingException(- STATE_STORAGE_FAILED, "database update failed, no rows updated"- );- }- }+ // Do standard JSR-380 validation on the event+ Set<ConstraintViolation<AttributeEvent>> validationFailures = ValueUtil.validate(enrichedEvent);- long processingMillis = System.currentTimeMillis() - startMillis;+ if (!validationFailures.isEmpty()) {+ String msg = "Event processing failed value failed constraint validation: realm=" + enrichedEvent.getRealm() + ", attribute=" + enrichedEvent.getRef() + ", event value type=" + enrichedEvent.getValue().map(v -> v.getClass().getName()).orElse("null") + ", attribute value type=" + enrichedEvent.getTypeClass();+ throw new AssetProcessingException(INVALID_VALUE, msg);+ }- if (processingMillis > 50) {- if (LOG.isLoggable(Level.INFO)) {- LOG.info("<<< Attribute event processing took a long time " + processingMillis + "ms: attribute=" + attributeStrSupplier.get() + ", consumer=" + consumerProcessor + ", timings=" + processorTimings);+ // TODO: Remove AttributeExecuteStatus+// // For executable attributes, non-sensor sources can set a writable attribute execute status+// if (attribute.getType() == ValueType.EXECUTION_STATUS && source != SENSOR) {+// Optional<AttributeExecuteStatus> status = event.getValue()+// .flatMap(ValueUtil::getString)+// .flatMap(AttributeExecuteStatus::fromString);+//+// if (status.isPresent() && !status.get().isWrite()) {+// throw new AssetProcessingException(INVALID_ATTRIBUTE_EXECUTE_STATUS);+// }+// }++ String interceptorName = null;+ boolean intercepted = false;++ for (AttributeEventInterceptor interceptor : eventInterceptors) {+ try {+ intercepted = interceptor.intercept(em, enrichedEvent);+ } catch (AssetProcessingException ex) {+ throw new AssetProcessingException(ex.getReason(), "Interceptor '" + interceptor + "' error=" + ex.getMessage());+ } catch (Throwable t) {+ throw new AssetProcessingException(+ INTERCEPTOR_FAILURE,+ "Interceptor '" + interceptor + "' uncaught exception error=" + t.getMessage(),+ t+ );+ }+ if (intercepted) {+ interceptorName = interceptor.getName();+ break;+ }}- } else {- if (LOG.isLoggable(Level.FINE)) {- LOG.fine("<<< Attribute event processed in " + processingMillis + "ms: attribute=" + attributeStrSupplier.get() + ", consumer=" + consumerProcessor);++ if (intercepted) {+ LOG.log(System.Logger.Level.TRACE, "Event intercepted: interceptor=" + interceptorName + ", ref=" + enrichedEvent.getRef() + ", source=" + enrichedEvent.getSource());+ } else {+ if (enrichedEvent.isOutdated()) {+ LOG.log(System.Logger.Level.INFO, () -> "Event is older than current attribute value so marking as outdated: ref=" + enrichedEvent.getRef() + ", event=" + Instant.ofEpochMilli(enrichedEvent.getTimestamp()) + ", previous=" + Instant.ofEpochMilli(enrichedEvent.getOldValueTimestamp()));+ // Generate an event for this so internal subscribers can act on it if needed+ clientEventService.publishEvent(new OutdatedAttributeEvent(enrichedEvent));+ } else if (!assetStorageService.updateAttributeValue(em, enrichedEvent)) {+ throw new AssetProcessingException(+ STATE_STORAGE_FAILED, "database update failed, no rows updated"+ );+ }}- }- return complete;+ });++ return true;}@Override@@ -504,4 +465,13 @@ public class AssetProcessingService extends RouteBuilder implements ContainerSerreturn getClass().getSimpleName() + "{" +'}';}++ protected int getEventProcessingRouteNumber(String assetId) {+ int charCode = Character.codePointAt(assetId, 0);+ return (charCode % eventProcessingThreadCount) + 1;+ }++ protected String getEventProcessingRouteURI(int routeNumber) {+ return EVENT_PROCESSOR_URI_PREFIX + routeNumber + EVENT_PROCESSOR_URI_SUFFIX;+ }}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.javaindex 1c4d95ab0..862f2a315 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetResourceImpl.java@@ -53,8 +53,7 @@ import java.util.logging.Logger;import java.util.stream.IntStream;import static jakarta.ws.rs.core.Response.Status.*;-import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_QUEUE;-import static org.openremote.model.attribute.AttributeEvent.Source.CLIENT;+import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_ROUTER_QUEUE;import static org.openremote.model.query.AssetQuery.Access;import static org.openremote.model.value.MetaItemType.*;@@ -435,10 +434,9 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourif (result.getFailure() != null) {status = switch (result.getFailure()) {- case ILLEGAL_SOURCE, INSUFFICIENT_ACCESS, INVALID_REALM -> FORBIDDEN;case ASSET_NOT_FOUND, ATTRIBUTE_NOT_FOUND -> NOT_FOUND;- case INVALID_AGENT_LINK, ILLEGAL_AGENT_UPDATE, INVALID_ATTRIBUTE_EXECUTE_STATUS, INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE ->- NOT_ACCEPTABLE;+ case INVALID_VALUE -> NOT_ACCEPTABLE;+ case QUEUE_FULL -> TOO_MANY_REQUESTS;default -> BAD_REQUEST;};}@@ -453,7 +451,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourreturn Arrays.stream(attributeStates).map(attributeState -> {AttributeEvent event = new AttributeEvent(attributeState);if (!clientEventService.authorizeEventWrite(getRequestRealmName(), getAuthContext(), event)) {- return new AttributeWriteResult(event.getAttributeRef(), AttributeWriteFailure.INSUFFICIENT_ACCESS);+ return new AttributeWriteResult(event.getRef(), AttributeWriteFailure.INSUFFICIENT_ACCESS);}return doAttributeWrite(event);}).toArray(AttributeWriteResult[]::new);@@ -551,16 +549,20 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourprotected AttributeWriteResult doAttributeWrite(AttributeEvent event) {AttributeWriteFailure failure = null;+ if (event.getTimestamp() <= 0) {+ event.setTimestamp(timerService.getCurrentTimeMillis());+ }+try {if (LOG.isLoggable(Level.FINE)) {LOG.fine("Write attribute value request: " + event);}- // Process synchronously+ // Process synchronously - need to directly use the ATTRIBUTE_EVENT_QUEUE as the client inbound queue+ // has multiple consumers and so doesn't support In/Out MEPObject result = messageBrokerService.getFluentProducerTemplate().withBody(event)- .withHeader(AttributeEvent.HEADER_SOURCE, CLIENT)- .to(ATTRIBUTE_EVENT_QUEUE)+ .to(ATTRIBUTE_EVENT_ROUTER_QUEUE).request();if (result instanceof AssetProcessingException processingException) {@@ -573,7 +575,7 @@ public class AssetResourceImpl extends ManagerWebResource implements AssetResourfailure = AttributeWriteFailure.UNKNOWN;}- return new AttributeWriteResult(event.getAttributeRef(), failure);+ return new AttributeWriteResult(event.getRef(), failure);}@Overridediff --git a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.javaindex 4f79f3e29..90b4e61ea 100644--- a/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AssetStorageService.java@@ -49,7 +49,6 @@ import org.openremote.model.asset.impl.ThingAsset;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeMap;-import org.openremote.model.event.shared.AssetInfo;import org.openremote.model.event.shared.EventRequestResponseWrapper;import org.openremote.model.event.shared.EventSubscription;import org.openremote.model.event.shared.SharedEvent;@@ -61,8 +60,6 @@ import org.openremote.model.security.User;import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;import org.openremote.model.util.ValueUtil;-import org.openremote.model.validation.AssetStateStore;-import org.openremote.model.value.MetaItemType;import org.postgresql.util.PGobject;import java.sql.*;@@ -76,6 +73,7 @@ import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;import java.util.stream.IntStream;+import java.util.stream.Stream;import static java.util.logging.Level.FINE;import static java.util.stream.Collectors.groupingBy;@@ -153,17 +151,19 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicreturn false;}- // Superuser can get events for any asset in any realm- if (auth != null && auth.isSuperUser()) {- return true;- }-AssetFilter<T> filter = (AssetFilter<T>) subscription.getFilter();if (filter == null) {filter = new AssetFilter<>();subscription.setFilter(filter);}+ filter.setInternal(subscription.isInternal());++ // Internal subscribers and superusers can get events for any asset in any realm+ if (subscription.isInternal() || (auth != null && auth.isSuperUser())) {+ return true;+ }+requestRealm = filter.getRealm() != null ? filter.getRealm() : requestRealm;boolean isAnonymous = auth == null;boolean isRestricted = identityService.getIdentityProvider().isRestrictedUser(auth);@@ -521,7 +521,7 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}/**- * @param loadComplete If the whole asset data (including path and attributes) should be loaded.+ * @param loadComplete If the whole asset data (including attributes) should be loaded.*/public Asset<?> find(String assetId, boolean loadComplete) {if (assetId == null)@@ -1343,48 +1343,37 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic* generate the {@link AttributeEvent}*/@SuppressWarnings("unchecked")- protected boolean updateAttributeValue(EntityManager em, Asset<?> asset, Attribute<?> attribute) throws ConstraintViolationException {+ protected boolean updateAttributeValue(EntityManager em, AttributeEvent event) throws ConstraintViolationException {- long timestamp = attribute.getTimestamp().orElseGet(timerService::getCurrentTimeMillis);-- // TODO: Reuse AssetState and change validator over to that class- // Do standard JSR-380 validation on the new value (needs attribute descriptor to do this)- Set<ConstraintViolation<AssetStateStore>> validationFailures = ValueUtil.validate(new AssetStateStore(asset.getType(), attribute));-- if (!validationFailures.isEmpty()) {- String msg = "Attribute update failed as value failed constraint validation: attribute=" + attribute;- ConstraintViolationException ex = new ConstraintViolationException(validationFailures);- LOG.log(Level.WARNING, msg + ", exception=" + ex.getMessage());- throw ex;- }+ long timestamp = event.getTimestamp() > 0 ? event.getTimestamp() : timerService.getCurrentTimeMillis();try {PGobject valueTimestampJSON = new PGobject();valueTimestampJSON.setType("jsonb");- valueTimestampJSON.setValue("{\"value\":" + ValueUtil.asJSON(attribute.getValue().orElse(null)).orElse(ValueUtil.NULL_LITERAL) + ",\"timestamp\":" + timestamp + "}");+ valueTimestampJSON.setValue("{\"value\":" + ValueUtil.asJSON(event.getValue().orElse(null)).orElse(ValueUtil.NULL_LITERAL) + ",\"timestamp\":" + timestamp + "}");// TODO: Use jsonb type directly to optimise over wire data (couldn't get this to work even after seeing https://stackoverflow.com/questions/53847917/postgresql-throws-column-is-of-type-jsonb-but-expression-is-of-type-bytea-with)Query query = em.createNativeQuery("UPDATE asset SET attributes[?] = attributes[?] || ?\\:\\:jsonb where id = ?")- .setParameter(1, attribute.getName())- .setParameter(2, attribute.getName())- .setParameter(3, "{\"value\":" + ValueUtil.asJSON(attribute.getValue().orElse(null)).orElse(ValueUtil.NULL_LITERAL) + ",\"timestamp\":" + timestamp + "}")- .setParameter(4, asset.getId());+ .setParameter(1, event.getName())+ .setParameter(2, event.getName())+ .setParameter(3, "{\"value\":" + ValueUtil.asJSON(event.getValue().orElse(null)).orElse(ValueUtil.NULL_LITERAL) + ",\"timestamp\":" + timestamp + "}")+ .setParameter(4, event.getId());int affectedRows = query.executeUpdate();boolean success = affectedRows == 1;if (success) {if (LOG.isLoggable(Level.FINEST)) {- LOG.finest("Updated attribute value assetID=" + asset.getId() + ", attributeName=" + attribute.getName() + ", timestamp=" + timestamp);+ LOG.finest("Updated attribute value assetID=" + event.getId() + ", attributeName=" + event.getName() + ", timestamp=" + timestamp);}} else {if (LOG.isLoggable(Level.FINE)) {- LOG.fine("Failed to update attribute value assetID=" + asset.getId() + ", attributeName=" + attribute.getName() + ", timestamp=" + timestamp);+ LOG.fine("Failed to update attribute value assetID=" + event.getId() + ", attributeName=" + event.getName() + ", timestamp=" + timestamp);}}if (success) {- publishAttributeEvent(asset, attribute);+ clientEventService.publishEvent(event);}return success;@@ -1395,22 +1384,6 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic}}- protected void publishAttributeEvent(Asset<?> asset, Attribute<?> attribute) {- clientEventService.publishEvent(- new AttributeEvent(- asset.getId(),- attribute.getName(),- attribute.getValue().orElse(null),- attribute.getTimestamp().orElse(timerService.getCurrentTimeMillis())- )- .setParentId(asset.getParentId())- .setRealm(asset.getRealm())- .setPath(asset.getPath())- .setAccessRestrictedRead(attribute.getMetaValue(MetaItemType.ACCESS_RESTRICTED_READ).orElse(false))- .setAccessPublicRead(attribute.getMetaValue(MetaItemType.ACCESS_PUBLIC_READ).orElse(false))- );- }-protected void publishModificationEvents(PersistenceEvent<Asset<?>> persistenceEvent) {Asset<?> asset = persistenceEvent.getEntity();switch (persistenceEvent.getCause()) {@@ -1428,41 +1401,34 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicclientEventService.publishEvent(new AssetEvent(AssetEvent.Cause.CREATE, loadedAsset, null));- }-// // Raise attribute event for each attribute-// asset.getAttributes().forEach(newAttribute ->-// clientEventService.publishEvent(-// new AttributeEvent(asset.getId(),-// newAttribute.getName(),-// newAttribute.getValue().orElse(null),-// newAttribute.getTimestamp().orElse(timerService.getCurrentTimeMillis()))-// .setParentId(asset.getParentId()).setRealm(asset.getRealm())-// ));+ // Raise attribute event for each created attribute+ asset.getAttributes().forEach(newAttribute ->+ clientEventService.publishEvent(+ new AttributeEvent(+ asset,+ newAttribute,+ getClass().getName(),+ newAttribute.getValue().orElse(null),+ newAttribute.getTimestamp().orElse(0L),+ newAttribute.getValue().orElse(null),+ newAttribute.getTimestamp().orElse(0L))+ ));+ }case UPDATE -> {+ boolean nonAttributeChange = persistenceEvent.getPropertyNames().size() > 1 || !persistenceEvent.hasPropertyChanged("attributes");boolean attributesChanged = persistenceEvent.hasPropertyChanged("attributes");+ LOG.finest(() -> "Asset updated: " + persistenceEvent);-// String[] updatedProperties = Arrays.stream(persistenceEvent.getPropertyNames()).filter(propertyName -> {-// Object oldValue = persistenceEvent.getPreviousState(propertyName);-// Object newValue = persistenceEvent.getCurrentState(propertyName);-// return !Objects.deepEquals(oldValue, newValue);-// }).toArray(String[]::new);-- // Fully load the asset- Asset<?> loadedAsset = find(new AssetQuery().ids(asset.getId()));- if (loadedAsset == null) {- return;- }- LOG.finest("Asset updated: " + persistenceEvent);clientEventService.publishEvent(- new AssetEvent(AssetEvent.Cause.UPDATE, loadedAsset, persistenceEvent.getPropertyNames().toArray(String[]::new))+ new AssetEvent(AssetEvent.Cause.UPDATE, asset, persistenceEvent.getPropertyNames().toArray(String[]::new)));- // Did any attributes change if so raise attribute events on the event bus- if (attributesChanged) {- AttributeMap oldAttributes = persistenceEvent.getPreviousState("attributes");- AttributeMap newAttributes = persistenceEvent.getCurrentState("attributes");+ AttributeMap oldAttributes = attributesChanged ? persistenceEvent.getPreviousState("attributes") : asset.getAttributes();+ AttributeMap newAttributes = attributesChanged ? persistenceEvent.getCurrentState("attributes") : asset.getAttributes();+ // Publish events for deleted attributes+ if (attributesChanged) {// Get removed attributes and raise an attribute event with deleted flag in attribute stateoldAttributes.stream().filter(oldAttribute ->@@ -1471,16 +1437,34 @@ public class AssetStorageService extends RouteBuilder implements ContainerServic)).forEach(obsoleteAttribute ->clientEventService.publishEvent(- AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())+ new AttributeEvent(asset, obsoleteAttribute, getClass().getName(), null, timerService.getCurrentTimeMillis(), null, 0L)+ .setDeleted(true)));+ }+ Stream<Attribute<?>> attributeStream;++ if (nonAttributeChange) {+ // If something other than attributes has changed then treat as if attributes changed as path etc could have changed+ attributeStream = newAttributes.values().stream();+ } else {// Get new or modified attributes- getAddedOrModifiedAttributes(oldAttributes.values(),- newAttributes.values())- .forEach(newOrModifiedAttribute ->- publishAttributeEvent(asset, newOrModifiedAttribute)- );+ attributeStream = getAddedOrModifiedAttributes(oldAttributes.values(), newAttributes.values());}++ attributeStream+ .forEach(newOrModifiedAttribute -> {+ Optional<Attribute<?>> oldAttribute = oldAttributes.get(newOrModifiedAttribute.getName());+ clientEventService.publishEvent(new AttributeEvent(+ asset,+ newOrModifiedAttribute,+ getClass().getSimpleName(),+ newOrModifiedAttribute.getValue().orElse(null),+ newOrModifiedAttribute.getTimestamp().orElse(0L),+ oldAttribute.flatMap(Attribute::getValue).orElse(null),+ oldAttribute.flatMap(Attribute::getTimestamp).orElse(0L)+ ));+ });}case DELETE -> {if (LOG.isLoggable(Level.FINEST)) {@@ -1496,7 +1480,8 @@ public class AssetStorageService extends RouteBuilder implements ContainerServicAttributeMap deletedAttributes = asset.getAttributes();deletedAttributes.forEach(obsoleteAttribute ->clientEventService.publishEvent(- AttributeEvent.deletedAttribute(asset.getId(), obsoleteAttribute.getName())+ new AttributeEvent(asset, obsoleteAttribute, getClass().getName(), null, timerService.getCurrentTimeMillis(), null, 0L)+ .setDeleted(true)));}}diff --git a/manager/src/main/java/org/openremote/manager/asset/AssetUpdateProcessor.java b/manager/src/main/java/org/openremote/manager/asset/AssetUpdateProcessor.javadeleted file mode 100644index 5ba3bc8c5..000000000--- a/manager/src/main/java/org/openremote/manager/asset/AssetUpdateProcessor.java+++ /dev/null@@ -1,46 +0,0 @@-/*- * Copyright 2016, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <http://www.gnu.org/licenses/>.- */-package org.openremote.manager.asset;--import org.openremote.model.asset.Asset;-import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeEvent.Source;--import jakarta.persistence.EntityManager;--/**- * Process update of an asset attribute (value), from a {@link Source}.- */-public interface AssetUpdateProcessor {-- /**- * @param em The current session and transaction on the database, processors may use this to query additional data.- * @param asset The current asset state with the old value and old value timestamp.- * @param attribute The attribute to be updated, with new value and value timestamp already set. Value and timestamp can be mutated by processors.- * @param source The source of the update.- * @return <code>true</code> if processing is complete and subsequent processor should be skipped.- * @throws AssetProcessingException When processing failed and the update can not continue.- */- boolean processAssetUpdate(EntityManager em, Asset<?> asset, Attribute<?> attribute, Source source) throws AssetProcessingException;-- /* TODO Processors should be transactional, so an exception in one processor can roll back the update in others */- // void commit();- // void rollback();-}diff --git a/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java b/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.javaindex 4ab159b3a..f587430a1 100644--- a/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java+++ b/manager/src/main/java/org/openremote/manager/asset/AttributeLinkingService.java@@ -20,13 +20,16 @@package org.openremote.manager.asset;import org.openremote.manager.agent.AgentService;+import org.openremote.manager.event.ClientEventService;import org.openremote.manager.gateway.GatewayService;-import org.openremote.model.attribute.AttributeWriteFailure;import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.asset.Asset;-import org.openremote.model.attribute.*;-import org.openremote.model.attribute.AttributeEvent.Source;+import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeLink;+import org.openremote.model.attribute.AttributeRef;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.RealmPredicate;@@ -34,57 +37,18 @@ import org.openremote.model.util.Pair;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaItemType;-import jakarta.persistence.EntityManager;import java.util.Arrays;import java.util.NoSuchElementException;import java.util.Optional;import java.util.logging.Logger;-import static org.openremote.model.attribute.AttributeEvent.Source.ATTRIBUTE_LINKING_SERVICE;-/**- * This service processes asset updates on attributes that have one or more {@link MetaItemType#ATTRIBUTE_LINKS} meta items.- * <p>- * If such an event occurs then the event is 'forwarded' to the linked attribute; an attribute can contain multiple- * {@link MetaItemType#ATTRIBUTE_LINKS} meta items; optionally the value forwarded to the linked attribute can be modified- * by configuring the converter property in the meta item's value:- * <p>- * By default the exact value of the attribute is forwarded unless a key exists in the converter JSON Object that- * matches the value as a string (note matches are case sensitive so booleans should be lower case i.e. true or false);- * in that case the value of the key is forwarded instead. There are several special conversions available by using- * the value of a {@link AttributeLink.ConverterType} as the value. This allows for example a button press to toggle a boolean- * attribute or for a particular value to be ignored.- * <p>- * To convert null values the converter key of "NULL" can be used.+ * This service generates new {@link AttributeEvent}s for any {@link Attribute} that contains an+ * {@link MetaItemType#ATTRIBUTE_LINKS} meta item when the {@link Attribute} is updated.* <p>- * Example {@link MetaItemType#ATTRIBUTE_LINKS} meta items:- * <blockquote><pre>{@code- * [- * "name": "urn:openremote:asset:meta:attributeLink",- * "value": {- * "ref": ["0oI7Gf_kTh6WyRJFUTr8Lg", "light1"],- * "converter": {- * "PRESSED": "@TOGGLE",- * "LONG_PRESSED": "@IGNORE",- * "RELEASED": "@IGNORE"- * }- * }- * ],- * [- * "name": "urn:openremote:asset:meta:attributeLink",- * "value": {- * "ref": ["0oI7Gf_kTh6WyRJFUTr8Lg", "light2"],- * "converter": {- * "0": true,- * "1": false- * "NULL": "@IGNORE"- * }- * }- * ]- * }</pre></blockquote>+ * See {@link AttributeLink} for capabilities.*/-// TODO: Improve AttributeLinkingService so that outbound events are synchronsied with inbound-public class AttributeLinkingService implements ContainerService, AssetUpdateProcessor {+public class AttributeLinkingService implements ContainerService {private static final Logger LOG = Logger.getLogger(AttributeLinkingService.class.getName());protected AssetProcessingService assetProcessingService;@@ -92,17 +56,14 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProprotected AgentService agentService;protected GatewayService gatewayService;- @Override- public int getPriority() {- return ContainerService.DEFAULT_PRIORITY;- }-@Overridepublic void init(Container container) throws Exception {assetProcessingService = container.getService(AssetProcessingService.class);assetStorageService = container.getService(AssetStorageService.class);agentService = container.getService(AgentService.class);gatewayService = container.getService(GatewayService.class);+ ClientEventService clientEventService = container.getService(ClientEventService.class);+ clientEventService.addInternalSubscription(AttributeEvent.class, null, this::onAttributeEvent);}@Override@@ -113,44 +74,34 @@ public class AttributeLinkingService implements ContainerService, AssetUpdatePropublic void stop(Container container) throws Exception {}- @Override- public boolean processAssetUpdate(EntityManager em,- Asset<?> asset,- Attribute<?> attribute,- Source source) throws AssetProcessingException {- if (source == ATTRIBUTE_LINKING_SERVICE) {- LOG.finest("Attribute update came from this service so ignoring to avoid infinite loops: " + attribute);- return false;+ public void onAttributeEvent(AttributeEvent event) {+ if (getClass().getName().equals(event.getSource())) {+ LOG.finest("Attribute update came from this service so ignoring to avoid infinite loops: ref=" + event.getRef());+ return;}- // If asset is a gateway descendant then don't action any links- attribute.getMetaValue(MetaItemType.ATTRIBUTE_LINKS)- .map(attributeLinks -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), asset.getParentId()) != null ? null : attributeLinks)+ event.getMetaValue(MetaItemType.ATTRIBUTE_LINKS).ifPresent(attributeLinks ->Arrays.stream(attributeLinks).forEach(attributeLink ->- processLinkedAttributeUpdate(em, asset, attribute, attributeLink)));-- return false;+ processLinkedAttributeUpdate(event, attributeLink)));}protected void sendAttributeEvent(AttributeEvent attributeEvent) {LOG.finest("Sending attribute event for linked attribute: " + attributeEvent);- assetProcessingService.sendAttributeEvent(attributeEvent, ATTRIBUTE_LINKING_SERVICE);+ assetProcessingService.sendAttributeEvent(attributeEvent, getClass().getName());}- protected void processLinkedAttributeUpdate(EntityManager em, Asset<?> asset, Attribute<?> attribute, AttributeLink attributeLink) {- LOG.finest("Processing attribute state for linked attribute");-+ protected void processLinkedAttributeUpdate(AttributeInfo attributeInfo, AttributeLink attributeLink) {if (attributeLink == null) {- throw new AssetProcessingException(AttributeWriteFailure.INVALID_ATTRIBUTE_LINK);+ return;}+ LOG.finest("Processing attribute links for updated attribute ref=" + attributeInfo.getRef());+// Convert the value as requiredPair<Boolean, Object> sendConvertedValue = convertValueForLinkedAttribute(- em,assetStorageService,- asset,- attribute,+ attributeInfo,attributeLink);@@ -162,7 +113,7 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProfinal Object[] value = {sendConvertedValue.value};// Get the attribute and try and coerce the value into the correct type- getAttribute(em, assetStorageService, asset.getRealm(), attributeLink.getAttributeRef()).ifPresent(attr -> {+ getAttribute(assetStorageService, attributeInfo.getRealm(), attributeLink.getAttributeRef()).ifPresent(attr -> {if (value[0] != null) {@@ -183,13 +134,11 @@ public class AttributeLinkingService implements ContainerService, AssetUpdatePro});}- protected Pair<Boolean, Object> convertValueForLinkedAttribute(EntityManager em,- AssetStorageService assetStorageService,- Asset<?> asset,- Attribute<?> attribute,- AttributeLink attributeLink) throws AssetProcessingException {+ protected Pair<Boolean, Object> convertValueForLinkedAttribute(AssetStorageService assetStorageService,+ AttributeInfo attributeInfo,+ AttributeLink attributeLink) throws AssetProcessingException {- Object originalValue = attribute.getValue().orElse(null);+ Object originalValue = attributeInfo.getValue().orElse(null);// Filter the value firstif (attributeLink.getFilters() != null) {@@ -210,9 +159,8 @@ public class AttributeLinkingService implements ContainerService, AssetUpdatePro// Do special value conversionreturn getSpecialConverter(converterValue.value).map(specialConverter -> doSpecialConversion(- em,assetStorageService,- asset,+ attributeInfo,specialConverter,attributeLink.getAttributeRef())@@ -228,26 +176,19 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProreturn Optional.empty();}- protected static Pair<Boolean, Object> doSpecialConversion(EntityManager em,- AssetStorageService assetStorageService,- Asset<?> asset,+ protected static Pair<Boolean, Object> doSpecialConversion(AssetStorageService assetStorageService,+ AttributeInfo attributeInfo,AttributeLink.ConverterType converter,- AttributeRef linkedAttributeRef) throws AssetProcessingException {+ AttributeRef linkedAttributeRef) throws RuntimeException {switch (converter) {case TOGGLE -> {// Look up current value of the linked attribute within the same database sessiontry {- Attribute<?> currentAttribute = getAttribute(em, assetStorageService, asset.getRealm(), linkedAttributeRef).orElseThrow(- () -> new AssetProcessingException(- AttributeWriteFailure.ATTRIBUTE_NOT_FOUND,- "cannot toggle value as attribute cannot be found: " + linkedAttributeRef- )- );+ Attribute<?> currentAttribute = getAttribute(assetStorageService, attributeInfo.getRealm(), linkedAttributeRef).orElseThrow(+ () -> new RuntimeException("Cannot toggle value as attribute cannot be found: " + linkedAttributeRef));if (!ValueUtil.isBoolean(currentAttribute.getTypeClass())) {- throw new AssetProcessingException(- AttributeWriteFailure.LINKED_ATTRIBUTE_CONVERSION_FAILURE,- "cannot toggle value as attribute is not of type BOOLEAN: " + linkedAttributeRef- );+ throw new RuntimeException(+ "Cannot toggle value as attribute is not of type BOOLEAN: " + linkedAttributeRef);}return new Pair<>(false, !(Boolean) currentAttribute.getValue(Boolean.class).orElse(false));} catch (NoSuchElementException e) {@@ -258,16 +199,12 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProcase INCREMENT, DECREMENT -> {// Look up current value of the linked attribute within the same database sessiontry {- Attribute<?> currentAttribute = getAttribute(em, assetStorageService, asset.getRealm(), linkedAttributeRef).orElseThrow(- () -> new AssetProcessingException(- AttributeWriteFailure.ATTRIBUTE_NOT_FOUND,- "cannot toggle value as attribute cannot be found: " + linkedAttributeRef- )- );+ Attribute<?> currentAttribute = getAttribute(assetStorageService, attributeInfo.getRealm(), linkedAttributeRef)+ .orElseThrow(() ->+ new RuntimeException("Cannot toggle value as attribute cannot be found: " + linkedAttributeRef));if (!ValueUtil.isNumber(currentAttribute.getTypeClass())) {- throw new AssetProcessingException(- AttributeWriteFailure.LINKED_ATTRIBUTE_CONVERSION_FAILURE,- "cannot increment/decrement value as attribute is not of type NUMBER: " + linkedAttributeRef+ throw new RuntimeException(+ "Cannot increment/decrement value as attribute is not of type NUMBER: " + linkedAttributeRef);}int change = converter == AttributeLink.ConverterType.INCREMENT ? +1 : -1;@@ -277,20 +214,15 @@ public class AttributeLinkingService implements ContainerService, AssetUpdateProreturn new Pair<>(true, null);}}- default -> throw new AssetProcessingException(- AttributeWriteFailure.LINKED_ATTRIBUTE_CONVERSION_FAILURE,- "converter is not supported: " + converter- );+ default -> throw new RuntimeException("Converter is not supported ref=" + linkedAttributeRef + ": " + converter);}}- protected static Optional<Attribute<?>> getAttribute(EntityManager em,- AssetStorageService assetStorageService,- String realm,- AttributeRef attributeRef) {+ protected static Optional<Attribute<?>> getAttribute(AssetStorageService assetStorageService,+ String realm,+ AttributeRef attributeRef) {// Get the full asset as shared emAsset<?> asset = assetStorageService.find(- em,new AssetQuery().realm(new RealmPredicate(realm)).ids(attributeRef.getId())diff --git a/manager/src/main/java/org/openremote/manager/asset/OutdatedAttributeEvent.java b/manager/src/main/java/org/openremote/manager/asset/OutdatedAttributeEvent.javanew file mode 100644index 000000000..eb674fcbf--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/asset/OutdatedAttributeEvent.java@@ -0,0 +1,17 @@+package org.openremote.manager.asset;++import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.event.Event;++/**+ * This is an event for use by internal event subscribers to be notified of an outdated {@link AttributeEvent} that has+ * just been processed.+ */+public class OutdatedAttributeEvent extends Event {+ protected AttributeEvent event;++ public OutdatedAttributeEvent(AttributeEvent event) {+ super(event.getTimestamp());+ this.event = event;+ }+}diff --git a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.javaindex 2ced44b16..e775a50ac 100644--- a/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/asset/console/ConsoleResourceImpl.java@@ -67,7 +67,7 @@ public class ConsoleResourceImpl extends ManagerWebResource implements ConsoleReprotected void onAssetChange(AssetEvent event) {// Remove any parent console asset mapping if the asset gets deletedif (event.getCause() == AssetEvent.Cause.DELETE) {- realmConsoleParentMap.values().remove(event.getAssetId());+ realmConsoleParentMap.values().remove(event.getId());}}diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.javaindex 980800ac3..8331e46cf 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AbstractDatapointService.java@@ -95,21 +95,21 @@ public abstract class AbstractDatapointService<T extends Datapoint> implements C}public void upsertValue(String assetId, String attributeName, Object value, long timestamp) throws IllegalStateException {persistenceService.doTransaction(em ->- em.unwrap(Session.class).doWork(connection -> {-- getLogger().finest("Storing datapoint for: id=" + assetId + ", name=" + attributeName + ", timestamp=" + timestamp + ", value=" + value);- PreparedStatement st;-- try {- st = getUpsertPreparedStatement(connection);- setUpsertValues(st, assetId, attributeName, value, timestamp);- st.executeUpdate();- } catch (Exception e) {- String msg = "Failed to insert/update data point: ";- getLogger().log(Level.WARNING, msg, e);- throw new IllegalStateException(msg, e);- }- }));+ em.unwrap(Session.class).doWork(connection -> {++ getLogger().finest("Storing datapoint for: id=" + assetId + ", name=" + attributeName + ", timestamp=" + timestamp + ", value=" + value);+ PreparedStatement st;++ try {+ st = getUpsertPreparedStatement(connection);+ setUpsertValues(st, assetId, attributeName, value, timestamp);+ st.executeUpdate();+ } catch (Exception e) {+ String msg = "Failed to insert/update data point: ";+ getLogger().log(Level.WARNING, msg, e);+ throw new IllegalStateException(msg, e);+ }+ }));}public void upsertValues(String assetId, String attributeName, List<ValueDatapoint<?>> valuesAndTimestamps) throws IllegalStateException {diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.javaindex a28f36671..8e29f88a4 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointResourceImpl.java@@ -48,8 +48,6 @@ import org.openremote.model.value.MetaItemType;import java.io.File;import java.io.FileInputStream;-import java.util.concurrent.ArrayBlockingQueue;-import java.util.concurrent.BlockingQueue;import java.util.concurrent.ScheduledFuture;import java.util.logging.Level;import java.util.logging.Logger;@@ -63,7 +61,7 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements Asprivate static final Logger LOG = Logger.getLogger(AssetDatapointResourceImpl.class.getName());private static final Logger DATA_EXPORT_LOG = SyslogCategory.getLogger(DATA, AssetDatapointResourceImpl.class);- private static final BlockingQueue<AsyncResponse> suspended = new ArrayBlockingQueue<>(5);+protected final AssetStorageService assetStorageService;protected final AssetDatapointService assetDatapointService;@@ -173,9 +171,7 @@ public class AssetDatapointResourceImpl extends ManagerWebResource implements As}@Override- public void getDatapointExport(String attributeRefsString, long fromTimestamp, long toTimestamp) throws InterruptedException {- final AsyncResponse asyncResponse = suspended.take();-+ public void getDatapointExport(AsyncResponse asyncResponse, String attributeRefsString, long fromTimestamp, long toTimestamp) {try {AttributeRef[] attributeRefs = JSON.readValue(attributeRefsString, AttributeRef[].class);diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.javaindex 8a73e7102..f6d4b0462 100644--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetDatapointService.java@@ -4,14 +4,15 @@ import org.openremote.agent.protocol.ProtocolDatapointService;import org.openremote.container.timer.TimerService;import org.openremote.container.util.UniqueIdentifierGenerator;import org.openremote.manager.asset.AssetProcessingException;+import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.asset.AssetUpdateProcessor;+import org.openremote.manager.event.ClientEventService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Container;import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeEvent.Source;+import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.attribute.AttributeWriteFailure;import org.openremote.model.datapoint.AssetDatapoint;@@ -19,6 +20,7 @@ import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.AttributePredicate;import org.openremote.model.query.filter.NameValuePredicate;import org.openremote.model.util.Pair;+import org.openremote.model.value.MetaHolder;import org.openremote.model.value.MetaItemType;import jakarta.persistence.EntityManager;@@ -51,7 +53,7 @@ import static org.openremote.model.value.MetaItemType.STORE_DATA_POINTS;* and {@link #OR_DATA_POINTS_MAX_AGE_DAYS} setting; storage duration defaults to {@value #OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULT}* days.*/-public class AssetDatapointService extends AbstractDatapointService<AssetDatapoint> implements AssetUpdateProcessor, ProtocolDatapointService {+public class AssetDatapointService extends AbstractDatapointService<AssetDatapoint> implements ProtocolDatapointService {public static final String OR_DATA_POINTS_MAX_AGE_DAYS = "OR_DATA_POINTS_MAX_AGE_DAYS";public static final int OR_DATA_POINTS_MAX_AGE_DAYS_DEFAULT = 31;@@ -77,6 +79,8 @@ public class AssetDatapointService extends AbstractDatapointService<AssetDatapoiif (maxDatapointAgeDays <= 0) {LOG.warning(OR_DATA_POINTS_MAX_AGE_DAYS + " value is not a valid value so data points won't be auto purged");+ } else {+ LOG.log(Level.INFO, "Data point purge interval days = " + maxDatapointAgeDays);}Path storageDir = persistenceService.getStorageDir();@@ -97,26 +101,23 @@ public class AssetDatapointService extends AbstractDatapointService<AssetDatapoiDuration.ofDays(1).toMillis(), TimeUnit.MILLISECONDS);}- }- public static boolean attributeIsStoreDatapoint(Attribute<?> attribute) {- return attribute.getMetaValue(STORE_DATA_POINTS).orElse(attribute.hasMeta(MetaItemType.AGENT_LINK));+ ClientEventService clientEventService = container.getService(ClientEventService.class);+ clientEventService.addInternalSubscription(AttributeEvent.class, null, this::onAttributeEvent);}- @Override- public boolean processAssetUpdate(EntityManager em,- Asset<?> asset,- Attribute<?> attribute,- Source source) throws AssetProcessingException {+ public static boolean attributeIsStoreDatapoint(MetaHolder attributeInfo) {+ return attributeInfo.getMetaValue(STORE_DATA_POINTS).orElse(attributeInfo.hasMeta(MetaItemType.AGENT_LINK));+ }- if (attributeIsStoreDatapoint(attribute) && attribute.getValue().isPresent()) { // Don't store datapoints with null value+ public void onAttributeEvent(AttributeEvent attributeEvent) {+ if (attributeIsStoreDatapoint(attributeEvent) && attributeEvent.getValue().isPresent()) { // Don't store datapoints with null valuetry {- upsertValue(asset.getId(), attribute.getName(), attribute.getValue().orElse(null), LocalDateTime.ofInstant(Instant.ofEpochMilli(attribute.getTimestamp().orElseGet(timerService::getCurrentTimeMillis)), ZoneId.systemDefault()));+ upsertValue(attributeEvent.getId(), attributeEvent.getName(), attributeEvent.getValue().orElse(null), LocalDateTime.ofInstant(Instant.ofEpochMilli(attributeEvent.getTimestamp()), ZoneId.systemDefault()));} catch (Exception e) {- throw new AssetProcessingException(AttributeWriteFailure.STATE_STORAGE_FAILED, "Failed to insert or update asset data point for attribute: " + attribute, e);+ throw new AssetProcessingException(AttributeWriteFailure.STATE_STORAGE_FAILED, "Failed to insert or update asset data point for attribute: " + attributeEvent, e);}}- return false;}@Overridediff --git a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.javaindex 3b9ad30f6..985e21994 100644--- a/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java+++ b/manager/src/main/java/org/openremote/manager/energy/EnergyOptimisationService.java@@ -171,42 +171,42 @@ public class EnergyOptimisationService extends RouteBuilder implements Container}protected void processAttributeEvent(AttributeEvent attributeEvent) {- OptimisationInstance optimisationInstance = assetOptimisationInstanceMap.get(attributeEvent.getAssetId());+ OptimisationInstance optimisationInstance = assetOptimisationInstanceMap.get(attributeEvent.getId());if (optimisationInstance != null) {processOptimisationAssetAttributeEvent(optimisationInstance, attributeEvent);return;}- String attributeName = attributeEvent.getAttributeName();+ String attributeName = attributeEvent.getName();if ((attributeName.equals(ElectricityChargerAsset.VEHICLE_CONNECTED.getName()) || attributeName.equals(ElectricVehicleAsset.CHARGER_CONNECTED.getName()))- && attributeEvent.<Boolean>getValue().orElse(false)) {+ && (Boolean)attributeEvent.getValue().orElse(false)) {// Look for forced charge asset- if (forceChargeAssetIds.remove(attributeEvent.getAssetId())) {- LOG.fine("Previously force charged asset has now been disconnected so clearing force charge flag: " + attributeEvent.getAssetId());+ if (forceChargeAssetIds.remove(attributeEvent.getId())) {+ LOG.fine("Previously force charged asset has now been disconnected so clearing force charge flag: " + attributeEvent.getId());}return;}// Check for request to force chargeif (attributeName.equals(ElectricityStorageAsset.FORCE_CHARGE.getName())) {- Asset<?> asset = assetStorageService.find(attributeEvent.getAssetId());+ Asset<?> asset = assetStorageService.find(attributeEvent.getId());if (!(asset instanceof ElectricityStorageAsset)) {- LOG.fine("Request to force charge asset will be ignored as asset not found or is not of type '" + ElectricityStorageAsset.class.getSimpleName() + "': " + attributeEvent.getAssetId());+ LOG.fine("Request to force charge asset will be ignored as asset not found or is not of type '" + ElectricityStorageAsset.class.getSimpleName() + "': " + attributeEvent.getId());return;}ElectricityStorageAsset storageAsset = (ElectricityStorageAsset) asset;- if (attributeEvent.<AttributeExecuteStatus>getValue().orElse(null) == AttributeExecuteStatus.REQUEST_START) {+ if (attributeEvent.getValue().orElse(null) == AttributeExecuteStatus.REQUEST_START) {double powerImportMax = storageAsset.getPowerImportMax().orElse(Double.MAX_VALUE);double maxEnergyLevel = getElectricityStorageAssetEnergyLevelMax(storageAsset);double currentEnergyLevel = storageAsset.getEnergyLevel().orElse(0d);- LOG.fine("Request to force charge asset '" + attributeEvent.getAssetId() + "': attempting to set powerSetpoint=" + powerImportMax);+ LOG.fine("Request to force charge asset '" + attributeEvent.getId() + "': attempting to set powerSetpoint=" + powerImportMax);- if (forceChargeAssetIds.contains(attributeEvent.getAssetId())) {+ if (forceChargeAssetIds.contains(attributeEvent.getId())) {LOG.fine("Request to force charge asset will be ignored as force charge already requested for asset: " + storageAsset);return;}@@ -216,16 +216,16 @@ public class EnergyOptimisationService extends RouteBuilder implements Containerreturn;}- forceChargeAssetIds.add(attributeEvent.getAssetId());- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, powerImportMax));- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.RUNNING));+ forceChargeAssetIds.add(attributeEvent.getId());+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, powerImportMax), getClass().getSimpleName());+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.RUNNING), getClass().getSimpleName());} else if (attributeEvent.<AttributeExecuteStatus>getValue().orElse(null) == AttributeExecuteStatus.REQUEST_CANCEL) {- if (forceChargeAssetIds.remove(attributeEvent.getAssetId())) {+ if (forceChargeAssetIds.remove(attributeEvent.getId())) {LOG.info("Request to cancel force charge asset: " + storageAsset.getId());- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, 0d));- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.CANCELLED));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, 0d), getClass().getSimpleName());+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.CANCELLED), getClass().getSimpleName());}}}@@ -239,15 +239,15 @@ public class EnergyOptimisationService extends RouteBuilder implements Containerprotected synchronized void processOptimisationAssetAttributeEvent(OptimisationInstance optimisationInstance, AttributeEvent attributeEvent) {- if (EnergyOptimisationAsset.FINANCIAL_SAVING.getName().equals(attributeEvent.getAttributeName())- || EnergyOptimisationAsset.CARBON_SAVING.getName().equals(attributeEvent.getAttributeName())) {+ if (EnergyOptimisationAsset.FINANCIAL_SAVING.getName().equals(attributeEvent.getName())+ || EnergyOptimisationAsset.CARBON_SAVING.getName().equals(attributeEvent.getName())) {// These are updated by this servicereturn;}- if (attributeEvent.getAttributeName().equals(EnergyOptimisationAsset.OPTIMISATION_DISABLED.getName())) {- boolean disabled = attributeEvent.<Boolean>getValue().orElse(false);+ if (attributeEvent.getName().equals(EnergyOptimisationAsset.OPTIMISATION_DISABLED.getName())) {+ boolean disabled = (Boolean)attributeEvent.getValue().orElse(false);if (!disabled && assetOptimisationInstanceMap.containsKey(optimisationInstance.optimisationAsset.getId())) {// Nothing to do herereturn;@@ -258,10 +258,10 @@ public class EnergyOptimisationService extends RouteBuilder implements Container}LOG.info("Processing optimisation asset attribute event: " + attributeEvent);- stopOptimisation(attributeEvent.getAssetId());+ stopOptimisation(attributeEvent.getId());// Get latest asset from storage- EnergyOptimisationAsset asset = (EnergyOptimisationAsset) assetStorageService.find(attributeEvent.getAssetId());+ EnergyOptimisationAsset asset = (EnergyOptimisationAsset) assetStorageService.find(attributeEvent.getId());if (asset != null && !asset.isOptimisationDisabled().orElse(false)) {startOptimisation(asset);@@ -463,9 +463,9 @@ public class EnergyOptimisationService extends RouteBuilder implements ContainerLOG.info("Force charged asset has reached maxEnergyLevelPercentage so stopping charging: " + asset.getId());forceChargeAssetIds.remove(asset.getId());assetProcessingService.sendAttributeEvent(- new AttributeEvent(asset.getId(), ElectricityStorageAsset.POWER_SETPOINT, 0d)- );- assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.COMPLETED));+ new AttributeEvent(asset.getId(), ElectricityStorageAsset.POWER_SETPOINT, 0d),+ getClass().getSimpleName());+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityStorageAsset.FORCE_CHARGE, AttributeExecuteStatus.COMPLETED), getClass().getSimpleName());}return false;}@@ -735,7 +735,7 @@ public class EnergyOptimisationService extends RouteBuilder implements ContainerassetPredictedDatapointService.updateValues(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT.getName(), valuesAndTimestamps);}- assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, setpoints != null ? setpoints[0] : null));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(storageAsset.getId(), ElectricityAsset.POWER_SETPOINT, setpoints != null ? setpoints[0] : null), getClass().getSimpleName());// Update unoptimised power for this assetobsoleteUnoptimisedAssetIds.remove(storageAsset.getId());@@ -767,8 +767,8 @@ public class EnergyOptimisationService extends RouteBuilder implements ContaineroptimisationInstance.optimisationAsset.setCarbonSaving(carbonSaving);// Push new values into the DB- assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.FINANCIAL_SAVING, financialSaving));- assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.CARBON_SAVING, carbonSaving));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.FINANCIAL_SAVING, financialSaving), getClass().getSimpleName());+ assetProcessingService.sendAttributeEvent(new AttributeEvent(optimisationAssetId, EnergyOptimisationAsset.CARBON_SAVING, carbonSaving), getClass().getSimpleName());}protected boolean isElectricityGroupAsset(Asset<?> asset) {diff --git a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.javaindex 4a143a15b..f2dcdfe6f 100644--- a/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java+++ b/manager/src/main/java/org/openremote/manager/energy/ForecastSolarService.java@@ -1,11 +1,11 @@package org.openremote.manager.energy;import com.fasterxml.jackson.annotation.JsonProperty;+import jakarta.ws.rs.core.Response;import org.apache.camel.builder.RouteBuilder;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;import org.openremote.container.message.MessageBrokerService;-import org.openremote.model.PersistenceEvent;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;@@ -15,15 +15,14 @@ import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.rules.RulesService;import org.openremote.model.Container;import org.openremote.model.ContainerService;+import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.impl.ElectricityProducerSolarAsset;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.geo.GeoJSONPoint;import org.openremote.model.query.AssetQuery;import org.openremote.model.syslog.SyslogCategory;-import org.openremote.model.util.TextUtil;-import jakarta.ws.rs.core.Response;import java.io.IOException;import java.time.Instant;import java.time.LocalDateTime;@@ -169,40 +168,40 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServiprotected synchronized void processElectricityProducerSolarAssetAttributeEvent(AttributeEvent attributeEvent) {- if (ElectricityProducerSolarAsset.POWER.getName().equals(attributeEvent.getAttributeName())- || ElectricityProducerSolarAsset.POWER_FORECAST.getName().equals(attributeEvent.getAttributeName())) {+ if (ElectricityProducerSolarAsset.POWER.getName().equals(attributeEvent.getName())+ || ElectricityProducerSolarAsset.POWER_FORECAST.getName().equals(attributeEvent.getName())) {// These are updated by this servicereturn;}- if (attributeEvent.getAttributeName().equals(ElectricityProducerSolarAsset.INCLUDE_FORECAST_SOLAR_SERVICE.getName())) {- boolean enabled = attributeEvent.<Boolean>getValue().orElse(false);- if (enabled && calculationFutures.containsKey(attributeEvent.getAssetId())) {+ if (attributeEvent.getName().equals(ElectricityProducerSolarAsset.INCLUDE_FORECAST_SOLAR_SERVICE.getName())) {+ boolean enabled = (Boolean)attributeEvent.getValue().orElse(false);+ if (enabled && calculationFutures.containsKey(attributeEvent.getId())) {// Nothing to do herereturn;- } else if (!enabled && !calculationFutures.containsKey(attributeEvent.getAssetId())) {+ } else if (!enabled && !calculationFutures.containsKey(attributeEvent.getId())) {// Nothing to do herereturn;}LOG.fine("Processing producer solar asset attribute event: " + attributeEvent);- stopProcessing(attributeEvent.getAssetId());+ stopProcessing(attributeEvent.getId());// Get latest asset from storage- ElectricityProducerSolarAsset asset = (ElectricityProducerSolarAsset) assetStorageService.find(attributeEvent.getAssetId());+ ElectricityProducerSolarAsset asset = (ElectricityProducerSolarAsset) assetStorageService.find(attributeEvent.getId());if (asset != null && asset.isIncludeForecastSolarService().orElse(false)) {startProcessing(asset);}}- if (attributeEvent.getAttributeName().equals(ElectricityProducerSolarAsset.SET_ACTUAL_SOLAR_VALUE_WITH_FORECAST.getName())) {+ if (attributeEvent.getName().equals(ElectricityProducerSolarAsset.SET_ACTUAL_SOLAR_VALUE_WITH_FORECAST.getName())) {// Get latest asset from storage- ElectricityProducerSolarAsset asset = (ElectricityProducerSolarAsset) assetStorageService.find(attributeEvent.getAssetId());+ ElectricityProducerSolarAsset asset = (ElectricityProducerSolarAsset) assetStorageService.find(attributeEvent.getId());// Check if power is currently zero and set it if power forecast has an valueif (asset.getPower().orElse(0d) == 0d && asset.getPowerForecast().orElse(0d) != 0d) {- assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityProducerSolarAsset.POWER, asset.getPowerForecast().orElse(0d)));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityProducerSolarAsset.POWER, asset.getPowerForecast().orElse(0d)), getClass().getSimpleName());}}}@@ -262,11 +261,11 @@ public class ForecastSolarService extends RouteBuilder implements ContainerServiassetPredictedDatapointService.updateValue(electricityProducerSolarAsset.getId(), ElectricityProducerSolarAsset.POWER.getName(), -wattItem.getValue() / 1000, timestamp);if (setActualValueForecastPower && timestamp.isAfter(now)) {- assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerSolarAsset.getId(), ElectricityProducerSolarAsset.POWER_FORECAST, -wattItem.getValue() / 1000));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerSolarAsset.getId(), ElectricityProducerSolarAsset.POWER_FORECAST, -wattItem.getValue() / 1000), getClass().getSimpleName());setActualValueForecastPower = false;if (setActualValuePower) {- assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerSolarAsset.getId(), ElectricityProducerSolarAsset.POWER, -wattItem.getValue() / 1000));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerSolarAsset.getId(), ElectricityProducerSolarAsset.POWER, -wattItem.getValue() / 1000), getClass().getSimpleName());setActualValuePower = false;}}diff --git a/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java b/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.javaindex 66437c234..3f7c54c3e 100644--- a/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java+++ b/manager/src/main/java/org/openremote/manager/energy/ForecastWindService.java@@ -1,11 +1,11 @@package org.openremote.manager.energy;import com.fasterxml.jackson.annotation.JsonProperty;+import jakarta.ws.rs.core.Response;import org.apache.camel.builder.RouteBuilder;import org.jboss.resteasy.client.jaxrs.ResteasyClient;import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;import org.openremote.container.message.MessageBrokerService;-import org.openremote.model.PersistenceEvent;import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.datapoint.AssetPredictedDatapointService;@@ -14,13 +14,13 @@ import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.rules.RulesService;import org.openremote.model.Container;import org.openremote.model.ContainerService;+import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.impl.ElectricityProducerAsset;import org.openremote.model.asset.impl.ElectricityProducerWindAsset;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.query.AssetQuery;import org.openremote.model.syslog.SyslogCategory;-import jakarta.ws.rs.core.Response;import java.io.IOException;import java.time.Instant;import java.time.LocalDateTime;@@ -206,40 +206,40 @@ public class ForecastWindService extends RouteBuilder implements ContainerServicprotected synchronized void processElectricityProducerWindAssetAttributeEvent(AttributeEvent attributeEvent) {- if (ElectricityProducerWindAsset.POWER.getName().equals(attributeEvent.getAttributeName())- || ElectricityProducerWindAsset.POWER_FORECAST.getName().equals(attributeEvent.getAttributeName())) {+ if (ElectricityProducerWindAsset.POWER.getName().equals(attributeEvent.getName())+ || ElectricityProducerWindAsset.POWER_FORECAST.getName().equals(attributeEvent.getName())) {// These are updated by this servicereturn;}- if (attributeEvent.getAttributeName().equals(ElectricityProducerWindAsset.INCLUDE_FORECAST_WIND_SERVICE.getName())) {- boolean enabled = attributeEvent.<Boolean>getValue().orElse(false);- if (enabled && calculationFutures.containsKey(attributeEvent.getAssetId())) {+ if (attributeEvent.getName().equals(ElectricityProducerWindAsset.INCLUDE_FORECAST_WIND_SERVICE.getName())) {+ boolean enabled = (Boolean)attributeEvent.getValue().orElse(false);+ if (enabled && calculationFutures.containsKey(attributeEvent.getId())) {// Nothing to do herereturn;- } else if (!enabled && !calculationFutures.containsKey(attributeEvent.getAssetId())) {+ } else if (!enabled && !calculationFutures.containsKey(attributeEvent.getId())) {// Nothing to do herereturn;}LOG.fine("Processing producer wind asset attribute event: " + attributeEvent);- stopCalculation(attributeEvent.getAssetId());+ stopCalculation(attributeEvent.getId());// Get latest asset from storage- ElectricityProducerWindAsset asset = (ElectricityProducerWindAsset) assetStorageService.find(attributeEvent.getAssetId());+ ElectricityProducerWindAsset asset = (ElectricityProducerWindAsset) assetStorageService.find(attributeEvent.getId());if (asset != null && asset.isIncludeForecastWindService().orElse(false) && asset.getLocation().isPresent()) {startCalculation(asset);}}- if (attributeEvent.getAttributeName().equals(ElectricityProducerWindAsset.SET_ACTUAL_WIND_VALUE_WITH_FORECAST.getName())) {+ if (attributeEvent.getName().equals(ElectricityProducerWindAsset.SET_ACTUAL_WIND_VALUE_WITH_FORECAST.getName())) {// Get latest asset from storage- ElectricityProducerWindAsset asset = (ElectricityProducerWindAsset) assetStorageService.find(attributeEvent.getAssetId());+ ElectricityProducerWindAsset asset = (ElectricityProducerWindAsset) assetStorageService.find(attributeEvent.getId());// Check if power is currently zero and set it if power forecast has an valueif (asset.getPower().orElse(0d) == 0d && asset.getPowerForecast().orElse(0d) != 0d) {- assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityProducerWindAsset.POWER, asset.getPowerForecast().orElse(0d)));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), ElectricityProducerWindAsset.POWER, asset.getPowerForecast().orElse(0d)), getClass().getSimpleName());}}}@@ -285,10 +285,10 @@ public class ForecastWindService extends RouteBuilder implements ContainerServicdouble currentPower = calculatePower(electricityProducerWindAsset, weatherForecastResponseModel.current);- assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerWindAsset.getId(), ElectricityProducerAsset.POWER_FORECAST.getName(), -currentPower));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerWindAsset.getId(), ElectricityProducerAsset.POWER_FORECAST.getName(), -currentPower), getClass().getSimpleName());if (electricityProducerWindAsset.isSetActualWindValueWithForecast().orElse(false)) {- assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerWindAsset.getId(), ElectricityProducerAsset.POWER.getName(), -currentPower));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(electricityProducerWindAsset.getId(), ElectricityProducerAsset.POWER.getName(), -currentPower), getClass().getSimpleName());}for (WeatherForecastModel weatherForecastModel : weatherForecastResponseModel.getList()) {diff --git a/manager/src/main/java/org/openremote/manager/event/AttributeEventInterceptor.java b/manager/src/main/java/org/openremote/manager/event/AttributeEventInterceptor.javanew file mode 100644index 000000000..a74c34517--- /dev/null+++ b/manager/src/main/java/org/openremote/manager/event/AttributeEventInterceptor.java@@ -0,0 +1,56 @@+/*+ * Copyright 2016, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.event;++import jakarta.persistence.EntityManager;+import org.openremote.manager.asset.AssetProcessingException;+import org.openremote.model.attribute.AttributeEvent;++/**+ * An interceptor that can choose to intercept the {@link org.openremote.model.attribute.AttributeEvent} passing through+ * the system; if it is handled by this interceptor then the event will not be passed to any more interceptors and will+ * not reach the DB.+ */+public interface AttributeEventInterceptor {++ int DEFAULT_PRIORITY = 1000;++ /**+ * Gets the priority of this interceptor which is used to determine the order in which the interceptors are called;+ * interceptors with a lower priority are called first.+ */+ default int getPriority() {+ return DEFAULT_PRIORITY;+ }++ default String getName() {+ return getClass().getSimpleName();+ }++ /**+ * @param em The current session and transaction on the database, processors may use this to query additional+ * data.+ * @param event The {@link AttributeEvent} enriched with {@link org.openremote.model.asset.Asset} and+ * {@link org.openremote.model.attribute.Attribute} data.+ * @return <code>true</code> if interceptor has handled event and subsequent interceptors should be skipped.+ * @throws AssetProcessingException When processing failed and the update can not continue.+ */+ boolean intercept(EntityManager em, AttributeEvent event) throws AssetProcessingException;+}diff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex 8cc768a53..b01b3ec8b 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -19,6 +19,9 @@*/package org.openremote.manager.event;+import io.micrometer.core.instrument.Counter;+import io.micrometer.core.instrument.MeterRegistry;+import io.micrometer.core.instrument.Tags;import io.undertow.websockets.core.WebSocketChannel;import io.undertow.websockets.spi.WebSocketHttpExchange;import org.apache.camel.Exchange;@@ -39,6 +42,7 @@ import org.openremote.model.Constants;import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.event.Event;import org.openremote.model.event.shared.*;import org.openremote.model.syslog.SyslogEvent;@@ -48,18 +52,15 @@ import java.util.Collection;import java.util.HashSet;import java.util.Objects;import java.util.Set;-import java.util.concurrent.ConcurrentHashMap;-import java.util.concurrent.ConcurrentMap;-import java.util.concurrent.CopyOnWriteArraySet;-import java.util.concurrent.ExecutorService;+import java.util.concurrent.*;import java.util.function.Consumer;import static java.lang.System.Logger.Level.*;import static org.apache.camel.builder.PredicateBuilder.or;-import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_QUEUE;+import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_ROUTER_QUEUE;+import static org.openremote.manager.asset.AssetProcessingService.ATTRIBUTE_EVENT_ROUTE_CONFIG_ID;+import static org.openremote.manager.system.HealthService.OR_CAMEL_ROUTE_METRIC_PREFIX;import static org.openremote.model.Constants.*;-import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;-import static org.openremote.model.attribute.AttributeEvent.Source.CLIENT;/*** Receives and publishes messages, handles the client/server event bus.@@ -138,14 +139,8 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceprotected EventSubscriptions eventSubscriptions;protected GatewayService gatewayService;protected Set<EventSubscription<?>> pendingInternalSubscriptions;- protected boolean stopped;-- /**- * Method to stop further processing of the exchange- */- public static void stopMessage(Exchange exchange) {- exchange.setRouteStop(true);- }+ protected boolean started;+ protected Counter queueFullCounter;public static String getSessionKey(Exchange exchange) {return exchange.getIn().getHeader(SESSION_KEY, String.class);@@ -171,8 +166,11 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceidentityService = container.getService(ManagerIdentityService.class);gatewayService = container.getService(GatewayService.class);executorService = container.getExecutorService();+ MeterRegistry meterRegistry = container.getMeterRegistry();- ManagerWebService webService = container.getService(ManagerWebService.class);+ if (meterRegistry != null) {+ queueFullCounter = meterRegistry.counter(OR_CAMEL_ROUTE_METRIC_PREFIX + "_failed_queue_full", Tags.empty());+ }eventSubscriptions = new EventSubscriptions(container.getService(TimerService.class)@@ -196,7 +194,7 @@ public class ClientEventService extends RouteBuilder implements ContainerService// Add pending internal subscriptionsif (pendingInternalSubscriptions != null && !pendingInternalSubscriptions.isEmpty()) {pendingInternalSubscriptions.forEach(subscription ->- eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, subscription));+ doInternalSubscription(INTERNAL_SESSION_KEY, subscription));}pendingInternalSubscriptions = null;@@ -209,11 +207,11 @@ public class ClientEventService extends RouteBuilder implements ContainerService// Route for deserializing incoming websocket messages and normalising them for the INBOUND_CLIENT_QUEUEfrom(WEBSOCKET_URI).routeId("ClientInbound-Websocket")+ .routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID).process(exchange -> {String connectionKey = exchange.getIn().getHeader(UndertowConstants.CONNECTION_KEY, String.class);exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, HEADER_CONNECTION_TYPE_WEBSOCKET);exchange.getIn().setHeader(SESSION_KEY, connectionKey);- exchange.getIn().setHeader(HEADER_SOURCE, CLIENT);}).choice().when(header(UndertowConstants.EVENT_TYPE))@@ -318,7 +316,7 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceSharedEvent event = exchange.getIn().getBody(SharedEvent.class);if (!authorizeEventWrite(realm, authContext, event)) {- stopMessage(exchange);+ exchange.setRouteStop(true);}} else if (exchange.getIn().getBody() instanceof EventSubscription<?>) {EventSubscription<?> subscription = exchange.getIn().getBody(EventSubscription.class);@@ -326,7 +324,7 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceif (!authorizeEventSubscription(realm, authContext, subscription)) {sendToSession(sessionKey, new UnauthorizedEventSubscription<>(subscription));- stopMessage(exchange);+ exchange.setRouteStop(true);}}})@@ -335,18 +333,27 @@ public class ClientEventService extends RouteBuilder implements ContainerServicefrom(CLIENT_INBOUND_QUEUE).routeId("ClientInbound-EventProcessor")- .process(this::passToInterceptors)+ .routeConfigurationId(ATTRIBUTE_EVENT_ROUTE_CONFIG_ID)+ .process(exchange -> {+ // Process session open before passing to any interceptors+ Boolean isSessionOpen = exchange.getIn().getHeader(SESSION_OPEN, Boolean.class);+ if (isSessionOpen != null) {+ String sessionKey = getSessionKey(exchange);+ LOG.log(TRACE, "Adding session: " + sessionKey);+ sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));+ }+ passToInterceptors(exchange);+ }).choice().when(body().isInstanceOf(AttributeEvent.class))- .to(ATTRIBUTE_EVENT_QUEUE)- .stop()- .endChoice()- .when(header(SESSION_OPEN)).process(exchange -> {- String sessionKey = getSessionKey(exchange);- LOG.log(TRACE, "Adding session: " + sessionKey);- sessionKeyInfoMap.put(sessionKey, createSessionInfo(sessionKey, exchange));+ AttributeEvent attributeEvent = exchange.getIn().getBody(AttributeEvent.class);+ // Set timestamp as early as possible if not set+ if (attributeEvent.getTimestamp() <= 0) {+ attributeEvent.setTimestamp(timerService.getCurrentTimeMillis());+ }})+ .to(ATTRIBUTE_EVENT_ROUTER_QUEUE).stop().when(or(header(SESSION_CLOSE),@@ -389,7 +396,7 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceString sessionKey = getSessionKey(exchange);SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);if (sessionInfo == null) {- LOG.log(INFO, "Cannot send to requested session it doesn't exist or is disconnected:" + sessionKey);+ LOG.log(INFO, "Cannot send to requested session it doesn't exist or is disconnected: " + sessionKey);return;}exchange.getIn().setHeader(HEADER_CONNECTION_TYPE, sessionInfo.connectionType);@@ -413,10 +420,10 @@ public class ClientEventService extends RouteBuilder implements ContainerService/*** Make an internal subscription to {@link SharedEvent}s sent on the client event bus*/- public <T extends SharedEvent> String addInternalSubscription(Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) {+ public <T extends SharedEvent> String addInternalSubscription(Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) throws IllegalStateException {return addInternalSubscription(Integer.toString(Objects.hash(eventClass, filter, eventConsumer)), eventClass, filter, eventConsumer);}- public <T extends SharedEvent> String addInternalSubscription(String subscriptionId, Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) {+ public <T extends SharedEvent> String addInternalSubscription(String subscriptionId, Class<T> eventClass, EventFilter<T> filter, Consumer<T> eventConsumer) throws IllegalStateException {EventSubscription<T> subscription = new EventSubscription<T>(eventClass, filter, subscriptionId, eventConsumer);if (eventSubscriptions == null) {@@ -426,11 +433,19 @@ public class ClientEventService extends RouteBuilder implements ContainerService}pendingInternalSubscriptions.add(subscription);} else {- eventSubscriptions.createOrUpdate(INTERNAL_SESSION_KEY, subscription);+ doInternalSubscription(INTERNAL_SESSION_KEY, subscription);}return subscriptionId;}+ protected void doInternalSubscription(String sessionKey, EventSubscription<?> eventSubscription) throws IllegalStateException {+ if (!authorizeEventSubscription(null, null, eventSubscription)) {+ LOG.log(WARNING, () -> "Internal subscription failed: " + eventSubscription);+ throw new IllegalStateException("Internal subscription failed");+ }+ eventSubscriptions.createOrUpdate(sessionKey, eventSubscription);+ }+public void cancelInternalSubscription(String sessionId) {eventSubscriptions.cancel(INTERNAL_SESSION_KEY, new CancelEventSubscription(sessionId));}@@ -441,12 +456,12 @@ public class ClientEventService extends RouteBuilder implements ContainerService@Overridepublic void start(Container container) {- stopped = false;+ started = true;}@Overridepublic void stop(Container container) {- stopped = true;+ started = false;}public void addExchangeInterceptor(Consumer<Exchange> exchangeInterceptor) throws RuntimeException {@@ -506,9 +521,9 @@ public class ClientEventService extends RouteBuilder implements ContainerService/*** Publish an event to interested clients*/- public <T extends SharedEvent> void publishEvent(T event) {- // Only publish if service is not stopped- if (stopped) {+ public <T extends Event> void publishEvent(T event) {+ // Only publish if service is started+ if (!started) {return;}@@ -529,7 +544,7 @@ public class ClientEventService extends RouteBuilder implements ContainerServiceLOG.log(TRACE, () -> "Sending to session '" + sessionKey + "': " + data);SessionInfo sessionInfo = sessionKeyInfoMap.get(sessionKey);if (sessionInfo == null) {- LOG.log(INFO, "Cannot send to requested session it doesn't exist or is disconnected:" + sessionKey);+ LOG.log(DEBUG, () -> "Send to session '" + sessionKey + "' failed, it doesn't exist or is disconnected: " + data);return;}messageBrokerService.getFluentProducerTemplate()diff --git a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.javaindex f58a6c09e..3239b6380 100644--- a/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java+++ b/manager/src/main/java/org/openremote/manager/event/EventSubscriptions.java@@ -154,17 +154,17 @@ public class EventSubscriptions {List<T> events = Collections.singletonList(filteredEvent);TriggeredEventSubscription<T> triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSub.subscriptionId);- if (sessionSub.subscription.getInternalConsumer() == null) {+ if (sessionSub.subscription.isInternal()) {+ if (triggeredEventSubscription.getEvents() != null) {+ triggeredEventSubscription.getEvents().forEach(e ->+ sessionSub.subscription.getInternalConsumer().accept(e));+ }+ } else {Message msg = new DefaultMessage(exchange.getContext());msg.setBody(triggeredEventSubscription); // Don't copy the event, use same referencemsg.setHeaders(new HashMap<>(exchange.getIn().getHeaders())); // Copy headersmsg.setHeader(SESSION_KEY, sessionKey);messageList.add(msg);- } else {- if (triggeredEventSubscription.getEvents() != null) {- triggeredEventSubscription.getEvents().forEach(e ->- sessionSub.subscription.getInternalConsumer().accept(e));- }}}}diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.javaindex ce3782b4d..8cf0c86d7 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayClientService.java@@ -303,7 +303,7 @@ public class GatewayClientService extends RouteBuilder implements ContainerServiclientRealmMap.put(connection.getLocalRealm(), null);}} else if (event instanceof AttributeEvent) {- assetProcessingService.sendAttributeEvent((AttributeEvent)event, AttributeEvent.Source.INTERNAL);+ assetProcessingService.sendAttributeEvent((AttributeEvent)event, getClass().getName());} else if (event instanceof AssetEvent) {AssetEvent assetEvent = (AssetEvent)event;if (assetEvent.getCause() == AssetEvent.Cause.CREATE || assetEvent.getCause() == AssetEvent.Cause.UPDATE) {diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.javaindex eb5773ba3..958610e28 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayConnector.java@@ -93,7 +93,7 @@ public class GatewayConnector {IntStream.rangeClosed('A', 'Z').boxed()),IntStream.rangeClosed('0', '9').boxed()- ).collect(Collectors.toList())+ ).toList());}@@ -161,8 +161,9 @@ public class GatewayConnector {this.disconnectRunnable = disconnectRunnable;initialSyncInProgress = true;+LOG.fine("Gateway connector starting: Gateway ID=" + gatewayId);- assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTING), AttributeEvent.Source.GATEWAY);+ sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTING));// Reinitialise statesyncProcessorFuture = null;@@ -199,7 +200,9 @@ public class GatewayConnector {// Wait a short while to disconnect to allow disconnect message to be deliveredLOG.fine("Gateway connector disconnected: Gateway ID=" + gatewayId);executorService.schedule(disconnectRunnable, 1, TimeUnit.SECONDS);- assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED), AttributeEvent.Source.GATEWAY);+ if (!isDisabled()) {+ sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));+ }}public boolean isConnected() {@@ -234,13 +237,17 @@ public class GatewayConnector {disconnect();}LOG.fine("Gateway connector disabled: Gateway ID=" + gatewayId);- assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISABLED), AttributeEvent.Source.GATEWAY);+ sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISABLED));} else {LOG.info("Gateway connector enabled: Gateway ID=" + gatewayId);- assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED), AttributeEvent.Source.GATEWAY);+ sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.DISCONNECTED));}}+ protected void sendAttributeEvent(AttributeEvent event) {+ assetProcessingService.sendAttributeEvent(event, GatewayService.class.getName());+ }+synchronized protected void onGatewayEvent(String messageId, SharedEvent e) {if (!isConnected()) {return;@@ -393,10 +400,10 @@ public class GatewayConnector {// Remove any assets that have been deleted since requestedcachedAssetEvents.removeIf(assetEvent -> {- boolean remove = requestedAssetIds.stream().anyMatch(id -> id.equals(assetEvent.getAssetId()) && assetEvent.getCause() == AssetEvent.Cause.DELETE);+ boolean remove = requestedAssetIds.stream().anyMatch(id -> id.equals(assetEvent.getId()) && assetEvent.getCause() == AssetEvent.Cause.DELETE);if (remove) {- syncAssetIds.remove(assetEvent.getAssetId());- requestedAssetIds.remove(assetEvent.getAssetId());+ syncAssetIds.remove(assetEvent.getId());+ requestedAssetIds.remove(assetEvent.getId());}return remove;});@@ -419,7 +426,7 @@ public class GatewayConnector {final AtomicReference<Asset<?>> latestAssetVersion = new AtomicReference<>(returnedAsset);cachedAssetEvents.removeIf(assetEvent -> {- boolean remove = assetEvent.getAssetId().equals(returnedAsset.getId()) && (assetEvent.getCause() == AssetEvent.Cause.UPDATE || assetEvent.getCause() == AssetEvent.Cause.READ);+ boolean remove = assetEvent.getId().equals(returnedAsset.getId()) && (assetEvent.getCause() == AssetEvent.Cause.UPDATE || assetEvent.getCause() == AssetEvent.Cause.READ);if (remove && assetEvent.getAsset().getVersion() > latestAssetVersion.get().getVersion()) {latestAssetVersion.set(assetEvent.getAsset());}@@ -439,16 +446,16 @@ public class GatewayConnector {cachedAssetEvents.forEach(assetEvent -> {if (assetEvent.getCause() == AssetEvent.Cause.DELETE) {- syncAssetIds.remove(assetEvent.getAssetId());+ syncAssetIds.remove(assetEvent.getId());} else if (assetEvent.getCause() == AssetEvent.Cause.CREATE) {- syncAssetIds.add(assetEvent.getAssetId());+ syncAssetIds.add(assetEvent.getId());try {saveAssetLocally(assetEvent.getAsset());} catch (Exception ex) {LOG.log(Level.SEVERE, "Failed to add new gateway asset (Gateway ID=" + gatewayId + ", Asset=" + assetEvent.getAsset(), ex);}} else {- refreshAssets.add(assetEvent.getAssetId());+ refreshAssets.add(assetEvent.getId());}});@@ -458,7 +465,7 @@ public class GatewayConnector {// Refresh attributes that have changedcachedAttributeEvents.forEach(attributeEvent -> {- String assetId = attributeEvent.getAssetId();+ String assetId = attributeEvent.getId();if (!refreshAssets.contains(assetId)) {LOG.info("1 or more gateway asset attribute values have changed so requesting the asset again (Gateway ID=" + gatewayId + ", Asset<?> ID=" + assetId);refreshAssets.add(assetId);@@ -485,8 +492,9 @@ public class GatewayConnector {// Delete obsolete assetsList<String> obsoleteLocalAssetIds = localAssets.stream()- .filter(localAsset -> !syncAssetIds.contains(mapAssetId(gatewayId, localAsset.getId(), true)))- .map(Asset::getId).collect(Collectors.toList());+ .map(Asset::getId)+ .filter(id -> !syncAssetIds.contains(mapAssetId(gatewayId, id, true)))+ .toList();if (!obsoleteLocalAssetIds.isEmpty()) {boolean deleted = deleteAssetsLocally(obsoleteLocalAssetIds);@@ -500,7 +508,7 @@ public class GatewayConnector {initialSyncInProgress = false;cachedAssetEvents.clear();cachedAttributeEvents.clear();- assetProcessingService.sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTED), AttributeEvent.Source.GATEWAY);+ sendAttributeEvent(new AttributeEvent(gatewayId, GatewayAsset.STATUS, ConnectionStatus.CONNECTED));}@SuppressWarnings("unchecked")@@ -587,7 +595,7 @@ public class GatewayConnector {throw new IllegalStateException(msg);}- List<String> originalIds = assetIds.stream().map(id -> mapAssetId(gatewayId, id, true)).collect(Collectors.toList());+ List<String> originalIds = assetIds.stream().map(id -> mapAssetId(gatewayId, id, true)).toList();pendingAssetDelete.set(new EventRequestResponseWrapper<>(UniqueIdentifierGenerator.generateId(),@@ -634,13 +642,9 @@ public class GatewayConnector {synchronized protected void onAssetEvent(AssetEvent e) {switch (e.getCause()) {-- case CREATE:- case READ:- case UPDATE:- String assetId = e.getAssetId();+ case CREATE, READ, UPDATE -> {+ String assetId = e.getId();Asset<?> mergedAsset = saveAssetLocally(e.getAsset());-synchronized (pendingAssetMerges) {if (pendingAssetMerges.containsKey(assetId)) {@SuppressWarnings("OptionalGetWithoutIsPresent")@@ -648,30 +652,27 @@ public class GatewayConnector {String id = pendingAssetMergeEntry.getKey();pendingAssetMergeEntry.setValue(mergedAsset);- //noinspection SynchronizationOnLocalVariableOrMethodParameter+ //noinspectionsynchronized (id) {// Notify the waiting merge threadid.notify();}}}- break;- case DELETE:+ }+ case DELETE -> {try {- deleteAssetsLocally(Collections.singletonList(mapAssetId(gatewayId, e.getAssetId(), false)));+ deleteAssetsLocally(Collections.singletonList(mapAssetId(gatewayId, e.getId(), false)));} catch (Exception ex) {- LOG.log(Level.SEVERE, "Removing obsolete asset failed: " + e.getAssetId(), ex);+ LOG.log(Level.SEVERE, "Removing obsolete asset failed: " + e.getId(), ex);}- break;+ }}}protected void onAttributeEvent(AttributeEvent e) {// Just push the event through the processing chain- assetProcessingService.sendAttributeEvent(- new AttributeEvent(mapAssetId(gatewayId, e.getAssetId(), false), e.getAttributeName(), e.getValue().orElse(null), e.getTimestamp()),- AttributeEvent.Source.GATEWAY- );+ sendAttributeEvent(new AttributeEvent(mapAssetId(gatewayId, e.getId(), false), e.getName(), e.getValue().orElse(null), e.getTimestamp()));}protected <T extends Asset<?>> T saveAssetLocally(T asset) {diff --git a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.javaindex a5fb513d9..1847764a1 100644--- a/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java+++ b/manager/src/main/java/org/openremote/manager/gateway/GatewayService.java@@ -27,7 +27,7 @@ import org.openremote.container.message.MessageBrokerService;import org.openremote.manager.asset.AssetProcessingException;import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.asset.AssetUpdateProcessor;+import org.openremote.manager.event.AttributeEventInterceptor;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.rules.RulesService;import org.openremote.manager.rules.RulesetStorageService;@@ -39,7 +39,6 @@ import org.openremote.model.ContainerService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.impl.GatewayAsset;-import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeEvent;import org.openremote.model.attribute.AttributeMap;import org.openremote.model.attribute.AttributeWriteFailure;@@ -52,6 +51,7 @@ import org.openremote.model.security.Realm;import org.openremote.model.security.User;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.TextUtil;+import org.openremote.model.value.MetaItemType;import java.util.*;import java.util.concurrent.ScheduledExecutorService;@@ -72,8 +72,12 @@ import static org.openremote.model.syslog.SyslogCategory.GATEWAY;* Manages {@link org.openremote.model.asset.impl.GatewayAsset}s in the local instance by creating Keycloak clients* for them and handles the connection logic of gateways; it is the gateways responsibility to connect to this instance,* it is then up to this instance to authenticate the gateway and to initiate synchronisation of gateway assets.+ * <p>+ * Also adds an {@link AttributeEventInterceptor} to consume any {@link AttributeEvent} destined for a+ * {@link GatewayAsset} descendant {@link Asset} and routes them to the underlying gateway for handling; the underlying+ * gateway then notifies us if/when the action has succeeded.*/-public class GatewayService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {+public class GatewayService extends RouteBuilder implements ContainerService {// Need a high priority so that event authorizer can get the events before other authorizerspublic static final int PRIORITY = HIGH_PRIORITY + 100;@@ -181,6 +185,19 @@ public class GatewayService extends RouteBuilder implements ContainerService, As// TODO: Introduce gateway realm rolereturn isGatewayClientId(clientId);});++ assetProcessingService.addEventInterceptor(new AttributeEventInterceptor() {+ @Override+ public int getPriority() {+ // Needs to be the first interceptor+ return AttributeEventInterceptor.DEFAULT_PRIORITY - 1000;+ }++ @Override+ public boolean intercept(EntityManager em, AttributeEvent event) throws AssetProcessingException {+ return onAttributeEventIntercepted(em, event);+ }+ });}}@@ -195,7 +212,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As.stream().map(asset -> (GatewayAsset)asset).collect(Collectors.toList());- List<String> gatewayIds = gateways.stream().map(Asset::getId).collect(Collectors.toList());+ List<String> gatewayIds = gateways.stream().map(Asset::getId).toList();gateways = gateways.stream().filter(gateway ->Arrays.stream(gateway.getPath()).noneMatch(p -> !p.equals(gateway.getId()) && gatewayIds.contains(p)))@@ -295,6 +312,7 @@ public class GatewayService extends RouteBuilder implements ContainerService, As}if (header(SESSION_OPEN).matches(exchange)) {+ // We need to delay processing here so that ClientEventService has fully initialised the sessionString sessionKey = ClientEventService.getSessionKey(exchange);processGatewayConnected(clientId, sessionKey);return;@@ -307,83 +325,104 @@ public class GatewayService extends RouteBuilder implements ContainerService, As// Inbound shared eventsif (body().isInstanceOf(SharedEvent.class).matches(exchange)) {- ClientEventService.stopMessage(exchange);+ exchange.setRouteStop(true);String gatewayId = getGatewayIdFromClientId(clientId);onGatewayClientEventReceived(gatewayId, exchange.getIn().getHeader(ClientEventService.HEADER_REQUEST_RESPONSE_MESSAGE_ID, String.class), exchange.getIn().getBody(SharedEvent.class));}}- @Override- public boolean processAssetUpdate(EntityManager em, Asset<?> asset, Attribute<?> attribute, AttributeEvent.Source source) throws AssetProcessingException {+ public boolean onAttributeEventIntercepted(EntityManager em, AttributeEvent event) throws AssetProcessingException {++ // If the event came from a gateway then we don't want to process it here+ if (getClass().getName().equals(event.getSource())) {- // If the update was generated by a gateway then we don't want to process it here- if (source == AttributeEvent.Source.GATEWAY) {+ // Clear out agentlink meta so agent interceptor doesn't try intercepting the event+ event.getMeta().remove(MetaItemType.AGENT_LINK);return false;}- GatewayConnector connector = gatewayConnectorMap.get(asset.getId().toLowerCase(Locale.ROOT));+ GatewayConnector connector = gatewayConnectorMap.get(event.getId().toLowerCase(Locale.ROOT));if (connector != null) {- LOG.fine("Attribute event for a locally registered gateway asset (Asset ID=" + asset.getId() + "): " + attribute);-- GatewayAsset gatewayAsset = (GatewayAsset)asset;+ LOG.fine("Attribute event for a locally registered gateway asset (Asset ID=" + event.getId() + "): " + event.getRef());// This is a change to a locally registered gateway- if (GatewayAsset.DISABLED.getName().equals(attribute.getName())) {- boolean disabled = attribute.getValue(Boolean.class).orElse(false);- boolean isAlreadyDisabled = gatewayAsset.getDisabled().orElse(false);- gatewayAsset.setDisabled(disabled); // Ensure we update state+ if (GatewayAsset.DISABLED.getName().equals(event.getName())) {+ boolean disabled = (Boolean)event.getValue().orElse(false);+ boolean isAlreadyDisabled = (Boolean)event.getOldValue().orElse(false);if (disabled != isAlreadyDisabled) {+ GatewayAsset gatewayAsset = assetStorageService.find(event.getId(), GatewayAsset.class);+ if (gatewayAsset == null) {+ String msg = "Gateway asset not found: ref=" + event.getRef();+ LOG.info(msg);+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, msg);+ }+ LOG.fine("Gateway client disabled attribute updated so updating gateway service user enabled flag: (gatewayId=" + event.getId() + ")");+ // Push value into asset for service user method+ gatewayAsset.setDisabled(disabled);createUpdateGatewayServiceUser(gatewayAsset);if (disabled) {connector.sendMessageToGateway(new GatewayDisconnectEvent(GatewayDisconnectEvent.Reason.DISABLED));}connector.setDisabled(disabled);}- }-- if (GatewayAsset.CLIENT_SECRET.getName().equals(attribute.getName())) {- String newSecret = attribute.getValue(String.class).orElse(null);- if (!TextUtil.isNullOrEmpty(newSecret)) {- LOG.fine("Gateway client secret attribute updated so updating gateway service user secret to match: (Gateway ID=" + asset.getId() + ")");- User gatewayServiceUser = identityProvider.getUserByUsername(asset.getRealm(), User.SERVICE_ACCOUNT_PREFIX + ((GatewayAsset) asset).getClientId().orElse(""));- if (gatewayServiceUser != null) {- identityProvider.resetSecret(asset.getRealm(), gatewayServiceUser.getId(), newSecret);- } else {- LOG.info("Couldn't retrieve gateway service user to update secret: (Gateway ID=" + asset.getId() + ")");- }- } else {- // Push old secret back- assetProcessingService.sendAttributeEvent(new AttributeEvent(asset.getId(), GatewayAsset.CLIENT_SECRET, gatewayAsset.getClientSecret().orElseThrow(() -> new IllegalStateException("Gateway client secret is null which was not expected"))));+ } else if (GatewayAsset.CLIENT_SECRET.getName().equals(event.getName())) {+ String newSecret = (String)event.getValue().orElse(null);+ GatewayAsset gatewayAsset = assetStorageService.find(event.getId(), GatewayAsset.class);+ if (gatewayAsset == null) {+ String msg = "Gateway asset not found: ref=" + event.getRef();+ LOG.warning(msg);+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, msg);}+ LOG.fine("Gateway client secret attribute updated so updating gateway service user secret: (gatewayId=" + event.getId() + ")");+ User gatewayServiceUser = identityProvider.getUserByUsername(event.getRealm(), User.SERVICE_ACCOUNT_PREFIX + gatewayAsset.getClientId().orElseThrow(() -> {+ String msg = "Gateway asset client ID is missing";+ LOG.warning(msg);+ return new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, msg);+ }));+ if (gatewayServiceUser == null) {+ String msg = "Couldn't retrieve gateway service user to update secret: (gatewayId=" + event.getId() + ")";+ LOG.warning(msg);+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, msg);+ }+ newSecret = identityProvider.resetSecret(event.getRealm(), gatewayServiceUser.getId(), newSecret);++ // Update the event value with the potentially newly generated secret+ event.setValue(newSecret);}- } else {- String gatewayId = assetIdGatewayIdMap.get(asset.getId());- if (gatewayId != null) {- LOG.fine("Attribute event for a gateway descendant asset (Asset<?> ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);- connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));- if (connector == null) {- LOG.warning("Gateway not found for descendant asset, this should not happen!!! (Asset<?> ID=" + asset.getId() + ", Gateway ID=" + gatewayId + ")");- } else {- if (!connector.isConnected()) {- LOG.info("Gateway is not connected so attribute event for descendant asset will be dropped (Asset<?> ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);- throw new AssetProcessingException(AttributeWriteFailure.GATEWAY_DISCONNECTED, "Gateway is not connected: Gateway ID=" + connector.gatewayId);- }- LOG.fine("Attribute event for a gateway descendant asset being forwarded to the gateway (Asset<?> ID=" + asset.getId() + ", Gateway ID=" + gatewayId + "): " + attribute);- connector.sendMessageToGateway(- new AttributeEvent(- mapAssetId(gatewayId, asset.getId(), true),- attribute.getName(),- attribute.getValue().orElse(null),- attribute.getTimestamp().orElse(0L))- .setParentId(mapAssetId(gatewayId, asset.getParentId(), true)).setRealm(asset.getRealm()));- }+ return false;+ }++ String gatewayId = assetIdGatewayIdMap.get(event.getId());- // Consume this event as it is for a gateway descendant and we've sent it to that gateway for processing- return true;+ if (gatewayId != null) {+ LOG.fine("Attribute event for a gateway descendant asset (assetId=" + event.getId() + ", gatewayId=" + gatewayId + ")");+ connector = gatewayConnectorMap.get(gatewayId.toLowerCase(Locale.ROOT));+ if (connector == null) {+ String msg = "Gateway not found for descendant asset, this should not happen!!! assetId=" + event.getId() + ", gatewayId=" + gatewayId + ")";+ LOG.warning(msg);+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, msg);+ }++ if (!connector.isConnected()) {+ LOG.info("Gateway is not connected so attribute event for descendant asset will be dropped (assetId=" + event.getId() + ", gatewayId=" + gatewayId + ")");+ throw new AssetProcessingException(AttributeWriteFailure.CANNOT_PROCESS, "Gateway is not connected: gatewayId=" + connector.gatewayId);}++ LOG.fine("Attribute event for a gateway descendant asset being forwarded to the gateway (assetRef=" + event.getRef() + ", gatewayId=" + gatewayId + ")");+ connector.sendMessageToGateway(+ new AttributeEvent(+ mapAssetId(gatewayId, event.getId(), true),+ event.getName(),+ event.getValue().orElse(null),+ event.getTimestamp())+ .setParentId(mapAssetId(gatewayId, event.getParentId(), true))+ .setRealm(event.getRealm()));++ // Consume this event as it is for a gateway descendant and we've sent it to that gateway for processing+ return true;}// Don't consume event for non gateway descendant assets@@ -530,14 +569,14 @@ public class GatewayService extends RouteBuilder implements ContainerService, Asswitch (persistenceEvent.getCause()) {- case CREATE:+ case CREATE -> {createUpdateGatewayServiceUser(gateway);synchronized (gatewayConnectorMap) {GatewayConnector connector = new GatewayConnector(assetStorageService, assetProcessingService, executorService, gateway);gatewayConnectorMap.put(gateway.getId().toLowerCase(Locale.ROOT), connector);}- break;- case UPDATE:+ }+ case UPDATE -> {// Check if this gateway has a connectorGatewayConnector connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));if (connector == null) {@@ -562,10 +601,10 @@ public class GatewayService extends RouteBuilder implements ContainerService, AscreateUpdateGatewayServiceUser(gateway);}}- break;- case DELETE:+ }+ case DELETE -> {// Check if this gateway has a connector- connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));+ GatewayConnector connector = gatewayConnectorMap.get(gateway.getId().toLowerCase(Locale.ROOT));if (connector == null) {break;}@@ -579,25 +618,23 @@ public class GatewayService extends RouteBuilder implements ContainerService, As}removeGatewayServiceUser(gateway);- break;+ }}}protected void processGatewayChildAssetChange(String gatewayId, Asset<?> childAsset, PersistenceEvent<Asset<?>> persistenceEvent) {// The asset would have been modified by the gateway connector so all we need to do here is update the id mapswitch (persistenceEvent.getCause()) {-- case CREATE:- case UPDATE:+ case CREATE, UPDATE -> {synchronized (assetIdGatewayIdMap) {assetIdGatewayIdMap.put(childAsset.getId(), gatewayId);}- break;- case DELETE:+ }+ case DELETE -> {synchronized (assetIdGatewayIdMap) {assetIdGatewayIdMap.remove(childAsset.getId());}- break;+ }}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.javaindex b32e4a0a0..6c5c607f6 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ActiveMQORSecurityManager.java@@ -25,7 +25,6 @@ import org.apache.activemq.artemis.core.security.Role;import org.apache.activemq.artemis.core.server.ActiveMQServer;import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;import org.apache.activemq.artemis.spi.core.security.ActiveMQJAASSecurityManager;-import org.apache.commons.lang3.tuple.Triple;import org.keycloak.KeycloakSecurityContext;import org.keycloak.adapters.KeycloakDeployment;import org.openremote.container.security.keycloak.KeycloakIdentityProvider;@@ -43,6 +42,7 @@ import java.util.logging.Level;import java.util.logging.Logger;import static org.apache.activemq.artemis.core.remoting.CertificateUtil.getCertsFromConnection;+import static org.openremote.manager.mqtt.MQTTBrokerService.connectionToString;import static org.openremote.model.syslog.SyslogCategory.API;/**@@ -119,9 +119,8 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {Subject subject = lc.getSubject();if (subject != null) {- // Ensure subject is available when afterCreateConnection is fired+ // Set subject here so any code that calls this method behaves like a normal ActiveMQ SecurityStoreImpl::authenticate callremotingConnection.setSubject(subject);-subject.getPrincipals().add(new RemotingConnectionPrincipal(remotingConnection));}@@ -184,7 +183,7 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {// See if a custom handler wants to handle authorisation for this topic pub/subfor (MQTTHandler handler : brokerService.getCustomHandlers()) {if (handler.handlesTopic(topic)) {- LOG.finest("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", " + brokerService.connectionToString(connection));+ LOG.finest("Passing topic to handler for " + (isWrite ? "pub" : "sub") + ": handler=" + handler.getName() + ", topic=" + topic + ", " + connectionToString(connection));boolean result;if (isWrite) {@@ -193,15 +192,15 @@ public class ActiveMQORSecurityManager extends ActiveMQJAASSecurityManager {result = handler.checkCanSubscribe(connection, securityContext, topic);}if (result) {- LOG.finest("Handler '" + handler.getName() + "' has authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + brokerService.connectionToString(connection));+ LOG.finest("Handler '" + handler.getName() + "' has authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(connection));} else {- LOG.finest("Handler '" + handler.getName() + "' has not authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + brokerService.connectionToString(connection));+ LOG.finest("Handler '" + handler.getName() + "' has not authorised " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(connection));}return result;}}- LOG.info("Un-supported request " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + brokerService.connectionToString(connection));+ LOG.info("Un-supported request " + (isWrite ? "pub" : "sub") + ": topic=" + topic + ", " + connectionToString(connection));return false;}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.javaindex 0b8024ef3..c085eaa0a 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/ConnectionMonitorHandler.java@@ -302,7 +302,7 @@ public class ConnectionMonitorHandler extends MQTTHandler {LOG.fine("Updating connected status for '" + userID + "' on " + attributeRefs.size() + " attribute(s) connected=" + connected);attributeRefs.forEach(attributeRef ->- assetProcessingService.sendAttributeEvent(new AttributeEvent(attributeRef, connected)));+ assetProcessingService.sendAttributeEvent(new AttributeEvent(attributeRef, connected), getClass().getSimpleName()));}protected Pair<String, Set<AttributeRef>> getUserIDAndAttributeRefs(RemotingConnection connection) {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.javaindex e45a3d0b5..452c642f4 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/DefaultMQTTHandler.java@@ -23,7 +23,6 @@ import com.google.common.cache.Cache;import com.google.common.cache.CacheBuilder;import io.netty.buffer.ByteBuf;import io.netty.handler.codec.mqtt.MqttQoS;-import org.apache.activemq.artemis.api.core.SimpleString;import org.apache.activemq.artemis.spi.core.protocol.RemotingConnection;import org.apache.activemq.artemis.utils.collections.ConcurrentHashSet;import org.apache.camel.builder.RouteBuilder;@@ -57,8 +56,6 @@ import static org.apache.camel.support.builder.PredicateBuilder.and;import static org.openremote.manager.event.ClientEventService.*;import static org.openremote.manager.mqtt.MQTTBrokerService.getConnectionIDString;import static org.openremote.model.Constants.*;-import static org.openremote.model.attribute.AttributeEvent.HEADER_SOURCE;-import static org.openremote.model.attribute.AttributeEvent.Source.CLIENT;import static org.openremote.model.syslog.SyslogCategory.API;/**@@ -93,6 +90,7 @@ public class DefaultMQTTHandler extends MQTTHandler {private static final Logger LOG = SyslogCategory.getLogger(API, DefaultMQTTHandler.class);protected final ConcurrentMap<String, SubscriberInfo> connectionSubscriberInfoMap = new ConcurrentHashMap<>();// An authorisation cache for publishing+ // TODO: Switch to caffeine library once ActiveMQ has migratedprotected final Cache<String, ConcurrentHashSet<String>> authorizationCache = CacheBuilder.newBuilder().maximumSize(100000).expireAfterWrite(300000, TimeUnit.MILLISECONDS)@@ -146,7 +144,7 @@ public class DefaultMQTTHandler extends MQTTHandler {// Put a close connection runnable into the headers for the client event serviceRunnable closeRunnable = () -> {if (mqttBrokerService != null) {- LOG.fine("Calling client session closed force disconnect runnable: " + mqttBrokerService.connectionToString(connection));+ LOG.fine("Calling client session closed force disconnect runnable: " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.doForceDisconnect(connection);}};@@ -452,7 +450,7 @@ public class DefaultMQTTHandler extends MQTTHandler {protected static AttributeEvent buildAttributeEvent(List<String> topicTokens, Object value) {String attributeName = topicTokens.get(3);String assetId = topicTokens.get(4);- return new AttributeEvent(assetId, attributeName, value);+ return new AttributeEvent(assetId, attributeName, value).setSource(DefaultMQTTHandler.class.getSimpleName());}protected static AssetFilter<?> buildAssetFilter(Topic topic) {@@ -551,7 +549,7 @@ public class DefaultMQTTHandler extends MQTTHandler {if (isAssetTopic) {String topicStr = topic.toString();String replaceToken = topicStr.endsWith(TOKEN_MULTI_LEVEL_WILDCARD) ? TOKEN_MULTI_LEVEL_WILDCARD : topicStr.endsWith(TOKEN_SINGLE_LEVEL_WILDCARD) ? TOKEN_SINGLE_LEVEL_WILDCARD : null;- topicExpander = ev -> replaceToken != null ? topicStr.replace(replaceToken, ((AssetEvent)ev).getAssetId()) : topicStr;+ topicExpander = ev -> replaceToken != null ? topicStr.replace(replaceToken, ((AssetEvent)ev).getId()) : topicStr;} else {String topicStr = topic.toString();boolean injectAttributeName = TOKEN_SINGLE_LEVEL_WILDCARD.equals(topicTokenIndexToString(topic, 3));@@ -563,9 +561,9 @@ public class DefaultMQTTHandler extends MQTTHandler {String replaceToken = topicStr.endsWith(TOKEN_MULTI_LEVEL_WILDCARD) ? TOKEN_MULTI_LEVEL_WILDCARD : topicStr.endsWith(TOKEN_SINGLE_LEVEL_WILDCARD) ? TOKEN_SINGLE_LEVEL_WILDCARD : null;String finalTopicStr = topicStr;topicExpander = ev -> {- String expanded = replaceToken != null ? finalTopicStr.replace(replaceToken, ((AttributeEvent)ev).getAssetId()) : finalTopicStr;+ String expanded = replaceToken != null ? finalTopicStr.replace(replaceToken, ((AttributeEvent)ev).getId()) : finalTopicStr;if (injectAttributeName) {- expanded = expanded.replace("$", ((AttributeEvent)ev).getAttributeName());+ expanded = expanded.replace("$", ((AttributeEvent)ev).getName());}return expanded;};@@ -607,7 +605,6 @@ public class DefaultMQTTHandler extends MQTTHandler {headers.put(SESSION_KEY, getConnectionIDString(connection));headers.put(HEADER_CONNECTION_TYPE, ClientEventService.HEADER_CONNECTION_TYPE_MQTT);headers.put(REALM_PARAM_NAME, requestRealm);- headers.put(HEADER_SOURCE, CLIENT);return headers;}}diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.javaindex 551d58ef1..a3ac8e029 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTBrokerService.java@@ -262,11 +262,12 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}});- // Create internal producer for producing and consuming messages+ // Create internal producer for producing messagesServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");ClientSessionFactory factory = serverLocator.createSessionFactory();String internalClientID = UniqueIdentifierGenerator.generateId("Internal client");internalSession = (ClientSessionInternal) factory.createSession(null, null, false, true, true, true, serverLocator.getAckBatchSize(), internalClientID);+ internalSession.addMetaData(ClientSession.JMS_SESSION_IDENTIFIER_PROPERTY, "Internal session");ServerSession serverSession = server.getActiveMQServer().getSessionByID(internalSession.getName());serverSession.disableSecurity();internalSession.start();@@ -507,7 +508,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,}}} catch (Exception e) {- LOG.log(WARNING, "Couldn't send AttributeEvent to MQTT client", e);+ LOG.log(WARNING, "Couldn't publish to MQTT client: topic=" + topic, e);}}@@ -524,7 +525,7 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,return ID instanceof ChannelId ? ((ChannelId) ID).asLongText() : ID.toString();}- public String connectionToString(RemotingConnection connection) {+ public static String connectionToString(RemotingConnection connection) {if (connection == null) {return "";}@@ -533,20 +534,13 @@ public class MQTTBrokerService extends RouteBuilder implements ContainerService,Subject subject = connection.getSubject();if (subject != null) {- username = getSubjectNameAndRealm(subject);+ username = getSubjectName(subject);}return "connection=" + connection.getRemoteAddress() + ", clientID=" + connection.getClientID() + ", subject=" + username;}public static String getSubjectName(Subject subject) {- return subject.getPrincipals().stream().filter(principal -> principal instanceof UserPrincipal)- .findFirst()- .map(Principal::getName)- .orElse(KeycloakIdentityProvider.getSubjectName(subject));- }-- public static String getSubjectNameAndRealm(Subject subject) {return subject.getPrincipals().stream().filter(principal -> principal instanceof UserPrincipal).findFirst().map(Principal::getName)diff --git a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.javaindex 5547b76de..4b3513bb6 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/MQTTHandler.java@@ -121,12 +121,14 @@ public abstract class MQTTHandler {String clientID = message.getStringProperty(MessageUtil.CONNECTION_ID_PROPERTY_NAME);RemotingConnection connection = mqttBrokerService.getConnectionFromClientID(clientID);+ // TODO: This is not ideal as publish has been accepted and then we drop the message if the client has disconnected before it is processed+ // Need to be able to get connection/auth from the message somehowif (connection == null) {LOG.log(DEBUG, "Client is no longer connected so dropping publish to topic '" + topic + "': clientID=" + clientID);return;}- getLogger().fine("Client published to '" + publishTopic + "': " + mqttBrokerService.connectionToString(connection));+ getLogger().fine("Client published to '" + publishTopic + "': " + MQTTBrokerService.connectionToString(connection));onPublish(connection, publishTopic, message.getReadOnlyBodyBuffer().byteBuf());});} catch (ActiveMQException e) {diff --git a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.javaindex 410169c01..5ac7e9986 100644--- a/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java+++ b/manager/src/main/java/org/openremote/manager/mqtt/UserAssetProvisioningMQTTHandler.java@@ -140,7 +140,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {public boolean checkCanSubscribe(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {// Skip standard checksif (!canSubscribe(connection, securityContext, topic)) {- getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", connection" + connection);+ getLogger().fine("Cannot subscribe to this topic, topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));return false;}return true;@@ -150,7 +150,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {public boolean checkCanPublish(RemotingConnection connection, KeycloakSecurityContext securityContext, Topic topic) {// Skip standard checksif (!canPublish(connection, securityContext, topic)) {- getLogger().fine("Cannot publish to this topic, topic=" + topic + ", connection" + connection);+ getLogger().fine("Cannot publish to this topic, topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));return false;}return true;@@ -215,7 +215,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {ProvisioningMessage provisioningMessage = ValueUtil.parse(payloadContent, ProvisioningMessage.class).orElseGet(() -> {- LOG.info("Failed to parse message from client: topic=" + topic + mqttBrokerService.connectionToString(connection));+ LOG.info("Failed to parse message from client: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.MESSAGE_INVALID), MqttQoS.AT_MOST_ONCE);return null;});@@ -257,10 +257,10 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {protected void processX509ProvisioningMessage(RemotingConnection connection, Topic topic, X509ProvisioningMessage provisioningMessage) {- LOG.fine("Processing provisioning message on: topic=" + topic + mqttBrokerService.connectionToString(connection));+ LOG.fine(() -> "Processing provisioning message on: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));if (TextUtil.isNullOrEmpty(provisioningMessage.getCert())) {- LOG.warning("Certificate is missing from X509 provisioning message: topic=" + topic + mqttBrokerService.connectionToString(connection));+ LOG.warning("Certificate is missing from X509 provisioning message: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -270,7 +270,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {try {clientCertificate = ProvisioningUtil.getX509Certificate(provisioningMessage.getCert());} catch (CertificateException e) {- LOG.log(Level.WARNING, "Failed to parse client X.509 certificate: topic=" + topic+ mqttBrokerService.connectionToString(connection), e);+ LOG.log(Level.WARNING, "Failed to parse X.509 certificate: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection), e);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CERTIFICATE_INVALID), MqttQoS.AT_MOST_ONCE);return;}@@ -278,14 +278,14 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {X509ProvisioningConfig matchingConfig = getMatchingX509ProvisioningConfig(connection, clientCertificate);if (matchingConfig == null) {- LOG.finest("No matching provisioning config found for client certificate: topic=" + topic+ mqttBrokerService.connectionToString(connection));+ LOG.fine("No matching provisioning config found for X.509 certificate");mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNAUTHORIZED), MqttQoS.AT_MOST_ONCE);return;}// Check if config is disabledif (matchingConfig.isDisabled()) {- LOG.finest("Matching provisioning config is disabled for client certificate: topic=" + topic+ mqttBrokerService.connectionToString(connection));+ LOG.fine("Matching provisioning config is disabled for X.509 certificate");mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.CONFIG_DISABLED), MqttQoS.AT_MOST_ONCE);return;}@@ -295,13 +295,13 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {String uniqueId = topicTokenIndexToString(topic, 1);if (TextUtil.isNullOrEmpty(certUniqueId)) {- LOG.finest("Client X.509 certificate missing unique ID in subject CN: topic=" + topic+ mqttBrokerService.connectionToString(connection));+ LOG.info(() -> "X.509 certificate missing unique ID in subject CN: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);return;}if (TextUtil.isNullOrEmpty(uniqueId) || !certUniqueId.equals(uniqueId)) {- LOG.finest("Client X.509 certificate unique ID doesn't match topic unique ID: topic=" + topic+ mqttBrokerService.connectionToString(connection));+ LOG.info(() -> "X.509 certificate unique ID doesn't match topic unique ID: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.UNIQUE_ID_MISMATCH), MqttQoS.AT_MOST_ONCE);return;}@@ -317,19 +317,23 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {User serviceUser;try {+ LOG.finest("Checking service user for this client");serviceUser = identityProvider.getUserByUsername(realm, User.SERVICE_ACCOUNT_PREFIX + serviceUsername);if (serviceUser != null) {if (!serviceUser.getEnabled()) {- LOG.finest("Client service user has been disabled: topic=" + topic+ mqttBrokerService.connectionToString(connection));+ LOG.info(() -> "Service user exists and has been disabled so cannot continue: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection));mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.USER_DISABLED), MqttQoS.AT_MOST_ONCE);return;}+ LOG.finest("Service user exists and is enabled");} else {+ LOG.fine("Creating service user");serviceUser = createClientServiceUser(realm, serviceUsername, matchingConfig);+ LOG.fine("Service user has been created: username=" + serviceUser.getUsername());}} catch (Exception e) {- LOG.log(Level.WARNING, "Failed to retrieve/create service user: topic=" + topic + mqttBrokerService.connectionToString(connection), e);+ LOG.log(Level.WARNING, "Failed to retrieve/create service user: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection), e);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);return;}@@ -338,32 +342,30 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {// Prepend realm name to unique ID to generate asset ID to further improve uniquenessString assetId = UniqueIdentifierGenerator.generateId(matchingConfig.getRealm() + uniqueId);- LOG.finest("Client unique ID '" + uniqueId + "' mapped to asset ID '" + assetId + "': topic=" + topic+ mqttBrokerService.connectionToString(connection));try {+ LOG.finest(() -> "Checking provisioned asset: assetId=" + assetId);// Look for existing assetasset = assetStorageService.find(assetId);if (asset != null) {- LOG.finest("Client asset found: topic=" + topic + mqttBrokerService.connectionToString(connection) + ", assetId=" + assetId);+ LOG.finest("Asset exists");if (!matchingConfig.getRealm().equals(asset.getRealm())) {- LOG.warning("Client asset realm mismatch : topic=" + topic + mqttBrokerService.connectionToString(connection) + ", assetId=" + assetId);+ LOG.warning("Client asset realm mismatch");mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.ASSET_ERROR), MqttQoS.AT_MOST_ONCE);return;}} else {- LOG.finest("Creating client asset: topic=" + topic + mqttBrokerService.connectionToString(connection) + ", assetId=" + assetId);+ LOG.finest("Asset doesn't exist so creating");asset = createClientAsset(realm, assetId, uniqueId, serviceUser, matchingConfig);}} catch (Exception e) {- LOG.log(Level.WARNING, "Failed to retrieve/create asset: topic=" + topic + mqttBrokerService.connectionToString(connection) + ", config=" + matchingConfig, e);+ LOG.log(Level.WARNING, "Failed to retrieve/create asset: topic=" + topic + ", " + MQTTBrokerService.connectionToString(connection) + ", config=" + matchingConfig, e);mqttBrokerService.publishMessage(getResponseTopic(topic), new ErrorResponseMessage(ErrorResponseMessage.Error.SERVER_ERROR), MqttQoS.AT_MOST_ONCE);return;}- LOG.fine("Client successfully initialised: topic=" + topic + mqttBrokerService.connectionToString(connection) + ", config=" + matchingConfig + ", username=" + serviceUsername + ", asset=" + asset);-// Authenticate the connection using this service user's credentials - this will also update the connection's subjectconnection.setSubject(null); // Clear existing anonymous subjectmqttBrokerService.securityManager.authenticate(realm + ":" + serviceUser.getUsername(), serviceUser.getSecret(), connection, null);@@ -377,6 +379,7 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {return connections;});+ LOG.fine("Client successfully provisioned");mqttBrokerService.publishMessage(getResponseTopic(topic), new SuccessResponseMessage(realm, asset), MqttQoS.AT_MOST_ONCE);}@@ -391,28 +394,28 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {X509Certificate caCertificate = config.getCertificate();if (caCertificate != null) {if (caCertificate.getSubjectX500Principal().getName().equals(clientCertificate.getIssuerX500Principal().getName())) {- LOG.fine("Client certificate issuer matches provisioning config CA certificate subject: connection=" + connection + ", config=" + config);+ LOG.finest(() -> "Client certificate issuer matches provisioning config CA certificate subject: " + MQTTBrokerService.connectionToString(connection) + ", config=" + config);Date now = Date.from(timerService.getNow());try {clientCertificate.verify(caCertificate.getPublicKey());- LOG.fine("Client certificate verified against CA certificate: connection=" + connection + ", config=" + config);+ LOG.finest(() -> "Client certificate verified against CA certificate: " + MQTTBrokerService.connectionToString(connection) + ", config=" + config);if (!config.getData().isIgnoreExpiryDate()) {- LOG.fine("Validating client certificate validity: connection=" + connection + ", timestamp=" + now);+ LOG.finest(() -> "Validating client certificate validity: " + MQTTBrokerService.connectionToString(connection) + ", timestamp=" + now);clientCertificate.checkValidity(now);}return true;} catch (CertificateExpiredException | CertificateNotYetValidException e) {- LOG.log(Level.INFO, "Client certificate failed validity check: connection=" + connection + ", timestamp=" + now, e);+ LOG.log(Level.INFO, "Client certificate failed validity check: " + MQTTBrokerService.connectionToString(connection) + ", timestamp=" + now, e);} catch (Exception e) {- LOG.log(Level.INFO, "Client certificate failed verification against CA certificate: connection=" + connection + ", config=" + config, e);+ LOG.log(Level.INFO, "Client certificate failed verification against CA certificate: " + MQTTBrokerService.connectionToString(connection) + ", config=" + config, e);}}}} catch (Exception e) {- LOG.log(Level.WARNING, "Failed to extract certificate from provisioning config: config=" + config, e);+ LOG.log(Level.WARNING, "Failed to extract certificate from provisioning config: " + MQTTBrokerService.connectionToString(connection) + ", config=" + config, e);}return false;})@@ -487,10 +490,10 @@ public class UserAssetProvisioningMQTTHandler extends MQTTHandler {// Force disconnect of each connection and the disconnect handler will remove the connection from the mapconnections.forEach(connection -> {try {- LOG.fine("Force disconnecting client that is using provisioning config ID '" + provisioningConfigId + "': " + mqttBrokerService.connectionToString(connection));+ LOG.fine("Force disconnecting client that is using provisioning config ID '" + provisioningConfigId + "': " + MQTTBrokerService.connectionToString(connection));connection.disconnect(false);} catch (Exception e) {- getLogger().log(Level.WARNING, "Failed to disconnect client: connection=" + connection, e);+ getLogger().log(Level.WARNING, "Failed to disconnect client: " + MQTTBrokerService.connectionToString(connection), e);}});connections.clear();diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.javaindex 0398e2b74..d5e7f4126 100644--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java@@ -30,7 +30,6 @@ import org.openremote.model.asset.Asset;import org.openremote.model.notification.AbstractNotificationMessage;import org.openremote.model.notification.EmailNotificationMessage;import org.openremote.model.notification.Notification;-import org.openremote.model.notification.NotificationSendResult;import org.openremote.model.query.UserQuery;import org.openremote.model.query.filter.RealmPredicate;import org.openremote.model.query.filter.StringPredicate;@@ -44,8 +43,8 @@ import java.util.logging.Logger;import java.util.stream.Collectors;import static org.openremote.container.util.MapAccess.*;-import static org.openremote.model.security.User.EMAIL_NOTIFICATIONS_DISABLED_ATTRIBUTE;import static org.openremote.model.Constants.*;+import static org.openremote.model.security.User.EMAIL_NOTIFICATIONS_DISABLED_ATTRIBUTE;public class EmailNotificationHandler implements NotificationHandler {@@ -279,13 +278,7 @@ public class EmailNotificationHandler implements NotificationHandler {}@Override- public NotificationSendResult sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message) {-- // Check handler is valid- if (!isValid()) {- LOG.warning("SMTP invalid configuration so ignoring");- return NotificationSendResult.failure("SMTP invalid configuration so ignoring");- }+ public void sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message) throws Exception {List<EmailNotificationMessage.Recipient> toRecipients = new ArrayList<>();List<EmailNotificationMessage.Recipient> ccRecipients = new ArrayList<>();@@ -294,20 +287,17 @@ public class EmailNotificationHandler implements NotificationHandler {String targetId = target.getId();switch (targetType) {-- case USER:- case ASSET:+ case USER, ASSET -> {// Recipient should be stored from earlier mapping call- EmailNotificationMessage.Recipient recipient = (EmailNotificationMessage.Recipient)target.getData();-+ EmailNotificationMessage.Recipient recipient = (EmailNotificationMessage.Recipient) target.getData();if (recipient == null) {LOG.warning("User or asset recipient missing: id=" + targetId);} else {LOG.finest("Adding to recipient: " + recipient);toRecipients.add(recipient);}- break;- case CUSTOM:+ }+ case CUSTOM ->// This recipient list is the target IDArrays.stream(targetId.split(";")).forEach(customRecipient -> {if (customRecipient.startsWith("to:")) {@@ -327,68 +317,53 @@ public class EmailNotificationHandler implements NotificationHandler {toRecipients.add(new EmailNotificationMessage.Recipient(customRecipient));}});-- break;- default:- LOG.warning("Target type not supported: " + targetType);+ default -> LOG.warning("Target type not supported: " + targetType);}- try {-- MimeMessage email = new MimeMessage(mailSession);+ MimeMessage email = new MimeMessage(mailSession);- if (!toRecipients.isEmpty()) {- for (EmailNotificationMessage.Recipient recipient : toRecipients) {- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {- email.addRecipient(Message.RecipientType.TO, convertRecipient(recipient));- }+ if (!toRecipients.isEmpty()) {+ for (EmailNotificationMessage.Recipient recipient : toRecipients) {+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {+ email.addRecipient(Message.RecipientType.TO, convertRecipient(recipient));}}- if (!ccRecipients.isEmpty()) {- for (EmailNotificationMessage.Recipient recipient : ccRecipients) {- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {- email.addRecipient(Message.RecipientType.CC, convertRecipient(recipient));- }+ }+ if (!ccRecipients.isEmpty()) {+ for (EmailNotificationMessage.Recipient recipient : ccRecipients) {+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {+ email.addRecipient(Message.RecipientType.CC, convertRecipient(recipient));}}- if (!bccRecipients.isEmpty()) {- for (EmailNotificationMessage.Recipient recipient : bccRecipients) {- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {- email.addRecipient(Message.RecipientType.BCC, convertRecipient(recipient));- }+ }+ if (!bccRecipients.isEmpty()) {+ for (EmailNotificationMessage.Recipient recipient : bccRecipients) {+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {+ email.addRecipient(Message.RecipientType.BCC, convertRecipient(recipient));}}+ }- buildEmail(id, (EmailNotificationMessage) message, email);+ buildEmail(id, (EmailNotificationMessage) message, email);- Address[] recipients = email.getAllRecipients();- if (recipients == null || recipients.length == 0) {- return NotificationSendResult.failure("No recipients set for " + targetType.name().toLowerCase() + ": " + targetId);- }-- // Set from based on source if not already set- if (email.getFrom() == null || email.getFrom().length == 0) {- email.setFrom(new InternetAddress(defaultFrom));- }+ Address[] recipients = email.getAllRecipients();+ if (recipients == null || recipients.length == 0) {+ throw new NotificationProcessingException(NotificationProcessingException.Reason.INVALID_MESSAGE, "No recipients set for " + targetType.name().toLowerCase() + ": " + targetId);+ }- return sendMessage(email);- } catch (Exception e) {- LOG.log(Level.WARNING, "Email send failed: " + e.getMessage(), e);- return NotificationSendResult.failure("Email send failed: " + e.getMessage());+ // Set from based on source if not already set+ if (email.getFrom() == null || email.getFrom().length == 0) {+ email.setFrom(new InternetAddress(defaultFrom));}++ sendMessage(email);}- protected NotificationSendResult sendMessage(Message email) {- try {- if (!mailTransport.isConnected()) {- mailTransport.connect();- }- mailTransport.sendMessage(email, email.getAllRecipients());- return NotificationSendResult.success();- } catch (Exception e) {- LOG.log(Level.WARNING, "Email send failed: " + e.getMessage(), e);- return NotificationSendResult.failure("Email send failed: " + e.getMessage());+ protected void sendMessage(Message email) throws Exception {+ if (!mailTransport.isConnected()) {+ mailTransport.connect();}+ mailTransport.sendMessage(email, email.getAllRecipients());}protected void buildEmail(long id, EmailNotificationMessage emailNotificationMessage, MimeMessage email) throws Exception {diff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/NotificationHandler.javaindex d27c3e12a..1a479c521 100644--- a/manager/src/main/java/org/openremote/manager/notification/NotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationHandler.java@@ -76,5 +76,5 @@ public interface NotificationHandler extends ContainerService {* The ID can be used by the {@link NotificationHandler} to update the delivered and/or acknowledged status of the notification* by calling {@link NotificationService#setNotificationDelivered} or {@link NotificationService#setNotificationAcknowleged}*/- NotificationSendResult sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message);+ void sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message) throws Exception;}diff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationProcessingException.java b/manager/src/main/java/org/openremote/manager/notification/NotificationProcessingException.javaindex aea6b94fa..6fe3e122b 100644--- a/manager/src/main/java/org/openremote/manager/notification/NotificationProcessingException.java+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationProcessingException.java@@ -99,4 +99,12 @@ public class NotificationProcessingException extends RuntimeException {public String getReasonPhrase() {return getReason() + (message != null ? " (" + message + ")": "");}++ @Override+ public String toString() {+ return NotificationProcessingException.class.getSimpleName() + "{" ++ "reason=" + reason ++ ", message='" + message + '\'' ++ '}';+ }}diff --git a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java b/manager/src/main/java/org/openremote/manager/notification/NotificationService.javaindex 560355978..0a7ff24f8 100644--- a/manager/src/main/java/org/openremote/manager/notification/NotificationService.java+++ b/manager/src/main/java/org/openremote/manager/notification/NotificationService.java@@ -19,12 +19,9 @@*/package org.openremote.manager.notification;-import org.apache.camel.Exchange;-import org.apache.camel.Processor;+import jakarta.persistence.Query;+import jakarta.persistence.TypedQuery;import org.apache.camel.builder.RouteBuilder;-import org.openremote.model.asset.agent.Protocol;-import org.openremote.model.Container;-import org.openremote.model.ContainerService;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.security.AuthContext;@@ -33,6 +30,8 @@ import org.openremote.manager.asset.AssetStorageService;import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.web.ManagerWebService;import org.openremote.model.Constants;+import org.openremote.model.Container;+import org.openremote.model.ContainerService;import org.openremote.model.asset.Asset;import org.openremote.model.notification.Notification;import org.openremote.model.notification.NotificationSendResult;@@ -44,8 +43,6 @@ import org.openremote.model.security.User;import org.openremote.model.util.TextUtil;import org.openremote.model.util.TimeUtil;-import jakarta.persistence.Query;-import jakarta.persistence.TypedQuery;import java.time.Instant;import java.time.temporal.ChronoUnit;import java.util.*;@@ -72,37 +69,6 @@ public class NotificationService extends RouteBuilder implements ContainerServicprotected MessageBrokerService messageBrokerService;protected Map<String, NotificationHandler> notificationHandlerMap = new HashMap<>();- protected static Processor handleNotificationProcessingException() {- return exchange -> {- Notification notification = exchange.getIn().getBody(Notification.class);- Exception exception = (Exception) exchange.getProperty(Exchange.EXCEPTION_CAUGHT);-- StringBuilder error = new StringBuilder();-- Notification.Source source = exchange.getIn().getHeader(HEADER_SOURCE, "unknown source", Notification.Source.class);- if (source != null) {- error.append("Error processing from ").append(source);- }-- String protocolName = exchange.getIn().getHeader(Protocol.SENSOR_QUEUE_SOURCE_PROTOCOL, String.class);- if (protocolName != null) {- error.append(" (protocol: ").append(protocolName).append(")");- }-- // TODO Better exception handling - dead letter queue?- if (exception instanceof NotificationProcessingException processingException) {- error.append(" - ").append(processingException.getReasonPhrase());- error.append(": ").append(notification.toString());- NotificationService.LOG.warning(error.toString());- } else {- error.append(": ").append(notification.toString());- NotificationService.LOG.log(Level.WARNING, error.toString(), exception);- }-- exchange.getMessage().setBody(false);- };- }-@Overridepublic int getPriority() {return ContainerService.DEFAULT_PRIORITY;@@ -143,7 +109,6 @@ public class NotificationService extends RouteBuilder implements ContainerServicfrom(NOTIFICATION_QUEUE).routeId("NotificationQueue")- .doTry().process(exchange -> {Notification notification = exchange.getIn().getBody(Notification.class);@@ -253,27 +218,30 @@ public class NotificationService extends RouteBuilder implements ContainerServiclong id = sentNotification.getId();try {- NotificationSendResult result = handler.sendMessage(+ handler.sendMessage(id,source,sourceId.get(),target,notification.getMessage());- if (result.isSuccess()) {- LOG.fine("Notification sent '" + id + "': " + target);- } else {- LOG.warning("Notification failed '" + id + "': " + target + ", reason=" + result.getMessage());- sentNotification.setError(TextUtil.isNullOrEmpty(result.getMessage()) ? "Unknown error" : result.getMessage());- }+ NotificationSendResult result = NotificationSendResult.success();+ LOG.fine("Notification sent '" + id + "': " + target);+// Merge the sent notification again with the message included just in case the handler modified the messagesentNotification.setMessage(notification.getMessage());- em.merge(sentNotification);+} catch (Exception e) {- LOG.log(Level.SEVERE,- "Notification handler threw an exception whilst sending notification '" + id + "'",- e);- sentNotification.setError(TextUtil.isNullOrEmpty(e.getMessage()) ? "Unknown error" : e.getMessage());+ NotificationProcessingException notificationProcessingException;+ if (e instanceof NotificationProcessingException) {+ notificationProcessingException = (NotificationProcessingException) e;+ } else {+ notificationProcessingException = new NotificationProcessingException(SEND_FAILURE, e.getMessage());+ }+ LOG.warning("Notification failed '" + id + "': " + target + ", reason=" + notificationProcessingException);+ sentNotification.setError(TextUtil.isNullOrEmpty(notificationProcessingException.getMessage()) ? "Unknown error" : notificationProcessingException.getMessage());+ throw(notificationProcessingException);+ } finally {em.merge(sentNotification);}return sentNotification.getError() == null;@@ -286,9 +254,13 @@ public class NotificationService extends RouteBuilder implements ContainerServicexchange.getMessage().setBody(success.get());})- .endDoTry()- .doCatch(NotificationProcessingException.class)- .process(handleNotificationProcessingException());+ .onException(Exception.class)+ .logStackTrace(false)+ .handled(true)+ .process(exchange -> {+ // Just notify sender in case of RequestReply+ exchange.getMessage().setBody(false);+ });}public boolean sendNotification(Notification notification) {diff --git a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.javaindex c421b5f83..214fdb55e 100644--- a/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java+++ b/manager/src/main/java/org/openremote/manager/notification/PushNotificationHandler.java@@ -39,7 +39,6 @@ import org.openremote.model.asset.impl.ConsoleAsset;import org.openremote.model.console.ConsoleProvider;import org.openremote.model.notification.AbstractNotificationMessage;import org.openremote.model.notification.Notification;-import org.openremote.model.notification.NotificationSendResult;import org.openremote.model.notification.PushNotificationMessage;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.UserQuery;@@ -54,7 +53,6 @@ import java.nio.file.Files;import java.nio.file.Paths;import java.util.*;import java.util.concurrent.ConcurrentHashMap;-import java.util.logging.Level;import java.util.logging.Logger;import java.util.stream.Collectors;@@ -295,27 +293,24 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio}@Override- public NotificationSendResult sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message) {+ public void sendMessage(long id, Notification.Source source, String sourceId, Notification.Target target, AbstractNotificationMessage message) throws Exception {Notification.TargetType targetType = target.getType();String targetId = target.getId();if (targetType != Notification.TargetType.ASSET && targetType != Notification.TargetType.CUSTOM) {- LOG.warning("Target type not supported: " + targetType);- return NotificationSendResult.failure("Target type not supported: " + targetType);- }-- if (!isValid()) {- LOG.warning("FCM invalid configuration so ignoring");- return NotificationSendResult.failure("FCM invalid configuration so ignoring");+ String msg = "Target type not supported: " + targetType;+ LOG.warning(msg);+ throw new Exception(msg);}// Check this asset has an FCM token (i.e. it is registered for push notifications)String fcmToken = consoleFCMTokenMap.get(targetId);if (TextUtil.isNullOrEmpty(fcmToken)) {- LOG.finer("No FCM token found for console: " + targetId);- return NotificationSendResult.failure("No FCM token found for console: " + targetId);+ String msg = "No FCM token found for console: " + targetId;+ LOG.finer(msg);+ throw new Exception(msg);}PushNotificationMessage pushMessage = (PushNotificationMessage) message;@@ -326,19 +321,18 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatio}switch (pushMessage.getTargetType()) {- case DEVICE:+ case DEVICE ->// Always use fcm token from the console asset (so users cannot target other devices)pushMessage.setTarget(fcmToken);- break;- case TOPIC:- // TODO: Decide how to handle FCM topic support (too much power for users to put anything in target)- break;- case CONDITION:- // TODO: Decide how to handle conditions support (too much power for users to put anything in target)- break;+ case TOPIC -> {+ }+ // TODO: Decide how to handle FCM topic support (too much power for users to put anything in target)+ case CONDITION -> {+ }+ // TODO: Decide how to handle conditions support (too much power for users to put anything in target)}- return sendMessage(buildFCMMessage(id, pushMessage));+ sendMessage(buildFCMMessage(id, pushMessage));}// public NotificationSendResult sendMessage(PushNotificationMessage.TargetType targetType, String fcmTarget, Message.Builder messageBuilder) {@@ -370,14 +364,8 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatioreturn valid;}- public NotificationSendResult sendMessage(Message message) {- try {- FirebaseMessaging.getInstance().send(message);- return NotificationSendResult.success();- } catch (FirebaseMessagingException e) {- handleFcmException(e);- return NotificationSendResult.failure("FCM send failed: " + e.getErrorCode());- }+ public void sendMessage(Message message) throws Exception {+ FirebaseMessaging.getInstance().send(message);}protected boolean isConsoleSubscribedToTopic(ConsoleAsset consoleAsset, String topic) {@@ -508,21 +496,4 @@ public class PushNotificationHandler extends RouteBuilder implements Notificatiocase CREATE, UPDATE -> getFcmToken(asset).ifPresent(token -> consoleFCMTokenMap.put(asset.getId(), token));}}-- protected void handleFcmException(FirebaseMessagingException e) {-- LOG.log(Level.WARNING, "FCM send failed: " + e.getErrorCode(), e);--// // TODO: Implement backoff and blacklisting-// switch (e.getErrorCode()) {-//-// case INVALID_ARGUMENT:-// case UNAUTHENTICATED:-// LOG.severe("FCM critical error so marking FCM as invalid no more messages will be sent");-// break;-// case UNAVAILABLE:-// case INTERNAL:-// break;-// }- }}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateConsumer.java b/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateConsumer.javaindex 8fba2440a..40c3b0f42 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateConsumer.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateConsumer.java@@ -27,6 +27,6 @@ public interface AssetLocationPredicateConsumer {/*** Called when one or more asset's location predicate rules change; the initialising flag indicates if the*/- void accept(RulesEngine t, List<RulesEngine.AssetStateLocationPredicates> u);+ void accept(RulesEngine t, List<RulesEngine.AssetLocationPredicates> u);}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateProcessor.java b/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateProcessor.javaindex 245846668..02111feeb 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateProcessor.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetLocationPredicateProcessor.java@@ -24,6 +24,6 @@ import java.util.List;@FunctionalInterfacepublic interface AssetLocationPredicateProcessor {- void accept(RulesEngine t, List<RulesEngine.AssetStateLocationPredicates> u);+ void accept(RulesEngine t, List<RulesEngine.AssetLocationPredicates> u);}diff --git a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.javaindex 1443a1f8b..23a5cc4e6 100644--- a/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java+++ b/manager/src/main/java/org/openremote/manager/rules/AssetQueryPredicate.java@@ -22,11 +22,11 @@ package org.openremote.manager.rules;import com.fasterxml.jackson.databind.JsonNode;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetStorageService;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.attribute.MetaMap;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.LogicGroup;import org.openremote.model.query.filter.*;-import org.openremote.model.rules.AssetState;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaHolder;import org.openremote.model.value.NameValueHolder;@@ -38,9 +38,9 @@ import java.util.function.Predicate;import java.util.function.Supplier;/**- * Test an {@link AssetState} with a {@link AssetQuery}.+ * Test an {@link AttributeInfo} with a {@link AssetQuery}.*/-public class AssetQueryPredicate implements Predicate<AssetState<?>> {+public class AssetQueryPredicate implements Predicate<AttributeInfo> {final protected AssetQuery query;final protected TimerService timerService;@@ -60,7 +60,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {}@Override- public boolean test(AssetState<?> assetState) {+ public boolean test(AttributeInfo assetState) {if (query.ids != null && query.ids.length > 0) {if (Arrays.stream(query.ids).noneMatch(id -> assetState.getId().equals(id))) {@@ -104,7 +104,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {if (query.attributes != null) {// TODO: LogicGroup AND doesn't make much sense when applying to a single asset state- Set<AssetState<?>> matches = asAssetStateMatcher(timerService::getCurrentTimeMillis, query.attributes).apply(Collections.singleton(assetState));+ Set<AttributeInfo> matches = asAttributeMatcher(timerService::getCurrentTimeMillis, query.attributes).apply(Collections.singleton(assetState));if (matches == null) {return false;}@@ -118,7 +118,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {return true;}- public static Predicate<AssetState<?>> asPredicate(ParentPredicate predicate) {+ public static Predicate<AttributeInfo> asPredicate(ParentPredicate predicate) {return assetState ->Objects.equals(predicate.id, assetState.getParentId());}@@ -127,7 +127,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {return givenPath -> Arrays.equals(predicate.path, givenPath);}- public static Predicate<AssetState<?>> asPredicate(RealmPredicate predicate) {+ public static Predicate<AttributeInfo> asPredicate(RealmPredicate predicate) {return assetState ->predicate == null || (predicate.name != null && predicate.name.equals(assetState.getRealm()));}@@ -180,28 +180,28 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {}/**- * A function for matching {@link AssetState}s of an asset; the states must be related to the same asset to allow+ * A function for matching {@link AttributeInfo}s of an asset; the infos must be related to the same asset to allow* {@link LogicGroup.Operator#AND} to be applied.* @return The matched asset states or null if there is no match*/@SuppressWarnings("unchecked")- public static Function<Collection<AssetState<?>>, Set<AssetState<?>>> asAssetStateMatcher(Supplier<Long> currentMillisProducer, LogicGroup<AttributePredicate> condition) {+ public static Function<Collection<AttributeInfo>, Set<AttributeInfo>> asAttributeMatcher(Supplier<Long> currentMillisProducer, LogicGroup<AttributePredicate> condition) {if (groupIsEmpty(condition)) {return as -> Collections.EMPTY_SET;}LogicGroup.Operator operator = condition.operator == null ? LogicGroup.Operator.AND : condition.operator;- List<Function<Collection<AssetState<?>>, Set<AssetState<?>>>> assetStateMatchers = new ArrayList<>();- List<Predicate<AssetState<?>>> attributePredicates = new ArrayList<>();+ List<Function<Collection<AttributeInfo>, Set<AttributeInfo>>> assetStateMatchers = new ArrayList<>();+ List<Predicate<AttributeInfo>> attributePredicates = new ArrayList<>();if (condition.getItems().size() > 0) {condition.getItems().stream().forEach(p -> {- attributePredicates.add((Predicate<AssetState<?>>)(Predicate)asPredicate(currentMillisProducer, p));+ attributePredicates.add((Predicate<AttributeInfo>)(Predicate)asPredicate(currentMillisProducer, p));- AtomicReference<Predicate<AssetState<?>>> metaPredicate = new AtomicReference<>(nameValueHolder -> true);- AtomicReference<Predicate<AssetState<?>>> oldValuePredicate = new AtomicReference<>(value -> true);+ AtomicReference<Predicate<AttributeInfo>> metaPredicate = new AtomicReference<>(nameValueHolder -> true);+ AtomicReference<Predicate<AttributeInfo>> oldValuePredicate = new AtomicReference<>(value -> true);if (p.meta != null) {final Predicate<NameValueHolder<?>> innerMetaPredicate = Arrays.stream(p.meta)@@ -228,7 +228,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {if (operator == LogicGroup.Operator.AND) {// All predicates must match at least one of the asset's stateassetStateMatchers.add(assetStates -> {- Set<AssetState<?>> matchedAssetStates = new HashSet<>();+ Set<AttributeInfo> matchedAssetStates = new HashSet<>();boolean allPredicatesMatch = attributePredicates.stream().allMatch(attributePredicate -> {// Find the first match as an attribute predicate shouldn't match more than one asset statereturn assetStates.stream().filter(attributePredicate).findFirst().map(matchedAssetState -> {@@ -241,7 +241,7 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {} else {// Any of the predicates must match at least one of the asset's stateassetStateMatchers.add(assetStates -> {- AtomicReference<AssetState<?>> firstMatch = new AtomicReference<>();+ AtomicReference<AttributeInfo> firstMatch = new AtomicReference<>();boolean anyPredicateMatch = attributePredicates.stream().anyMatch(attributePredicate -> {// Find the first match as an attribute predicate shouldn't match more than one asset statereturn assetStates.stream().filter(attributePredicate).findFirst().map(matchedAssetState -> {@@ -258,15 +258,15 @@ public class AssetQueryPredicate implements Predicate<AssetState<?>> {if (condition.groups != null && condition.groups.size() > 0) {assetStateMatchers.addAll(condition.groups.stream()- .map(c -> asAssetStateMatcher(currentMillisProducer, c)).toList()+ .map(c -> asAttributeMatcher(currentMillisProducer, c)).toList());}return assetStates -> {- Set<AssetState<?>> matchedStates = new HashSet<>();+ Set<AttributeInfo> matchedStates = new HashSet<>();- for (Function<Collection<AssetState<?>>, Set<AssetState<?>>> matcher : assetStateMatchers) {- Set<AssetState<?>> matcherMatchedStates = matcher.apply(assetStates);+ for (Function<Collection<AttributeInfo>, Set<AttributeInfo>> matcher : assetStateMatchers) {+ Set<AttributeInfo> matcherMatchedStates = matcher.apply(assetStates);if (matcherMatchedStates != null) {// We have a matchdiff --git a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.javaindex 5fa938415..225a237f6 100644--- a/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java+++ b/manager/src/main/java/org/openremote/manager/rules/JsonRulesBuilder.java@@ -25,6 +25,8 @@ import org.openremote.manager.asset.AssetStorageService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.asset.UserAssetLink;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.attribute.AttributeRef;import org.openremote.model.geo.GeoJSONPoint;import org.openremote.model.notification.EmailNotificationMessage;@@ -78,7 +80,7 @@ public class JsonRulesBuilder extends RulesBuilder {}/**- * Stores all state for a given {@link RuleCondition} and calculates which {@link AssetState}s match and don't+ * Stores all state for a given {@link RuleCondition} and calculates which {@link AttributeInfo}s match and don't* match the condition.*/class RuleConditionState {@@ -89,10 +91,10 @@ public class JsonRulesBuilder extends RulesBuilder {AssetQuery.OrderBy orderBy;int limit;LogicGroup<AttributePredicate> attributePredicates = null;- Function<Collection<AssetState<?>>, Set<AssetState<?>>> assetPredicate = null;- Set<AssetState<?>> unfilteredAssetStates = new HashSet<>();- Set<AssetState<?>> previouslyMatchedAssetStates = new HashSet<>();- Set<AssetState<?>> previouslyUnmatchedAssetStates;+ Function<Collection<AttributeInfo>, Set<AttributeInfo>> assetPredicate = null;+ Set<AttributeInfo> unfilteredAssetStates = new HashSet<>();+ Set<AttributeInfo> previouslyMatchedAssetStates = new HashSet<>();+ Set<AttributeInfo> previouslyUnmatchedAssetStates;Predicate<Long> timePredicate;RuleConditionEvaluationResult lastEvaluationResult;@@ -191,7 +193,7 @@ public class JsonRulesBuilder extends RulesBuilder {// Only supports a single level or logic group for attributes (i.e. cannot nest groups in the UI so// don't support it here either)attributePredicates.groups = null;- assetPredicate = AssetQueryPredicate.asAssetStateMatcher(timerService::getCurrentTimeMillis, attributePredicates);+ assetPredicate = AssetQueryPredicate.asAttributeMatcher(timerService::getCurrentTimeMillis, attributePredicates);}ruleCondition.assets.orderBy = null;ruleCondition.assets.limit = 0;@@ -260,22 +262,22 @@ public class JsonRulesBuilder extends RulesBuilder {return;}- List<AssetState<?>> matchedAssetStates;- List<AssetState<?>> unmatchedAssetStates = Collections.emptyList();+ List<AttributeInfo> matchedAssetStates;+ List<AttributeInfo> unmatchedAssetStates = Collections.emptyList();Collection<String> unmatchedAssetIds = Collections.emptyList();if (attributePredicates == null) {matchedAssetStates = new ArrayList<>(unfilteredAssetStates);} else {- Map<Boolean, List<AssetState<?>>> results = new HashMap<>();- ArrayList<AssetState<?>> matched = new ArrayList<>();- ArrayList<AssetState<?>> unmatched = new ArrayList<>();+ Map<Boolean, List<AttributeInfo>> results = new HashMap<>();+ ArrayList<AttributeInfo> matched = new ArrayList<>();+ ArrayList<AttributeInfo> unmatched = new ArrayList<>();results.put(true, matched);results.put(false, unmatched);- unfilteredAssetStates.stream().collect(Collectors.groupingBy(AssetState::getId)).forEach((id, states) -> {- Set<AssetState<?>> matches = assetPredicate.apply(states);+ unfilteredAssetStates.stream().collect(Collectors.groupingBy(AttributeInfo::getId)).forEach((id, states) -> {+ Set<AttributeInfo> matches = assetPredicate.apply(states);if (matches != null) {matched.addAll(matches);unmatched.addAll(states.stream().filter(matches::contains).collect(Collectors.toSet()));@@ -301,7 +303,7 @@ public class JsonRulesBuilder extends RulesBuilder {// Remove previous matches where the asset state no longer matchespreviouslyMatchedAssetStates.removeIf(previousAssetState -> {- Optional<AssetState<?>> matched = matchedAssetStates.stream()+ Optional<AttributeInfo> matched = matchedAssetStates.stream().filter(matchedAssetState -> Objects.equals(previousAssetState, matchedAssetState)).findFirst();@@ -330,7 +332,7 @@ public class JsonRulesBuilder extends RulesBuilder {matchedAssetStates.removeIf(previouslyMatchedAssetStates::contains);// Select unique asset states based on asset id- Stream<AssetState<?>> matchedAssetStateStream = matchedAssetStates.stream().filter(distinctByKey(AssetState::getId));+ Stream<AttributeInfo> matchedAssetStateStream = matchedAssetStates.stream().filter(distinctByKey(AttributeInfo::getId));// Order asset states before applying limitif (orderBy != null) {@@ -340,15 +342,15 @@ public class JsonRulesBuilder extends RulesBuilder {matchedAssetStateStream = matchedAssetStateStream.limit(limit);}- Collection<String> matchedAssetIds = matchedAssetStateStream.map(AssetState::getId).collect(Collectors.toList());+ Collection<String> matchedAssetIds = matchedAssetStateStream.map(AttributeInfo::getId).collect(Collectors.toList());if (trackUnmatched) {// Select unique asset states based on asset id- Stream<AssetState<?>> unmatchedAssetStateStream = unmatchedAssetStates.stream().filter(distinctByKey(AssetState::getId));+ Stream<AttributeInfo> unmatchedAssetStateStream = unmatchedAssetStates.stream().filter(distinctByKey(AttributeInfo::getId));// Filter out unmatched asset ids that are in the matched listunmatchedAssetIds = unmatchedAssetStateStream- .map(AssetState::getId)+ .map(AttributeInfo::getId).filter(id -> !matchedAssetIds.contains(id)).collect(Collectors.toList());}@@ -379,12 +381,12 @@ public class JsonRulesBuilder extends RulesBuilder {*/static class RuleConditionEvaluationResult {boolean matches;- Collection<AssetState<?>> matchedAssetStates;- Collection<AssetState<?>> unmatchedAssetStates;+ Collection<AttributeInfo> matchedAssetStates;+ Collection<AttributeInfo> unmatchedAssetStates;Collection<String> matchedAssetIds;Collection<String> unmatchedAssetIds;- public RuleConditionEvaluationResult(boolean matches, Collection<AssetState<?>> matchedAssetStates, Collection<String> matchedAssetIds, Collection<AssetState<?>> unmatchedAssetStates, Collection<String> unmatchedAssetIds) {+ public RuleConditionEvaluationResult(boolean matches, Collection<AttributeInfo> matchedAssetStates, Collection<String> matchedAssetIds, Collection<AttributeInfo> unmatchedAssetStates, Collection<String> unmatchedAssetIds) {this.matches = matches;this.matchedAssetStates = matchedAssetStates;this.matchedAssetIds = matchedAssetIds;@@ -842,7 +844,7 @@ public class JsonRulesBuilder extends RulesBuilder {String finalBody = body;Collection<Notification> customNotifications = userIds.stream().map(userId -> {// Extract asset states for matched asset IDs that are linked to this user- Map<String, Set<AssetState<?>>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, userAssetLinks, userId);+ Map<String, Set<AttributeInfo>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, userAssetLinks, userId);Notification customNotification = ValueUtil.clone(notification);String newBody = insertTriggeredAssetInfo(finalBody, assetStates, isHtml, false);@@ -883,7 +885,7 @@ public class JsonRulesBuilder extends RulesBuilder {// Inject triggered asset info if neededif (bodyContainsTriggeredAssetInfo) {// Extract asset states for matched asset IDs- Map<String, Set<AssetState<?>>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, null, null);+ Map<String, Set<AttributeInfo>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, null, null);body = insertTriggeredAssetInfo(body, assetStates, isHtml, false);if (isEmail) {@@ -914,7 +916,7 @@ public class JsonRulesBuilder extends RulesBuilder {// Replace %TRIGGER_ASSETS% with the triggered assets in JSON format.if (!TextUtil.isNullOrEmpty(webhook.getPayload()) && webhook.getPayload().contains(PLACEHOLDER_TRIGGER_ASSETS)) {- Map<String, Set<AssetState<?>>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, null, null);+ Map<String, Set<AttributeInfo>> assetStates = getMatchedAssetStates(ruleState, useUnmatched, null, null);String triggeredAssetInfoPayload = insertTriggeredAssetInfo(webhook.getPayload(), assetStates, false, true);webhook.setPayload(triggeredAssetInfoPayload);}@@ -986,7 +988,7 @@ public class JsonRulesBuilder extends RulesBuilder {} else {matchingAssetIds = facts.matchAssetState(ruleAction.target.assets)- .map(AssetState::getId)+ .map(AttributeInfo::getId).distinct().collect(Collectors.toList());}@@ -997,7 +999,7 @@ public class JsonRulesBuilder extends RulesBuilder {}// Look for the current value within the asset state facts (asset/attribute has to be in scope of this rule engine and have a rule state meta item)- List<AssetState<?>> matchingAssetStates = matchingAssetIds+ List<AttributeInfo> matchingAssetStates = matchingAssetIds.stream().map(assetId ->facts.getAssetStates()@@ -1092,7 +1094,7 @@ public class JsonRulesBuilder extends RulesBuilder {return null;}- protected Map<String, Set<AssetState<?>>> getMatchedAssetStates(RuleState ruleState, boolean useUnmatched, Collection<UserAssetLink> userAssetLinks, String userId) {+ protected Map<String, Set<AttributeInfo>> getMatchedAssetStates(RuleState ruleState, boolean useUnmatched, Collection<UserAssetLink> userAssetLinks, String userId) {Set<String> assetIds = useUnmatched ? ruleState.otherwiseMatchedAssetIds : ruleState.thenMatchedAssetIds;return assetIds == null || assetIds.isEmpty()@@ -1100,14 +1102,14 @@ public class JsonRulesBuilder extends RulesBuilder {: ruleState.conditionStateMap.values().stream().filter(conditionState -> conditionState.lastEvaluationResult.matches).flatMap(conditionState -> {- Collection<AssetState<?>> as = useUnmatched+ Collection<AttributeInfo> as = useUnmatched? conditionState.lastEvaluationResult.unmatchedAssetStates: conditionState.lastEvaluationResult.matchedAssetStates;return as.stream();})// Get the asset states that are in the assetId list and optionally linked to this user.filter(assetState -> assetIds.contains(assetState.getId()) && (userAssetLinks == null || userAssetLinks.stream().anyMatch(ual -> ual.getId().getAssetId().equals(assetState.getId()) && ual.getId().getUserId().equals(userId))))- .collect(Collectors.groupingBy(AssetState::getId, Collectors.toSet()));+ .collect(Collectors.groupingBy(AttributeInfo::getId, Collectors.toSet()));}protected String getRealm() {@@ -1120,7 +1122,7 @@ public class JsonRulesBuilder extends RulesBuilder {return realm;}- protected String insertTriggeredAssetInfo(String sourceText, Map<String, Set<AssetState<?>>> assetStates, boolean isHtml, boolean isJson) {+ protected String insertTriggeredAssetInfo(String sourceText, Map<String, Set<AttributeInfo>> assetStates, boolean isHtml, boolean isJson) {StringBuilder sb = new StringBuilder();if (isHtml) {@@ -1140,7 +1142,7 @@ public class JsonRulesBuilder extends RulesBuilder {sb.append("</table>");} else if (isJson) {try {- return ValueUtil.JSON.writeValueAsString(assetStates);+ return ValueUtil.JSON.writerWithView(AttributeEvent.Enhanced.class).writeValueAsString(assetStates);} catch (Exception e) {LOG.warning(e.getMessage());}@@ -1181,7 +1183,7 @@ public class JsonRulesBuilder extends RulesBuilder {if (target.matchedAssets != null) {return facts.matchAssetState(target.matchedAssets)- .map(AssetState::getId)+ .map(AttributeInfo::getId).distinct().filter(compareAssetIds::contains).collect(Collectors.toList());diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.javaindex 067c4adec..ced5cc15b 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesEngine.java@@ -40,6 +40,8 @@ import org.openremote.manager.security.ManagerIdentityService;import org.openremote.manager.webhook.WebhookService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;+import org.openremote.model.attribute.AttributeInfo;+import org.openremote.model.attribute.AttributeRef;import org.openremote.model.query.filter.GeofencePredicate;import org.openremote.model.query.filter.LocationAttributePredicate;import org.openremote.model.rules.*;@@ -58,13 +60,13 @@ import static org.openremote.model.rules.RulesetStatus.*;public class RulesEngine<T extends Ruleset> {/**- * Allows rule deployments to track changes to the {@link AssetState}s in scope+ * Allows rule deployments to track changes to the {@link AttributeInfo}s in scope*/public static final class AssetStateChangeEvent {public PersistenceEvent.Cause cause;- public AssetState<?> assetState;+ public AttributeInfo assetState;- public AssetStateChangeEvent(PersistenceEvent.Cause cause, AssetState<?> assetState) {+ public AssetStateChangeEvent(PersistenceEvent.Cause cause, AttributeInfo assetState) {this.cause = cause;this.assetState = assetState;}@@ -73,12 +75,12 @@ public class RulesEngine<T extends Ruleset> {/*** Identifies a set of {@link LocationAttributePredicate}s associated with a particular {@link Asset}*/- public static final class AssetStateLocationPredicates {+ public static final class AssetLocationPredicates {final String assetId;final Set<GeofencePredicate> locationPredicates;- public AssetStateLocationPredicates(String assetId, Set<GeofencePredicate> locationPredicates) {+ public AssetLocationPredicates(String assetId, Set<GeofencePredicate> locationPredicates) {this.assetId = assetId;this.locationPredicates = locationPredicates;}@@ -152,7 +154,7 @@ public class RulesEngine<T extends Ruleset> {AssetsFacade<T> assetsFacade = new AssetsFacade<>(id, assetStorageService, attributeEvent -> {try {- assetProcessingService.sendAttributeEvent(attributeEvent);+ assetProcessingService.sendAttributeEvent(attributeEvent, getClass().getSimpleName());} catch (Exception e) {LOG.log(Level.SEVERE, "Failed to dispatch attribute event");}@@ -210,14 +212,14 @@ public class RulesEngine<T extends Ruleset> {/*** @return a shallow copy of the asset state facts.*/- public synchronized Set<AssetState<?>> getAssetStates() {+ public synchronized Set<AttributeInfo> getAssetStates() {return new HashSet<>(facts.getAssetStates());}/*** @return a shallow copy of the asset event facts.*/- public synchronized List<TemporaryFact<AssetState<?>>> getAssetEvents() {+ public synchronized List<TemporaryFact<AttributeInfo>> getAssetEvents() {return new ArrayList<>(facts.getAssetEvents());}@@ -319,7 +321,7 @@ public class RulesEngine<T extends Ruleset> {return;}- LOG.info("Starting");+ LOG.info("Starting: " + id);running = true;trackLocationPredicates(true);@@ -360,7 +362,7 @@ public class RulesEngine<T extends Ruleset> {return;}running = false;- LOG.info("Stopping");+ LOG.info("Stopping: " + id);if (fireTimer != null) {fireTimer.cancel(true);fireTimer = null;@@ -527,33 +529,37 @@ public class RulesEngine<T extends Ruleset> {}}- public synchronized void updateOrInsertAssetState(AssetState<?> assetState, boolean insert) {- facts.putAssetState(assetState);+ public synchronized void updateOrInsertAttributeInfo(AttributeInfo attributeInfo, boolean insert) {+ facts.putAssetState(attributeInfo);// Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates- trackLocationPredicates(trackLocationPredicates || (insert && assetState.getName().equals(Asset.LOCATION.getName())));- notifyAssetStatesChanged(new AssetStateChangeEvent(insert ? PersistenceEvent.Cause.CREATE : PersistenceEvent.Cause.UPDATE, assetState));+ trackLocationPredicates(trackLocationPredicates || (insert && attributeInfo.getName().equals(Asset.LOCATION.getName())));+ notifyAssetStatesChanged(new AssetStateChangeEvent(insert ? PersistenceEvent.Cause.CREATE : PersistenceEvent.Cause.UPDATE, attributeInfo));if (running) {scheduleFire(true);}}- public synchronized void removeAssetState(AssetState<?> assetState) {- facts.removeAssetState(assetState);+ public synchronized void removeAttributeInfo(AttributeInfo attributeInfo) {+ facts.removeAssetState(attributeInfo);// Make sure location predicate tracking is activated before notifying the deployments otherwise they won't report location predicates- trackLocationPredicates(trackLocationPredicates || assetState.getName().equals(Asset.LOCATION.getName()));- notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.DELETE, assetState));+ trackLocationPredicates(trackLocationPredicates || attributeInfo.getName().equals(Asset.LOCATION.getName()));+ notifyAssetStatesChanged(new AssetStateChangeEvent(PersistenceEvent.Cause.DELETE, attributeInfo));if (running) {scheduleFire(true);}}- public synchronized void insertAssetEvent(long expiresMillis, AssetState<?> assetState) {- facts.insertAssetEvent(expiresMillis, assetState);+ public synchronized void insertAttributeEvent(long expiresMillis, AttributeInfo attributeInfo) {+ facts.insertAttributeEvent(expiresMillis, attributeInfo);if (running) {scheduleFire(true);}}+ public synchronized void removeAttributeEvents(AttributeRef attributeRef) {+ facts.removeAttributeEvents(attributeRef);+ }+protected void updateDeploymentInfo() {deploymentInfo = Arrays.toString(deployments.values().stream()@@ -563,8 +569,8 @@ public class RulesEngine<T extends Ruleset> {}protected synchronized void printSessionStats() {- Collection<AssetState<?>> assetStateFacts = facts.getAssetStates();- Collection<TemporaryFact<AssetState<?>>> assetEventFacts = facts.getAssetEvents();+ Collection<AttributeInfo> assetStateFacts = facts.getAssetStates();+ Collection<TemporaryFact<AttributeInfo>> assetEventFacts = facts.getAssetEvents();Map<String, Object> namedFacts = facts.getNamedFacts();Collection<Object> anonFacts = facts.getAnonymousFacts();long temporaryFactsCount = facts.getTemporaryFacts().count();@@ -583,7 +589,7 @@ public class RulesEngine<T extends Ruleset> {/*** This is called with all the asset's that have a location attribute currently loaded into this engine.*/- protected void processLocationRules(List<AssetStateLocationPredicates> assetStateLocationPredicates) {+ protected void processLocationRules(List<AssetLocationPredicates> assetStateLocationPredicates) {if (assetLocationPredicatesConsumer != null) {assetLocationPredicatesConsumer.accept(this, assetStateLocationPredicates);}diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.javaindex 20c87cbbb..d691b6966 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesFacts.java@@ -28,9 +28,10 @@ import org.openremote.manager.asset.AssetStorageService;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetEvent;import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeInfo;+import org.openremote.model.attribute.AttributeRef;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.filter.GeofencePredicate;-import org.openremote.model.rules.AssetState;import org.openremote.model.rules.Assets;import org.openremote.model.rules.RulesClock;import org.openremote.model.rules.TemporaryFact;@@ -67,8 +68,8 @@ public class RulesFacts extends Facts implements RuleListener {final protected Assets assetsFacade;final protected Object loggingContext;final protected Logger LOG;- final protected Map<String, Collection<AssetState<?>>> assetIdIndex = new HashMap<>();- final protected Map<String, Collection<AssetState<?>>> assetTypeIndex = new HashMap<>();+ final protected Map<String, Collection<AttributeInfo>> assetIdIndex = new HashMap<>();+ final protected Map<String, Collection<AttributeInfo>> assetTypeIndex = new HashMap<>();protected int triggerCount;protected boolean trackLocationRules;protected Map<String, Set<GeofencePredicate>> assetStateLocationPredicateMap = null;@@ -80,7 +81,7 @@ public class RulesFacts extends Facts implements RuleListener {this.loggingContext = loggingContext;this.LOG = logger;- super.put(ASSET_STATES, new ArrayDeque<AssetState<?>>(INITIAL_CAPACITY));+ super.put(ASSET_STATES, new ArrayDeque<AttributeInfo>(INITIAL_CAPACITY));super.put(ASSET_EVENTS, new ArrayDeque<AssetEvent>(INITIAL_CAPACITY));super.put(EXECUTION_VARS, new HashMap<>());super.put(ANONYMOUS_FACTS, new ArrayDeque<>(INITIAL_CAPACITY));@@ -91,25 +92,25 @@ public class RulesFacts extends Facts implements RuleListener {trackLocationRules = true;}- protected List<RulesEngine.AssetStateLocationPredicates> stopTrackingLocationRules() {+ protected List<RulesEngine.AssetLocationPredicates> stopTrackingLocationRules() {LOG.finest("Tracking location predicate rules: stopping");trackLocationRules = false;Map<String, Set<GeofencePredicate>> assetStateLocationPredicateMap = this.assetStateLocationPredicateMap;this.assetStateLocationPredicateMap = null;return assetStateLocationPredicateMap == null ? null : assetStateLocationPredicateMap.entrySet().stream().map(assetStateSetEntry ->- new RulesEngine.AssetStateLocationPredicates(+ new RulesEngine.AssetLocationPredicates(assetStateSetEntry.getKey(),assetStateSetEntry.getValue())).collect(Collectors.toList());}@SuppressWarnings("unchecked")- public Collection<AssetState<?>> getAssetStates() {+ public Collection<AttributeInfo> getAssetStates() {return get(ASSET_STATES);}@SuppressWarnings("unchecked")- public Collection<TemporaryFact<AssetState<?>>> getAssetEvents() {+ public Collection<TemporaryFact<AttributeInfo>> getAssetEvents() {return get(ASSET_EVENTS);}@@ -199,7 +200,7 @@ public class RulesFacts extends Facts implements RuleListener {return this;}- public RulesFacts putAssetState(AssetState<?> assetState) {+ public RulesFacts putAssetState(AttributeInfo assetState) {if (LOG.isLoggable(Level.FINEST)) {LOG.finest("Fact change (UPDATE): " + assetState + " - on: " + loggingContext);}@@ -219,20 +220,20 @@ public class RulesFacts extends Facts implements RuleListener {return this;}- public RulesFacts removeAssetState(AssetState<?> assetState) {+ public RulesFacts removeAssetState(AttributeInfo assetState) {if (LOG.isLoggable(Level.FINEST)) {LOG.finest("Fact change (DELETE): " + assetState + " - on: " + loggingContext);}getAssetStates().remove(assetState);// Maintain index of all asset states for this asset by ID- Collection<AssetState<?>> assetIdIndexCollection = assetIdIndex.get(assetState.getId());+ Collection<AttributeInfo> assetIdIndexCollection = assetIdIndex.get(assetState.getId());if (assetIdIndexCollection != null) {assetIdIndexCollection.remove(assetState);}// Maintain index of all asset states for this asset by type- Collection<AssetState<?>> assetTypeIndexCollection = assetTypeIndex.get(assetState.getAssetType());+ Collection<AttributeInfo> assetTypeIndexCollection = assetTypeIndex.get(assetState.getAssetType());if (assetTypeIndexCollection != null) {assetTypeIndexCollection.remove(assetState);}@@ -240,8 +241,8 @@ public class RulesFacts extends Facts implements RuleListener {return this;}- public RulesFacts insertAssetEvent(long expiresMilliSeconds, AssetState<?> assetState) {- TemporaryFact<AssetState<?>> fact = new TemporaryFact<>(assetState.getTimestamp(), expiresMilliSeconds, assetState);+ public RulesFacts insertAttributeEvent(long expiresMilliSeconds, AttributeInfo attributeInfo) {+ TemporaryFact<AttributeInfo> fact = new TemporaryFact<>(attributeInfo.getTimestamp(), expiresMilliSeconds, attributeInfo);if (LOG.isLoggable(Level.FINEST)) {LOG.finest("Fact change (INSERT): " + fact + " - on: " + loggingContext);}@@ -249,6 +250,14 @@ public class RulesFacts extends Facts implements RuleListener {return this;}+ public RulesFacts removeAttributeEvents(AttributeRef attributeRef) {+ if (LOG.isLoggable(Level.FINEST)) {+ LOG.finest("Fact change (DELETE ALL): " + attributeRef + " on: " + loggingContext);+ }+ getAssetEvents().removeIf(fact -> fact.getFact().getRef().equals(attributeRef));+ return this;+ }+public RulesFacts putTemporary(String expires, Object value) {return putTemporary(TimeUtil.parseTimeDuration(expires), value);}@@ -411,39 +420,39 @@ public class RulesFacts extends Facts implements RuleListener {.map(fact -> (T) fact);}- public Optional<AssetState<?>> matchFirstAssetState(AssetQuery assetQuery) {+ public Optional<AttributeInfo> matchFirstAssetState(AssetQuery assetQuery) {return matchAssetState(assetQuery).findFirst();}- public Stream<AssetState<?>> matchAssetState(AssetQuery assetQuery) {+ public Stream<AttributeInfo> matchAssetState(AssetQuery assetQuery) {if (trackLocationRules && assetQuery.attributes != null) {storeLocationPredicates(getLocationPredicates(assetQuery.attributes));}- Predicate<AssetState<?>> p = new AssetQueryPredicate(timerService, assetStorageService, assetQuery);+ Predicate<AttributeInfo> p = new AssetQueryPredicate(timerService, assetStorageService, assetQuery);return matchAssetState(p);}- public Stream<AssetState<?>> matchAssetState(Predicate<AssetState<?>> p) {+ public Stream<AttributeInfo> matchAssetState(Predicate<AttributeInfo> p) {// Match against all asset states by default- Stream<AssetState<?>> assetStates = getAssetStates().stream();+ Stream<AttributeInfo> assetStates = getAssetStates().stream();return assetStates.parallel().filter(p);}- public Optional<TemporaryFact<AssetState<?>>> matchFirstAssetEvent(AssetQuery assetQuery) {+ public Optional<TemporaryFact<AttributeInfo>> matchFirstAssetEvent(AssetQuery assetQuery) {return matchAssetEvent(assetQuery).findFirst();}- public Optional<TemporaryFact<AssetState<?>>> matchLastAssetEvent(AssetQuery assetQuery) {+ public Optional<TemporaryFact<AttributeInfo>> matchLastAssetEvent(AssetQuery assetQuery) {return matchAssetEvent(assetQuery).reduce((first, second) -> second);}@SuppressWarnings("unchecked")- public Stream<TemporaryFact<AssetState<?>>> matchAssetEvent(AssetQuery assetQuery) {- Predicate<AssetState<?>> p = new AssetQueryPredicate(timerService, assetStorageService, assetQuery);+ public Stream<TemporaryFact<AttributeInfo>> matchAssetEvent(AssetQuery assetQuery) {+ Predicate<AttributeInfo> p = new AssetQueryPredicate(timerService, assetStorageService, assetQuery);return getAssetEvents().stream().parallel()- .filter(fact -> this.matchFact(fact, (Class<AssetState<?>>)(Class)AssetState.class, p).isPresent());+ .filter(fact -> this.matchFact(fact, AttributeInfo.class, p).isPresent());}public RulesFacts updateAssetState(String assetId, String attributeName, Object value) {@@ -508,20 +517,20 @@ public class RulesFacts extends Facts implements RuleListener {haveLog = true;}- List<AssetState<?>> sortedAssetStates = new ArrayList<>(getAssetStates());+ List<AttributeInfo> sortedAssetStates = new ArrayList<>(getAssetStates());sortedAssetStates.sort(Comparator.naturalOrder());if (sortedAssetStates.size() > 0) {logger.log(level, "--------------------------------- ASSET STATES (" + sortedAssetStates.size() + ") ---------------------------------");- for (AssetState<?> assetState : sortedAssetStates) {+ for (AttributeInfo assetState : sortedAssetStates) {logger.log(level, assetState.toString());}haveLog = true;}- List<TemporaryFact<AssetState<?>>> sortedAssetEvents = new ArrayList<>(getAssetEvents());+ List<TemporaryFact<AttributeInfo>> sortedAssetEvents = new ArrayList<>(getAssetEvents());if (sortedAssetEvents.size() > 0) {logger.log(level, "--------------------------------- ASSET EVENTS (" + sortedAssetEvents.size() + ") ---------------------------------");sortedAssetEvents.sort(Comparator.comparing(TemporaryFact::getTime));- for (TemporaryFact<AssetState<?>> assetEvent : sortedAssetEvents) {+ for (TemporaryFact<AttributeInfo> assetEvent : sortedAssetEvents) {logger.log(level, assetEvent.toString());}haveLog = true;@@ -564,9 +573,9 @@ public class RulesFacts extends Facts implements RuleListener {return timerService;}- public static Comparator<AssetState<?>> asComparator(AssetQuery.OrderBy orderBy) {+ public static Comparator<AttributeInfo> asComparator(AssetQuery.OrderBy orderBy) {- Function<AssetState<?>, String> keyExtractor = AssetState::getAssetName;+ Function<AttributeInfo, String> keyExtractor = AttributeInfo::getAssetName;boolean reverse = orderBy.descending;switch (orderBy.property) {@@ -575,17 +584,17 @@ public class RulesFacts extends Facts implements RuleListener {keyExtractor = assetState -> Long.toString(assetState.getCreatedOn().getTime());break;case ASSET_TYPE:- keyExtractor = AssetState::getAssetType;+ keyExtractor = AttributeInfo::getAssetType;break;case PARENT_ID:- keyExtractor = AssetState::getParentId;+ keyExtractor = AttributeInfo::getParentId;break;case REALM:- keyExtractor = AssetState::getRealm;+ keyExtractor = AttributeInfo::getRealm;break;}- Comparator<AssetState<?>> comparator = Comparator.comparing(keyExtractor);+ Comparator<AttributeInfo> comparator = Comparator.comparing(keyExtractor);if (reverse) {comparator = comparator.reversed();@@ -604,12 +613,12 @@ public class RulesFacts extends Facts implements RuleListener {LOG.finest("Fact change (INTERNAL DELETE): " + assetState + " - on: " + loggingContext);}// Maintain index of all asset states for this asset by ID- Collection<AssetState<?>> assetIdIndexCollection = assetIdIndex.get(assetState.getId());+ Collection<AttributeInfo> assetIdIndexCollection = assetIdIndex.get(assetState.getId());if (assetIdIndexCollection != null) {assetIdIndexCollection.remove(assetState);}// Maintain index of all asset states for this asset by type- Collection<AssetState<?>> assetTypeIndexCollection = assetTypeIndex.get(assetState.getAssetType());+ Collection<AttributeInfo> assetTypeIndexCollection = assetTypeIndex.get(assetState.getAssetType());if (assetTypeIndexCollection != null) {assetTypeIndexCollection.remove(assetState);}@@ -631,7 +640,7 @@ public class RulesFacts extends Facts implements RuleListener {LOG.finest("Location predicate found");// Collect asset states only where the attribute is location (location predicates only make sense when the location// attribute is exposed to rules - we don't support RULE_EVENT facts just RULE_STATE- Collection<AssetState<?>> locationAssetStates = getAssetStates().stream().filter(assetState -> assetState.getName().equalsIgnoreCase(Asset.LOCATION.getName())).collect(Collectors.toSet());+ Collection<AttributeInfo> locationAssetStates = getAssetStates().stream().filter(assetState -> assetState.getName().equalsIgnoreCase(Asset.LOCATION.getName())).collect(Collectors.toSet());if (assetStateLocationPredicateMap == null) {assetStateLocationPredicateMap = new HashMap<>(locationAssetStates.size());diff --git a/manager/src/main/java/org/openremote/manager/rules/RulesService.java b/manager/src/main/java/org/openremote/manager/rules/RulesService.javaindex 27d5038f4..87746c0c0 100644--- a/manager/src/main/java/org/openremote/manager/rules/RulesService.java+++ b/manager/src/main/java/org/openremote/manager/rules/RulesService.java@@ -20,17 +20,18 @@package org.openremote.manager.rules;import io.micrometer.core.instrument.MeterRegistry;-import jakarta.persistence.EntityManager;+import io.micrometer.core.instrument.binder.jvm.ExecutorServiceMetrics;import org.apache.camel.builder.RouteBuilder;+import org.openremote.container.concurrent.ContainerScheduledExecutor;import org.openremote.container.message.MessageBrokerService;import org.openremote.container.persistence.PersistenceService;import org.openremote.container.timer.TimerService;import org.openremote.manager.asset.AssetProcessingException;import org.openremote.manager.asset.AssetProcessingService;import org.openremote.manager.asset.AssetStorageService;-import org.openremote.manager.asset.AssetUpdateProcessor;import org.openremote.manager.datapoint.AssetDatapointService;import org.openremote.manager.datapoint.AssetPredictedDatapointService;+import org.openremote.manager.event.AttributeEventInterceptor;import org.openremote.manager.event.ClientEventService;import org.openremote.manager.gateway.GatewayService;import org.openremote.manager.notification.NotificationService;@@ -45,7 +46,8 @@ import org.openremote.model.ContainerService;import org.openremote.model.PersistenceEvent;import org.openremote.model.asset.Asset;import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeEvent.Source;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.attribute.AttributeMap;import org.openremote.model.query.AssetQuery;import org.openremote.model.query.RulesetQuery;@@ -80,12 +82,12 @@ import static org.openremote.manager.gateway.GatewayService.isNotForGateway;* Manages {@link RulesEngine}s for stored {@link Ruleset}s and processes asset attribute updates.* <p>* If an updated attribute doesn't have meta {@link MetaItemType#RULE_STATE} is false and the attribute has an {@link- * org.openremote.model.asset.agent.AgentLink} meta, this implementation of {@link AssetUpdateProcessor} converts the- * update message to an {@link AssetState} fact. This service keeps the facts and thus the state of rule facts are in- * sync with the asset state changes that occur. If an asset attribute value changes, the {@link AssetState} in the+ * org.openremote.model.asset.agent.AgentLink} meta, this implementation of {@link AttributeEventInterceptor} converts the+ * update message to an {@link AttributeInfo} fact. This service keeps the facts and thus the state of rule facts are in+ * sync with the asset state changes that occur. If an asset attribute value changes, the {@link AttributeInfo} in the* rules engines will be updated to reflect the change.* <p>- * If an updated attribute's {@link MetaItemType#RULE_EVENT} is true, another temporary {@link AssetState} fact is+ * If an updated attribute's {@link MetaItemType#RULE_EVENT} is true, another temporary {@link AttributeInfo} fact is* inserted in the rules engines in scope. This fact expires automatically if the lifetime set in {@link* RulesService#OR_RULE_EVENT_EXPIRES} is reached, or if the lifetime set in the attribute {@link* MetaItemType#RULE_EVENT_EXPIRES} is reached.@@ -98,7 +100,7 @@ import static org.openremote.manager.gateway.GatewayService.isNotForGateway;* </ol>* Processing order of rulesets with the same scope or same parent is not guaranteed.*/-public class RulesService extends RouteBuilder implements ContainerService, AssetUpdateProcessor {+public class RulesService extends RouteBuilder implements ContainerService {public static final int PRIORITY = LOW_PRIORITY;public static final String OR_RULE_EVENT_EXPIRES = "OR_RULE_EVENT_EXPIRES";@@ -134,13 +136,13 @@ public class RulesService extends RouteBuilder implements ContainerService, Asseprotected RulesEngine<GlobalRuleset> globalEngine;protected Realm[] realms;protected AssetLocationPredicateProcessor locationPredicateRulesConsumer;- protected final ConcurrentMap<RulesEngine<?>, List<RulesEngine.AssetStateLocationPredicates>> engineAssetLocationPredicateMap = new ConcurrentHashMap<>();+ protected final ConcurrentMap<RulesEngine<?>, List<RulesEngine.AssetLocationPredicates>> engineAssetLocationPredicateMap = new ConcurrentHashMap<>();protected final Set<String> assetsWithModifiedLocationPredicates = new HashSet<>();// Keep global list of asset states that have been pushed to any engines// The objects are already in memory inside the rule engines but keeping them// here means we can quickly insert facts into newly started engines- protected final Set<AssetState<?>> assetStates = new HashSet<>();- protected final Set<AssetState<?>> preInitAssetStates = new HashSet<>();+ protected final Set<AttributeEvent> attributeEvents = new HashSet<>();+ protected final Set<AttributeEvent> preInitAttributeEvents = new HashSet<>();protected long defaultEventExpiresMillis = 1000*60*60;protected long tempFactExpirationMillis;protected long quickFireMillis;@@ -155,7 +157,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse@Overridepublic void init(Container container) throws Exception {- executorService = container.getExecutorService();+ executorService = new ContainerScheduledExecutor(getClass().getSimpleName(), 1);timerService = container.getService(TimerService.class);persistenceService = container.getService(PersistenceService.class);rulesetStorageService = container.getService(RulesetStorageService.class);@@ -178,6 +180,10 @@ public class RulesService extends RouteBuilder implements ContainerService, AssemeterRegistry = container.getMeterRegistry();+ if (meterRegistry != null) {+ executorService = ExecutorServiceMetrics.monitor(meterRegistry, executorService, getClass().getSimpleName());+ }+clientEventService.addSubscriptionAuthorizer((realm, auth, subscription) -> {if (subscription.isEventType(RulesEngineStatusEvent.class) || subscription.isEventType(RulesetChangedEvent.class)) {@@ -203,6 +209,8 @@ public class RulesService extends RouteBuilder implements ContainerService, Assereturn false;});+ clientEventService.addInternalSubscription(AttributeEvent.class, null, this::onAttributeEvent);+ServiceLoader.load(GeofenceAssetAdapter.class).forEach(geofenceAssetAdapter -> {LOG.fine("Adding GeofenceAssetAdapter: " + geofenceAssetAdapter.getClass().getName());geofenceAssetAdapters.add(geofenceAssetAdapter);@@ -320,8 +328,15 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.forEach(pair -> {Asset<?> asset = pair.key;pair.value.forEach(ruleAttribute -> {- AssetState<?> assetState = new AssetState<>(asset, ruleAttribute, Source.INTERNAL);- updateAssetState(assetState);+ AttributeEvent attributeEvent = new AttributeEvent(+ asset,+ ruleAttribute,+ null,+ ruleAttribute.getValue().orElse(null),+ ruleAttribute.getTimestamp().orElse(0L),+ ruleAttribute.getValue().orElse(null),+ ruleAttribute.getTimestamp().orElse(0L));+ updateAttributeEvent(attributeEvent);});});@@ -332,10 +347,12 @@ public class RulesService extends RouteBuilder implements ContainerService, AsserealmEngines.values().forEach(RulesEngine::start);assetEngines.values().forEach(RulesEngine::start);- startDone = true;+ synchronized (preInitAttributeEvents) {+ startDone = true;+ }- preInitAssetStates.forEach(this::doProcessAssetUpdate);- preInitAssetStates.clear();+ preInitAttributeEvents.forEach(this::doProcessAttributeUpdate);+ preInitAttributeEvents.clear();}@Override@@ -358,8 +375,8 @@ public class RulesService extends RouteBuilder implements ContainerService, AsseglobalEngine = null;}- synchronized (assetStates) {- assetStates.clear();+ synchronized (attributeEvents) {+ attributeEvents.clear();}for (GeofenceAssetAdapter geofenceAssetAdapter : geofenceAssetAdapters) {@@ -376,64 +393,61 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.orElse(metaHolder.getMeta().has(MetaItemType.AGENT_LINK));}- @Override- public boolean processAssetUpdate(EntityManager em,- Asset<?> asset,- Attribute<?> attribute,- Source source) throws AssetProcessingException {- if (!startDone) {- preInitAssetStates.add(new AssetState<>(asset, attribute, source));- } else {- doProcessAssetUpdate(new AssetState<>(asset, attribute, source));+ /**+ * React to events that have been committed to the DB and inject them into the appropriate {@link RulesEngine}s.+ */+ public void onAttributeEvent(AttributeEvent event) throws AssetProcessingException {+ synchronized (preInitAttributeEvents) {+ if (!startDone) {+ preInitAttributeEvents.add(event);+ } else {+ doProcessAttributeUpdate(event);+ }}-- return false;}- protected void doProcessAssetUpdate(AssetState<?> assetState) {+ protected void doProcessAttributeUpdate(AttributeEvent attributeEvent) {// We might process two facts for a single attribute update, if that is what the user wants- // First as asset state- if (isRuleState(assetState)) {- updateAssetState(assetState);+ // First as attribute event+ retractAttributeEvent(attributeEvent);+ if (isRuleState(attributeEvent) && !attributeEvent.isDeleted()) {+ updateAttributeEvent(attributeEvent);}- // Then as asset event (if there wasn't an error), this will also fire the rules engines- if (assetState.getMetaValue(MetaItemType.RULE_EVENT).orElse(false)) {-- long expireMillis = assetState.getMetaValue(MetaItemType.RULE_EVENT_EXPIRES).map(expires -> {- long expMillis = defaultEventExpiresMillis;-- try {- expMillis = TimeUtil.parseTimeDuration(expires);- } catch (RuntimeException exception) {- LOG.log(Level.WARNING, "Failed to parse '" + MetaItemType.RULE_EVENT_EXPIRES.getName() + "' value '" + expires + "' for attribute: " + assetState, exception);- }- return expMillis;- }).orElse(defaultEventExpiresMillis);+ // Then as rule event+ removeAttributeEvents(attributeEvent);+ if (attributeEvent.getMetaValue(MetaItemType.RULE_EVENT).orElse(false)) {+ if (!attributeEvent.isDeleted()) {+ long expireMillis = attributeEvent.getMetaValue(MetaItemType.RULE_EVENT_EXPIRES).map(expires -> {+ long expMillis = defaultEventExpiresMillis;++ try {+ expMillis = TimeUtil.parseTimeDuration(expires);+ } catch (RuntimeException exception) {+ LOG.log(Level.WARNING, "Failed to parse '" + MetaItemType.RULE_EVENT_EXPIRES.getName() + "' value '" + expires + "' for attribute: " + attributeEvent, exception);+ }+ return expMillis;+ }).orElse(defaultEventExpiresMillis);- insertAssetEvent(assetState, expireMillis);+ insertAttributeEvent(attributeEvent, expireMillis);+ }}}public boolean isRulesetKnown(Ruleset ruleset) {if (ruleset instanceof GlobalRuleset) {return globalEngine != null- && globalEngine.deployments != null&& globalEngine.deployments.containsKey(ruleset.getId())&& globalEngine.deployments.get(ruleset.getId()).ruleset.getRules().equals(ruleset.getRules());}- if (ruleset instanceof RealmRuleset) {- RealmRuleset realmRuleset = (RealmRuleset) ruleset;+ if (ruleset instanceof RealmRuleset realmRuleset) {return realmEngines.get(realmRuleset.getRealm()) != null- && realmEngines.get(realmRuleset.getRealm()).deployments != null&& realmEngines.get(realmRuleset.getRealm()).deployments.containsKey(ruleset.getId())&& realmEngines.get(realmRuleset.getRealm()).deployments.get(ruleset.getId()).ruleset.getRules().equals(ruleset.getRules());}- if (ruleset instanceof AssetRuleset) {- AssetRuleset assetRuleset = (AssetRuleset) ruleset;+ if (ruleset instanceof AssetRuleset assetRuleset) {return assetEngines.get(assetRuleset.getAssetId()) != null- && assetEngines.get(assetRuleset.getAssetId()).deployments != null&& assetEngines.get(assetRuleset.getAssetId()).deployments.containsKey(ruleset.getId())&& assetEngines.get(assetRuleset.getAssetId()).deployments.get(ruleset.getId()).ruleset.getRules().equals(ruleset.getRules());}@@ -474,14 +488,13 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse}// Remove any asset rules engines for assets in this realm- assetEngines.values().stream()- .filter(re -> re.getId().getRealm().map(r -> r.equals(realm.getName())).orElse(false))- .forEach(RulesEngine::stop);- assetEngines.entrySet().removeIf(entry ->- entry.getValue().getId().getRealm().map(r -> r.equals(realm.getName())).orElse(- false)- );-+ assetEngines.values().removeIf(engine -> {+ boolean remove = engine.getId().getRealm().map(r -> r.equals(realm.getName())).orElse(false);+ if (remove) {+ engine.stop();+ }+ return remove;+ });} else {// Create realm rules engines for this realm if it has any rulesetsrulesetStorageService@@ -510,22 +523,22 @@ public class RulesService extends RouteBuilder implements ContainerService, Asseprotected void processAssetChange(Asset<?> asset, PersistenceEvent<Asset<?>> persistenceEvent) {switch (persistenceEvent.getCause()) {- case CREATE: {- // Load rule state attributes into rules engine- asset- .getAttributes()- .stream()- .filter(RulesService::isRuleState)- .forEach(attribute -> {- AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);- LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + assetState);- updateAssetState(assetState);- });- break;- }- case UPDATE: {-+ case DELETE ->+ // Remove any asset rules engines for this asset+ assetEngines.values().removeIf(re -> {+ if (re.getId().getAssetId().map(aId -> aId.equals(asset.getId())).orElse(false)) {+ re.stop();+ return true;+ }+ return false;+ });+ case UPDATE -> {boolean attributesChanged = persistenceEvent.hasPropertyChanged("attributes");++ // Attribute changes will be published to the client event bus - if anything else has changed then we need to reload+ if (!attributesChanged) {++ }AttributeMap oldAttributes = attributesChanged ? ((AttributeMap) persistenceEvent.getPreviousState("attributes")) : asset.getAttributes();AttributeMap currentAttributes = asset.getAttributes();@@ -536,32 +549,21 @@ public class RulesService extends RouteBuilder implements ContainerService, AsseList<Attribute<?>> newStateAttributes = currentAttributes.stream().filter(RulesService::isRuleState).toList();-- // Just retract all old attributes rather than compare every value that might cause asset state to mutate- oldStateAttributes.forEach(attribute -> {- AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);- LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), retracting obsolete fact: " + assetState);- retractAssetState(assetState);- });-- // Insert new states for new or changed attributes- newStateAttributes.forEach(attribute -> {- AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);- LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + assetState);- updateAssetState(assetState);- });- break;+//+// // Just retract all old attributes rather than compare every value that might cause asset state to mutate+// oldStateAttributes.forEach(attribute -> {+// AttributeEvent attributeEvent = new AttributeEvent(asset.getId(), attribute.getName(), null);+// LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), retracting obsolete fact: " + attributeEvent);+// retractAttributeEvent(attributeEvent);+// });+//+// // Insert new states for new or changed attributes+// newStateAttributes.forEach(attribute -> {+// AttributeEvent attributeEvent = new AttributeEvent(asset, attribute, null, attribute.getValue().orElse(null), attribute.getTimestamp().orElse(0L));+// LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), inserting fact: " + attributeEvent);+// updateAttributeEvent(attributeEvent);+// });}- case DELETE:- // Retract any facts that were associated with this asset- asset.getAttributes().stream()- .filter(RulesService::isRuleState)- .forEach(attribute -> {- AssetState<?> assetState = new AssetState<>(asset, attribute, Source.INTERNAL);- LOG.finest("Asset was persisted (" + persistenceEvent.getCause() + "), retracting fact: " + assetState);- retractAssetState(assetState);- });- break;}}@@ -581,9 +583,9 @@ public class RulesService extends RouteBuilder implements ContainerService, AsseRulesEngine<GlobalRuleset> engine = deployGlobalRuleset((GlobalRuleset) ruleset);if (isNewEngine) {- synchronized (assetStates) {+ synchronized (attributeEvents) {// Push all existing facts into the engine- assetStates.forEach(assetState -> engine.updateOrInsertAssetState(assetState, true));+ attributeEvents.forEach(assetState -> engine.updateOrInsertAttributeInfo(assetState, true));}}@@ -596,10 +598,10 @@ public class RulesService extends RouteBuilder implements ContainerService, Asseif (isNewEngine) {// Push all existing facts into the engine- synchronized (assetStates) {- assetStates.forEach(assetState -> {+ synchronized (attributeEvents) {+ attributeEvents.forEach(assetState -> {if (assetState.getRealm().equals(((RealmRuleset) ruleset).getRealm())) {- engine.updateOrInsertAssetState(assetState, true);+ engine.updateOrInsertAttributeInfo(assetState, true);}});}@@ -617,7 +619,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asseif (isNewEngine) {// Push all existing facts for this asset (and it's children into the engine)getAssetStatesInScope(((AssetRuleset) ruleset).getAssetId())- .forEach(assetState -> engine.updateOrInsertAssetState(assetState, true));+ .forEach(assetState -> engine.updateOrInsertAttributeInfo(assetState, true));}engine.start();@@ -721,16 +723,9 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse.collect(Collectors.groupingBy(assetAndRules -> assetAndRules.key.getRealm())).entrySet().stream()- .filter(es -> {- try {- return Arrays- .stream(realms)- .anyMatch(at -> es.getKey().equals(at.getName()));- } catch (Exception e) {- e.printStackTrace();- return false;- }- })+ .filter(es -> Arrays+ .stream(realms)+ .anyMatch(at -> es.getKey().equals(at.getName()))).flatMap(es -> {List<Pair<Asset<?>, List<AssetRuleset>>> realmAssetAndRules = es.getValue();@@ -783,52 +778,70 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse}}- protected void insertAssetEvent(AssetState<?> assetState, long expiresMillis) {+ protected void insertAttributeEvent(AttributeInfo event, long expiresMillis) {// Get the chain of rule engines that we need to pass through- List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(event.getRealm(), event.getPath());// Pass through each enginefor (RulesEngine<?> rulesEngine : rulesEngines) {- rulesEngine.insertAssetEvent(expiresMillis, assetState);+ rulesEngine.insertAttributeEvent(expiresMillis, event);}}- protected void updateAssetState(AssetState<?> assetState) {- LOG.finest("Updating asset state: " + assetState);-+ protected void removeAttributeEvents(AttributeInfo event) {// Get the chain of rule engines that we need to pass through- List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(event.getRealm(), event.getPath());++ // Remove all from each engine+ for (RulesEngine<?> rulesEngine : rulesEngines) {+ rulesEngine.removeAttributeEvents(event.getRef());+ }+ }++ protected void updateAttributeEvent(AttributeEvent attributeEvent) {+ LOG.finest("Updating attribute event: " + attributeEvent);++ synchronized (attributeEvents) {+ boolean isNewer = attributeEvents.stream().filter(event -> event.getRef().equals(attributeEvent.getRef())).findFirst()+ .map(existingEvent -> existingEvent.getTimestamp() < attributeEvent.getTimestamp()).orElse(true);++ if (!isNewer) {+ // Attribute event is older than the state already loaded+ return;+ }++ // Get the chain of rule engines that we need to pass through+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(attributeEvent.getRealm(), attributeEvent.getPath());- synchronized (assetStates) {// Remove asset state with same attribute ref as new state, add new state- boolean inserted = !assetStates.remove(assetState);- assetStates.add(assetState);+ boolean inserted = !attributeEvents.remove(attributeEvent);+ attributeEvents.add(attributeEvent);// Pass through each rules enginefor (RulesEngine<?> rulesEngine : rulesEngines) {- rulesEngine.updateOrInsertAssetState(assetState, inserted);+ rulesEngine.updateOrInsertAttributeInfo(attributeEvent, inserted);}}}- protected void retractAssetState(AssetState<?> assetState) {+ protected void retractAttributeEvent(AttributeEvent attributeEvent) {// Get the chain of rule engines that we need to pass through- List<RulesEngine<?>> rulesEngines = getEnginesInScope(assetState.getRealm(), assetState.getPath());+ List<RulesEngine<?>> rulesEngines = getEnginesInScope(attributeEvent.getRealm(), attributeEvent.getPath());- synchronized (assetStates) {+ synchronized (attributeEvents) {// Remove asset state with same attribute ref- assetStates.remove(assetState);+ attributeEvents.remove(attributeEvent);// Pass through each rules enginefor (RulesEngine<?> rulesEngine : rulesEngines) {- rulesEngine.removeAssetState(assetState);+ rulesEngine.removeAttributeInfo(attributeEvent);}}}- protected List<AssetState<?>> getAssetStatesInScope(String assetId) {- synchronized (assetStates) {- return assetStates+ protected List<AttributeInfo> getAssetStatesInScope(String assetId) {+ synchronized (attributeEvents) {+ return attributeEvents.stream().filter(assetState -> Arrays.asList(assetState.getPath()).contains(assetId)).collect(Collectors.toList());@@ -874,10 +887,10 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse/*** Called when an engine's rules change identifying assets with location attributes that also have {@link- * LocationAttributePredicate} in the rules. The job here is to identify the asset's (via {@link AssetState}) that+ * LocationAttributePredicate} in the rules. The job here is to identify the asset's (via {@link AttributeInfo}) that* have modified {@link LocationAttributePredicate}s and to notify the {@link GeofenceAssetAdapter}s.*/- protected void onEngineLocationRulesChanged(RulesEngine<?> rulesEngine, List<RulesEngine.AssetStateLocationPredicates> newEngineAssetStateLocationPredicates) {+ protected void onEngineLocationRulesChanged(RulesEngine<?> rulesEngine, List<RulesEngine.AssetLocationPredicates> newEngineAssetStateLocationPredicates) {synchronized (assetsWithModifiedLocationPredicates) {@@ -889,7 +902,7 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse// All location predicates have been removed so record each asset state as modifiedassetsWithModifiedLocationPredicates.addAll(existingAssetStateLocationPredicates.stream().map(- RulesEngine.AssetStateLocationPredicates::getAssetId).toList());+ RulesEngine.AssetLocationPredicates::getAssetId).toList());// Remove this engine from the mapreturn null;});@@ -902,13 +915,13 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse// All asset states are new so record them all as modifiedassetsWithModifiedLocationPredicates.addAll(newEngineAssetStateLocationPredicates.stream().map(- RulesEngine.AssetStateLocationPredicates::getAssetId).toList());+ RulesEngine.AssetLocationPredicates::getAssetId).toList());} else {// Find obsolete and modified asset statesexistingEngineAssetStateLocationPredicates.forEach(existingAssetStateLocationPredicates -> {// Check if there are no longer any location predicates for this asset- Optional<RulesEngine.AssetStateLocationPredicates> newAssetStateLocationPredicates = newEngineAssetStateLocationPredicates.stream()+ Optional<RulesEngine.AssetLocationPredicates> newAssetStateLocationPredicates = newEngineAssetStateLocationPredicates.stream().filter(assetStateLocationPredicates ->assetStateLocationPredicates.getAssetId().equals(existingAssetStateLocationPredicates.getAssetId()))@@ -960,12 +973,12 @@ public class RulesService extends RouteBuilder implements ContainerService, Assetry {// Find all location predicates associated with modified assets and pass through to the geofence adapters- List<RulesEngine.AssetStateLocationPredicates> assetLocationPredicates = new ArrayList<>(+ List<RulesEngine.AssetLocationPredicates> assetLocationPredicates = new ArrayList<>(assetsWithModifiedLocationPredicates.size());assetsWithModifiedLocationPredicates.forEach(assetId -> {- RulesEngine.AssetStateLocationPredicates locationPredicates = new RulesEngine.AssetStateLocationPredicates(+ RulesEngine.AssetLocationPredicates locationPredicates = new RulesEngine.AssetLocationPredicates(assetId,new HashSet<>());@@ -1027,12 +1040,12 @@ public class RulesService extends RouteBuilder implements ContainerService, Asse/*** Trigger rules engines which have the {@link org.openremote.model.value.MetaItemDescriptor} {@link org.openremote.model.rules.Ruleset#TRIGGER_ON_PREDICTED_DATA}- * and contain {@link AssetState} of the specified asset id. Use this when {@link PredictedDatapoints} has changed for this asset.+ * and contain {@link AttributeInfo} of the specified asset id. Use this when {@link PredictedDatapoints} has changed for this asset.* @param assetId of the asset which has new predicated data points.*/public void fireDeploymentsWithPredictedDataForAsset(String assetId) {- List<AssetState<?>> assetStates = getAssetStatesInScope(assetId);- if (assetStates.size() > 0) {+ List<AttributeInfo> assetStates = getAssetStatesInScope(assetId);+ if (!assetStates.isEmpty()) {String realm = assetStates.get(0).getRealm();String[] assetPaths = assetStates.stream().flatMap(assetState -> Arrays.stream(assetState.getPath())).toArray(String[]::new);for (RulesEngine<?> rulesEngine : getEnginesInScope(realm, assetPaths)) {diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.javaindex 32aeaa0de..5b6c346c4 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/AssetsFacade.java@@ -92,7 +92,7 @@ public class AssetsFacade<T extends Ruleset> extends Assets {return this;// Check if the asset ID of every event can be found with the default security of this facade- String[] ids = Arrays.stream(events).map(AttributeEvent::getAssetId).toArray(String[]::new);+ String[] ids = Arrays.stream(events).map(AttributeEvent::getId).toArray(String[]::new);AssetQuery query = new AssetQuery().ids(ids);long count = this.getResults(query).count();@@ -113,6 +113,6 @@ public class AssetsFacade<T extends Ruleset> extends Assets {}public AssetsFacade<T> dispatch(String assetId, String attributeName) {- return dispatch(new AttributeEvent(assetId, attributeName));+ return dispatch(new AttributeEvent(assetId, attributeName, null));}}diff --git a/manager/src/main/java/org/openremote/manager/rules/flow/NodeModel.java b/manager/src/main/java/org/openremote/manager/rules/flow/NodeModel.javaindex f9524bad7..b2900c88c 100644--- a/manager/src/main/java/org/openremote/manager/rules/flow/NodeModel.java+++ b/manager/src/main/java/org/openremote/manager/rules/flow/NodeModel.java@@ -1,10 +1,11 @@package org.openremote.manager.rules.flow;import org.openremote.manager.rules.RulesBuilder;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.query.AssetQuery;-import org.openremote.model.rules.AssetState;import org.openremote.model.rules.flow.*;import org.openremote.model.util.ValueUtil;+import org.openremote.model.value.ValueHolder;import java.util.List;import java.util.Objects;@@ -22,14 +23,14 @@ public enum NodeModel {AttributeInternalValue assetAttributePair = ValueUtil.JSON.convertValue(info.getInternals()[0].getValue(), AttributeInternalValue.class);String assetId = assetAttributePair.getAssetId();String attributeName = assetAttributePair.getAttributeName();- Optional<AssetState<?>> readValue = info.getFacts().matchFirstAssetState(new AssetQuery().ids(assetId).attributeName(attributeName));- return readValue.<Object>map(assetState -> assetState.getValue().orElse(null)).orElse(null);+ Optional<AttributeInfo> readValue = info.getFacts().matchFirstAssetState(new AssetQuery().ids(assetId).attributeName(attributeName));+ return readValue.flatMap(ValueHolder::getValue).orElse(null);},params -> {AttributeInternalValue internal = ValueUtil.JSON.convertValue(params.getNode().getInternals()[0].getValue(), AttributeInternalValue.class);String assetId = internal.getAssetId();String attributeName = internal.getAttributeName();- List<AssetState<?>> allAssets = params.getFacts().matchAssetState(new AssetQuery().ids(assetId).attributeName(attributeName)+ List<AttributeInfo> allAssets = params.getFacts().matchAssetState(new AssetQuery().ids(assetId).attributeName(attributeName)).toList();return allAssets.stream().anyMatch(state -> {@@ -53,7 +54,7 @@ public enum NodeModel {return;}AttributeInternalValue assetAttributePair = ValueUtil.JSON.convertValue(info.getInternals()[0].getValue(), AttributeInternalValue.class);- Optional<AssetState<?>> existingValue = info.getFacts().matchFirstAssetState(new AssetQuery().ids(assetAttributePair.getAssetId()).attributeName(assetAttributePair.getAttributeName()));+ Optional<AttributeInfo> existingValue = info.getFacts().matchFirstAssetState(new AssetQuery().ids(assetAttributePair.getAssetId()).attributeName(assetAttributePair.getAttributeName()));if (existingValue.isPresent())if (existingValue.get().getValue().isPresent())diff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/GeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/GeofenceAssetAdapter.javaindex 4d6c933f8..02dc9088e 100644--- a/manager/src/main/java/org/openremote/manager/rules/geofence/GeofenceAssetAdapter.java+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/GeofenceAssetAdapter.java@@ -32,8 +32,8 @@ import java.util.List;* {@link Asset} and can convert the {@link LocationAttributePredicate}s into Geofences that can be implemented by* the asset(s) itself.* <p>- * The adapter is notified when the {@link Asset}s {@link RulesEngine.AssetStateLocationPredicates} change; if the- * {@link RulesEngine.AssetStateLocationPredicates#getLocationPredicates()} is null or empty then it means that there+ * The adapter is notified when the {@link Asset}s {@link RulesEngine.AssetLocationPredicates} change; if the+ * {@link RulesEngine.AssetLocationPredicates#getLocationPredicates()} is null or empty then it means that there* are no longer any associated with that {@link Asset} so the adapter should clear any that already exist on that* {@link Asset}, it is the adapters job to maintain state if required (see initialising flag on {@link* #processLocationPredicates}).@@ -57,10 +57,10 @@ public interface GeofenceAssetAdapter extends ContainerService {/*** If an adapter handles the location predicates for a particular asset then the adapter should remove that item- * from the list to prevent other adapters from also handling it. If an {@link RulesEngine.AssetStateLocationPredicates#getLocationPredicates}+ * from the list to prevent other adapters from also handling it. If an {@link RulesEngine.AssetLocationPredicates#getLocationPredicates}* is empty then it means there are no longer any location predicates associated with that asset*/- void processLocationPredicates(List<RulesEngine.AssetStateLocationPredicates> modifiedAssetLocationPredicates);+ void processLocationPredicates(List<RulesEngine.AssetLocationPredicates> modifiedAssetLocationPredicates);/*** Called to return the active geofences for the specified {@link Asset}; if this adapter supports the requesteddiff --git a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.javaindex 74b2f60c7..551c1c60c 100644--- a/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java+++ b/manager/src/main/java/org/openremote/manager/rules/geofence/ORConsoleGeofenceAssetAdapter.java@@ -67,7 +67,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofepublic static final String NAME = "ORConsole";public static int NOTIFY_ASSETS_DEBOUNCE_MILLIS = 60000;public static int NOTIFY_ASSETS_BATCH_MILLIS = 10000;- protected Map<String, RulesEngine.AssetStateLocationPredicates> assetLocationPredicatesMap = new HashMap<>();+ protected Map<String, RulesEngine.AssetLocationPredicates> assetLocationPredicatesMap = new HashMap<>();protected NotificationService notificationService;protected AssetStorageService assetStorageService;protected GatewayService gatewayService;@@ -132,7 +132,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe}@Override- public void processLocationPredicates(List<RulesEngine.AssetStateLocationPredicates> modifiedAssetLocationPredicates) {+ public void processLocationPredicates(List<RulesEngine.AssetLocationPredicates> modifiedAssetLocationPredicates) {AtomicBoolean notifierDebounce = new AtomicBoolean(false);synchronized (notifyAssets) {@@ -148,7 +148,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofe!(locationPredicate instanceof RadialGeofencePredicate));- RulesEngine.AssetStateLocationPredicates existingPredicates = assetLocationPredicatesMap.get(+ RulesEngine.AssetLocationPredicates existingPredicates = assetLocationPredicatesMap.get(assetStateLocationPredicates.getAssetId());if (existingPredicates == null || !existingPredicates.getLocationPredicates().equals(assetStateLocationPredicates.getLocationPredicates())) {// We're not comparing before and after state as RulesService has done that although it could be@@ -206,7 +206,7 @@ public class ORConsoleGeofenceAssetAdapter extends RouteBuilder implements Geofereturn null;}- RulesEngine.AssetStateLocationPredicates assetStateLocationPredicates = assetLocationPredicatesMap.get(assetId);+ RulesEngine.AssetLocationPredicates assetStateLocationPredicates = assetLocationPredicatesMap.get(assetId);if (assetStateLocationPredicates == null) {// No geofences exist for this assetdiff --git a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.javaindex 40e42cb49..447e76a9c 100644--- a/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java+++ b/manager/src/main/java/org/openremote/manager/simulator/SimulatorService.java@@ -40,6 +40,7 @@ import java.util.logging.Logger;import static org.openremote.manager.event.ClientEventService.CLIENT_INBOUND_QUEUE;import static org.openremote.manager.event.ClientEventService.getSessionKey;+// RT: Removed this from META-INF as RequestSimulatorState not used anywhere/*** Connects the client/UI to the {@link SimulatorProtocol}.*/diff --git a/manager/src/main/java/org/openremote/manager/system/HealthService.java b/manager/src/main/java/org/openremote/manager/system/HealthService.javaindex 8d1c23d9f..80c701070 100644--- a/manager/src/main/java/org/openremote/manager/system/HealthService.java+++ b/manager/src/main/java/org/openremote/manager/system/HealthService.java@@ -64,6 +64,7 @@ public class HealthService implements ContainerService {public static final System.Logger LOG = System.getLogger(HealthService.class.getName());public static final String OR_METRICS_PORT = "OR_METRICS_PORT";public static final int OR_METRICS_PORT_DEFAULT = 8404;+ public static final String OR_CAMEL_ROUTE_METRIC_PREFIX = "or_camel_route";protected List<HealthStatusProvider> healthStatusProviderList = new ArrayList<>();protected boolean metricsEnabled;protected HTTPServer metricsServer;@@ -133,32 +134,32 @@ public class HealthService implements ContainerService {micrometerRoutePolicyFactory.setNamingStrategy(new MicrometerRoutePolicyNamingStrategy() {@Overridepublic String getName(Route route) {- return "or_camel_route";+ return OR_CAMEL_ROUTE_METRIC_PREFIX;}@Overridepublic String getExchangesSucceededName(Route route) {- return "or_camel_route_succeeded";+ return OR_CAMEL_ROUTE_METRIC_PREFIX + "_succeeded";}@Overridepublic String getExchangesFailedName(Route route) {- return "or_camel_route_failed";+ return OR_CAMEL_ROUTE_METRIC_PREFIX + "_failed";}@Overridepublic String getExchangesTotalName(Route route) {- return "or_camel_route_total";+ return OR_CAMEL_ROUTE_METRIC_PREFIX + "_total";}@Overridepublic String getFailuresHandledName(Route route) {- return "or_camel_route_failed_handled";+ return OR_CAMEL_ROUTE_METRIC_PREFIX + "_failed_handled";}@Overridepublic String getExternalRedeliveriesName(Route route) {- return "or_camel_route_redeliveries";+ return OR_CAMEL_ROUTE_METRIC_PREFIX + "_redeliveries";}@Overridediff --git a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerServiceindex 8fd6dfe33..1f063812c 100644--- a/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService+++ b/manager/src/main/resources/META-INF/services/org.openremote.model.ContainerService@@ -13,7 +13,6 @@ org.openremote.manager.asset.AssetProcessingServiceorg.openremote.container.message.MessageBrokerServiceorg.openremote.manager.system.HealthServiceorg.openremote.manager.agent.AgentService-org.openremote.manager.simulator.SimulatorServiceorg.openremote.manager.map.MapServiceorg.openremote.manager.notification.NotificationServiceorg.openremote.manager.webhook.WebhookServicediff --git a/manager/src/main/resources/logging-dev.properties b/manager/src/main/resources/logging-dev.propertiesindex 31d8d719f..50e16ace9 100644--- a/manager/src/main/resources/logging-dev.properties+++ b/manager/src/main/resources/logging-dev.properties@@ -6,7 +6,7 @@ handlers=java.util.logging.ConsoleHandler, \# Console handler###############################################################################-java.util.logging.ConsoleHandler.level=FINE+java.util.logging.ConsoleHandler.level=FINEST# Console format, you can colorize this in IntelliJ IDEA "Run Application " console# with the GrepConsole plugin and after copying the configuration file of your@@ -30,6 +30,7 @@ java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogForm# The root logger category of OpenRemoteorg.openremote.level=FINEorg.openremote.agent.protocol.level=FINEST+org.openremote.manager.mqtt.level=FINEST# Processing time logging#org.openremote.manager.asset.AssetProcessingService.level=FINEdiff --git a/manager/src/main/resources/logging.properties b/manager/src/main/resources/logging.propertiesindex 25fd846f7..11c5fe1fc 100644--- a/manager/src/main/resources/logging.properties+++ b/manager/src/main/resources/logging.properties@@ -32,8 +32,8 @@ java.util.logging.FileHandler.level=FINE# Output file and formatjava.util.logging.FileHandler.pattern=/deployment/openremote.logjava.util.logging.FileHandler.formatter=org.openremote.container.util.LogFormatter-# Rotate between 10 log files-java.util.logging.FileHandler.count=10+# Rotate between 30 log files+java.util.logging.FileHandler.count=30# 10 mb per filejava.util.logging.FileHandler.limit=10000000# Set to false to clear log on restartdiff --git a/model-util/build.gradle b/model-util/build.gradlenew file mode 100644index 000000000..9482e2f26--- /dev/null+++ b/model-util/build.gradle@@ -0,0 +1,74 @@+plugins {+ id 'java'+ id "maven-publish"+ id "signing"+}++repositories {+ mavenCentral()+}++dependencies {+ implementation findProject(":model")+ implementation findProject(":agent")+ implementation "cz.habarta.typescript-generator:typescript-generator-core:$typescriptGeneratorVersion"+}++jar {+ from sourceSets.main.allJava+ archivesBaseName = "openremote-${project.name}"+}++publishing {+ publications {+ maven(MavenPublication) {+ group = "io.openremote"+ artifactId = "openremote-${project.name}"+ from components.java+ pom {+ name = 'OpenRemote Model'+ description = 'Provides core model classes used within OpenRemote; add maven {url "https://repo.osgeo.org/repository/release/"} and maven {url "https://pkgs.dev.azure.com/OpenRemote/OpenRemote/_packaging/OpenRemote/maven/v1"} to resolve all dependencies'+ url = 'https://github.com/openremote/openremote'+ licenses {+ license {+ name = 'GNU Affero General Public License v3.0'+ url = 'https://www.gnu.org/licenses/agpl-3.0.en.html'+ }+ }+ developers {+ developer {+ id = 'developers'+ name = 'Developers'+ email = '[email protected]'+ organization = 'OpenRemote'+ organizationUrl = 'https://openremote.io'+ }+ }+ scm {+ connection = 'scm:git:git://github.com/openremote/openremote.git'+ developerConnection = 'scm:git:ssh://github.com:openremote/openremote.git'+ url = 'https://github.com/openremote/openremote/tree/master'+ }+ }+ }+ }++ repositories {+ maven {+ if (!version.endsWith('-LOCAL')) {+ credentials {+ username findProperty("publishUsername")+ password findProperty("publishPassword")+ }+ }+ url = version.endsWith('-LOCAL') ? layout.buildDirectory.dir('repo') : version.endsWith('-SNAPSHOT') ? findProperty("snapshotsRepoUrl") : findProperty("releasesRepoUrl")+ }+ }+}++signing {+ def signingKey = findProperty("signingKey")+ def signingPassword = findProperty("signingPassword")+ useInMemoryPgpKeys(signingKey, signingPassword)+ sign publishing.publications.maven+}diff --git a/ui/component/rest/src/main/java/AggregatedApiClient.java b/model-util/src/main/java/AggregatedApiClient.javasimilarity index 100%rename from ui/component/rest/src/main/java/AggregatedApiClient.javarename to model-util/src/main/java/AggregatedApiClient.javadiff --git a/ui/component/model/src/main/java/CustomExtension.java b/model-util/src/main/java/CustomExtension.javasimilarity index 100%rename from ui/component/model/src/main/java/CustomExtension.javarename to model-util/src/main/java/CustomExtension.javadiff --git a/ui/component/model/src/main/java/CustomTypeProcessor.java b/model-util/src/main/java/CustomTypeProcessor.javasimilarity index 100%rename from ui/component/model/src/main/java/CustomTypeProcessor.javarename to model-util/src/main/java/CustomTypeProcessor.javadiff --git a/ui/component/model/src/main/java/EnumWithInterfacesExtension.java b/model-util/src/main/java/EnumWithInterfacesExtension.javasimilarity index 100%rename from ui/component/model/src/main/java/EnumWithInterfacesExtension.javarename to model-util/src/main/java/EnumWithInterfacesExtension.javadiff --git a/ui/component/model/src/main/java/JsonSerializeExtension.java b/model-util/src/main/java/JsonSerializeExtension.javasimilarity index 100%rename from ui/component/model/src/main/java/JsonSerializeExtension.javarename to model-util/src/main/java/JsonSerializeExtension.javadiff --git a/ui/component/model/src/main/java/org/openremote/model/util/AssetModelInfoExtension.java b/model-util/src/main/java/org/openremote/model/util/AssetModelInfoExtension.javasimilarity index 100%rename from ui/component/model/src/main/java/org/openremote/model/util/AssetModelInfoExtension.javarename to model-util/src/main/java/org/openremote/model/util/AssetModelInfoExtension.javadiff --git a/model/src/main/java/org/openremote/model/Constants.java b/model/src/main/java/org/openremote/model/Constants.javaindex 21a17385a..9b7231207 100644--- a/model/src/main/java/org/openremote/model/Constants.java+++ b/model/src/main/java/org/openremote/model/Constants.java@@ -80,6 +80,12 @@ public interface Constants {String IP_REGEXP = "((^\\s*((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))|(^\\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?))$";String HTTP_URL_REGEXP = "^(http:\\/\\/www\\.|https:\\/\\/www\\.|http:\\/\\/|https:\\/\\/)?((((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))|(((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?)|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]))+(\\.([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9]))*)(:[0-9]{1,5})?(\\/.*)?$";String WS_URL_REGEXP = "^(ws:\\/\\/www\\.|wss:\\/\\/www\\.|ws:\\/\\/|wss:\\/\\/)?((((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))|(((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}))|:)))(%.+)?)|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9]))+(\\.([A-Za-z]|[A-Za-z][A-Za-z0-9\\-]*[A-Za-z0-9]))*)(:[0-9]{1,5})?(\\/.*)?$";+ String DYNAMIC_VALUE_PLACEHOLDER = "{$value}";+ String DYNAMIC_VALUE_PLACEHOLDER_REGEXP = "\"?\\{\\$value}\"?";+ /**+ * Specify to use time. First # delimiter used to specify format. Second # delimiter used to add or subtract millis+ */+ String DYNAMIC_TIME_PLACEHOLDER_REGEXP = "\"?\\{\\$time#?([a-z,A-Z,\\-,\\s,:]*)#?(-?\\d*)?}\"?";static String[] units(String...units) {return units;diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.javaindex 3e760ae83..2ba22ff6e 100644--- a/model/src/main/java/org/openremote/model/asset/Asset.java+++ b/model/src/main/java/org/openremote/model/asset/Asset.java@@ -227,7 +227,7 @@ import static org.openremote.model.Constants.PERSISTENCE_UNIQUE_ID_GENERATOR;@DynamicUpdate@TsIgnoreTypeParams@SuppressWarnings("unchecked")-public abstract class Asset<T extends Asset<?>> implements IdentifiableEntity<T> {+public abstract class Asset<T extends Asset<?>> implements IdentifiableEntity<T>, AssetInfo {/*** The purpose of this is to provide {@link org.openremote.model.attribute.Attribute.AttributeDeserializer} access@@ -368,6 +368,10 @@ public abstract class Asset<T extends Asset<?>> implements IdentifiableEntity<T>return name;}+ public String getAssetName() {+ return name;+ }+public T setName(String name) throws IllegalArgumentException {Objects.requireNonNull(name);this.name = name;@@ -478,6 +482,21 @@ public abstract class Asset<T extends Asset<?>> implements IdentifiableEntity<T>return (T) this;}+ @Override+ public String[] getAttributeNames() {+ return getAttributes().keySet().toArray(new String[0]);+ }++ @Override+ public String getAssetType() {+ return type;+ }++ @Override+ public Class<? extends Asset> getAssetClass() {+ return getClass();+ }+@Overridepublic String toString() {return getClass().getSimpleName() + "{" +diff --git a/model/src/main/java/org/openremote/model/asset/AssetEvent.java b/model/src/main/java/org/openremote/model/asset/AssetEvent.javaindex e013369a1..cae7996ec 100644--- a/model/src/main/java/org/openremote/model/asset/AssetEvent.java+++ b/model/src/main/java/org/openremote/model/asset/AssetEvent.java@@ -1,5 +1,5 @@/*- * Copyright 2019, OpenRemote Inc.+ * Copyright 2023, OpenRemote Inc.** See the CONTRIBUTORS.txt file in the distribution for a* full listing of individual contributors.@@ -21,11 +21,10 @@ package org.openremote.model.asset;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;-import org.openremote.model.event.shared.EventFilter;-import org.openremote.model.event.shared.AssetInfo;import org.openremote.model.event.shared.SharedEvent;import java.util.Arrays;+import java.util.Date;/*** This event is used when an {@link Asset} is created, read, updated or deleted (updates are only fired when one or more top@@ -53,7 +52,7 @@ public class AssetEvent extends SharedEvent implements AssetInfo {this.updatedProperties = updatedProperties;}- public String getAssetId() {+ public String getId() {return asset.id;}@@ -61,6 +60,21 @@ public class AssetEvent extends SharedEvent implements AssetInfo {return asset.name;}+ @Override+ public String getAssetType() {+ return asset.type;+ }++ @Override+ public Class<? extends Asset> getAssetClass() {+ return asset.getAssetClass();+ }++ @Override+ public Date getCreatedOn() {+ return asset.createdOn;+ }+@Overridepublic String getRealm() {return asset.realm;diff --git a/model/src/main/java/org/openremote/model/asset/AssetFilter.java b/model/src/main/java/org/openremote/model/asset/AssetFilter.javaindex c25f130f5..928508a87 100644--- a/model/src/main/java/org/openremote/model/asset/AssetFilter.java+++ b/model/src/main/java/org/openremote/model/asset/AssetFilter.java@@ -20,7 +20,6 @@package org.openremote.model.asset;import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.event.shared.AssetInfo;import org.openremote.model.event.shared.EventFilter;import org.openremote.model.event.shared.SharedEvent;import org.openremote.model.util.TextUtil;@@ -39,30 +38,72 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<public static final String FILTER_TYPE = "asset";- protected String[] assetIds;+ protected List<String> assetIds;+ protected List<String> assetTypes;+ protected List<Class<? extends Asset>> assetClasses;protected String realm;- protected String[] parentIds;- protected String[] path;- protected String[] attributeNames;+ protected List<String> parentIds;+ protected List<String> path;+ protected List<String> attributeNames;protected boolean publicEvents;protected boolean restrictedEvents;+ protected boolean internal;public AssetFilter() {}public AssetFilter(String... assetIds) {+ this.assetIds = Arrays.asList(assetIds);+ }++ public AssetFilter(List<String> assetIds) {this.assetIds = assetIds;}public String[] getAssetIds() {- return assetIds;+ return assetIds != null ? assetIds.toArray(new String[0]) : null;}- public AssetFilter<T> setAssetIds(String... assetIds) {+ public AssetFilter<T> setAssetIds(List<String> assetIds) {this.assetIds = assetIds;return this;}+ public AssetFilter<T> setAssetIds(String... assetIds) {+ this.assetIds = Arrays.asList(assetIds);+ return this;+ }++ public String[] getAssetTypes() {+ return assetTypes != null ? assetTypes.toArray(new String[0]) : null;+ }++ public AssetFilter<T> setAssetTypes(String... assetTypes) {+ this.assetTypes = Arrays.asList(assetTypes);+ return this;+ }++ public AssetFilter<T> setAssetTypes(List<String> assetTypes) {+ this.assetTypes = assetTypes;+ return this;+ }++ @SuppressWarnings("unchecked")+ public Class<? extends Asset>[] getAssetClasses() {+ return assetClasses != null ? assetClasses.toArray(new Class[0]) : null;+ }++ @SafeVarargs+ public final AssetFilter<T> setAssetClasses(Class<? extends Asset>... assetClasses) {+ this.assetClasses = Arrays.asList(assetClasses);+ return this;+ }++ public AssetFilter<T> setAssetClasses(List<Class<? extends Asset>> assetClasses) {+ this.assetClasses = assetClasses;+ return this;+ }+public String getRealm() {return realm;}@@ -73,29 +114,29 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<}public String[] getParentIds() {- return parentIds;+ return parentIds != null ? parentIds.toArray(new String[0]) : null;}public AssetFilter<T> setParentIds(String... parentIds) {- this.parentIds = parentIds;+ this.parentIds = Arrays.asList(parentIds);return this;}public String[] getPath() {- return path;+ return path != null ? path.toArray(new String[0]) : null;}public AssetFilter<T> setPath(String[] path) {- this.path = path;+ this.path = Arrays.asList(path);return this;}public String[] getAttributeNames() {- return attributeNames;+ return attributeNames != null ? attributeNames.toArray(new String[0]) : null;}public AssetFilter<T> setAttributeNames(String... attributeNames) {- this.attributeNames = attributeNames;+ this.attributeNames = Arrays.asList(attributeNames);return this;}@@ -117,6 +158,14 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<return this;}+ public boolean isInternal() {+ return internal;+ }++ public void setInternal(boolean internal) {+ this.internal = internal;+ }+@Overridepublic String getFilterType() {return FILTER_TYPE;@@ -128,9 +177,16 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<MetaItemDescriptor<?> filterAttributesBy = null;+ // Non internal subscribers of attribute events only get value updates so make sure the value has changed+ if (!internal && event instanceof AttributeEvent attributeEvent) {+ if (!attributeEvent.valueChanged()) {+ return null;+ }+ }+if (restrictedEvents) {if (event instanceof AttributeEvent attributeEvent) {- if (!attributeEvent.isRestrictedRead()) {+ if (!attributeEvent.getMetaValue(MetaItemType.ACCESS_RESTRICTED_READ).orElse(false)) {return null;}} else {@@ -140,7 +196,7 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<if (publicEvents) {if (event instanceof AttributeEvent attributeEvent) {- if (!attributeEvent.isPublicRead()) {+ if (!attributeEvent.getMetaValue(MetaItemType.ACCESS_PUBLIC_READ).orElse(false)) {return null;}} else if (event instanceof AssetEvent assetEvent) {@@ -152,21 +208,34 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<}}- if (assetIds != null && assetIds.length > 0) {- if (!Arrays.asList(assetIds).contains(event.getAssetId())) {+ if (assetIds != null && !assetIds.isEmpty()) {+ if (!assetIds.contains(event.getId())) {+ return null;+ }+ }++ if (assetTypes != null && !assetTypes.isEmpty()) {+ if (!assetTypes.contains(event.getAssetType())) {+ return null;+ }+ }++ if (assetClasses != null && !assetClasses.isEmpty()) {+ T finalEvent = event;+ if (assetClasses.stream().noneMatch(ac -> ac.isAssignableFrom(finalEvent.getAssetClass()))) {return null;}}- if (parentIds != null && parentIds.length > 0) {- if (!Arrays.asList(parentIds).contains(event.getParentId())) {+ if (parentIds != null && !parentIds.isEmpty()) {+ if (!parentIds.contains(event.getParentId())) {return null;}}- if(path != null && path.length > 0) {+ if(path != null && !path.isEmpty()) {List<String> pathList = Arrays.asList(event.getPath());- if (Arrays.stream(path).noneMatch(pathList::contains)) {+ if (path.stream().noneMatch(pathList::contains)) {return null;}}@@ -188,9 +257,9 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<}}- if (attributeNames != null && attributeNames.length > 0) {+ if (attributeNames != null && !attributeNames.isEmpty()) {List<String> eventAttributeNames = Arrays.asList(event.getAttributeNames());- if (Arrays.stream(attributeNames).noneMatch(eventAttributeNames::contains)) {+ if (attributeNames.stream().noneMatch(eventAttributeNames::contains)) {return null;}}@@ -201,10 +270,11 @@ public class AssetFilter<T extends SharedEvent & AssetInfo> extends EventFilter<@Overridepublic String toString() {return getClass().getSimpleName() + "{" +- "assetIds='" + (assetIds != null ? Arrays.toString(assetIds) : "") + '\'' +- ", parentIds='" + (parentIds != null ? Arrays.toString(parentIds) : "") + '\'' ++ "assetIds='" + (assetIds != null ? String.join(",", assetIds) : "") + '\'' ++ "assetTypes='" + (assetTypes != null ? String.join(",", assetTypes) : "") + '\'' ++ ", parentIds='" + (parentIds != null ? String.join(",", parentIds) : "") + '\'' +", realm='" + realm + '\'' +- ", attributeNames='" + (attributeNames != null ? Arrays.toString(attributeNames) : "") + '\'' ++ ", attributeNames='" + (attributeNames != null ? String.join(",", attributeNames) : "") + '\'' +'}';}}diff --git a/model/src/main/java/org/openremote/model/event/shared/AssetInfo.java b/model/src/main/java/org/openremote/model/asset/AssetInfo.javasimilarity index 82%rename from model/src/main/java/org/openremote/model/event/shared/AssetInfo.javarename to model/src/main/java/org/openremote/model/asset/AssetInfo.javaindex 1d623c32f..c9d279e4c 100644--- a/model/src/main/java/org/openremote/model/event/shared/AssetInfo.java+++ b/model/src/main/java/org/openremote/model/asset/AssetInfo.java@@ -17,11 +17,13 @@* You should have received a copy of the GNU Affero General Public License* along with this program. If not, see <http://www.gnu.org/licenses/>.*/-package org.openremote.model.event.shared;+package org.openremote.model.asset;++import java.util.Date;public interface AssetInfo {- String getAssetId();+ String getId();String getRealm();@@ -30,4 +32,12 @@ public interface AssetInfo {String[] getPath();String[] getAttributeNames();++ String getAssetName();++ String getAssetType();++ Class<? extends Asset> getAssetClass();++ Date getCreatedOn();}diff --git a/model/src/main/java/org/openremote/model/asset/agent/Agent.java b/model/src/main/java/org/openremote/model/asset/agent/Agent.javaindex 3aded4206..304251436 100644--- a/model/src/main/java/org/openremote/model/asset/agent/Agent.java+++ b/model/src/main/java/org/openremote/model/asset/agent/Agent.java@@ -20,13 +20,13 @@package org.openremote.model.asset.agent;import org.openremote.model.asset.Asset;-import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.MetaItem;import org.openremote.model.auth.OAuthGrant;import org.openremote.model.auth.UsernamePassword;import org.openremote.model.util.TsIgnoreTypeParams;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.MetaHolder;import org.openremote.model.value.MetaItemType;import org.openremote.model.value.ValueType;@@ -151,7 +151,7 @@ public abstract class Agent<T extends Agent<T, U, V>, U extends Protocol<T>, V e* Get the {@link AgentLink} for the specified linked agent*/@SuppressWarnings("unchecked")- public V getAgentLink(Attribute<?> attribute) {+ public <T extends MetaHolder> V getAgentLink(T attribute) {AgentLink<?> agentLink = attribute.getMetaValue(AGENT_LINK).orElseThrow(() -> new IllegalStateException("Failed to getAgentLink<?>despite attribute being linked to an agent"));return (V) agentLink;}diff --git a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.javaindex 38c70a343..726ae77fe 100644--- a/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java+++ b/model/src/main/java/org/openremote/model/asset/agent/AgentLink.java@@ -24,6 +24,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo;import com.fasterxml.jackson.databind.annotation.JsonSerialize;import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaFormat;import com.kjetland.jackson.jsonSchema.annotations.JsonSchemaInject;+import org.openremote.model.Constants;import org.openremote.model.attribute.Attribute;import org.openremote.model.query.filter.ValuePredicate;import org.openremote.model.util.JSONSchemaUtil;@@ -59,7 +60,7 @@ public abstract class AgentLink<T extends AgentLink<?>> implements Serializable@JsonSchemaInject(merge = false, jsonSupplierViaLookup = JSONSchemaUtil.SCHEMA_SUPPLIER_NAME_PATTERN_PROPERTIES_ANY_KEY_ANY_TYPE)@JsonPropertyDescription("Similar to valueConverter but will be applied to outgoing values allowing for the opposite conversion")protected Map<String, Object> writeValueConverter;- @JsonPropertyDescription("String to be used for attribute writes and can contain '" + Protocol.DYNAMIC_VALUE_PLACEHOLDER ++ @JsonPropertyDescription("String to be used for attribute writes and can contain '" + Constants.DYNAMIC_VALUE_PLACEHOLDER +"' placeholders to allow the written value to be injected into the string or to even hardcode the value written to the" +" protocol (particularly useful for executable attributes)")@JsonSchemaFormat("or-multiline")diff --git a/model/src/main/java/org/openremote/model/asset/agent/Protocol.java b/model/src/main/java/org/openremote/model/asset/agent/Protocol.javaindex 4311f63ec..5d4de71c8 100644--- a/model/src/main/java/org/openremote/model/asset/agent/Protocol.java+++ b/model/src/main/java/org/openremote/model/asset/agent/Protocol.java@@ -22,15 +22,13 @@ package org.openremote.model.asset.agent;import org.openremote.model.Container;import org.openremote.model.ContainerService;import org.openremote.model.asset.Asset;-import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.attribute.AttributeRef;-import org.openremote.model.attribute.MetaItem;+import org.openremote.model.attribute.*;+import org.openremote.model.protocol.ProtocolAssetService;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.TsIgnoreTypeParams;+import org.openremote.model.util.ValueUtil;import org.openremote.model.value.MetaItemType;import org.openremote.model.value.ValueFilter;-import org.openremote.model.util.ValueUtil;import java.util.Map;import java.util.logging.Logger;@@ -72,7 +70,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;* The protocol handles read and write of linked attributes:* <p>* If the actual state of the device (or service) changes, the linked protocol writes the new state into the attribute- * value and notifies the context broker of the change. A protocol updates a linked attributes' value by sending an+ * by sending an {@link AttributeEvent} using the {@link #}value and notifies the context broker of the change. A protocol updates a linked attributes' value by sending an* {@link AttributeEvent} messages on the {@link #SENSOR_QUEUE}, including the source protocol name in header {@link* #SENSOR_QUEUE_SOURCE_PROTOCOL}.* <p>@@ -100,7 +98,7 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;* <ol>* <li>Configurable value conversion which allows the value sent from the linked attribute to be converted in a* configurable way before being sent to the protocol for processing (see {@link AgentLink#getWriteValueConverter})- * <li>Configurable dynamic value insertion (replacement of {@link #DYNAMIC_VALUE_PLACEHOLDER} strings within a+ * <li>Configurable dynamic value insertion (replacement of {@link org.openremote.model.Constants#DYNAMIC_VALUE_PLACEHOLDER} strings within a* pre-defined JSON string with the value sent from the linked attribute (this allows for attribute values to be inserted* into a larger payload before processing by the protocol; it also allows the written value to be fixed or statically* converted (see {@link AgentLink#getWriteValue}).@@ -154,24 +152,6 @@ import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;public interface Protocol<T extends Agent<T, ?, ?>> {Logger LOG = SyslogCategory.getLogger(PROTOCOL, Protocol.class);- String ACTUATOR_TOPIC_TARGET_PROTOCOL = "Protocol";- String SENSOR_QUEUE_SOURCE_PROTOCOL = "Protocol";-- // TODO: Some of these options should be configurable depending on expected load etc.- // Message topic for communicating from asset/thing to protocol layer (asset attribute changed, trigger actuator)- String ACTUATOR_TOPIC = "seda://ActuatorTopic?multipleConsumers=true&concurrentConsumers=1&waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=true&limitConcurrentConsumers=false&size=1000";-- // Message queue for communicating from protocol to asset/thing layer (sensor changed, trigger asset attribute update)- String SENSOR_QUEUE = "seda://SensorQueue?waitForTaskToComplete=NEVER&purgeWhenStopping=true&discardIfNoConsumers=false&size=25000";-- String DYNAMIC_VALUE_PLACEHOLDER = "{$value}";-- String DYNAMIC_VALUE_PLACEHOLDER_REGEXP = "\"?\\{\\$value}\"?";-- /**- * Specify to use time. First # delimiter used to specify format. Second # delimiter used to add or subtract millis- */- String DYNAMIC_TIME_PLACEHOLDER_REGEXP = "\"?\\{\\$time#?([a-z,A-Z,\\-,\\s,:]*)#?(-?\\d*)?}\"?";/*** Prefixes the log message with {@link #getProtocolName} and {@link #getProtocolInstanceUri}.@@ -231,4 +211,40 @@ public interface Protocol<T extends Agent<T, ?, ?>> {* Get the {@link Agent} associated with this protocol instance.*/T getAgent();++ /**+ * Update the value of a linked {@link Attribute} linked to this {@link Protocol}; call this to publish new sensor+ * values. Implementators are responsible for ensuring the requested {@link Attribute} is actually linked to this+ * {@link Protocol}.+ */+ void updateLinkedAttribute(final AttributeState state, long timestamp);++ /**+ * Update the value of a linked {@link Attribute} linked to this {@link Protocol}; call this to publish new sensor+ * values. Implementators are responsible for ensuring the requested {@link Attribute} is actually linked to this+ * {@link Protocol}.+ */+ void updateLinkedAttribute(final AttributeState state);+++ /**+ * Called by instantiator during instantiation to allow the {@link Protocol} to interact with {@link Asset}s and+ * {@link Attribute}s.+ */+ void setAssetService(ProtocolAssetService assetService);++ /**+ * An Attribute event (write) has been requested for an attribute linked to this protocol; implementations should+ * handle multithreaded requests or use some sort of synchronisation for thread safety. The {@link AttributeEvent}+ * will be enriched with data about the relating asset and attribute.+ */+ void processLinkedAttributeWrite(AttributeEvent event);++ /**+ * Called when one of the associated {@link Agent}'s {@link Attribute}s are updated; the implementation can decide+ * if it wants to request the protocol instance to be re-created thus simplifying handling of config changes.+ *+ * @return true if the agent should be redeployed (this will stop current protocol instance and create a new one).+ */+ boolean onAgentAttributeChanged(AttributeEvent event);}diff --git a/model/src/main/java/org/openremote/model/asset/impl/ConsoleAsset.java b/model/src/main/java/org/openremote/model/asset/impl/ConsoleAsset.javaindex b58faf5b0..4af353128 100644--- a/model/src/main/java/org/openremote/model/asset/impl/ConsoleAsset.java+++ b/model/src/main/java/org/openremote/model/asset/impl/ConsoleAsset.java@@ -128,7 +128,7 @@ public class ConsoleAsset extends Asset<ConsoleAsset> {// valid = false;// }//-// Value providerValue = asset.getAttribute(Asset.CONSOLE_PROVIDERS.getAttributeName()).flatMap(AbstractValueHolder::getValue).orElse(null);+// Value providerValue = asset.getAttribute(Asset.CONSOLE_PROVIDERS.getName()).flatMap(AbstractValueHolder::getValue).orElse(null);//// if (providerValue != null && !getConsoleProviders(asset).isPresent()) {// if (validationFailures != null) {diff --git a/model/src/main/java/org/openremote/model/attribute/Attribute.java b/model/src/main/java/org/openremote/model/attribute/Attribute.javaindex 9287d30b3..54be4080f 100644--- a/model/src/main/java/org/openremote/model/attribute/Attribute.java+++ b/model/src/main/java/org/openremote/model/attribute/Attribute.java@@ -336,22 +336,6 @@ public class Attribute<T> extends AbstractNameValueHolder<T> implements MetaHoldreturn this;}- public <U> Optional<U> getMetaValue(MetaItemDescriptor<U> metaItemDescriptor) {- return getMeta().getValue(metaItemDescriptor);- }-- public boolean hasMeta(MetaItemDescriptor<?> metaItemDescriptor) {- return getMeta().has(metaItemDescriptor);- }-- public boolean hasMeta(String metaItemName) {- return getMeta().has(metaItemName);- }-- public <U> Optional<MetaItem<U>> getMetaItem(MetaItemDescriptor<U> metaItemDescriptor) {- return getMeta().get(metaItemDescriptor);- }-@SuppressWarnings("unchecked")@Overridepublic Optional<T> getValue() {diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.javaindex 3ce16561b..47b4ef953 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeEvent.java@@ -1,5 +1,5 @@/*- * Copyright 2017, OpenRemote Inc.+ * Copyright 2023, OpenRemote Inc.** See the CONTRIBUTORS.txt file in the distribution for a* full listing of individual contributors.@@ -21,115 +21,159 @@ package org.openremote.model.attribute;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonIgnore;-import com.fasterxml.jackson.annotation.JsonProperty;-import org.openremote.model.event.shared.AssetInfo;+import com.fasterxml.jackson.annotation.JsonView;+import com.fasterxml.jackson.databind.annotation.JsonSerialize;+import jakarta.annotation.Nonnull;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.AssetInfo;import org.openremote.model.event.shared.SharedEvent;+import org.openremote.model.util.TsIgnore;+import org.openremote.model.util.ValueUtil;+import org.openremote.model.validation.AttributeInfoValid;import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.ValueDescriptor;+import java.util.Date;import java.util.Objects;import java.util.Optional;/**- * A timestamped {@link AttributeState}.+ * Represents an {@link Attribute} value at a point in time.*/-public class AttributeEvent extends SharedEvent implements AssetInfo {+@SuppressWarnings({"rawtypes", "unchecked"})+@AttributeInfoValid+public class AttributeEvent extends SharedEvent implements AttributeInfo {- public static final String HEADER_SOURCE = AttributeEvent.class.getName() + ".SOURCE";+ static {+ // Set the default view for serialisation+ ValueUtil.JSON.setConfig(ValueUtil.JSON.getSerializationConfig().withView(Basic.class));+ }- /**- * Processing of the attribute event depends on the origin of the event.- */- public enum Source {-- /**- * The event was created by a client, it's a write request by a user.- */- CLIENT,-- /**- * The event was created by internal processing, for example, as a rule- * consequence. Protocols can also create events for internal processing,- * to update any assets' state.- */- INTERNAL, // This needs to be more fine grained-- /**- * An attribute event has been created by the linking service, which enables- * writing the same value onto another, linked attribute.- */- ATTRIBUTE_LINKING_SERVICE,-- /**- * The event is a value change on a sensor, created by a protocol.- */- SENSOR,-- /**- * An attribute event generated by a gateway connector.- */- GATEWAY- }-- protected AttributeState attributeState;+ @TsIgnore+ protected interface Basic {}+ @TsIgnore+ public interface Enhanced {}++ protected AttributeRef ref;+ protected Object value;+ protected boolean deleted;+ @JsonIgnore+ protected Object source;protected String realm;+ @JsonView(Enhanced.class)protected String parentId;+ @JsonIgnore+ protected ValueDescriptor<?> valueType;+ @JsonView(Enhanced.class)+ protected Object oldValue;+ @JsonView(Enhanced.class)+ protected long oldValueTimestamp;+ @JsonView(Enhanced.class)protected String[] path;+ @JsonView(Enhanced.class)+ protected String assetName;+ @JsonView(Enhanced.class)+ protected String assetType;@JsonIgnore- protected boolean publicRead;+ protected Class<? extends Asset> assetClass;+ @JsonView(Enhanced.class)+ protected Date createdOn;@JsonIgnore- protected boolean restrictedRead;+ protected MetaMap meta;public <T> AttributeEvent(String assetId, AttributeDescriptor<T> attributeDescriptor, T value) {this(assetId, attributeDescriptor.getName(), value);}public AttributeEvent(String assetId, String attributeName, Object value) {- this(new AttributeState(new AttributeRef(assetId, attributeName), value));- }-- public AttributeEvent(String assetId, String attributeName) {- this(new AttributeState(new AttributeRef(assetId, attributeName)));+ this(new AttributeRef(assetId, attributeName), value);}public AttributeEvent(String assetId, String attributeName, Object value, long timestamp) {- this(new AttributeState(new AttributeRef(assetId, attributeName), value), timestamp);+ this(new AttributeRef(assetId, attributeName), value, timestamp);}- public AttributeEvent(AttributeRef attributeRef, Object value) {- this(new AttributeState(attributeRef, value));+ public AttributeEvent(AttributeState attributeState) {+ this(attributeState.getRef(), attributeState.getValue().orElse(null));}- public AttributeEvent(AttributeRef attributeRef) {- this(new AttributeState(attributeRef));+ public AttributeEvent(AttributeState attributeState, long timestamp) {+ this(attributeState.getRef(), attributeState.getValue().orElse(null), timestamp);}- public AttributeEvent(AttributeRef attributeRef, Object value, long timestamp) {- this(new AttributeState(attributeRef, value), timestamp);+ public AttributeEvent(AttributeRef attributeRef, Object value) {+ this(attributeRef, value, 0L);}- public AttributeEvent(AttributeState attributeState) {- this.attributeState = attributeState;+ public AttributeEvent(AttributeRef ref, Object value, long timestamp) {+ super(timestamp);+ Objects.requireNonNull(ref);+ this.ref = ref;+ this.value = value;}@JsonCreator- public AttributeEvent(@JsonProperty("attributeState") AttributeState attributeState, @JsonProperty("timestamp") long timestamp) {+ @Deprecated+ // This allows backwards compatibility with old attribute event structure+ protected AttributeEvent(AttributeState attributeState, AttributeRef ref, Object value, long timestamp) {super(timestamp);- Objects.requireNonNull(attributeState);- this.attributeState = attributeState;+ if (attributeState != null) {+ this.ref = attributeState.getRef();+ this.value = attributeState.getValue().orElse(null);+ } else {+ this.ref = ref;+ this.value = value;+ }+ }++ public AttributeEvent(AssetInfo asset, Attribute<?> attribute, Object source, Object value, long valueTimestamp, Object oldValue, long oldValueTimestamp) {+ this(new AttributeRef(asset.getId(), attribute.getName()),value, valueTimestamp);++ this.oldValue = oldValue;+ this.oldValueTimestamp = oldValueTimestamp;+ this.source = source;++ this.valueType = attribute.getType();+ this.meta = attribute.getMeta();++ this.path = asset.getPath();+ this.createdOn = asset.getCreatedOn();+ this.assetName = asset.getAssetName();+ this.assetType = asset.getAssetType();+ this.assetClass = asset.getAssetClass();+ this.parentId = asset.getParentId();+ this.realm = asset.getRealm();}- public AttributeState getAttributeState() {- return attributeState;+ @Override+ public AttributeState getState() {+ return new AttributeState(ref, value);}- public AttributeRef getAttributeRef() {- return getAttributeState().getRef();+ @Override+ public long getTimestamp() {+ return super.getTimestamp();}- public String getAssetId() {- return getAttributeRef().getId();+ public Optional<Object> getOldValue() {+ return Optional.ofNullable(oldValue);}+ public long getOldValueTimestamp() {+ return oldValueTimestamp;+ }++ @Override+ public AttributeRef getRef() {+ return ref;+ }++ @Override+ public String getId() {+ return getRef().getId();+ }++ @Overridepublic String getRealm() {return realm;}@@ -139,6 +183,19 @@ public class AttributeEvent extends SharedEvent implements AssetInfo {return this;}+ public Object getSource() {+ return source;+ }++ public AttributeEvent setSource(Object source) {+ this.source = source;+ return this;+ }++ public boolean isOutdated() {+ return oldValueTimestamp - getTimestamp() > 0;+ }+@Overridepublic String getParentId() {return parentId;@@ -154,53 +211,136 @@ public class AttributeEvent extends SharedEvent implements AssetInfo {return path;}- public AttributeEvent setPath(String[] path) {- this.path = path;- return this;+ @Override+ public String[] getAttributeNames() {+ return new String[]{getRef().getName()};}@Override- public String[] getAttributeNames() {- return new String[]{getAttributeRef().getName()};+ public String getAssetName() {+ return assetName;+ }++ @Override+ public String getAssetType() {+ return assetType;}- public String getAttributeName() {- return getAttributeRef().getName();+ @Override+ public Class<? extends Asset> getAssetClass() {+ return assetClass;}- public <T> Optional<T> getValue() {- return getAttributeState().getValue();+ @Override+ public Date getCreatedOn() {+ return createdOn;}- public AttributeEvent setAccessPublicRead(boolean publicRead) {- this.publicRead = publicRead;+ @JsonView(Enhanced.class)+ @JsonSerialize(converter = ValueDescriptor.NameHolderToStringConverter.class)+ @Override+ public ValueDescriptor getType() {+ return valueType;+ }++ @Override+ public Class<?> getTypeClass() {+ return getType() != null ? getType().getType() : Object.class;+ }++ public AttributeEvent setValue(Object value) {+ this.value = value;return this;}- public AttributeEvent setAccessRestrictedRead(boolean restrictedRead) {- this.restrictedRead = restrictedRead;+ @Override+ public Optional<Object> getValue() {+ return Optional.ofNullable(value);+ }++ @Override+ public Optional getValue(@Nonnull Class valueType) {+ return ValueUtil.getValueCoerced(value, valueType);+ }++ @JsonIgnore+ @Override+ public MetaMap getMeta() {+ return meta;+ }++ @JsonIgnore+ @Override+ public String getName() {+ return ref.getName();+ }++ public boolean isDeleted() {+ return deleted;+ }++ public AttributeEvent setDeleted(boolean deleted) {+ this.deleted = deleted;return this;}- public boolean isPublicRead() {- return publicRead;+ /**+ * Compares entity identifier, attribute name, value, source, and optional timestamp.+ */+ public boolean matches(AttributeEvent event) {+ return getId().equals(event.getId())+ && getName().equals(event.getName())+ && isDeleted() == event.isDeleted()+ && getTimestamp() == event.getTimestamp();+ }++ @Override+ public int compareTo(AttributeInfo that) {+ int result = getId().compareTo(that.getId());+ if (result == 0)+ result = getName().compareTo(that.getName());+ if (result == 0)+ result = Long.compare(getTimestamp(), that.getTimestamp());+ return result;+ }++ public boolean valueChanged() {+ // Just use the timestamp for performance+ return oldValueTimestamp != getTimestamp();+ }++ /**+ * Simple equality comparing {@link #getId()} and {@link #getName()}+ */+ @Override+ public boolean equals(Object o) {+ if (this == o) return true;+ if (o == null || getClass() != o.getClass()) return false;+ AttributeEvent that = (AttributeEvent) o;+ return Objects.equals(getName(), that.getName())+ && Objects.equals(getId(), that.getId());}- public boolean isRestrictedRead() {- return restrictedRead;+ @Override+ public int hashCode() {+ return Objects.hash(getName(), getId());}@Overridepublic String toString() {+ String valueStr = Objects.toString(value);return getClass().getSimpleName() + "{" +- "timestamp=" + timestamp +- ", attributeState=" + attributeState ++ "timestamp=" + timestamp.toInstant() ++ ", ref=" + ref ++ ", value=" + (valueStr.length() > 100 ? valueStr.substring(0, 100) + "..." : valueStr) +"}";}- public static AttributeEvent deletedAttribute(String assetId, String attributeName) {- AttributeEvent event = new AttributeEvent(assetId, attributeName);- event.attributeState.deleted = true;- return event;+ public String toStringWithValueType() {+ return getClass().getSimpleName() + "{" ++ "timestamp=" + timestamp.toInstant() ++ ", ref=" + ref ++ ", valueType=" + (value != null ? value.getClass().getName() : "null") ++ "}";}}diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeInfo.java b/model/src/main/java/org/openremote/model/attribute/AttributeInfo.javanew file mode 100644index 000000000..f97bbbc1a--- /dev/null+++ b/model/src/main/java/org/openremote/model/attribute/AttributeInfo.java@@ -0,0 +1,19 @@+package org.openremote.model.attribute;++import org.openremote.model.asset.AssetInfo;+import org.openremote.model.value.MetaHolder;+import org.openremote.model.value.NameValueHolder;++import java.util.Optional;++public interface AttributeInfo extends AssetInfo, NameValueHolder<Object>, MetaHolder, Comparable<AttributeInfo> {+ long getTimestamp();+ AttributeRef getRef();+ String getId();+ String getName();+ AttributeState getState();++ Optional<Object> getOldValue();++ long getOldValueTimestamp();+}diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java b/model/src/main/java/org/openremote/model/attribute/AttributeLink.javaindex e2d3c364a..9ccdd33c4 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeLink.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeLink.java@@ -21,6 +21,7 @@ package org.openremote.model.attribute;import com.fasterxml.jackson.annotation.JsonCreator;import com.fasterxml.jackson.annotation.JsonProperty;+import org.openremote.model.value.MetaItemType;import org.openremote.model.value.ValueFilter;import java.io.Serializable;@@ -33,8 +34,44 @@ import static java.util.Objects.requireNonNull;/*** A link from one attribute to another with a definition of how to map the value from the source attribute* to the linked attribute.+ * <p>+ * By default the exact value of the attribute is forwarded unless a key exists in the converter JSON Object that+ * matches the value as a string (note matches are case sensitive so booleans should be lower case i.e. true or false);+ * in that case the value of the key is forwarded instead. There are several special conversions available by using+ * the value of a {@link AttributeLink.ConverterType} as the value. This allows for example a button press to toggle a+ * boolean attribute or for a particular value to be ignored.+ * <p>+ * The {@link #getFilters()} can be used to filter the source value before it is applied to the converter.+ * <p>+ * To convert null values the converter key of {@link org.openremote.model.util.ValueUtil#NULL_LITERAL} can be used.+ * <p>+ * Example {@link MetaItemType#ATTRIBUTE_LINKS} meta items:+ * <blockquote><pre>{@code+ * [+ * "name": "urn:openremote:asset:meta:attributeLink",+ * "value": {+ * "ref": ["0oI7Gf_kTh6WyRJFUTr8Lg", "light1"],+ * "converter": {+ * "PRESSED": "@TOGGLE",+ * "LONG_PRESSED": "@IGNORE",+ * "RELEASED": "@IGNORE"+ * }+ * }+ * ],+ * [+ * "name": "urn:openremote:asset:meta:attributeLink",+ * "value": {+ * "ref": ["0oI7Gf_kTh6WyRJFUTr8Lg", "light2"],+ * "converter": {+ * "0": true,+ * "1": false+ * "NULL": "@IGNORE"+ * }+ * }+ * ]+ * }</pre></blockquote>*/-// TODO: Somehow combine this with flow rules+//TODO: Somehow combine this with rulespublic class AttributeLink implements Serializable {public enum ConverterType {diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeState.java b/model/src/main/java/org/openremote/model/attribute/AttributeState.javaindex ae9be4b47..6040415f5 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeState.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeState.java@@ -36,7 +36,6 @@ public class AttributeState implements Serializable {@JsonPropertyprotected AttributeRef ref;protected Object value;- protected boolean deleted;AttributeState() {}@@ -73,17 +72,12 @@ public class AttributeState implements Serializable {this.value = value;}- public boolean isDeleted() {- return deleted;- }-@Overridepublic String toString() {String valueStr = Objects.toString(value);return getClass().getSimpleName() + "{" +"ref=" + ref +", value=" + (valueStr.length() > 100 ? valueStr.substring(0, 100) + "..." : valueStr) +- ", deleted=" + deleted +'}';}}diff --git a/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java b/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.javaindex dc09dc41a..ecbd2b474 100644--- a/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java+++ b/model/src/main/java/org/openremote/model/attribute/AttributeWriteFailure.java@@ -19,22 +19,8 @@*/package org.openremote.model.attribute;-import org.openremote.model.asset.agent.Agent;-import org.openremote.model.value.ValueType;-public enum AttributeWriteFailure {- /**- * Missing {@link AttributeEvent.Source}.- */- MISSING_SOURCE,-- /**- * The source of the event does not match the expected source (e.g. expected client message but received sensor- * read).- */- ILLEGAL_SOURCE,-/*** The asset does not exist.*/@@ -45,32 +31,6 @@ public enum AttributeWriteFailure {*/ATTRIBUTE_NOT_FOUND,- /**- * An actuator update must be for an attribute with a valid agent link.- */- INVALID_AGENT_LINK,-- /**- * An attribute is linked to another attribute, but the link is invalid.- */- INVALID_ATTRIBUTE_LINK,-- /**- * An attribute is linked to another attribute, but the linked attribute can't be written because conversion to the- * target attribute's value failed.- */- LINKED_ATTRIBUTE_CONVERSION_FAILURE,-- /**- * Attributes of an {@link Agent} can not be individually updated.- */- ILLEGAL_AGENT_UPDATE,-- /**- * Invalid {@link AttributeExecuteStatus} for {@link Attribute} of type {@link ValueType#EXECUTION_STATUS}.- */- INVALID_ATTRIBUTE_EXECUTE_STATUS,-/*** Realm configuration or user privileges do not allow update (e.g. realm inactive, user is missing required role,* attribute is read-only).@@ -78,24 +38,14 @@ public enum AttributeWriteFailure {INSUFFICIENT_ACCESS,/**- * The event timestamp is later than the processing time.+ * Value has incorrect type or has failed validation rules.*/- EVENT_IN_FUTURE,+ INVALID_VALUE,/**- * The event timestamp is older than the last updated timestamp of the attribute.+ * Any unknown error that is thrown by an interceptor.*/- EVENT_OUTDATED,-- /**- * Applying the event violates constraints of the attribute.- */- ATTRIBUTE_VALIDATION_FAILURE,-- /**- * Any other error, typically other runtime exceptions thrown by a processor.- */- PROCESSOR_FAILURE,+ INTERCEPTOR_FAILURE,/*** Writing the asset attribute state to database failed.@@ -103,22 +53,17 @@ public enum AttributeWriteFailure {STATE_STORAGE_FAILED,/**- * The event value is not the excepted value for the attribute- */- INVALID_VALUE_FOR_WELL_KNOWN_ATTRIBUTE,-- /**- * The event is for a gateway descendant asset but the gateway is not connected+ * The event interceptor/consumer cannot process the event.*/- GATEWAY_DISCONNECTED,+ CANNOT_PROCESS,/**- * The realm either doesn't exist or is inactive+ * The attribute event processor queue is full.*/- INVALID_REALM,+ QUEUE_FULL,/**- * Fallback failure when no other value makes sense+ * Fallback failure when no other value makes sense.*/UNKNOWN}diff --git a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.javaindex 7cf6b539b..96e547683 100644--- a/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java+++ b/model/src/main/java/org/openremote/model/datapoint/AssetDatapointResource.java@@ -25,6 +25,8 @@ import org.openremote.model.datapoint.query.AssetDatapointQuery;import org.openremote.model.http.RequestParams;import jakarta.annotation.security.RolesAllowed;import jakarta.ws.rs.*;+import jakarta.ws.rs.container.AsyncResponse;+import jakarta.ws.rs.container.Suspended;import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;@@ -60,8 +62,9 @@ public interface AssetDatapointResource {@Path("export")@Produces("application/zip")@RolesAllowed({Constants.READ_ASSETS_ROLE})- void getDatapointExport(@QueryParam("attributeRefs") String attributeRefsString,+ void getDatapointExport(@Suspended AsyncResponse asyncResponse,+ @QueryParam("attributeRefs") String attributeRefsString,@QueryParam("fromTimestamp") long fromTimestamp,- @QueryParam("toTimestamp") long toTimestamp) throws InterruptedException;+ @QueryParam("toTimestamp") long toTimestamp);}diff --git a/model/src/main/java/org/openremote/model/datapoint/Datapoint.java b/model/src/main/java/org/openremote/model/datapoint/Datapoint.javaindex 1bb83edec..aeeddc110 100644--- a/model/src/main/java/org/openremote/model/datapoint/Datapoint.java+++ b/model/src/main/java/org/openremote/model/datapoint/Datapoint.java@@ -63,7 +63,7 @@ public abstract class Datapoint implements Serializable {}public Datapoint(AttributeEvent stateEvent) {- this(stateEvent.getAttributeState(), stateEvent.getTimestamp());+ this(stateEvent.getState(), stateEvent.getTimestamp());}public Datapoint(AttributeRef attributeRef, Object value, long timestamp) {diff --git a/model/src/main/java/org/openremote/model/event/Event.java b/model/src/main/java/org/openremote/model/event/Event.javaindex 5e6434907..edf7b01a3 100644--- a/model/src/main/java/org/openremote/model/event/Event.java+++ b/model/src/main/java/org/openremote/model/event/Event.java@@ -20,6 +20,7 @@package org.openremote.model.event;import com.fasterxml.jackson.annotation.JsonIgnore;+import com.fasterxml.jackson.annotation.JsonProperty;import org.openremote.model.util.TextUtil;import jakarta.persistence.Column;@@ -40,7 +41,8 @@ public abstract class Event {@Temporal(TemporalType.TIMESTAMP)@Column(name = "TIMESTAMP", updatable = false, nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")- public Date timestamp;+ @JsonIgnore+ protected Date timestamp;protected Event(long timestamp) {this.timestamp = new Date(timestamp);@@ -65,6 +67,7 @@ public abstract class Event {return getEventType(getClass());}+ @JsonPropertypublic long getTimestamp() {return timestamp != null ? timestamp.getTime() : 0L;}diff --git a/model/src/main/java/org/openremote/model/protocol/ProtocolAssetService.java b/model/src/main/java/org/openremote/model/protocol/ProtocolAssetService.javanew file mode 100644index 000000000..45094c0a7--- /dev/null+++ b/model/src/main/java/org/openremote/model/protocol/ProtocolAssetService.java@@ -0,0 +1,82 @@+/*+ * Copyright 2017, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.model.protocol;++import org.openremote.model.PersistenceEvent;+import org.openremote.model.asset.Asset;+import org.openremote.model.asset.agent.Agent;+import org.openremote.model.attribute.AttributeEvent;+import org.openremote.model.query.AssetQuery;++import java.util.List;+import java.util.function.Consumer;++/**+ * Interface for protocols to perform limited asset related operations within their own realm+ */+public interface ProtocolAssetService {++ /**+ * Protocols may store assets in the context or update existing assets. A unique identifier must be set by the+ * protocol implementor, and the parent must be the {@link Agent} associated with the requesting protocol instance.+ * This operation stores transient or detached state and returns the current state. It will override any existing+ * stored asset data, ignoring versions.+ */+ <T extends Asset<?>> T mergeAsset(T asset);++ /**+ * Protocols may remove assets from the context store.+ *+ * @return <code>false</code> if the delete could not be performed (asset may have children?)+ */+ boolean deleteAssets(String...assetIds);++ /**+ * Get asset from the store by ID.+ */+ <T extends Asset<?>> T findAsset(String assetId);++ /**+ * Get assets by an {@link org.openremote.model.query.AssetQuery}; can only access {@link Asset}s that are+ * within the same realm as the {@link Agent}.+ */+ List<Asset<?>> findAssets(AssetQuery assetQuery);++ /**+ * Send an {@link AttributeEvent} through the system+ */+ void sendAttributeEvent(AttributeEvent attributeEvent);++ /**+ * Subscribe to changes of {@link Asset}s that are descendants of the agent.+ * <p>+ * When an agent is unlinked from a protocol then all subscriptions will be automatically removed also; it is safe+ * to call this method multiple times for the same agentId and assetChangeConsumer and only a single subscription+ * would actually be created.+ */+ void subscribeChildAssetChange(Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer);++ /**+ * Unsubscribe to changes of {@link Asset}s that are descendants of the agent.+ * <p>+ * When an agent is unlinked from a protocol then all subscriptions will be automatically removed also.+ */+ void unsubscribeChildAssetChange(Consumer<PersistenceEvent<Asset<?>>> assetChangeConsumer);+}diff --git a/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java b/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.javaindex 9e2683ed8..0b73b4e73 100644--- a/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java+++ b/model/src/main/java/org/openremote/model/protocol/ProtocolUtil.java@@ -19,22 +19,19 @@*/package org.openremote.model.protocol;-import org.apache.commons.codec.binary.BinaryCodec;-import org.apache.commons.codec.binary.Hex;-import org.apache.commons.lang3.ArrayUtils;+import org.openremote.model.Constants;import org.openremote.model.asset.agent.AgentLink;-import org.openremote.model.asset.agent.Protocol;import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeExecuteStatus;import org.openremote.model.attribute.AttributeLink;import org.openremote.model.attribute.AttributeState;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.query.filter.ValuePredicate;+import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.Pair;import org.openremote.model.util.TextUtil;import org.openremote.model.util.TsIgnore;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.ValueFilter;-import org.openremote.model.value.ValueType;import java.util.Locale;import java.util.Map;@@ -42,72 +39,37 @@ import java.util.Optional;import java.util.concurrent.atomic.AtomicReference;import java.util.function.Consumer;import java.util.function.Supplier;-import java.util.logging.Level;+import java.util.logging.Logger;-import static org.openremote.model.asset.agent.Protocol.DYNAMIC_VALUE_PLACEHOLDER;+import static org.openremote.model.Constants.DYNAMIC_VALUE_PLACEHOLDER;+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;import static org.openremote.model.util.ValueUtil.NULL_LITERAL;import static org.openremote.model.util.ValueUtil.applyValueFilters;@TsIgnorepublic final class ProtocolUtil {+ protected static Logger LOG = SyslogCategory.getLogger(PROTOCOL, ProtocolUtil.class);+protected ProtocolUtil() {}- public static String bytesToHexString(byte[] bytes) {- return Hex.encodeHexString(bytes).toUpperCase(Locale.ROOT);- }-- public static byte[] bytesFromHexString(String hex) {- try {- return Hex.decodeHex(hex.toCharArray());- } catch (Exception e) {- Protocol.LOG.log(Level.WARNING, "Failed to convert hex string to bytes", e);- return new byte[0];- }- }-- public static String bytesToBinaryString(byte[] bytes) {- // Need to reverse the array to get a sensible string out- ArrayUtils.reverse(bytes);- return BinaryCodec.toAsciiString(bytes);- }-- public static byte[] bytesFromBinaryString(String binary) {- try {- return BinaryCodec.fromAscii(binary.toCharArray());- } catch (Exception e) {- Protocol.LOG.log(Level.WARNING, "Failed to convert hex string to bytes", e);- return new byte[0];- }- }-/*** Will perform standard value processing for outbound values (Linked Attribute -> Protocol); the* containsDynamicPlaceholder flag is required so that the entire write value string is not* searched on every single write request (for performance reasons), instead this should be recorded when the* attribute is first linked.*/- public static Pair<Boolean, Object> doOutboundValueProcessing(String assetId, Attribute<?> attribute, AgentLink<?> agentLink, Object value, boolean containsDynamicPlaceholder) {+ public static Pair<Boolean, Object> doOutboundValueProcessing(String assetId, AttributeInfo attribute, AgentLink<?> agentLink, Object value, boolean containsDynamicPlaceholder) {String writeValue = agentLink.getWriteValue().orElse(null);Pair<Boolean, Object> ignoreAndConvertedValue;- // Check if attribute type is executable- if (ValueType.EXECUTION_STATUS.equals(attribute.getType())) {- AttributeExecuteStatus status = ValueUtil.getValueCoerced(value, AttributeExecuteStatus.class).orElse(null);-- if (status == AttributeExecuteStatus.REQUEST_START && writeValue != null) {- value = writeValue;- return new Pair<>(false, value);- }- }-// value conversionObject finalValue = value;ignoreAndConvertedValue = agentLink.getWriteValueConverter().map(converter -> {- Protocol.LOG.finest("Applying attribute write value converter to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.finest("Applying attribute write value converter to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());return applyValueConverter(finalValue, converter);}).orElse(new Pair<>(false, finalValue));@@ -124,7 +86,7 @@ public final class ProtocolUtil {if (hasWriteValue) {if (containsDynamicPlaceholder) {String valueStr = value == null ? NULL_LITERAL : ValueUtil.convert(value, String.class);- writeValue = writeValue.replaceAll(Protocol.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, valueStr);+ writeValue = writeValue.replaceAll(Constants.DYNAMIC_VALUE_PLACEHOLDER_REGEXP, valueStr);}value = writeValue;@@ -148,17 +110,17 @@ public final class ProtocolUtil {// value filteringagentLink.getValueFilters().ifPresent(valueFilters -> {- Protocol.LOG.finest("Applying attribute value filters to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.finest("Applying attribute value filters to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());Object o = ValueUtil.applyValueFilters(value, valueFilters);if (o == null) {- Protocol.LOG.info("Value filters generated a null value for attribute: assetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.info("Value filters generated a null value for attribute: assetId=" + assetId + ", attribute=" + attribute.getName());}valRef.set(o);});// value conversionignoreAndConvertedValue = agentLink.getValueConverter().map(converter -> {- Protocol.LOG.finest("Applying attribute value converter to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.finest("Applying attribute value converter to attribute: assetId=" + assetId + ", attribute=" + attribute.getName());return applyValueConverter(valRef.get(), converter);}).orElse(new Pair<>(false, valRef.get()));@@ -177,12 +139,12 @@ public final class ProtocolUtil {Class<?> fromType = valRef.get().getClass();if (toType != fromType) {- Protocol.LOG.finest("Applying built in attribute value conversion: " + fromType + " -> " + toType);+ LOG.finest("Applying built in attribute value conversion: " + fromType + " -> " + toType);valRef.set(ValueUtil.getValueCoerced(valRef.get(), toType).orElse(null));if (valRef.get() == null) {- Protocol.LOG.warning("Failed to convert value: " + fromType + " -> " + toType);- Protocol.LOG.warning("Cannot send linked attribute update: assetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.warning("Failed to convert value: " + fromType + " -> " + toType);+ LOG.warning("Cannot send linked attribute update: assetId=" + assetId + ", attribute=" + attribute.getName());return new Pair<>(true, null);}}@@ -197,7 +159,7 @@ public final class ProtocolUtil {return new Pair<>(false, value);}- String converterKey = ValueUtil.getValueCoerced(value, String.class).map(str -> str.toUpperCase(Locale.ROOT)).orElse(NULL_LITERAL.toUpperCase());+ String converterKey = ValueUtil.getValueCoerced(value, String.class).map(str -> str.toUpperCase(Locale.ROOT)).orElse(NULL_LITERAL);return Optional.ofNullable(converter.get(converterKey)).map(converterValue -> {@@ -244,7 +206,7 @@ public final class ProtocolUtil {Object messageFiltered = applyValueFilters(message, matchFilters);if (messageFiltered != null) {if (matchPredicate.asPredicate(currentMillisSupplier).test(messageFiltered)) {- Protocol.LOG.finest("Inbound message meets attribute matching meta so writing state to state consumer for attribute: asssetId=" + assetId + ", attribute=" + attribute.getName());+ LOG.finest("Inbound message meets attribute matching meta so writing state to state consumer for attribute: asssetId=" + assetId + ", attribute=" + attribute.getName());stateConsumer.accept(new AttributeState(assetId, attribute.getName(), message));}}diff --git a/model/src/main/java/org/openremote/model/query/AssetQuery.java b/model/src/main/java/org/openremote/model/query/AssetQuery.javaindex f46eaaad2..53c64251a 100644--- a/model/src/main/java/org/openremote/model/query/AssetQuery.java+++ b/model/src/main/java/org/openremote/model/query/AssetQuery.java@@ -31,7 +31,6 @@ import java.util.stream.Collectors;/*** Encapsulate asset query restriction, projection, and ordering of results.*/-// TODO: Add AssetQuery support for arbitrary attribute property path and value (e.g. consoleProviders.geofence.version == "ORConsole")public class AssetQuery implements Serializable {public static class Select {diff --git a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.javaindex 1e611bd58..74f7f98fa 100644--- a/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java+++ b/model/src/main/java/org/openremote/model/query/filter/AttributePredicate.java@@ -30,7 +30,7 @@ import java.util.Arrays;* presence/absence and/or values; there is an implicit OR between meta predicates (and condition can be achieved by* creating multiple {@link AttributePredicate}s in an {@link LogicGroup.Operator#AND} {@link LogicGroup}. Can also* predicate on the previous value of the {@link Attribute} which is only relevant when applied to {@link- * org.openremote.model.rules.AssetState}.+ * org.openremote.model.attribute.AttributeInfo}.*/public class AttributePredicate extends NameValuePredicate {diff --git a/model/src/main/java/org/openremote/model/rules/AssetState.java b/model/src/main/java/org/openremote/model/rules/AssetState.javadeleted file mode 100644index b7f7dad41..000000000--- a/model/src/main/java/org/openremote/model/rules/AssetState.java+++ /dev/null@@ -1,234 +0,0 @@-/*- * Copyright 2017, OpenRemote Inc.- *- * See the CONTRIBUTORS.txt file in the distribution for a- * full listing of individual contributors.- *- * This program is free software: you can redistribute it and/or modify- * it under the terms of the GNU Affero General Public License as- * published by the Free Software Foundation, either version 3 of the- * License, or (at your option) any later version.- *- * This program is distributed in the hope that it will be useful,- * but WITHOUT ANY WARRANTY; without even the implied warranty of- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the- * GNU Affero General Public License for more details.- *- * You should have received a copy of the GNU Affero General Public License- * along with this program. If not, see <http://www.gnu.org/licenses/>.- */-package org.openremote.model.rules;--import com.fasterxml.jackson.annotation.JsonProperty;-import com.fasterxml.jackson.databind.annotation.JsonSerialize;-import org.openremote.model.asset.Asset;-import org.openremote.model.attribute.Attribute;-import org.openremote.model.attribute.AttributeEvent;-import org.openremote.model.attribute.MetaMap;-import org.openremote.model.util.TsIgnore;-import org.openremote.model.util.ValueUtil;-import org.openremote.model.value.*;--import java.util.Date;-import java.util.Objects;-import java.util.Optional;--/**- * An asset attribute value update, capturing asset state at a point in time.- * <p>- * This class layout is convenient for writing rules. Two asset states- * are equal if they have the same asset ID and attribute name (the same attribute- * reference).- */-@TsIgnore-public class AssetState<T> implements Comparable<AssetState<?>>, NameValueHolder<T>, MetaHolder {-- @JsonProperty(value = "name")- final protected String attributeName;-- @JsonProperty(value = "type")- @JsonSerialize(converter = ValueDescriptor.NameHolderToStringConverter.class)- final protected ValueDescriptor<T> attributeValueType;-- final protected T value;-- final protected long timestamp;-- final protected AttributeEvent.Source source;-- final protected T oldValue;-- final protected long oldValueTimestamp;-- final protected String id;-- final protected String assetName;-- final protected String assetType;-- final protected Date createdOn;-- final protected String[] path;-- final protected String parentId;-- final protected String realm;-- final protected MetaMap meta;-- public AssetState(Asset<?> asset, Attribute<T> attribute, AttributeEvent.Source source) {- this.attributeName = attribute.getName();- this.attributeValueType = attribute.getType();- this.value = attribute.getValue().orElse(null);- this.timestamp = attribute.getTimestamp().orElse(-1L);- this.source = source;- this.oldValue = asset.<T>getAttribute(attributeName).flatMap(Attribute::getValue).orElse(null);- this.oldValueTimestamp = asset.getAttributes().get(attributeName).flatMap(Attribute::getTimestamp).orElse(-1L);- this.id = asset.getId();- this.assetName = asset.getName();- this.assetType = asset.getType();- this.createdOn = asset.getCreatedOn();- this.path = asset.getPath();- this.parentId = asset.getParentId();- this.realm = asset.getRealm();- this.meta = attribute.getMeta();- }-- @Override- public String getName() {- return attributeName;- }-- @Override- public ValueDescriptor<T> getType() {- return attributeValueType;- }-- @Override- public Class<?> getTypeClass() {- return getType() != null ? getType().getType() : Object.class;- }-- public Optional<T> getValue() {- return Optional.ofNullable(value);- }-- @Override- public <U> Optional<U> getValue(Class<U> valueType) {- return ValueUtil.getValueCoerced(value, valueType);- }-- public long getTimestamp() {- return timestamp;- }-- public AttributeEvent.Source getSource() {- return source;- }-- public Optional<Object> getOldValue() {- return Optional.ofNullable(oldValue);- }-- public long getOldValueTimestamp() {- return oldValueTimestamp;- }-- public String getId() {- return id;- }-- public Date getCreatedOn() {- return createdOn;- }-- public String getAssetName() {- return assetName;- }-- public String getAssetType() {- return assetType;- }-- public String[] getPath() {- return path;- }-- public String getParentId() {- return parentId;- }-- public String getRealm() {- return realm;- }-- public MetaMap getMeta() {- return meta;- }-- public <U> Optional<U> getMetaValue(MetaItemDescriptor<U> metaItemDescriptor) {- return getMeta().getValue(metaItemDescriptor);- }-- public boolean hasMeta(MetaItemDescriptor<?> metaItemDescriptor) {- return getMeta().has(metaItemDescriptor);- }-- /**- * Compares entity identifier, attribute name, value, and timestamp.- */- public boolean matches(AttributeEvent event) {- return matches(event, null, false);- }-- /**- * Compares entity identifier, attribute name, value, source, and optional timestamp.- */- public boolean matches(AttributeEvent event, AttributeEvent.Source source, boolean ignoreTimestamp) {- return getId().equals(event.getAssetId())- && getName().equals(event.getAttributeName())- && getValue().equals(event.getValue())- && (ignoreTimestamp || getTimestamp() == event.getTimestamp())- && (source == null || getSource() == source);- }-- @Override- public int compareTo(AssetState<?> that) {- int result = getId().compareTo(that.getId());- if (result == 0)- result = getName().compareTo(that.getName());- if (result == 0)- result = Long.compare(getTimestamp(), that.getTimestamp());- return result;- }-- @Override- public boolean equals(Object o) {- if (this == o) return true;- if (o == null || getClass() != o.getClass()) return false;- AssetState<?> that = (AssetState<?>) o;- return Objects.equals(attributeName, that.attributeName)- && Objects.equals(id, that.id);- }-- @Override- public int hashCode() {- return Objects.hash(attributeName, id);- }-- @Override- public String toString() {- return getClass().getSimpleName() + "{" +- "id='" + getId() + '\'' +- ", name='" + getAssetName() + '\'' +- ", type='" + getAssetType() + '\'' +- ", attributeName='" + getName() + '\'' +- ", attributeValueDescriptor=" + getType() +- ", value=" + getValue() +- ", timestamp=" + getTimestamp() +- ", oldValue=" + getOldValue() +- ", oldValueTimestamp=" + getOldValueTimestamp() +- ", source=" + getSource() +- '}';- }-}diff --git a/model/src/main/java/org/openremote/model/rules/json/JsonRule.java b/model/src/main/java/org/openremote/model/rules/json/JsonRule.javaindex ca19e8c51..3dd31b173 100644--- a/model/src/main/java/org/openremote/model/rules/json/JsonRule.java+++ b/model/src/main/java/org/openremote/model/rules/json/JsonRule.java@@ -28,11 +28,11 @@ import org.openremote.model.query.LogicGroup;* <h2>LHS</li>* <p>* {@link #when} - A series of grouped {@link RuleCondition}s to which bitwise operations can be performed based on the- * {@link LogicGroup#operator} of each group. These are used to filter {@link org.openremote.model.rules.AssetState}s+ * {@link LogicGroup#operator} of each group. These are used to filter {@link org.openremote.model.attribute.AttributeInfo}s* in the RuleEngine that this rule is loaded into to determine which {@link org.openremote.model.asset.Asset}s match;* if one or more assets match then the RHS {@link #then} will be triggered. Once a rule is triggered the- * {@link org.openremote.model.rules.AssetState}s that triggered the rule cannot trigger the rule again until it no longer- * matches (within the context of the {@link RuleCondition} i.e. an {@link org.openremote.model.rules.AssetState} can+ * {@link org.openremote.model.attribute.AttributeInfo}s that triggered the rule cannot trigger the rule again until it no longer+ * matches (within the context of the {@link RuleCondition} i.e. an {@link org.openremote.model.attribute.AttributeInfo} can* re-trigger a rule if it is matched by a different {@link RuleCondition})* <p>* The {@link #otherwise} {@link RuleAction}s are applied to the {@link org.openremote.model.asset.Asset}s filtered bydiff --git a/model/src/main/java/org/openremote/model/rules/json/RuleCondition.java b/model/src/main/java/org/openremote/model/rules/json/RuleCondition.javaindex e53e58190..233b7b019 100644--- a/model/src/main/java/org/openremote/model/rules/json/RuleCondition.java+++ b/model/src/main/java/org/openremote/model/rules/json/RuleCondition.java@@ -28,9 +28,9 @@ import org.openremote.model.rules.SunPositionTrigger;* <li>{@link #duration} - ISO8601 duration expression (e.g. 'PT1H') after which the condition becomes true - 1min precision</li>* <li>{@link #cron} - CRON expression (e.g. "*/5 * * * *") in UTC at which time the condition becomes true - 1min precision</li>* <li>{@link #sun} - {@link SunPositionTrigger} at which time the condition becomes true - 1min precision</li>- * <li>{@link #assets} - {@link AssetQuery} to be applied to the {@link org.openremote.model.rules.AssetState}s+ * <li>{@link #assets} - {@link AssetQuery} to be applied to the {@link org.openremote.model.attribute.AttributeInfo}s* available within the rule engine this rule is loaded into. Evaluates to true when one or more- * {@link org.openremote.model.rules.AssetState}s (referencing unique {@link org.openremote.model.asset.Asset}s) are+ * {@link org.openremote.model.attribute.AttributeInfo}s (referencing unique {@link org.openremote.model.asset.Asset}s) are* returned after applying the query.</li>* </ol>* <p>diff --git a/model/src/main/java/org/openremote/model/util/ValueUtil.java b/model/src/main/java/org/openremote/model/util/ValueUtil.javaindex e6b831826..305883261 100644--- a/model/src/main/java/org/openremote/model/util/ValueUtil.java+++ b/model/src/main/java/org/openremote/model/util/ValueUtil.java@@ -38,6 +38,9 @@ import jakarta.validation.ConstraintViolation;import jakarta.validation.Validation;import jakarta.validation.Validator;import jakarta.validation.constraints.NotNull;+import org.apache.commons.codec.binary.BinaryCodec;+import org.apache.commons.codec.binary.Hex;+import org.apache.commons.lang3.ArrayUtils;import org.hibernate.internal.util.SerializationHelper;import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext;import org.openremote.model.*;@@ -47,6 +50,7 @@ import org.openremote.model.asset.AssetTypeInfo;import org.openremote.model.asset.agent.Agent;import org.openremote.model.asset.agent.AgentDescriptor;import org.openremote.model.asset.agent.AgentLink;+import org.openremote.model.asset.agent.Protocol;import org.openremote.model.asset.impl.UnknownAsset;import org.openremote.model.attribute.Attribute;import org.openremote.model.syslog.SyslogCategory;@@ -951,7 +955,7 @@ public class ValueUtil {* each found constraint is then evaluated against the value supplied to the function.* <p>* Unfortunately JSR-380 can't be used (even Hibernate validator's programmatic API) because validators are type- * centric but here the type (e.g. {@link Attribute} or {@link org.openremote.model.rules.AssetState}) is fixed+ * centric but here the type (e.g. {@link Attribute} or {@link org.openremote.model.attribute.AttributeInfo}) is fixed* but the constraints to be applied are dynamic, would be nice if there was a solution to this problem but this* works for now.*/@@ -1304,4 +1308,32 @@ public class ValueUtil {metaItemDescriptors.toArray(new MetaItemDescriptor<?>[0]),valueDescriptors.toArray(new ValueDescriptor<?>[0]));}++ public static String bytesToHexString(byte[] bytes) {+ return Hex.encodeHexString(bytes).toUpperCase(Locale.ROOT);+ }++ public static byte[] bytesFromHexString(String hex) {+ try {+ return Hex.decodeHex(hex.toCharArray());+ } catch (Exception e) {+ Protocol.LOG.log(Level.WARNING, "Failed to convert hex string to bytes", e);+ return new byte[0];+ }+ }++ public static String bytesToBinaryString(byte[] bytes) {+ // Need to reverse the array to get a sensible string out+ ArrayUtils.reverse(bytes);+ return BinaryCodec.toAsciiString(bytes);+ }++ public static byte[] bytesFromBinaryString(String binary) {+ try {+ return BinaryCodec.fromAscii(binary.toCharArray());+ } catch (Exception e) {+ Protocol.LOG.log(Level.WARNING, "Failed to convert hex string to bytes", e);+ return new byte[0];+ }+ }}diff --git a/model/src/main/java/org/openremote/model/validation/AssetStateStore.java b/model/src/main/java/org/openremote/model/validation/AssetStateStore.javadeleted file mode 100644index 020973127..000000000--- a/model/src/main/java/org/openremote/model/validation/AssetStateStore.java+++ /dev/null@@ -1,27 +0,0 @@-package org.openremote.model.validation;--import org.openremote.model.attribute.Attribute;--/**- * This is just used as a wrapper for validation purposes- */-// TODO: Replace this with AssetState-@AssetStateValid-public class AssetStateStore {-- protected String assetType;- protected Attribute<?> attribute;-- public AssetStateStore(String assetType, Attribute<?> attribute) {- this.assetType = assetType;- this.attribute = attribute;- }-- public String getAssetType() {- return assetType;- }-- public Attribute<?> getAttribute() {- return attribute;- }-}diff --git a/model/src/main/java/org/openremote/model/validation/AssetValidator.java b/model/src/main/java/org/openremote/model/validation/AssetValidator.javaindex 0d73f8f0a..1e655c495 100644--- a/model/src/main/java/org/openremote/model/validation/AssetValidator.java+++ b/model/src/main/java/org/openremote/model/validation/AssetValidator.java@@ -12,6 +12,8 @@ import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.TsIgnore;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.AttributeDescriptor;+import org.openremote.model.value.MetaHolder;+import org.openremote.model.value.NameValueHolder;import java.time.Instant;import java.util.Map;@@ -72,7 +74,7 @@ public class AssetValidator implements HibernateConstraintValidator<AssetValid,// Check attribute types match descriptors and value constraints are metasset.getAttributes().values().forEach(attribute -> {- if (!validateAttribute(assetModelInfo, attribute, context, clockProvider.getClock().instant())) {+ if (!validateNameValueMetaHolder(assetModelInfo, attribute, context, clockProvider.getClock().instant())) {valid.set(false);}});@@ -81,7 +83,7 @@ public class AssetValidator implements HibernateConstraintValidator<AssetValid,}@SuppressWarnings("OptionalGetWithoutIsPresent")- public static boolean validateAttribute(AssetTypeInfo assetModelInfo, Attribute<?> attribute, ConstraintValidatorContext context, Instant now) {+ public static <U, T extends NameValueHolder<U> & MetaHolder> boolean validateNameValueMetaHolder(AssetTypeInfo assetModelInfo, T attribute, ConstraintValidatorContext context, Instant now) {boolean valid = true;AttributeDescriptor<?> descriptor = assetModelInfo == null ? null : assetModelInfo.getAttributeDescriptors().get(attribute.getName());diff --git a/model/src/main/java/org/openremote/model/validation/AssetStateValid.java b/model/src/main/java/org/openremote/model/validation/AttributeInfoValid.javasimilarity index 90%rename from model/src/main/java/org/openremote/model/validation/AssetStateValid.javarename to model/src/main/java/org/openremote/model/validation/AttributeInfoValid.javaindex 042f963be..8e46d3eca 100644--- a/model/src/main/java/org/openremote/model/validation/AssetStateValid.java+++ b/model/src/main/java/org/openremote/model/validation/AttributeInfoValid.java@@ -33,11 +33,11 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;@Target({ FIELD, METHOD, PARAMETER, ANNOTATION_TYPE, TYPE_USE })@Retention(RUNTIME)-@Repeatable(AssetStateValid.List.class)-@Constraint(validatedBy = AssetStateValidator.class)+@Repeatable(AttributeInfoValid.List.class)+@Constraint(validatedBy = AttributeInfoValidator.class)@Documented@TsIgnore-public @interface AssetStateValid {+public @interface AttributeInfoValid {String MESSAGE_TEMPLATE = "{Asset.invalid.message}";@@ -52,6 +52,6 @@ public @interface AssetStateValid {@Documented@interface List {- AssetStateValid[] value();+ AttributeInfoValid[] value();}}diff --git a/model/src/main/java/org/openremote/model/validation/AssetStateValidator.java b/model/src/main/java/org/openremote/model/validation/AttributeInfoValidator.javasimilarity index 63%rename from model/src/main/java/org/openremote/model/validation/AssetStateValidator.javarename to model/src/main/java/org/openremote/model/validation/AttributeInfoValidator.javaindex 4cd7ef90c..590490212 100644--- a/model/src/main/java/org/openremote/model/validation/AssetStateValidator.java+++ b/model/src/main/java/org/openremote/model/validation/AttributeInfoValidator.java@@ -7,17 +7,12 @@ import org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorimport org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorInitializationContext;import org.openremote.model.asset.Asset;import org.openremote.model.asset.AssetTypeInfo;-import org.openremote.model.attribute.Attribute;+import org.openremote.model.attribute.AttributeInfo;import org.openremote.model.syslog.SyslogCategory;import org.openremote.model.util.TsIgnore;import org.openremote.model.util.ValueUtil;import org.openremote.model.value.AttributeDescriptor;-import java.time.Instant;-import java.util.Arrays;-import java.util.Map;-import java.util.Objects;-/*** A JSR-380 validator that uses {@link ValueUtil} to ensure that the* {@link Asset#getAttributes} conforms to the {@link AttributeDescriptor}s for the@@ -29,22 +24,22 @@ import java.util.Objects;* </ul>*/@TsIgnore-public class AssetStateValidator implements HibernateConstraintValidator<AssetStateValid, AssetStateStore> {+public class AttributeInfoValidator implements HibernateConstraintValidator<AttributeInfoValid, AttributeInfo> {- public static final System.Logger LOG = System.getLogger(AssetStateValidator.class.getName() + "." + SyslogCategory.MODEL_AND_VALUES.name());+ public static final System.Logger LOG = System.getLogger(AttributeInfoValidator.class.getName() + "." + SyslogCategory.MODEL_AND_VALUES.name());protected ClockProvider clockProvider;@Override- public void initialize(ConstraintDescriptor<AssetStateValid> constraintDescriptor, HibernateConstraintValidatorInitializationContext initializationContext) {+ public void initialize(ConstraintDescriptor<AttributeInfoValid> constraintDescriptor, HibernateConstraintValidatorInitializationContext initializationContext) {clockProvider = initializationContext.getClockProvider();HibernateConstraintValidator.super.initialize(constraintDescriptor, initializationContext);}@Override- public boolean isValid(AssetStateStore assetState, ConstraintValidatorContext context) {+ public boolean isValid(AttributeInfo attributeInfo, ConstraintValidatorContext context) {- String type = assetState.getAssetType();+ String type = attributeInfo.getAssetType();AssetTypeInfo assetModelInfo = ValueUtil.getAssetInfo(type).orElse(null);- return AssetValidator.validateAttribute(assetModelInfo, assetState.getAttribute(), context, clockProvider.getClock().instant());+ return AssetValidator.validateNameValueMetaHolder(assetModelInfo, attributeInfo, context, clockProvider.getClock().instant());}}diff --git a/model/src/main/java/org/openremote/model/value/AbstractNameValueHolder.java b/model/src/main/java/org/openremote/model/value/AbstractNameValueHolder.javaindex a784c4f74..21511481f 100644--- a/model/src/main/java/org/openremote/model/value/AbstractNameValueHolder.java+++ b/model/src/main/java/org/openremote/model/value/AbstractNameValueHolder.java@@ -80,12 +80,6 @@ public abstract class AbstractNameValueHolder<T> implements NameValueHolder<T>,@SuppressWarnings("unchecked")@Overridepublic <U> Optional<U> getValue(@Nonnull Class<U> valueType) {- if (value == null) {- return Optional.empty();- }- if (valueType.isAssignableFrom(value.getClass())) {- return Optional.of((U)value);- }return ValueUtil.getValueCoerced(value, valueType);}diff --git a/model/src/main/java/org/openremote/model/value/MetaHolder.java b/model/src/main/java/org/openremote/model/value/MetaHolder.javaindex 73f5c5b7b..a741cb9d1 100644--- a/model/src/main/java/org/openremote/model/value/MetaHolder.java+++ b/model/src/main/java/org/openremote/model/value/MetaHolder.java@@ -20,10 +20,29 @@package org.openremote.model.value;import com.fasterxml.jackson.annotation.JsonProperty;+import org.openremote.model.attribute.MetaItem;import org.openremote.model.attribute.MetaMap;+import java.util.Optional;+public interface MetaHolder {@JsonPropertyMetaMap getMeta();++ default <U> Optional<U> getMetaValue(MetaItemDescriptor<U> metaItemDescriptor) {+ return getMeta() != null ? getMeta().getValue(metaItemDescriptor) : Optional.empty();+ }++ default boolean hasMeta(MetaItemDescriptor<?> metaItemDescriptor) {+ return getMeta() != null && getMeta().has(metaItemDescriptor);+ }++ default boolean hasMeta(String metaItemName) {+ return getMeta() != null && getMeta().has(metaItemName);+ }++ default <U> Optional<MetaItem<U>> getMetaItem(MetaItemDescriptor<U> metaItemDescriptor) {+ return getMeta() != null ? getMeta().get(metaItemDescriptor) : Optional.empty();+ }}diff --git a/model/src/main/java/org/openremote/model/value/MetaItemType.java b/model/src/main/java/org/openremote/model/value/MetaItemType.javaindex b0666cde2..de8e43379 100644--- a/model/src/main/java/org/openremote/model/value/MetaItemType.java+++ b/model/src/main/java/org/openremote/model/value/MetaItemType.java@@ -19,15 +19,14 @@*/package org.openremote.model.value;+import jakarta.validation.constraints.Pattern;import org.openremote.model.Constants;import org.openremote.model.asset.UserAssetLink;import org.openremote.model.asset.agent.AgentLink;import org.openremote.model.attribute.Attribute;import org.openremote.model.attribute.AttributeLink;-import org.openremote.model.rules.AssetState;import org.openremote.model.util.TsIgnore;-import jakarta.validation.constraints.Pattern;import java.util.Date;@TsIgnorediff --git a/model/src/main/java/org/openremote/model/value/impl/ColourRGB.java b/model/src/main/java/org/openremote/model/value/impl/ColourRGB.javaindex 9f73ae060..135b59598 100644--- a/model/src/main/java/org/openremote/model/value/impl/ColourRGB.java+++ b/model/src/main/java/org/openremote/model/value/impl/ColourRGB.java@@ -23,8 +23,8 @@ import com.fasterxml.jackson.annotation.JsonValue;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.annotation.JsonDeserialize;import com.fasterxml.jackson.databind.util.StdConverter;-import org.openremote.model.protocol.ProtocolUtil;import org.openremote.model.util.TextUtil;+import org.openremote.model.util.ValueUtil;import java.awt.*;import java.io.Serializable;@@ -100,7 +100,7 @@ public class ColourRGB implements Serializable {@JsonValue@Overridepublic String toString() {- return "#" + ProtocolUtil.bytesToHexString(new byte[] {(byte)r, (byte)g, (byte)b});+ return "#" + ValueUtil.bytesToHexString(new byte[] {(byte)r, (byte)g, (byte)b});}public static ColourRGB fromHexString(String hexString) {@@ -110,7 +110,7 @@ public class ColourRGB implements Serializable {if (hexString.charAt(0) == '#') {hexString = hexString.substring(1);}- byte[] values = ProtocolUtil.bytesFromHexString(hexString);+ byte[] values = ValueUtil.bytesFromHexString(hexString);if (values.length == 3) {return new ColourRGB(Byte.toUnsignedInt(values[0]),diff --git a/profile/deploy.yml b/profile/deploy.ymlindex 8c25f21b1..2da8a9382 100644--- a/profile/deploy.yml+++ b/profile/deploy.yml@@ -293,9 +293,12 @@ services:# Web server port used to host metrics endpointOR_METRICS_PORT: ${OR_METRICS_PORT:-8404}+ # Set the number of concurrent attribute event processors (defaults to number of CPU cores)+ OR_ATTRIBUTE_EVENT_THREADS:+# Configure the default max age for data points stored in the database (individual attributes can use a custom# value by using the DATA_POINTS_MAX_AGE_DAYS AssetMeta item).- # OR_DATA_POINTS_MAX_AGE_DAYS = 30+ # OR_DATA_POINTS_MAX_AGE_DAYS: 30# App id for the API of OpenWeather: https://openweathermap.org# OR_OPEN_WEATHER_API_APP_ID@@ -312,13 +315,8 @@ services:# THE FOLLOWING OPTIONS CAN BE USED TO ENABLE JMX; PORT MUST ALSO BE EXPOSED# ACCESS SHOULD BE RESTRICTED BY FIREWALL TO SPECIFIC IPs FOR DIAGNOSTIC PURPOSES#- # -Dcom.sun.management.jmxremote=true- # -Dcom.sun.management.jmxremote.ssl=false- # -Dcom.sun.management.jmxremote.authenticate=false- # -Dcom.sun.management.jmxremote.port=8085- # -Dcom.sun.management.jmxremote.rmi.port=8085- # -Djava.rmi.server.hostname=${OR_HOSTNAME:-localhost}-+ #JAVA_TOOL_OPTIONS: -Dcom.sun.management.jmxremote=true -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.port=8085 -Dcom.sun.management.jmxremote.rmi.port=8085 -Djava.rmi.server.hostname=${OR_HOSTNAME:-localhost}+ ## Custom JVM tool options (for remote debugging)#JAVA_TOOL_OPTIONS: '-agentlib:jdwp=transport=dt_socket,address=0.0.0.0:8000,server=y,suspend=n'diff --git a/project.gradle b/project.gradleindex 5f41c4373..012917422 100644--- a/project.gradle+++ b/project.gradle@@ -2,6 +2,9 @@import java.nio.file.*import java.nio.file.attribute.*import java.util.Set+import java.util.stream.Collectors+import java.util.stream.StreamSupport+import static org.apache.tools.ant.taskdefs.condition.Os.FAMILY_WINDOWSimport static org.apache.tools.ant.taskdefs.condition.Os.isFamilyimport static org.jetbrains.gradle.ext.ShortenCommandLine.MANIFEST@@ -255,6 +258,121 @@ def getDeploymentJars(Project project = project) {}}+/**+ * This defines reusable config for the typescript generator plugin+ */+def createTSGeneratorConfigForModel(String outputFileName, Project...customProjectsToScan) {+ def config = createTSGeneratorConfig(false, outputFileName, customProjectsToScan) <<+ {+ extensions = [+ "org.openremote.model.util.AssetModelInfoExtension",+ "CustomExtension",+ "JsonSerializeExtension"+ ]+ customTypeMappings = [+ "com.fasterxml.jackson.databind.node.ObjectNode:{ [id: string]: any }",+ "java.lang.Class<T>:string",+ "org.openremote.model.attribute.MetaItem<T>:any"+ ]+ customTypeProcessor = "CustomTypeProcessor"+ generateInfoJson = true+ }+ return config+}+def createTSGeneratorConfigForClient(String outputFileName, Project...customProjectsToScan) {+ def config = createTSGeneratorConfig(true, outputFileName, customProjectsToScan) <<+ {+ extensions = [+ "CustomExtension",+ "JsonSerializeExtension",+ "AggregatedApiClient",+ "cz.habarta.typescript.generator.ext.AxiosClientExtension"+ ]+ customTypeMappings = [+ "com.fasterxml.jackson.databind.node.ObjectNode:{ [id: string]: any }",+ "java.lang.Class<T>:string",+ "org.openremote.model.attribute.MetaItem<T>:any",+ "org.openremote.model.asset.Asset<T>:Model.Asset",+ "org.openremote.model.asset.AssetDescriptor<T>:Model.AssetDescriptor",+ "org.openremote.model.asset.agent.Agent<T,U,V>:Model.Agent",+ "org.openremote.model.asset.agent.AgentDescriptor<T,U,V>:Model.AgentDescriptor",+ "org.openremote.model.value.MetaItemDescriptor<T>:Model.MetaItemDescriptor",+ "org.openremote.model.value.ValueDescriptor<T>:Model.ValueDescriptor"+ ]+ moduleDependencies = [+ cz.habarta.typescript.generator.ModuleDependency.module(+ "@openremote/model",+ "Model",+ new File("$buildDir/typescript-generator-info.json"),+ (String) null,+ (String) null+ )+ ]+ restNamespacing = "perResource"+ }+ return config+}+def createTSGeneratorConfig(boolean outputAPIClient, String outputFileName, Project...customProjectsToScan) {++ def classPatternGlobs = Arrays.stream(customProjectsToScan).flatMap { project ->+ return project.sourceSets.findByName('main').java.srcDirs.stream().map {+ def srcPath = it+ def isPackageDir = true+ while (srcPath != null && isPackageDir) {+ def files = srcPath.listFiles()+ isPackageDir = files != null && files.length == 1 && files[0].isDirectory()+ if (isPackageDir) {+ srcPath = files[0]+ }+ }+ java.nio.file.Path packagePath = it.toPath().relativize(srcPath.toPath())+ return StreamSupport+ .stream(packagePath.spliterator(), false)+ .map(java.nio.file.Path::toString)+ .collect(Collectors.joining(".")) + (outputAPIClient ? ".**Resource" : ".**")+ }+ }.toList()++ return {+ jsonLibrary = "jackson2"+ classPatterns = [+ (outputAPIClient ? "org.openremote.model.**Resource" : "org.openremote.model.**")+ ] + classPatternGlobs+ customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf(\"\$\") > 0) return name.substr(name.lastIndexOf(\".\")+1).replace(\"\$\",\"\"); }"+ excludeClassPatterns = [+ "org.openremote.model.event.shared.*Filter**",+ "org.openremote.model.util.**",+ "org.openremote.model.flow.**",+ "java.io.**",+ "java.lang.**",+ "org.hibernate.**",+ "jakarta.**"+ ]+ mapEnum = cz.habarta.typescript.generator.EnumMapping.asEnum+ mapDate = cz.habarta.typescript.generator.DateMapping.asNumber+ optionalProperties = "all" // TODO: cleanup model to be more explicit about optional params+ outputFileType = "implementationFile"+ outputKind = "module"+ outputFile = outputFileName+ jackson2Configuration = [+ fieldVisibility: "ANY",+ creatorVisibility: "ANY",+ getterVisibility: "NONE",+ isGetterVisibility: "NONE",+ setterVisibility: "NONE"+ ]+ jackson2Modules = [+ "com.fasterxml.jackson.datatype.jdk8.Jdk8Module",+ "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule",+ "com.fasterxml.jackson.module.paramnames.ParameterNamesModule"+ ]+ }+}++def resolveDependency(String path) {+ "io.openremote:openremote-" + path.substring(1) + ":" + version+}+ext {resolvePath = this.&resolvePathresolveProject = this.&resolveProject@@ -262,6 +380,9 @@ ext {isCustomProject = this.&isCustomProjectgetYarnInstallTask = this.&getYarnInstallTaskgetDeploymentJars = this.&getDeploymentJars+ resolveDependency = this.&resolveDependency+ createTSGeneratorConfigForClient = this.&createTSGeneratorConfigForClient+ createTSGeneratorConfigForModel = this.&createTSGeneratorConfigForModel}// Add UI tasksdiff --git a/settings.gradle b/settings.gradleindex 5fe99d718..0b8768826 100644--- a/settings.gradle+++ b/settings.gradle@@ -2,12 +2,14 @@ pluginManagement {plugins {id "com.cherryperry.gradle-file-encrypt" version "2.0.3"id "org.jetbrains.gradle.plugin.idea-ext" version "1.1.3"+ id 'cz.habarta.typescript-generator' version "$typescriptGeneratorVersion"}}plugins {id "com.cherryperry.gradle-file-encrypt" apply falseid "org.jetbrains.gradle.plugin.idea-ext" apply false+ id "cz.habarta.typescript-generator" apply false}rootProject.name = "openremote"diff --git a/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java b/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.javaindex 48a78d264..3c052caaa 100644--- a/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java+++ b/setup/src/integration/java/org/openremote/setup/integration/protocol/MockProtocol.java@@ -83,11 +83,11 @@ public class MockProtocol extends AbstractProtocol<MockAgent, MockAgentLink> imp}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, MockAgentLink agentLink, AttributeEvent event, Object processedValue) {- protocolMethodCalls.add("WRITE_ATTRIBUTE:" + event.getAssetId() + ":" + attribute.getName());+ protected void doLinkedAttributeWrite(MockAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protocolMethodCalls.add("WRITE_ATTRIBUTE:" + event.getId() + ":" + event.getName());protocolWriteAttributeEvents.add(event);if (updateSensor) {- updateReceived(event.getAttributeState());+ updateReceived(event.getState());}}diff --git a/setup/src/integration/java/org/openremote/setup/integration/protocol/http/TestHTTPServerProtocol.java b/setup/src/integration/java/org/openremote/setup/integration/protocol/http/TestHTTPServerProtocol.javaindex 9b8078be8..9f6e1413d 100644--- a/setup/src/integration/java/org/openremote/setup/integration/protocol/http/TestHTTPServerProtocol.java+++ b/setup/src/integration/java/org/openremote/setup/integration/protocol/http/TestHTTPServerProtocol.java@@ -53,7 +53,7 @@ public class TestHTTPServerProtocol extends AbstractHTTPServerProtocol<TestHTTPS}@Override- protected void doLinkedAttributeWrite(Attribute<?> attribute, DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {+ protected void doLinkedAttributeWrite(DefaultAgentLink agentLink, AttributeEvent event, Object processedValue) {}diff --git a/setup/src/load1/resources/org/openremote/setup/load1/WeatherAssetCalculations.groovy b/setup/src/load1/resources/org/openremote/setup/load1/WeatherAssetCalculations.groovyindex f1a955259..309f3bda1 100644--- a/setup/src/load1/resources/org/openremote/setup/load1/WeatherAssetCalculations.groovy+++ b/setup/src/load1/resources/org/openremote/setup/load1/WeatherAssetCalculations.groovy@@ -4,7 +4,6 @@ import org.openremote.manager.rules.RulesBuilderimport org.openremote.model.asset.impl.WeatherAssetimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.query.AssetQuery-import org.openremote.model.rules.AssetStateimport org.openremote.model.rules.Assetsimport org.openremote.model.rules.Notificationsimport org.openremote.model.rules.Users@@ -34,7 +33,7 @@ rules.add()new AssetQuery().types(WeatherAsset).attributeNames(WeatherAsset.TEMPERATURE.name, WeatherAsset.RAINFALL.name)- ).collect(Collectors.groupingBy{AssetState<?> state -> state.id})+ ).collect(Collectors.groupingBy{state -> state.id}).forEach{id, states ->// See if either attribute has changed for this assetif (states.any {state -> !Objects.equals(state.value.orElse(null), state.oldValue.orElse(null))}) {diff --git a/setup/src/load1/resources/org/openremote/setup/load1/ca.srl b/setup/src/load1/resources/org/openremote/setup/load1/ca.srlindex bd7e3e0e8..11a6f4de4 100644--- a/setup/src/load1/resources/org/openremote/setup/load1/ca.srl+++ b/setup/src/load1/resources/org/openremote/setup/load1/ca.srl@@ -1 +1 @@-542AE76EF6F52D185F1EF1332339AD042EDA3588+542AE76EF6F52D185F1EF1332339AD042EDA3664diff --git a/test/load1/load1.jmx b/test/load1/load1.jmxindex 81c86033b..fa508ad4e 100644--- a/test/load1/load1.jmx+++ b/test/load1/load1.jmx@@ -12,19 +12,19 @@</TestPlan><hashTree><ThreadGroup guiclass="ThreadGroupGui" testclass="ThreadGroup" testname="mqtt-client-auto-provision-pub" enabled="true">- <stringProp name="ThreadGroup.on_sample_error">continue</stringProp>+ <stringProp name="ThreadGroup.on_sample_error">stoptestnow</stringProp><elementProp name="ThreadGroup.main_controller" elementType="LoopController" guiclass="LoopControlPanel" testclass="LoopController" enabled="true"><boolProp name="LoopController.continue_forever">false</boolProp><stringProp name="LoopController.loops">1</stringProp></elementProp>- <stringProp name="ThreadGroup.num_threads">1</stringProp>- <stringProp name="ThreadGroup.ramp_time">1</stringProp>+ <stringProp name="ThreadGroup.num_threads">100</stringProp>+ <stringProp name="ThreadGroup.ramp_time">10</stringProp><stringProp name="ThreadGroup.start_time"></stringProp><stringProp name="ThreadGroup.end_time"></stringProp><boolProp name="ThreadGroup.scheduler">false</boolProp><stringProp name="ThreadGroup.duration">0</stringProp><stringProp name="ThreadGroup.delay"></stringProp>- <boolProp name="ThreadGroup.same_user_on_next_iteration">true</boolProp>+ <boolProp name="ThreadGroup.same_user_on_next_iteration">false</boolProp></ThreadGroup><hashTree><CacheManager guiclass="CacheManagerGui" testclass="CacheManager" testname="Cache" enabled="true">@@ -62,7 +62,7 @@</ConfigTestElement><hashTree/><CSVDataSet guiclass="TestBeanGUI" testclass="CSVDataSet" testname="CSV devices.csv" enabled="true">- <stringProp name="filename">C:\Users\Richard\code\openremote\test\load1\tmp\devices.csv</stringProp>+ <stringProp name="filename">.\tmp\devices.csv</stringProp><stringProp name="delimiter">,</stringProp><boolProp name="quotedData">false</boolProp><boolProp name="recycle">false</boolProp>@@ -89,7 +89,7 @@<stringProp name="mqtt.conn_attampt_max">0</stringProp><stringProp name="mqtt.reconn_attampt_max">0</stringProp><stringProp name="mqtt.conn_clean_session">true</stringProp>- <stringProp name="mqtt.user_name"></stringProp>+ <stringProp name="mqtt.user_name">${id}</stringProp><stringProp name="mqtt.password"></stringProp><stringProp name="mqtt.connect_name">mqttconn</stringProp><stringProp name="mqtt.conn_attempt_max">0</stringProp>@@ -105,7 +105,7 @@<stringProp name="mqtt.sample_condition">number of received messages</stringProp><stringProp name="mqtt.sample_condition_value">1</stringProp><stringProp name="mqtt.connect_name">mqttconn</stringProp>- <stringProp name="mqtt.sample_condition_value_opt">10000</stringProp>+ <stringProp name="mqtt.sample_condition_value_opt">30000</stringProp></net.xmeter.samplers.SubSampler><hashTree/><net.xmeter.samplers.PubSampler guiclass="net.xmeter.gui.PubSamplerUI" testclass="net.xmeter.samplers.PubSampler" testname="Publish provisioning" enabled="true">@@ -145,7 +145,7 @@<hashTree/><LoopController guiclass="LoopControlPanel" testclass="LoopController" testname="Loop Controller" enabled="true"><boolProp name="LoopController.continue_forever">true</boolProp>- <intProp name="LoopController.loops">-1</intProp>+ <stringProp name="LoopController.loops">100</stringProp></LoopController><hashTree><net.xmeter.samplers.PubSampler guiclass="net.xmeter.gui.PubSamplerUI" testclass="net.xmeter.samplers.PubSampler" testname="Publish rainfall" enabled="true">@@ -169,8 +169,13 @@<boolProp name="mqtt.retained_message">false</boolProp><stringProp name="mqtt.connect_name">mqttconn</stringProp></net.xmeter.samplers.PubSampler>- <hashTree/>- <net.xmeter.samplers.AsyncResponseSampler guiclass="net.xmeter.gui.AsyncResponseSamplerUI" testclass="net.xmeter.samplers.AsyncResponseSampler" testname="Check attribute response" enabled="true"/>+ <hashTree>+ <ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="Constant Timer" enabled="true">+ <stringProp name="ConstantTimer.delay">100</stringProp>+ </ConstantTimer>+ <hashTree/>+ </hashTree>+ <net.xmeter.samplers.AsyncResponseSampler guiclass="net.xmeter.gui.AsyncResponseSamplerUI" testclass="net.xmeter.samplers.AsyncResponseSampler" testname="Check attribute response" enabled="false"/><hashTree><ConstantTimer guiclass="ConstantTimerGui" testclass="ConstantTimer" testname="Constant Timer" enabled="false"><stringProp name="ConstantTimer.delay">50</stringProp>@@ -191,7 +196,7 @@</net.xmeter.samplers.DisConnectSampler><hashTree/><ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="true">- <boolProp name="ResultCollector.error_logging">false</boolProp>+ <boolProp name="ResultCollector.error_logging">true</boolProp><objProp><name>saveConfig</name><value class="SampleSaveConfiguration">diff --git a/test/src/logging-test.properties b/test/src/logging-test.propertiesindex 2e8dbd251..e4c0eb674 100644--- a/test/src/logging-test.properties+++ b/test/src/logging-test.properties@@ -6,7 +6,7 @@ handlers=java.util.logging.ConsoleHandler, \# Console handler###############################################################################-java.util.logging.ConsoleHandler.level=FINE+java.util.logging.ConsoleHandler.level=FINEST# Console format, you can colorize this in IntelliJ IDEA "Run Application " console# with the GrepConsole plugin and after copying the configuration file of yourdiff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovyindex 412b45611..a26daecc0 100644--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy@@ -7,7 +7,6 @@ import org.openremote.manager.datapoint.AssetDatapointServiceimport org.openremote.manager.datapoint.AssetPredictedDatapointServiceimport org.openremote.manager.setup.SetupServiceimport org.openremote.model.attribute.AttributeRef-import org.openremote.model.datapoint.AssetDatapointResourceimport org.openremote.model.datapoint.AssetPredictedDatapointResourceimport org.openremote.model.datapoint.ValueDatapointimport org.openremote.model.datapoint.query.AssetDatapointIntervalQuery@@ -58,7 +57,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait"testuser1","testuser1").token- def datapointResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetDatapointResource.class)+ // Resteasy client has issues with @Suspended annotation so not used for now+ //def datapointResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetDatapointResource.class)def predictedDatapointResource = getClientApiTarget(serverUri(serverPort), MASTER_REALM, accessToken).proxy(AssetPredictedDatapointResource.class)then: "the simulator protocol instance should have been initialised and attributes linked"@@ -128,31 +128,25 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitexpect: "the datapoints to be stored"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List- assert datapoints.size() == 5+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))+ // Can include initial value when container is re-used between tests+ assert datapoints.size() >= 5// Note that the "No value" sensor update should not have created a datapoint, the first// datapoint is the last sensor update with an actual value- assert ValueUtil.getValue(datapoints.get(0).value, Double.class).orElse(null) == 15.5d- assert datapoints.get(0).timestamp == datapoint3ExpectedTimestamp-- assert ValueUtil.getValue(datapoints.get(1).value, Double.class).orElse(null) == 14.4d- assert datapoints.get(1).timestamp == datapoint2ExpectedTimestamp-- assert ValueUtil.getValue(datapoints.get(2).value, Double.class).orElse(null) == 13.5d- assert datapoints.get(2).timestamp == datapoint1ExpectedTimestamp-- assert ValueUtil.getValue(datapoints.get(3).value, Double.class).orElse(null) == 13.3d-- assert ValueUtil.getValue(datapoints.get(4).value, Double.class).orElse(null) == 13.3d+ assert datapoints.any{ it.value == 15.5d && it.timestamp == datapoint3ExpectedTimestamp}+ assert datapoints.any{ it.value == 14.4d && it.timestamp == datapoint2ExpectedTimestamp}+ assert datapoints.any{ it.value == 13.5d && it.timestamp == datapoint1ExpectedTimestamp}+ assert datapoints.count{ it.value == 13.3d} == 2}and: "the aggregated datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = datapointResource.getDatapoints(- null,+ //def aggregatedDatapoints = datapointResource.getDatapoints(+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(thing.getId(),"light1PowerConsumption",new AssetDatapointIntervalQuery(@@ -176,8 +170,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitand: "when the step size is set on the datapoint retrieval then the datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = datapointResource.getDatapoints(- null,+ //def aggregatedDatapoints = datapointResource.getDatapoints(+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(thing.getId(),"light1PowerConsumption",new AssetDatapointIntervalQuery(@@ -233,24 +227,23 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitexpect: "the datapoints to be stored"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List- assert datapoints.size() == 3+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))+ assert datapoints.size() >= 3assert !ValueUtil.getBoolean(datapoints.get(0).value).orElse(null)assert datapoints.get(0).timestamp == datapoint3ExpectedTimestamp- assert ValueUtil.getBoolean(datapoints.get(1).value).orElse(null)- assert datapoints.get(1).timestamp == datapoint2ExpectedTimestamp-- assert !ValueUtil.getBoolean(datapoints.get(2).value).orElse(null)- assert datapoints.get(2).timestamp == datapoint1ExpectedTimestamp+ assert datapoints.any {it.timestamp == datapoint3ExpectedTimestamp && !(it.value as Boolean)}+ assert datapoints.any {it.timestamp == datapoint2ExpectedTimestamp && (it.value as Boolean)}+ assert datapoints.any {it.timestamp == datapoint3ExpectedTimestamp && !(it.value as Boolean)}}and: "the aggregated datapoints should match"conditions.eventually {def thing = assetStorageService.find(managerTestSetup.thingId, true)- def aggregatedDatapoints = datapointResource.getDatapoints(- null,+ //def aggregatedDatapoints = datapointResource.getDatapoints(+ def aggregatedDatapoints = assetDatapointService.queryDatapoints(thing.getId(),thingLightToggleAttributeName,new AssetDatapointIntervalQuery(@@ -283,16 +276,16 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitthen: "the datapoints should be stored"conditions.eventually {- def powerDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List- def toggleDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ //def powerDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ //def toggleDatapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def powerDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))+ def toggleDatapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))- assert powerDatapoints.size() == 6- assert ValueUtil.getValue(powerDatapoints.get(0).value, Double.class).orElse(null) == 17.5d- assert powerDatapoints.get(0).timestamp == datapoint4ExpectedTimestamp+ assert powerDatapoints.size() >= 6+ assert powerDatapoints.any {it.timestamp == datapoint4ExpectedTimestamp && it.value == 17.5 }- assert toggleDatapoints.size() == 4- assert ValueUtil.getBoolean(toggleDatapoints.get(0).value).orElse(false)- assert toggleDatapoints.get(0).timestamp == datapoint4ExpectedTimestamp+ assert toggleDatapoints.size() >= 4+ assert toggleDatapoints.any {it.timestamp == datapoint4ExpectedTimestamp && it.value}}when: "the clock advances to the next days purge routine execution time"@@ -303,7 +296,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitthen: "data points older than purge days should be purged for the power sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))assert datapoints.size() == 1assert ValueUtil.getValue(datapoints.get(0).value, Double.class).orElse(null) == 17.5dassert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp@@ -311,10 +305,11 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitand: "no data points should have been purged for the toggle sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List- assert datapoints.size() == 4+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))+ assert datapoints.size() >= 4assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)- assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp+ assert datapoints.any {it.timestamp == datapoint4ExpectedTimestamp && it.value}}when: "the clock advances 3 times the purge duration"@@ -325,16 +320,17 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitthen: "all data points should be purged for power sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, "light1PowerConsumption", null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))assert datapoints.isEmpty()}and: "no data points should have been purged for the toggle sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List- assert datapoints.size() == 4- assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)- assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))+ assert datapoints.size() >= 4+ assert datapoints.any {it.timestamp == datapoint4ExpectedTimestamp && it.value}}when: "the clock advances 3 times the purge duration"@@ -345,7 +341,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitthen: "data points older than 7 days should have been purged for the toggle sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))assert datapoints.size() == 1assert ValueUtil.getBoolean(datapoints.get(0).value).orElse(false)assert datapoints.get(0).timestamp == datapoint4ExpectedTimestamp@@ -359,7 +356,8 @@ class AssetDatapointTest extends Specification implements ManagerContainerTraitthen: "all data points should have been purged for the toggle sensor"conditions.eventually {- def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ //def datapoints = datapointResource.getDatapoints(null, managerTestSetup.thingId, thingLightToggleAttributeName, null) as List+ def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))assert datapoints.isEmpty()}diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovydeleted file mode 100644index 96969650b..000000000--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy+++ /dev/null@@ -1,234 +0,0 @@-package org.openremote.test.assets--import org.openremote.manager.agent.AgentService-import org.openremote.manager.asset.*-import org.openremote.manager.datapoint.AssetDatapointService-import org.openremote.manager.rules.RulesService-import org.openremote.manager.setup.SetupService-import org.openremote.model.asset.agent.DefaultAgentLink-import org.openremote.setup.integration.protocol.MockAgentLink-import org.openremote.setup.integration.KeycloakTestSetup-import org.openremote.model.asset.Asset-import org.openremote.model.asset.agent.ConnectionStatus-import org.openremote.model.asset.impl.ThingAsset-import org.openremote.model.attribute.*-import org.openremote.test.ManagerContainerTrait-import org.openremote.setup.integration.protocol.MockAgent-import org.openremote.setup.integration.protocol.MockProtocol-import spock.lang.Specification-import spock.util.concurrent.PollingConditions--import jakarta.persistence.EntityManager--import static org.openremote.model.value.ValueType.*-import static org.openremote.model.value.MetaItemType.*--class AssetProcessingTest extends Specification implements ManagerContainerTrait {-- def "Check processing of asset updates through the processing chain"() {-- given: "expected conditions"- def conditions = new PollingConditions(timeout: 10, delay: 0.2)- def startRecording = [false]-- and: "mock attribute state consumers"- List<AttributeEvent> updatesPassedStartOfProcessingChain = []- List<AttributeEvent> updatesPassedAgentService = []- List<AttributeEvent> updatesPassedRulesService = []- List<AttributeEvent> updatesPassedDatapointService = []- List<AttributeEvent> updatesPassedAttributeLinkingService = []-- AssetUpdateProcessor firstProcessor = new AssetUpdateProcessor() {- @Override- boolean processAssetUpdate(EntityManager em, Asset asset, Attribute attribute, AttributeEvent.Source source) throws AssetProcessingException {- if (startRecording[0]) updatesPassedStartOfProcessingChain.add(new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().get()))- false- }- }-- AssetUpdateProcessor afterAgentServiceProcessor = new AssetUpdateProcessor() {- @Override- boolean processAssetUpdate(EntityManager em, Asset asset, Attribute attribute, AttributeEvent.Source source) throws AssetProcessingException {- if (startRecording[0]) updatesPassedAgentService.add(new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().get()))- false- }- }-- AssetUpdateProcessor afterRulesServiceProcessor = new AssetUpdateProcessor() {- @Override- boolean processAssetUpdate(EntityManager em, Asset asset, Attribute attribute, AttributeEvent.Source source) throws AssetProcessingException {- if (startRecording[0]) updatesPassedRulesService.add(new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().get()))- false- }- }-- AssetUpdateProcessor afterDatapointServiceProcessor = new AssetUpdateProcessor() {- @Override- boolean processAssetUpdate(EntityManager em, Asset asset, Attribute attribute, AttributeEvent.Source source) throws AssetProcessingException {- if (startRecording[0]) updatesPassedDatapointService.add(new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().get()))- false- }- }-- AssetUpdateProcessor afterAttributeLinkingServiceProcessor = new AssetUpdateProcessor() {- @Override- boolean processAssetUpdate(EntityManager em, Asset asset, Attribute attribute, AttributeEvent.Source source) throws AssetProcessingException {- if (startRecording[0]) updatesPassedAttributeLinkingService.add(new AttributeEvent(new AttributeState(asset.getId(), attribute), attribute.getTimestamp().get()))- false- }- }-- when: "the container is started"- def container = startContainer(defaultConfig(), defaultServices())- def agentService = container.getService(AgentService.class)- def assetStorageService = container.getService(AssetStorageService.class)- def assetProcessingService = container.getService(AssetProcessingService.class)- def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)-- then: "register mock asset processors"- assetProcessingService.processors.add(0, firstProcessor)- assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AgentService}+1, afterAgentServiceProcessor)- assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof RulesService}+1, afterRulesServiceProcessor)- assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AssetDatapointService}+1, afterDatapointServiceProcessor)- assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AttributeLinkingService}+1, afterAttributeLinkingServiceProcessor)-- when: "a mock agent that uses the mock protocol is created"- def mockAgent = new MockAgent("Mock agent")- .setRealm(keycloakTestSetup.realmMaster.name)- .setRequired(true)- mockAgent = assetStorageService.merge(mockAgent)-- and: "a mock thing asset is created with a valid agent linked attribute, an invalid protocol attribute and a plain attribute"- def mockThing = new ThingAsset("Mock Thing Asset")- .setParent(mockAgent)- mockThing.addOrReplaceAttributes(- new Attribute<>("light1Toggle", BOOLEAN, true)- .addOrReplaceMeta(- new MetaItem<>(- AGENT_LINK,- new MockAgentLink(mockAgent.id)- .setRequiredValue("true")- )- ),- new Attribute<>("light2Toggle", BOOLEAN, true)- .addOrReplaceMeta(- new MetaItem<>(- AGENT_LINK,- new DefaultAgentLink("INVALID AGENT ID")- )- ),- new Attribute<>("plainAttribute", TEXT, "demo")- )- mockThing = assetStorageService.merge(mockThing)-- then: "all agents should become connected"- conditions.eventually {- assert agentService.agents.values().every {it.getAgentStatus().orElse(null) == ConnectionStatus.CONNECTED}- }-- then: "the mock thing to be deployed to the protocol instance"- conditions.eventually {- assert agentService.getProtocolInstance(mockAgent.id).linkedAttributes.size() == 1- }-- then: "the system should settle down"- conditions.eventually {- assert noEventProcessedIn(assetProcessingService, 300)- }-- when: "the mock protocol instance is configured to not update the sensor on write"- startRecording[0] = true- ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).updateSensor = false-- and: "an attribute event occurs for a valid protocol linked attribute on the test asset"- def light1toggleOn = new AttributeEvent(- new AttributeState(new AttributeRef(mockThing.getId(), "light1Toggle"), false)- )- assetProcessingService.sendAttributeEvent(light1toggleOn)-- then: "the attribute event should reach the protocol and stop at the agent service, not be in the database"- conditions.eventually {- assert updatesPassedStartOfProcessingChain.size() == 1- assert updatesPassedStartOfProcessingChain[0].attributeName == "light1Toggle"- assert !updatesPassedStartOfProcessingChain[0].value.get()- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).protocolMethodCalls.last() == "WRITE_ATTRIBUTE:" + mockThing.id + ":light1Toggle"- assert updatesPassedAgentService.size() == 0- assert updatesPassedRulesService.size() == 0- assert updatesPassedDatapointService.size() == 0- assert updatesPassedAttributeLinkingService.size() == 0- // Light toggle attribute value should still be true in database- def asset = assetStorageService.find(mockThing.getId(), true)- assert asset.getAttribute("light1Toggle").flatMap{it.value}.orElse(false)- }-- when: "the protocol updates the attributes value with the value it just received"- updatesPassedStartOfProcessingChain.clear()- updatesPassedRulesService.clear()- updatesPassedAgentService.clear()- updatesPassedDatapointService.clear()- updatesPassedAttributeLinkingService.clear()- ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).updateReceived(new AttributeState(light1toggleOn.attributeRef, light1toggleOn.value.orElse(null)))-- then: "a new attribute event should occur and reach the end of the processing chain, be stored in database"- conditions.eventually {- assert updatesPassedStartOfProcessingChain.size() == 1- assert updatesPassedStartOfProcessingChain[0].attributeName == "light1Toggle"- assert !updatesPassedStartOfProcessingChain[0].value.get()- assert updatesPassedAgentService.size() == 1- assert updatesPassedRulesService.size() == 1- assert updatesPassedDatapointService.size() == 1- assert updatesPassedAttributeLinkingService.size() == 1- // Light toggle should be off in database- def asset = assetStorageService.find(mockThing.getId(), true)- assert !asset.getAttribute("light1Toggle").flatMap{it.value}.orElse(false)- }-- when: "an attribute event occurs for the invalid protocol attribute"- updatesPassedStartOfProcessingChain.clear()- updatesPassedRulesService.clear()- updatesPassedAgentService.clear()- updatesPassedDatapointService.clear()- updatesPassedAttributeLinkingService.clear()- ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).protocolWriteAttributeEvents.clear()- def light2toggleOn = new AttributeEvent(- new AttributeState(new AttributeRef(mockThing.getId(), "light2Toggle"), true)- )- assetProcessingService.sendAttributeEvent(light2toggleOn)-- then: "the attribute event should pass the start of the processing chain, but not reach the mock protocol or the end of the processing chain and error should be populated"- conditions.eventually {- assert updatesPassedStartOfProcessingChain.size() == 1- assert updatesPassedStartOfProcessingChain[0].attributeName == "light2Toggle"- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).protocolWriteAttributeEvents.size() == 0- assert updatesPassedAgentService.size() == 0- assert updatesPassedRulesService.size() == 0- assert updatesPassedDatapointService.size() == 0- assert updatesPassedAttributeLinkingService.size() == 0- }-- when: "an attribute event occurs for the plain attribute"- updatesPassedStartOfProcessingChain.clear()- updatesPassedRulesService.clear()- updatesPassedAgentService.clear()- updatesPassedDatapointService.clear()- updatesPassedAttributeLinkingService.clear()- ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).protocolWriteAttributeEvents.clear()- def plainAttributeTest = new AttributeEvent(- new AttributeState(new AttributeRef(mockThing.getId(), "plainAttribute"), "test")- )- assetProcessingService.sendAttributeEvent(plainAttributeTest)-- then: "the attribute event should pass the start of the processing chain and reach the end of the processing chain"- conditions.eventually {- assert updatesPassedStartOfProcessingChain.size() == 1- assert updatesPassedAgentService.size() == 1- assert updatesPassedRulesService.size() == 1- assert updatesPassedDatapointService.size() == 1- assert updatesPassedAttributeLinkingService.size() == 1- assert updatesPassedAttributeLinkingService[0].attributeName == "plainAttribute"- assert updatesPassedAttributeLinkingService[0].value.orElse(null) == "test"- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent.id)).protocolWriteAttributeEvents.size() == 0- }-- }-}diff --git a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovyindex 05c35b808..0068dc9a0 100644--- a/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy+++ b/test/src/test/groovy/org/openremote/test/console/ConsoleTest.groovy@@ -3,7 +3,6 @@ package org.openremote.test.consoleimport com.google.firebase.messaging.Messageimport jakarta.ws.rs.WebApplicationException-import net.sf.saxon.s9api.push.Pushimport org.openremote.container.timer.TimerServiceimport org.openremote.container.util.UniqueIdentifierGeneratorimport org.openremote.manager.asset.AssetStorageServicediff --git a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovyindex 3621008a8..66e173581 100644--- a/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy+++ b/test/src/test/groovy/org/openremote/test/energy/EnergyOptimisationAssetTest.groovy@@ -326,9 +326,12 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaiassert (assetStorageService.find(managerTestSetup.electricityOptimisationAssetId) as EnergyOptimisationAsset).getFinancialSaving().orElse(0d) == -1.5}- when: "another optimisation run occurs"+ when: "the battery energy level changes and time advances"batteryAsset.setEnergyLevel(70d)batteryAsset = assetStorageService.merge(batteryAsset)+ advancePseudoClock(1, TimeUnit.SECONDS, container) // This prevents attribute timestamp issues++ and: "another optimisation run occurs"optimisationService.runOptimisation(managerTestSetup.electricityOptimisationAssetId, optimisationTime.plus((long)optimiser.intervalSize*60, ChronoUnit.MINUTES))then: "the optimisation saving should have decreased by the same amount (i.e. cost)"@@ -337,9 +340,12 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaiassert (assetStorageService.find(managerTestSetup.electricityOptimisationAssetId) as EnergyOptimisationAsset).getFinancialSaving().orElse(0d) == -3.0}- when: "another optimisation run occurs"+ when: "the battery energy level changes and time advances"batteryAsset.setEnergyLevel(100d)batteryAsset = assetStorageService.merge(batteryAsset)+ advancePseudoClock(1, TimeUnit.SECONDS, container) // This prevents attribute timestamp issues++ and: "another optimisation run occurs"optimisationService.runOptimisation(managerTestSetup.electricityOptimisationAssetId, optimisationTime.plus((long)optimiser.intervalSize*60*2, ChronoUnit.MINUTES))then: "the optimisation saving should have decreased by the same amount (i.e. cost)"@@ -348,11 +354,13 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaiassert (assetStorageService.find(managerTestSetup.electricityOptimisationAssetId) as EnergyOptimisationAsset).getFinancialSaving().orElse(0d) == -4.5}- when: "another optimisation run occurs"+ when: "the battery energy level changes and time advances"batteryAsset.setEnergyLevel(130d)batteryAsset = assetStorageService.merge(batteryAsset)+ advancePseudoClock(1, TimeUnit.SECONDS, container) // This prevents attribute timestamp issues++ and: "another optimisation run occurs"optimisationService.runOptimisation(managerTestSetup.electricityOptimisationAssetId, optimisationTime.plus((long)optimiser.intervalSize*60*3, ChronoUnit.MINUTES))- println("DONE")then: "the optimisation saving should have decreased by the same amount (i.e. cost)"conditions.eventually {@@ -373,6 +381,9 @@ class EnergyOptimisationAssetTest extends Specification implements ManagerContaiwhen: "another optimisation run occurs (this should now be exporting from storage)"batteryAsset.setEnergyLevel(160d)batteryAsset = assetStorageService.merge(batteryAsset)+ advancePseudoClock(1, TimeUnit.SECONDS, container) // This prevents attribute timestamp issues++ and: "another optimisation run occurs"optimisationService.runOptimisation(managerTestSetup.electricityOptimisationAssetId, optimisationTime.plus((long)optimiser.intervalSize*60*4, ChronoUnit.MINUTES))then: "the optimisation saving should have increased by the cost to import 30kWh (as battery will now be exporting)"diff --git a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovyindex f538c876b..fef5ee602 100644--- a/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy+++ b/test/src/test/groovy/org/openremote/test/gateway/GatewayTest.groovy@@ -922,10 +922,10 @@ class GatewayTest extends Specification implements ManagerContainerTrait {Thread.sleep(1000)conditions.eventually {assert attributeEvents.size() == 4- assert attributeEvents.any{it.assetId == managerTestSetup.light2Id && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}- assert attributeEvents.any{it.assetId == managerTestSetup.light1Id && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}- assert attributeEvents.any{it.assetId == mapAssetId(gateway.id, managerTestSetup.light2Id, false) && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}- assert attributeEvents.any{it.assetId == mapAssetId(gateway.id, managerTestSetup.light1Id, false) && it.attributeName == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.id == managerTestSetup.light2Id && it.name == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.id == managerTestSetup.light1Id && it.name == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.id == mapAssetId(gateway.id, managerTestSetup.light2Id, false) && it.name == LightAsset.ON_OFF.name && it.value.orElse(false)}+ assert attributeEvents.any{it.id == mapAssetId(gateway.id, managerTestSetup.light1Id, false) && it.name == LightAsset.ON_OFF.name && it.value.orElse(false)}}when: "a gateway client asset is deleted"diff --git a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovyindex f211b832d..3fa154981 100644--- a/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy+++ b/test/src/test/groovy/org/openremote/test/model/AssetModelTest.groovy@@ -8,6 +8,7 @@ import org.openremote.agent.protocol.simulator.SimulatorAgentimport org.openremote.agent.protocol.velbus.VelbusTCPAgentimport org.openremote.container.persistence.PersistenceServiceimport org.openremote.container.timer.TimerService+import org.openremote.container.util.UniqueIdentifierGeneratorimport org.openremote.manager.asset.AssetModelServiceimport org.openremote.manager.asset.AssetStorageServiceimport org.openremote.manager.setup.SetupService@@ -19,10 +20,10 @@ import org.openremote.model.asset.impl.GroupAssetimport org.openremote.model.asset.impl.LightAssetimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.Attribute+import org.openremote.model.attribute.AttributeEventimport org.openremote.model.attribute.AttributeMapimport org.openremote.model.attribute.MetaItemimport org.openremote.model.geo.GeoJSONPoint-import org.openremote.model.rules.AssetStateimport org.openremote.model.util.TimeUtilimport org.openremote.model.util.ValueUtilimport org.openremote.model.value.*@@ -481,8 +482,12 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {def "Serialize/Deserialize asset model"() {given: "An asset"+ def parentId = UniqueIdentifierGenerator.generateId()+ def createdDate = new Date()def asset = new LightAsset("Test light")+ .setId(UniqueIdentifierGenerator.generateId()).setRealm(MASTER_REALM)+ .setParentId(parentId).setTemperature(100I).setColourRGB(new ColourRGB(50, 100, 200)).addAttributes(@@ -493,7 +498,6 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {.setPagingMode(true))))-asset.getAttribute(LightAsset.COLOUR_RGB).ifPresent({it.addOrReplaceMeta(new MetaItem<>(MetaItemType.AGENT_LINK, new DefaultAgentLink("agent_id")@@ -503,6 +507,10 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {))})+ // Inject properties which are normally done by the backend on retrieval+ asset.path = [asset.id, parentId]+ asset.createdOn = createdDate+ asset.getAttributes().values().forEach {it.setTimestamp(asset.createdOn.getTime())}expect: "the attributes to match the set values"asset.getTemperature().orElse(null) == 100I@@ -516,7 +524,10 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {then: "the string should be valid JSON"def assetObjectNode = ValueUtil.parse(assetStr, ObjectNode.class).get()assetObjectNode.get("name").asText() == "Test light"- assetObjectNode.get("attributes").get("colourRGB").get("timestamp") == null+ assetObjectNode.get("path").get(0).asText() == asset.id+ assetObjectNode.get("path").get(1).asText() == parentId++ assetObjectNode.get("attributes").get("colourRGB").get("timestamp").asLong() == createdDate.getTime()assetObjectNode.get("attributes").get("colourRGB").get("meta").get(MetaItemType.AGENT_LINK.name).isObject()assetObjectNode.get("attributes").get("colourRGB").get("meta").get(MetaItemType.AGENT_LINK.name).get("id").asText() == "agent_id"assetObjectNode.get("attributes").get("colourRGB").get("meta").get(MetaItemType.AGENT_LINK.name).get("type").asText() == DefaultAgentLink.class.getSimpleName()@@ -531,6 +542,9 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {then: "it should match the original"asset.getName() == asset2.getName()asset2.getType() == asset.getType()+ asset2.getCreatedOn() == asset.getCreatedOn()+ asset2.getParentId() == parentId+ asset2.getPath() == asset.getPath()asset2.getTemperature().orElse(null) == asset.getTemperature().orElse(null)asset2.getColourRGB().map{it.getR()}.orElse(null) == asset.getColourRGB().map{it.getR()}.orElse(null)asset2.getColourRGB().map{it.getG()}.orElse(null) == asset.getColourRGB().map{it.getG()}.orElse(null)@@ -548,14 +562,35 @@ class AssetModelTest extends Specification implements ManagerContainerTrait {clonedAttribute.getValue().orElse(null) == attribute.getValue().orElse(null)clonedAttribute.getMeta() == attribute.getMeta()- when: "an asset state is serialized"- def assetState = new AssetState(asset2, attribute, null)- def assetStateStr = ValueUtil.asJSON(assetState).orElse(null)+ when: "an attribute event is serialized"+ def attributeEvent = new AttributeEvent(asset2, attribute, "Test", attribute.getValue().orElse(null), attribute.getTimestamp().orElse(0L), null, 0L).setDeleted(true)+ def attributeEventStr = ValueUtil.asJSON(attributeEvent).orElse(null)++ then: "it should look as expected"+ def attributeEventObjectNode = ValueUtil.parse(attributeEventStr, ObjectNode.class).get()+ attributeEventObjectNode.get("ref").get("id").asText() == asset2.id+ attributeEventObjectNode.get("ref").get("name").asText() == LightAsset.COLOUR_RGB.name+ attributeEventObjectNode.get("timestamp").asLong() == createdDate.getTime()+ attributeEventObjectNode.has("realm")+ attributeEventObjectNode.get("value").isTextual()+ attributeEventObjectNode.get("value").asText() == "#3264C8"+ attributeEventObjectNode.get("deleted").asBoolean()+ !attributeEventObjectNode.has("source")+ !attributeEventObjectNode.has("meta")++ when: "the attribute event is serialized with the enhanced view"+ def attributeEventStr2 = ValueUtil.JSON.writerWithView(AttributeEvent.Enhanced.class).writeValueAsString(attributeEvent)then: "it should look as expected"- def assetStateObjectNode = ValueUtil.parse(assetStateStr, ObjectNode.class).get()- assetStateObjectNode.get("name").asText() == LightAsset.COLOUR_RGB.name- assetStateObjectNode.get("value").isTextual()- assetStateObjectNode.get("value").asText() == "#3264C8"+ def attributeEventObjectNode2 = ValueUtil.parse(attributeEventStr2, ObjectNode.class).get()+ attributeEventObjectNode2.get("ref").get("id").asText() == asset2.id+ attributeEventObjectNode2.get("ref").get("name").asText() == LightAsset.COLOUR_RGB.name+ attributeEventObjectNode2.get("timestamp").asLong() == createdDate.getTime()+ attributeEventObjectNode2.has("realm")+ attributeEventObjectNode2.get("value").isTextual()+ attributeEventObjectNode2.get("value").asText() == "#3264C8"+ attributeEventObjectNode2.get("deleted").asBoolean()+ !attributeEventObjectNode2.has("source")+ !attributeEventObjectNode2.has("meta")}}diff --git a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovyindex 745419001..5bd8f9bbb 100644--- a/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy+++ b/test/src/test/groovy/org/openremote/test/mqtt/MqttBrokerTest.groovy@@ -172,8 +172,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 50}receivedEvents.clear()@@ -186,8 +186,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "presenceDetected"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "presenceDetected"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(false) == true}receivedEvents.clear()@@ -202,8 +202,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 70dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 70d}receivedEvents.clear()@@ -218,8 +218,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert !assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("lights").get().value.orElse(true)assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "lights"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "lights"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(true) == false}receivedEvents.clear()@@ -234,8 +234,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("lights").get().value.orElse(false)assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "lights"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "lights"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(false) == true}receivedEvents.clear()@@ -281,8 +281,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 30}receivedEvents.clear()@@ -327,8 +327,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 40}receivedEvents.clear()@@ -359,8 +359,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert receivedValues.get(0) == "50"assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 50}receivedEvents.clear()@@ -374,8 +374,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "presenceDetected"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "presenceDetected"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(true) == falseassert receivedValues.size() == 0}@@ -431,7 +431,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AssetEvent- assert (receivedEvents.get(0) as AssetEvent).assetId == managerTestSetup.smartBuildingId+ assert (receivedEvents.get(0) as AssetEvent).id == managerTestSetup.smartBuildingIdassert (receivedEvents.get(0) as AssetEvent).asset.attributes.get("temp") != nullassert (receivedEvents.get(0) as AssetEvent).asset.attributes.get("temp").flatMap(){it.getValue()}.orElse(null) == "hello world"}@@ -459,7 +459,7 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {conditions.eventually {assert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AssetEvent- assert (receivedEvents.get(0) as AssetEvent).assetId == childAsset.id+ assert (receivedEvents.get(0) as AssetEvent).id == childAsset.idassert (receivedEvents.get(0) as AssetEvent).assetName == childAsset.name}receivedEvents.clear()@@ -510,8 +510,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 170dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 170d}receivedEvents.clear()@@ -532,8 +532,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 1000dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 1000d}receivedEvents.clear()@@ -548,8 +548,8 @@ class MqttBrokerTest extends Specification implements ManagerContainerTrait {assert assetStorageService.find(managerTestSetup.apartment1HallwayId).getAttribute("motionSensor").get().value.orElse(0) == 170dassert receivedEvents.size() == 1assert receivedEvents.get(0) instanceof AttributeEvent- assert (receivedEvents.get(0) as AttributeEvent).assetId == managerTestSetup.apartment1HallwayId- assert (receivedEvents.get(0) as AttributeEvent).attributeName == "motionSensor"+ assert (receivedEvents.get(0) as AttributeEvent).id == managerTestSetup.apartment1HallwayId+ assert (receivedEvents.get(0) as AttributeEvent).name == "motionSensor"assert (receivedEvents.get(0) as AttributeEvent).value.orElse(0) == 170d}receivedEvents.clear()diff --git a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovyindex 31e723776..44af91d75 100644--- a/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/BasicProtocolTest.groovy@@ -306,8 +306,8 @@ class BasicProtocolTest extends Specification implements ManagerContainerTrait {then: "the update should reach the protocol as an attribute write request"conditions.eventually {assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.size() == 1- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).attributeName == "tempTarget1"- assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).attributeRef.id == mockThing.id+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).name == "tempTarget1"+ assert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).ref.id == mockThing.idassert ((MockProtocol)agentService.getProtocolInstance(mockAgent1.id)).protocolWriteAttributeEvents.get(0).value.orElse(null) == 30d}diff --git a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovyindex 8938ef4fe..de4bcbc2c 100644--- a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy@@ -87,7 +87,7 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrdef "Basic agent and attribute linking"() {given: "expected conditions"- def conditions = new PollingConditions(timeout: 15, delay: 0.2)+ def conditions = new PollingConditions(timeout: 20, delay: 0.2)and: "some mailbox messages"sendMessage("from@localhost")@@ -101,7 +101,9 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrdef agentService = container.getService(AgentService.class)def clientEventService = container.getService(ClientEventService.class)List<AttributeEvent> attributeEvents = []- def eventSessionID = clientEventService.addInternalSubscription(AttributeEvent.class, null, it -> attributeEvents.add(it))+ def eventSessionID = clientEventService.addInternalSubscription(AttributeEvent.class, null, it -> {+ attributeEvents.add(it)+ })when: "a mail client agent is created"def agent = new MailAgent("Test agent")@@ -151,6 +153,14 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrand: "the asset is merged into the asset service"asset = assetStorageService.merge(asset)+ then: "attribute events for the new asset should arrive"+ conditions.eventually {+ assert attributeEvents.count {it.id == asset.id} == 6+ }++ when: "the attribute events are cleared"+ attributeEvents.clear()+and: "the agent is enabled"agent.setDisabled(false)agent = assetStorageService.merge(agent)@@ -163,17 +173,17 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrthen: "the agent should become connected"conditions.eventually {- assert attributeEvents.any {it.assetId == agent.id && it.attributeName == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.CONNECTED}+ assert attributeEvents.any {it.id == agent.id && it.name == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.CONNECTED}}then: "the agent should have finished message checking"conditions.eventually {- assert attributeEvents.any {it.assetId == agent.id && it.attributeName == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.WAITING}+ assert attributeEvents.any {it.id == agent.id && it.name == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.WAITING}}then: "the linked attributes should not have been updated with mailbox messages (as creation time of agent is after message timestamps)"conditions.eventually {- assert attributeEvents.count {it.assetId == asset.id} == 0+ assert attributeEvents.count {it.id == asset.id} == 0}when: "more messages are received in the mailbox"@@ -185,12 +195,12 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrthen: "the matching linked attributes should have been updated with the new mailbox messages"conditions.eventually {- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 2- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectAndFromMatchUseBody" && it.value.orElse(null) == "Not a test body 2"} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 2+ assert attributeEvents.count {it.id == asset.id && it.name == "subjectAndFromMatchUseBody" && it.value.orElse(null) == "Not a test body 2"} == 1}when: "more messages are received in the mailbox"@@ -199,12 +209,12 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTrthen: "the matching linked attributes should have been updated with the new mailbox messages"conditions.eventually {- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 3- assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectAndFromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2+ assert attributeEvents.count {it.id == asset.id && it.name == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1+ assert attributeEvents.count {it.id == asset.id && it.name == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 3+ assert attributeEvents.count {it.id == asset.id && it.name == "subjectAndFromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1}cleanup: "the event subscription is removed"diff --git a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovyindex 973996879..915a151e2 100644--- a/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/mqtt/MQTTClientProtocolTest.groovy@@ -34,7 +34,6 @@ import org.openremote.manager.setup.SetupServiceimport org.openremote.model.Constantsimport org.openremote.model.asset.agent.Agentimport org.openremote.model.asset.agent.ConnectionStatus-import org.openremote.model.asset.agent.Protocolimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.Attributeimport org.openremote.model.attribute.AttributeEvent@@ -98,7 +97,7 @@ class MQTTClientProtocolTest extends Specification implements ManagerContainerTrnew MetaItem<>(AGENT_LINK, new MQTTAgentLink(agent.id).setSubscriptionTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}").setPublishTopic("${keycloakTestSetup.realmBuilding.name}/$clientId/${DefaultMQTTHandler.ATTRIBUTE_VALUE_WRITE_TOPIC}/targetTemperature/${managerTestSetup.apartment1LivingroomId}")- .setWriteValue("${Protocol.DYNAMIC_VALUE_PLACEHOLDER}")+ .setWriteValue("${Constants.DYNAMIC_VALUE_PLACEHOLDER}"))))diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovyindex 1778daa51..2f603e9d4 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy@@ -31,7 +31,6 @@ import org.openremote.model.asset.AssetFilterimport org.openremote.model.asset.ReadAttributeEventimport org.openremote.model.asset.agent.Agentimport org.openremote.model.asset.agent.ConnectionStatus-import org.openremote.model.asset.agent.Protocolimport org.openremote.model.asset.impl.ThingAssetimport org.openremote.model.attribute.Attributeimport org.openremote.model.attribute.AttributeEvent@@ -196,12 +195,12 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai"targetTemperature",0.12345)).orElse(ValueUtil.NULL_LITERAL)- .replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER)+ .replace("0.12345", Constants.DYNAMIC_VALUE_PLACEHOLDER)).setMessageMatchFilters([new RegexValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX + "(.*)", true, false).setMatchGroup(1),- new JsonPathFilter("\$..attributeState.ref.name", true, false)+ new JsonPathFilter("\$..ref.name", true, false)] as ValueFilter[]).setMessageMatchPredicate(@@ -210,7 +209,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai.setValueFilters([new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),- new JsonPathFilter("\$..events[?(@.attributeState.ref.name == \"targetTemperature\")].attributeState.value", true, false)+ new JsonPathFilter("\$..events[?(@.ref.name == \"targetTemperature\")].value", true, false)] as ValueFilter[]).setWebsocketSubscriptions(@@ -231,7 +230,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai.setMessageMatchFilters([new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),- new JsonPathFilter("\$..attributeState.ref.name", true, false)+ new JsonPathFilter("\$..ref.name", true, false)] as ValueFilter[]).setMessageMatchPredicate(@@ -240,7 +239,7 @@ class WebsocketClientProtocolTest extends Specification implements ManagerContai.setValueFilters([new SubStringValueFilter(TriggeredEventSubscription.MESSAGE_PREFIX.length()),- new JsonPathFilter("\$..events[?(@.attributeState.ref.name == \"co2Level\")].attributeState.value", true, false),+ new JsonPathFilter("\$..events[?(@.ref.name == \"co2Level\")].value", true, false),] as ValueFilter[]).setWebsocketSubscriptions(diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovyindex f4f6192a9..c88a5bbee 100644--- a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy@@ -202,8 +202,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitTriggeredEventSubscription triggeredEvent = receivedMessages[0] as TriggeredEventSubscriptionassert triggeredEvent.subscriptionId == "attributes"assert triggeredEvent.events.size() == 1- assert ((AttributeEvent) triggeredEvent.events[0]).assetId == managerTestSetup.apartment1LivingroomId- assert ((AttributeEvent) triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent) triggeredEvent.events[0]).id == managerTestSetup.apartment1LivingroomId+ assert ((AttributeEvent) triggeredEvent.events[0]).name == "targetTemperature"assert ((AttributeEvent) triggeredEvent.events[0]).value.orElse(0) == 5}@@ -223,8 +223,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitTriggeredEventSubscription triggeredEvent = receivedMessages2[0] as TriggeredEventSubscriptionassert triggeredEvent.subscriptionId == "attributes2"assert triggeredEvent.events.size() == 1- assert ((AttributeEvent) triggeredEvent.events[0]).assetId == managerTestSetup.apartment2LivingroomId- assert ((AttributeEvent) triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent) triggeredEvent.events[0]).id == managerTestSetup.apartment2LivingroomId+ assert ((AttributeEvent) triggeredEvent.events[0]).name == "targetTemperature"assert ((AttributeEvent) triggeredEvent.events[0]).value.orElse(0) == 15}@@ -247,7 +247,7 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitassert (receivedMessages.get(0) as TriggeredEventSubscription).subscriptionId == "assets"assert (receivedMessages.get(0) as TriggeredEventSubscription).events.size() == 1assert (receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) instanceof AssetEvent- assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).assetId == managerTestSetup.apartment1LivingroomId+ assert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).id == managerTestSetup.apartment1LivingroomIdassert ((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.size() == 7assert !((receivedMessages.get(0) as TriggeredEventSubscription).events.get(0) as AssetEvent).attributeNames.contains("testAttribute")}@@ -342,8 +342,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitTriggeredEventSubscription triggeredEvent = receivedMessages[0] as TriggeredEventSubscriptionassert triggeredEvent.subscriptionId == "1"assert triggeredEvent.events.size() == 1- assert ((AttributeEvent)triggeredEvent.events[0]).assetId == managerTestSetup.apartment1LivingroomId- assert ((AttributeEvent)triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent)triggeredEvent.events[0]).id == managerTestSetup.apartment1LivingroomId+ assert ((AttributeEvent)triggeredEvent.events[0]).name == "targetTemperature"assert ((AttributeEvent)triggeredEvent.events[0]).value.orElse(0) == 5}@@ -357,8 +357,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitTriggeredEventSubscription triggeredEvent = receivedMessages[0] as TriggeredEventSubscriptionassert triggeredEvent.subscriptionId == "1"assert triggeredEvent.events.size() == 1- assert ((AttributeEvent)triggeredEvent.events[0]).assetId == managerTestSetup.apartment1LivingroomId- assert ((AttributeEvent)triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent)triggeredEvent.events[0]).id == managerTestSetup.apartment1LivingroomId+ assert ((AttributeEvent)triggeredEvent.events[0]).name == "targetTemperature"assert ((AttributeEvent)triggeredEvent.events[0]).value.orElse(0) == 5}@@ -372,8 +372,8 @@ class WebsocketClientTest extends Specification implements ManagerContainerTraitTriggeredEventSubscription triggeredEvent = receivedMessages[0] as TriggeredEventSubscriptionassert triggeredEvent.subscriptionId == "1"assert triggeredEvent.events.size() == 1- assert ((AttributeEvent)triggeredEvent.events[0]).assetId == managerTestSetup.apartment1LivingroomId- assert ((AttributeEvent)triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent)triggeredEvent.events[0]).id == managerTestSetup.apartment1LivingroomId+ assert ((AttributeEvent)triggeredEvent.events[0]).name == "targetTemperature"assert !((AttributeEvent)triggeredEvent.events[0]).value.isPresent()}diff --git a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovyindex 3f1e0b3ee..fa613444f 100644--- a/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy+++ b/test/src/test/groovy/org/openremote/test/provisioning/UserAndAssetProvisioningTest.groovy@@ -317,8 +317,8 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContaand: "the client should have been notified about the attribute change"conditions.eventually {assert attributeEvents.size() == 1- assert attributeEvents.get(0).assetId == asset.id- assert attributeEvents.get(0).attributeName == "customAttribute"+ assert attributeEvents.get(0).id == asset.id+ assert attributeEvents.get(0).name == "customAttribute"assert attributeEvents.get(0).value.orElse(0d) == 99d}@@ -329,18 +329,16 @@ class UserAndAssetProvisioningTest extends Specification implements ManagerContathen: "the client should have been notified about the asset change and all attributes should have generated an attribute event"conditions.eventually {assert assetEvents.size() == 1- assert assetEvents.get(0).assetId == asset.id+ assert assetEvents.get(0).id == asset.idassert assetEvents.get(0).assetName == asset.name}when: "another asset's attribute is updated within the system"- assetProcessingService.sendAttributeEvent(- new AttributeEvent(managerTestSetup.apartment2LivingroomId, "lightSwitch", true)- )+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerTestSetup.apartment2LivingroomId, "lightSwitch", true))then: "the internal consumer should have been notified"conditions.eventually {- assert internalAttributeEvents.find{it.assetId == managerTestSetup.apartment2LivingroomId && it.attributeName == "lightSwitch" && it.value.orElse(false)} != null+ assert internalAttributeEvents.find{it.id == managerTestSetup.apartment2LivingroomId && it.name == "lightSwitch" && it.value.orElse(false)} != null}and: "the client should not have been notified"diff --git a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovyindex 87303309b..6f9291a5e 100644--- a/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/BasicRulesProcessingTest.groovy@@ -150,7 +150,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainerthen: "after a few seconds the engines in scope should have facts and rules should have fired"conditions.eventually {- assert rulesService.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 1+ assert rulesService.attributeEvents.size() == DEMO_RULE_STATES_GLOBAL + 1assert rulesImport.globalEngine.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 1assert rulesImport.masterEngine.assetStates.size() == DEMO_RULE_STATES_SMART_OFFICEassert rulesImport.realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING + 1@@ -196,7 +196,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainerthen: "no rules should have fired"conditions.eventually {- assert rulesService.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 1+ assert rulesService.attributeEvents.size() == DEMO_RULE_STATES_GLOBAL + 1assert rulesImport.globalEngine.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 1assert rulesImport.masterEngine.assetStates.size() == DEMO_RULE_STATES_SMART_OFFICEassert rulesImport.realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING + 1@@ -222,7 +222,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainerthen: "the facts should be removed from the rule engines and rules should have fired"conditions.eventually {- assert rulesService.assetStates.size() == DEMO_RULE_STATES_GLOBAL+ assert rulesService.attributeEvents.size() == DEMO_RULE_STATES_GLOBALassert rulesImport.globalEngine.assetStates.size() == DEMO_RULE_STATES_GLOBALassert rulesImport.masterEngine.assetStates.size() == DEMO_RULE_STATES_SMART_OFFICEassert rulesImport.realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING@@ -253,7 +253,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainerthen: "the facts should be added to the rule engines and rules should have fired"conditions.eventually {- assert rulesService.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 2+ assert rulesService.attributeEvents.size() == DEMO_RULE_STATES_GLOBAL + 2assert rulesImport.globalEngine.assetStates.size() == DEMO_RULE_STATES_GLOBAL + 2assert rulesImport.masterEngine.assetStates.size() == DEMO_RULE_STATES_SMART_OFFICEassert rulesImport.realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDING + 2@@ -274,7 +274,7 @@ class BasicRulesProcessingTest extends Specification implements ManagerContainerthen: "the facts should be removed from the rule engines and rules should have fired"conditions.eventually {- assert rulesService.assetStates.size() == DEMO_RULE_STATES_GLOBAL+ assert rulesService.attributeEvents.size() == DEMO_RULE_STATES_GLOBALassert rulesImport.globalEngine.assetStates.size() == DEMO_RULE_STATES_GLOBALassert rulesImport.masterEngine.assetStates.size() == DEMO_RULE_STATES_SMART_OFFICEassert rulesImport.realmBuildingEngine.assetStates.size() == DEMO_RULE_STATES_SMART_BUILDINGdiff --git a/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovyindex c90d626f8..80a039a5b 100644--- a/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/ChildAssetControlRulesTest.groovy@@ -7,7 +7,6 @@ import org.openremote.manager.rules.RulesEngineimport org.openremote.manager.rules.RulesServiceimport org.openremote.manager.rules.RulesetStorageServiceimport org.openremote.model.Constants-import org.openremote.model.asset.impl.GroupAssetimport org.openremote.model.asset.impl.LightAssetimport org.openremote.model.attribute.Attributeimport org.openremote.model.attribute.AttributeEvent@@ -76,7 +75,7 @@ class ChildAssetControlRulesTest extends Specification implements ManagerContainwhen: "The on/off attribute of the parent asset is written to"assetProcessingService.sendAttributeEvent(- new AttributeEvent(parentAsset.id, LightAsset.ON_OFF, true)+ new AttributeEvent(parentAsset.id, LightAsset.ON_OFF, true))then: "the parent asset attribute should be updated"diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovyindex 4be66fa9d..02677e8e6 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/FlowRulesTest.groovy@@ -10,7 +10,6 @@ import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.rules.GlobalRulesetimport org.openremote.model.rules.Ruleset-import org.openremote.model.rules.TemporaryFactimport org.openremote.model.rules.flow.NodeCollectionimport org.openremote.model.util.ValueUtilimport org.openremote.test.ManagerContainerTraitdiff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovyindex ec5d0423d..fe75e7ec2 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy@@ -293,7 +293,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {)and: "the console location is set to the apartment"- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION))then: "the consoles location should have been updated"conditions.eventually {@@ -317,7 +317,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves outside the home geofence (as defined in the rule)"def outsideLocation = new GeoJSONPoint(0d, 0d)- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, outsideLocation), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, outsideLocation))then: "the apartment lights should be switched off"conditions.eventually {@@ -367,7 +367,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves back inside the home geofence (as defined in the rule)"def lastFireTimestamp = realmBuildingEngine.lastFireTimestamp- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION))then: "the engine fires at least one more time"conditions.eventually {@@ -376,7 +376,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves outside the home geofence again (as defined in the rule)"emailMessages.clear()- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(-10d, -4d)), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(-10d, -4d)))then: "another notification should have been sent to the console"conditions.eventually {@@ -393,7 +393,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console sends a location update with a new location but still outside the geofence"def timestamp = assetStorageService.find(consoleRegistration.id, true).getAttribute(Asset.LOCATION).flatMap{it.getTimestamp()}.orElse(timerService.getCurrentTimeMillis())def attributeEvent = new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(10d, 10d), timestamp)- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "after a few seconds the rule should not have fired again"new PollingConditions(timeout: 5, initialDelay: 1).eventually {@@ -420,7 +420,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves back inside the home geofence (as defined in the rule)"lastFireTimestamp = realmBuildingEngine.lastFireTimestamp- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION))then: "the engine fires at least one more time"conditions.eventually {@@ -429,7 +429,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves outside the home geofence again (as defined in the rule)"attributeEvent = new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(0d, 0d))- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "after a few seconds the rule should not have fired again"new PollingConditions(timeout: 5, initialDelay: 1).eventually {@@ -438,7 +438,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the console device moves back inside the home geofence (as defined in the rule)"lastFireTimestamp = realmBuildingEngine.lastFireTimestamp- assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION), AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, ManagerTestSetup.SMART_BUILDING_LOCATION))then: "the engine fires at least one more time"conditions.eventually {@@ -450,7 +450,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {and: "the console device moves outside the home geofence again (as defined in the rule)"attributeEvent = new AttributeEvent(consoleRegistration.id, Asset.LOCATION.name, new GeoJSONPoint(0d, 0d))- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "another notification should have been sent to the console"conditions.eventually {@@ -479,7 +479,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the same AttributeEvent is sent"timestamp = attributeEvent.getTimestamp()+1attributeEvent.setTimestamp(timestamp)- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "the event should have been committed to the DB"conditions.eventually {@@ -501,7 +501,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the same AttributeEvent is sent"timestamp = attributeEvent.getTimestamp()+1attributeEvent.setTimestamp(timestamp)- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "the event should have been committed to the DB"conditions.eventually {@@ -525,7 +525,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {when: "the same AttributeEvent is sent"timestamp = attributeEvent.getTimestamp()+1attributeEvent.setTimestamp(timestamp)- assetProcessingService.sendAttributeEvent(attributeEvent, AttributeEvent.Source.CLIENT)+ assetProcessingService.sendAttributeEvent(attributeEvent)then: "the event should have been committed to the DB"conditions.eventually {diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovyindex 87f1b6080..f7c06678c 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAllLightsOffTest.groovy@@ -10,7 +10,6 @@ import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.rules.AssetRulesetimport org.openremote.model.rules.Ruleset-import org.openremote.model.rules.TemporaryFactimport org.openremote.test.ManagerContainerTraitimport spock.lang.Ignoreimport spock.lang.Specification@@ -83,7 +82,7 @@ class ResidenceAllLightsOffTest extends Specification implements ManagerContaineand: "we turn the light on again in a room"assetProcessingService.sendAttributeEvent(- new AttributeEvent(managerTestSetup.apartment2LivingroomId, "lightSwitch", true)+ new AttributeEvent(managerTestSetup.apartment2LivingroomId, "lightSwitch", true))then: "the light should still be on after a few seconds (the all lights off event expires after 3 seconds)"diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovyindex cad13283a..6e0fedcb7 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceAutoVentilationTest.groovy@@ -11,7 +11,6 @@ import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.rules.AssetRulesetimport org.openremote.model.rules.Ruleset-import org.openremote.model.rules.TemporaryFactimport org.openremote.test.ManagerContainerTraitimport spock.lang.Ignoreimport spock.lang.Specification@@ -19,7 +18,6 @@ import spock.util.concurrent.PollingConditionsimport static java.util.concurrent.TimeUnit.MINUTESimport static org.openremote.setup.integration.ManagerTestSetup.DEMO_RULE_STATES_APARTMENT_1-import static org.openremote.model.attribute.AttributeEvent.Source.SENSOR// Ignore this test as temporary facts (rule events) cause the rule engine to continually fire, need to decide if// we support rule events or should it just be something rules do internallydiff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovyindex cc8c7f9ca..f4bf5f6dd 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidencePresenceDetectionTest.groovy@@ -7,11 +7,10 @@ import org.openremote.manager.rules.RulesEngineimport org.openremote.manager.rules.RulesServiceimport org.openremote.manager.rules.RulesetStorageServiceimport org.openremote.manager.setup.SetupService-import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.rules.AssetRulesetimport org.openremote.model.rules.Ruleset-import org.openremote.model.rules.TemporaryFact+import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.test.ManagerContainerTraitimport spock.lang.Ignoreimport spock.lang.Specification@@ -19,7 +18,6 @@ import spock.util.concurrent.PollingConditionsimport static java.util.concurrent.TimeUnit.MINUTESimport static org.openremote.setup.integration.ManagerTestSetup.DEMO_RULE_STATES_APARTMENT_1-import static org.openremote.model.attribute.AttributeEvent.Source.SENSOR// Ignore this test as temporary facts (rule events) cause the rule engine to continually fire, need to decide if// we support rule events or should it just be something rules do internallydiff --git a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovyindex 9fb30c147..8e218dd73 100644--- a/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy+++ b/test/src/test/groovy/org/openremote/test/rules/residence/ResidenceSmartSwitchTest.groovy@@ -10,7 +10,6 @@ import org.openremote.setup.integration.ManagerTestSetupimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.rules.AssetRulesetimport org.openremote.model.rules.Ruleset-import org.openremote.model.rules.TemporaryFactimport org.openremote.test.ManagerContainerTraitimport spock.lang.Ignoreimport spock.lang.Specificationdiff --git a/test/src/test/resources/org/openremote/test/rules/ChildAssetControl.groovy b/test/src/test/resources/org/openremote/test/rules/ChildAssetControl.groovyindex a16f74b73..a1b46cf1a 100644--- a/test/src/test/resources/org/openremote/test/rules/ChildAssetControl.groovy+++ b/test/src/test/resources/org/openremote/test/rules/ChildAssetControl.groovy@@ -8,7 +8,7 @@ package org.openremote.setup.integration.rulesimport org.openremote.manager.rules.RulesBuilderimport org.openremote.model.attribute.AttributeEventimport org.openremote.model.query.AssetQuery-import org.openremote.model.rules.AssetState+import org.openremote.model.attribute.AttributeInfoimport org.openremote.model.rules.Assetsimport org.openremote.model.rules.Notificationsimport org.openremote.model.rules.Users@@ -29,7 +29,7 @@ rules.add().when({ facts ->- List<AssetState<?>> changes = Collections.synchronizedList(new ArrayList<>())+ List<AttributeInfo> changes = Collections.synchronizedList(new ArrayList<>())def childUpdates = facts.matchAssetState(new AssetQuery().ids(parentAssetId)@@ -37,7 +37,7 @@ rules.add()def changed = false// Get previous state from facts- def previous = facts.matchFirst(state.id + state.name) as Optional<AssetState<?>>+ def previous = facts.matchFirst(state.id + state.name) as Optional<AttributeInfo>if (previous.isEmpty()) {// Store initial state to avoid triggering on first run@@ -73,7 +73,7 @@ rules.add()}).then({ facts ->- def changes = facts.bound("changes") as List<AssetState<?>>+ def changes = facts.bound("changes") as List<AttributeInfo>def updates = facts.bound("updates") as List<AttributeEvent>// Create fact for state of attributediff --git a/test/src/test/resources/org/openremote/test/rules/ResidencePresenceDetection.groovy b/test/src/test/resources/org/openremote/test/rules/ResidencePresenceDetection.groovyindex 088150d41..b11246ad7 100644--- a/test/src/test/resources/org/openremote/test/rules/ResidencePresenceDetection.groovy+++ b/test/src/test/resources/org/openremote/test/rules/ResidencePresenceDetection.groovy@@ -13,7 +13,7 @@ import org.openremote.manager.rules.RulesBuilderimport org.openremote.model.asset.impl.BuildingAssetimport org.openremote.model.asset.impl.RoomAssetimport org.openremote.model.query.AssetQuery-import org.openremote.model.rules.AssetState+import org.openremote.model.attribute.AttributeInfoimport org.openremote.model.util.Pairimport java.time.temporal.ChronoUnit@@ -135,7 +135,7 @@ rules.add()!it.value.isPresent() || it.value.timestamp}).map { outdatedLastPresenceDetected ->// keep the room and the new timestamp- new Pair<AssetState, Double>(outdatedLastPresenceDetected, roomWithMotionSensorTriggered.timestamp)+ new Pair<AttributeInfo, Double>(outdatedLastPresenceDetected, roomWithMotionSensorTriggered.timestamp)}}}.findFirst().map { pair ->diff --git a/ui/app/manager/src/pages/page-map.ts b/ui/app/manager/src/pages/page-map.tsindex 719ff280c..7b2a63595 100644--- a/ui/app/manager/src/pages/page-map.ts+++ b/ui/app/manager/src/pages/page-map.ts@@ -68,8 +68,8 @@ const pageMapSlice = createSlice({const assets = state.assets;const attrsOfInterest = action.payload[0];const attrEvent = action.payload[1];- const attrName = attrEvent.attributeState.ref.name;- const assetId = attrEvent.attributeState.ref.id;+ const attrName = attrEvent.ref.name;+ const assetId = attrEvent.ref.id;const index = assets.findIndex((asst) => asst.id === assetId);const asset = index >= 0 ? assets[index] : null;@@ -77,7 +77,7 @@ const pageMapSlice = createSlice({return state;}- if (attrName === WellknownAttributes.LOCATION && attrEvent.attributeState.deleted) {+ if (attrName === WellknownAttributes.LOCATION && attrEvent.deleted) {return {...state,assets: [...assets.splice(index, 1)]diff --git a/ui/component/core/src/event.ts b/ui/component/core/src/event.tsindex 013efd501..f789c9597 100644--- a/ui/component/core/src/event.ts+++ b/ui/component/core/src/event.ts@@ -394,7 +394,7 @@ abstract class EventProviderImpl implements EventProvider {// Build a filter to only respond to the callback for the requested attributesconst eventFilter = (e: AttributeEvent) => {- const eventRef = e.attributeState!.ref!;+ const eventRef = e.ref!;if (isAttributeRef) {(ids as AttributeRef[]).forEach((ref: AttributeRef) => {@@ -446,12 +446,10 @@ abstract class EventProviderImpl implements EventProvider {eventFilter({eventType: "attribute",timestamp: attr.timestamp,- attributeState: {- value: attr.value,- ref: {- id: asset.id,- name: attributeName- }+ value: attr.value,+ ref: {+ id: asset.id,+ name: attributeName}});}diff --git a/ui/component/core/src/util.ts b/ui/component/core/src/util.tsindex bb6b30318..f5ef193be 100644--- a/ui/component/core/src/util.ts+++ b/ui/component/core/src/util.ts@@ -938,15 +938,15 @@ function doStandardTranslationLookup(lookup: WellknownMetaItems.LABEL | Wellknow*/export function updateAsset(asset: Asset, event: AttributeEvent): Asset {- const attributeName = event.attributeState!.ref!.name!;+ const attributeName = event.ref!.name!;if (asset.attributes) {- if (event.attributeState!.deleted) {+ if (event.deleted) {delete asset.attributes![attributeName];} else {const attribute = asset.attributes[attributeName];if (attribute) {- attribute.value = event.attributeState!.value;+ attribute.value = event.value;attribute.timestamp = event.timestamp;}}diff --git a/ui/component/model/.gitignore b/ui/component/model/.gitignoreindex b1d8d3c24..4279df1d8 100644--- a/ui/component/model/.gitignore+++ b/ui/component/model/.gitignore@@ -1 +1,2 @@src/model.ts+src/typescript-generator-info.jsondiff --git a/ui/component/model/build.gradle b/ui/component/model/build.gradleindex f98a98922..c991d5b50 100644--- a/ui/component/model/build.gradle+++ b/ui/component/model/build.gradle@@ -1,9 +1,3 @@-import com.fasterxml.jackson.annotation.JsonAutoDetect-import cz.habarta.typescript.generator.Jackson2Configuration--apply plugin: "groovy"-apply plugin: "cz.habarta.typescript-generator"-buildscript {repositories {mavenCentral()@@ -14,13 +8,13 @@ buildscript {}}+plugins {+ id 'groovy'+ id 'cz.habarta.typescript-generator'+}+dependencies {- if (isCustomProject()) {- def customModel = findProject(":model")- if (customModel != null) {- implementation customModel- }- }+ compileOnly findProject(":model-util")implementation resolveProject(":model")implementation resolveProject(":agent")implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"@@ -29,50 +23,7 @@ dependencies {implementation "cz.habarta.typescript-generator:typescript-generator-core:$typescriptGeneratorVersion"}-generateTypeScript {- jsonLibrary = "jackson2"- classPatterns = [- "org.openremote.model.**"- ]- excludeClassPatterns = [- "org.openremote.model.event.shared.*Filter**",- "org.openremote.model.util.**",- "org.openremote.model.flow.**",- "java.io.**",- "org.hibernate.**",- "jakarta.**"- ]- customTypeProcessor = "CustomTypeProcessor"- customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf(\"\$\") > 0) return name.substr(name.lastIndexOf(\".\")+1).replace(\"\$\",\"\"); }"- customTypeMappings = [- "com.fasterxml.jackson.databind.node.ObjectNode:{ [id: string]: any }",- "java.lang.Class<T>:string",- "org.openremote.model.attribute.MetaItem<T>:any"- ]- optionalProperties = "all" // TODO: cleanup model to be more explicit about optional params- mapEnum = cz.habarta.typescript.generator.EnumMapping.asEnum- mapDate = cz.habarta.typescript.generator.DateMapping.asNumber- outputFile = "src/model.ts"- outputFileType = "implementationFile"- outputKind = "module"- jackson2Configuration = [- fieldVisibility: "ANY",- creatorVisibility: "ANY",- getterVisibility: "NONE",- isGetterVisibility: "NONE",- setterVisibility: "NONE"- ]- jackson2Modules = [- "com.fasterxml.jackson.datatype.jdk8.Jdk8Module",- "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule",- "com.fasterxml.jackson.module.paramnames.ParameterNamesModule"- ]- extensions = [- "org.openremote.model.util.AssetModelInfoExtension",- "CustomExtension",- "JsonSerializeExtension"- ]-}+generateTypeScript createTSGeneratorConfigForModel("src/model.ts")build.dependsOn generateTypeScript, npmBuildnpmBuild.dependsOn generateTypeScriptdiff --git a/ui/component/or-asset-viewer/src/index.ts b/ui/component/or-asset-viewer/src/index.tsindex 4404e5015..7db3a0264 100644--- a/ui/component/or-asset-viewer/src/index.ts+++ b/ui/component/or-asset-viewer/src/index.ts@@ -1514,7 +1514,7 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem_onEvent(event: SharedEvent) {const assetId = this.ids && this.ids.length > 0 ? this.ids[0] : undefined;- const processEvent = (event.eventType === "asset" && (event as AssetEvent).asset!.id === assetId) || (event.eventType === "attribute" && (event as AttributeEvent).attributeState!.ref!.id == assetId);+ const processEvent = (event.eventType === "asset" && (event as AssetEvent).asset!.id === assetId) || (event.eventType === "attribute" && (event as AttributeEvent).ref!.id == assetId);if (!processEvent) {return;@@ -1580,7 +1580,7 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem// Inject the attribute as we don't subscribe to events from individual attribute inputsconst attributeEvent = event as AttributeEvent;- const attrName = attributeEvent.attributeState!.ref!.name!;+ const attrName = attributeEvent.ref!.name!;if (asset && asset.attributes && asset.attributes[attrName]) {@@ -1589,7 +1589,7 @@ export class OrAssetViewer extends subscribe(manager)(translate(i18next)(LitElem// Update attribute within the assetconst attr = {...asset.attributes[attrName]};- attr.value = attributeEvent.attributeState!.value;+ attr.value = attributeEvent.value;attr.timestamp = attributeEvent.timestamp;asset.attributes[attrName] = attr;diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.tsindex c99fd3548..57f840393 100644--- a/ui/component/or-attribute-card/src/index.ts+++ b/ui/component/or-attribute-card/src/index.ts@@ -737,7 +737,7 @@ export class OrAttributeCard extends LitElement {} as ReadAttributeEvent});- this.mainValue = currentValue.attributeState!.value;+ this.mainValue = currentValue.value;this.formattedMainValue = this.getFormattedValue(this.mainValue!);const response = await manager.rest.api.AssetDatapointResource.getDatapoints(diff --git a/ui/component/or-attribute-input/src/index.ts b/ui/component/or-attribute-input/src/index.tsindex 289bf0915..0342c53ee 100644--- a/ui/component/or-attribute-input/src/index.ts+++ b/ui/component/or-attribute-input/src/index.ts@@ -665,7 +665,7 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitE}protected getValue(): any {- return this._attributeEvent ? this._attributeEvent.attributeState!.value : this.attribute ? this.attribute.value : this.value;+ return this._attributeEvent ? this._attributeEvent.value : this.attribute ? this.attribute.value : this.value;}protected getTimestamp(): number | undefined {@@ -682,7 +682,7 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitEconst oldValue = this.getValue();this._attributeEvent = event as AttributeEvent;- this._onAttributeValueChanged(oldValue, this._attributeEvent.attributeState!.value, event.timestamp);+ this._onAttributeValueChanged(oldValue, this._attributeEvent.value, event.timestamp);}public checkValidity(): boolean {@@ -741,10 +741,8 @@ export class OrAttributeInput extends subscribe(manager)(translate(i18next)(LitEsuper._sendEvent({eventType: "attribute",- attributeState: {- ref: attributeRef,- value: newValue- }+ ref: attributeRef,+ value: newValue} as AttributeEvent);this._writeTimeoutHandler = window.setTimeout(() => this._onWriteTimeout(), this.writeTimeout);diff --git a/ui/component/or-map/src/markers/or-map-marker-asset.ts b/ui/component/or-map/src/markers/or-map-marker-asset.tsindex b7a47231a..78fa39e56 100644--- a/ui/component/or-map/src/markers/or-map-marker-asset.ts+++ b/ui/component/or-map/src/markers/or-map-marker-asset.ts@@ -146,8 +146,8 @@ export class OrMapMarkerAsset extends subscribe(manager)(OrMapMarker) {if (event.eventType === "attribute") {const attributeEvent = event as AttributeEvent;- if (attributeEvent.attributeState!.ref!.name === WellknownAttributes.LOCATION) {- this._updateLocation(attributeEvent.attributeState!.value as GeoJSONPoint);+ if (attributeEvent.ref!.name === WellknownAttributes.LOCATION) {+ this._updateLocation(attributeEvent.value as GeoJSONPoint);return;}diff --git a/ui/component/or-rules/src/index.ts b/ui/component/or-rules/src/index.tsindex 48d2fbd02..b74845996 100644--- a/ui/component/or-rules/src/index.ts+++ b/ui/component/or-rules/src/index.ts@@ -198,7 +198,7 @@ export const RuleViewInfoMap: {[key in RulesetLang]: RuleViewInfo} = {"\n" +"import org.openremote.manager.rules.RulesBuilder\n" +"import org.openremote.model.notification.*\n" +- "import org.openremote.model.rules.AssetState\n" ++ "import org.openremote.model.attribute.AttributeInfo\n" +"import org.openremote.model.asset.Asset\n" +"import org.openremote.model.asset.impl.*\n" +"import org.openremote.model.query.*\n" +@@ -292,7 +292,7 @@ export const RuleViewInfoMap: {[key in RulesetLang]: RuleViewInfo} = {" facts ->\n" +"\n" +" // Extract any binded facts\n" +- " AssetState assetState = facts.bound(\"assetState\")\n" ++ " AttributeInfo assetState = facts.bound(\"assetState\")\n" +"\n" +" // Insert the custom fact to prevent rule loop\n" +" facts.put(\"someAttribute\", assetState.getTimestamp())\n" +diff --git a/ui/component/or-thermostat/src/index.ts b/ui/component/or-thermostat/src/index.tsindex 82645ab13..050b62ef2 100644--- a/ui/component/or-thermostat/src/index.ts+++ b/ui/component/or-thermostat/src/index.ts@@ -29,13 +29,13 @@ class OrThermostat extends LitElement {let callback: (event: AttributeEvent) => void = (event) => {console.log("Event Received:" + JSON.stringify(event, null, 2));- if (event.attributeState- && event.attributeState.ref!.attributeName === "targetTemperature") {- this.targetTemperature = event.attributeState!.value;+ if (event+ && event.ref!.attributeName === "targetTemperature") {+ this.targetTemperature = event.value;}- if (event.attributeState- && event.attributeState.ref!.attributeName === "currentTemperature") {- this.currentTemperature = event.attributeState!.value;+ if (event+ && event.ref!.attributeName === "currentTemperature") {+ this.currentTemperature = event.value;}};diff --git a/ui/component/rest/build.gradle b/ui/component/rest/build.gradleindex e8059dd54..6367c2a41 100644--- a/ui/component/rest/build.gradle+++ b/ui/component/rest/build.gradle@@ -1,6 +1,3 @@-apply plugin: "groovy"-apply plugin: "cz.habarta.typescript-generator"-buildscript {repositories {mavenCentral()@@ -11,105 +8,26 @@ buildscript {}}+plugins {+ id 'groovy'+ id 'cz.habarta.typescript-generator'+}+dependencies {- if (isCustomProject()) {- def customModel = findProject(":model")- if (customModel != null) {- implementation customModel- }- }+ compileOnly findProject(":model-util")implementation resolveProject(":model")- implementation resolveProject(":ui:component:model")+ implementation resolveProject(":agent")implementation "cz.habarta.typescript-generator:typescript-generator-core:$typescriptGeneratorVersion"implementation "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"implementation "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:$jacksonVersion"implementation "com.fasterxml.jackson.module:jackson-module-parameter-names:$jacksonVersion"}-task generateTypescriptModelInfo(type: cz.habarta.typescript.generator.gradle.GenerateTask) {- jsonLibrary = "jackson2"- classPatterns = [- "org.openremote.model.**"- ]- outputKind = "module"- generateInfoJson = true- excludeClassPatterns = [- "org.openremote.model.event.shared.*Filter**",- "org.openremote.model.util.**",- "org.openremote.model.flow.**",- "java.io.**",- "java.lang.**",- "org.hibernate.**",- "jakarta.**"- ]- customTypeProcessor = "CustomTypeProcessor"- customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf(\"\$\") > 0) return name.substr(name.lastIndexOf(\".\")+1).replace(\"\$\",\"\"); }"- customTypeMappings = [- "com.fasterxml.jackson.databind.node.ObjectNode:{ [id: string]: any }",- "java.lang.Class<T>:string"- ]- optionalProperties = "all" // TODO: cleanup model to be more explicit about optional params- mapEnum = cz.habarta.typescript.generator.EnumMapping.asEnum- mapDate = cz.habarta.typescript.generator.DateMapping.asNumber- jackson2Modules = [- "com.fasterxml.jackson.datatype.jdk8.Jdk8Module",- "com.fasterxml.jackson.datatype.jsr310.JavaTimeModule",- "com.fasterxml.jackson.module.paramnames.ParameterNamesModule"- ]- extensions = [- "CustomExtension",- "JsonSerializeExtension"- ]-}+project.task(Collections.singletonMap(Task.TASK_TYPE, cz.habarta.typescript.generator.gradle.GenerateTask), "generateTypescriptModelInfo")+generateTypescriptModelInfo createTSGeneratorConfigForModel("$buildDir/model.ts")-generateTypeScript {- dependsOn generateTypescriptModelInfo- jsonLibrary = "jackson2"- customTypeNamingFunction = "function(name, simpleName) { if (name.indexOf(\"\$\") > 0) return name.substr(name.lastIndexOf(\".\")+1).replace(\"\$\",\"\"); }"- customTypeMappings = [- "com.fasterxml.jackson.databind.node.ObjectNode:{ [id: string]: any }",- "java.lang.Class<T>:string",- "org.openremote.model.asset.Asset<T>:Model.Asset",- "org.openremote.model.asset.AssetDescriptor<T>:Model.AssetDescriptor",- "org.openremote.model.asset.agent.Agent<T,U,V>:Model.Agent",- "org.openremote.model.asset.agent.AgentDescriptor<T,U,V>:Model.AgentDescriptor",- "org.openremote.model.value.MetaItemDescriptor<T>:Model.MetaItemDescriptor",- "org.openremote.model.value.ValueDescriptor<T>:Model.ValueDescriptor"- ]- classPatterns = [- "org.openremote.model.**Resource"- ]- excludeClassPatterns = [- "org.openremote.model.event.shared.*Filter**",- "org.openremote.model.util.**",- "org.openremote.model.flow.**",- "java.io.**",- "java.lang.**",- "org.hibernate.**",- "jakarta.**"- ]- moduleDependencies = [- cz.habarta.typescript.generator.ModuleDependency.module(- "@openremote/model",- "Model",- new File("$buildDir/typescript-generator/typescript-generator-info.json"),- (String)null,- (String)null- )- ]- outputFile = "src/restclient.ts"- outputFileType = "implementationFile"- outputKind = "module"- mapEnum = cz.habarta.typescript.generator.EnumMapping.asEnum- mapDate = cz.habarta.typescript.generator.DateMapping.asNumber- restNamespacing = "perResource"- extensions = [- "CustomExtension",- "JsonSerializeExtension",- "AggregatedApiClient",- "cz.habarta.typescript.generator.ext.AxiosClientExtension"- ]-}+generateTypeScript createTSGeneratorConfigForClient("src/restclient.ts")+generateTypeScript.dependsOn generateTypescriptModelInfoclean {doLast {diff --git a/ui/demo/demo-core/src/index.ts b/ui/demo/demo-core/src/index.tsindex 436218089..ed8ef1e35 100644--- a/ui/demo/demo-core/src/index.ts+++ b/ui/demo/demo-core/src/index.ts@@ -106,8 +106,8 @@ class OrDemo extends subscribe(manager)(LitElement) {if (event.eventType === "attribute") {const attributeEvent = event as AttributeEvent;- if (attributeEvent.attributeState && attributeEvent.attributeState.ref!.name === "alarmEnabled") {- this.alarmEnabled = attributeEvent.attributeState!.value;+ if (attributeEvent && attributeEvent.ref!.name === "alarmEnabled") {+ this.alarmEnabled = attributeEvent.value;}}}