This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-jbang-examples.git


The following commit(s) were added to refs/heads/main by this push:
     new cbc6595  connect-service/iot-telemetry - averages per time window, one 
alert per room per window, and offline sensors over MQTT (#153)
cbc6595 is described below

commit cbc6595870068fedd5bb027191469435dfd2b396
Author: Claus Ibsen <[email protected]>
AuthorDate: Mon Nov 30 07:06:03 2026 +0100

    connect-service/iot-telemetry - averages per time window, one alert per 
room per window, and offline sensors over MQTT (#153)
    
    Closes #152
    
    
    Claude-Session: https://claude.ai/code/session_016Y4dDi5iQNgtHrDdz2NpwF
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .github/workflows/build.yml                        |   2 +
 README.md                                          |   1 +
 camel-jbang-example-catalog.json                   |  54 +++++++++++
 connect-service/README.md                          |   1 +
 connect-service/iot-telemetry/README.md            | 103 ++++++++++++++++++++
 .../iot-telemetry/application.properties           |  25 +++++
 .../iot-telemetry/iot-telemetry.camel.yaml         | 104 +++++++++++++++++++++
 connect-service/iot-telemetry/metadata.json        |  46 +++++++++
 connect-service/iot-telemetry/sensors.camel.yaml   |  54 +++++++++++
 .../test/iot-telemetry.citrus.it.yaml              |  49 ++++++++++
 10 files changed, 439 insertions(+)

diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index dfbba05..79146c3 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -47,6 +47,8 @@ jobs:
             path: connect-service/sql
           - name: MQTT sensors
             path: connect-service/mqtt
+          - name: IoT telemetry
+            path: connect-service/iot-telemetry
           - name: Kafka orders
             path: connect-service/kafka-orders
           - name: Artemis queue
diff --git a/README.md b/README.md
index 46c1eb0..0b2dcfe 100644
--- a/README.md
+++ b/README.md
@@ -115,6 +115,7 @@ SQL, JMS, MQTT, Kafka and FTP against a service the Camel 
CLI starts for you wit
 | [Kafka orders](connect-service/kafka-orders/) | The three orders go through 
a shared validation route to a Kafka topic on a broker started with camel 
infra; the pending one is stopped, the paid ones are dispatched to the 
fulfilment and notifications topics, whose consumers log the picking and the 
email; camel cmd route-topology draws the flow. | `camel infra run kafka` |
 | [FTP courier](connect-service/ftp/) | Each order on the shipments queue of 
an Artemis broker becomes a file on the courier's FTP server, both started with 
camel infra; the log shows each shipment uploaded and camel infra get ftp shows 
where the files landed. | `camel infra run artemis ftp` |
 | [Camel 1.0 tribute](connect-service/camel-1-tribute/) | The very first Camel 
example of 2007, JMS to file: a timer sends ten messages to a queue on an 
Artemis broker started with camel infra, and the consumer writes each message 
to a file in outbox; the log shows both sides. | `camel infra run artemis` |
+| [IoT telemetry](connect-service/iot-telemetry/) | Three simulated cold-room 
sensors publish a reading every second to a Mosquitto broker started with camel 
infra; the shop subscribes with a wildcard topic and logs an average per room 
every ten seconds with an aggregator, at most one alert per room per window 
with a throttle per room, and a warning when a sensor goes silent with an 
aggregator completion timeout. | `camel infra run mosquitto` |
 
 ### [Contracts and security](contracts/)
 
diff --git a/camel-jbang-example-catalog.json b/camel-jbang-example-catalog.json
index b2bfcc9..c31f5db 100644
--- a/camel-jbang-example-catalog.json
+++ b/camel-jbang-example-catalog.json
@@ -457,6 +457,60 @@
         ],
         "order": 6
     },
+    {
+        "name": "connect-service/iot-telemetry",
+        "title": "IoT telemetry",
+        "description": "Three simulated cold-room sensors publish a reading 
every second to a Mosquitto broker started with camel infra; the shop 
subscribes with a wildcard topic and logs an average per room every ten seconds 
with an aggregator, at most one alert per room per window with a throttle per 
room, and a warning when a sensor goes silent with an aggregator completion 
timeout.",
+        "level": "connect-service",
+        "teaches": {
+            "components": [
+                "paho-mqtt5",
+                "timer",
+                "direct",
+                "log"
+            ],
+            "eips": [
+                "multicast",
+                "aggregate",
+                "throttle",
+                "filter",
+                "doTry",
+                "stop",
+                "setVariable",
+                "setBody",
+                "unmarshal"
+            ],
+            "languages": [
+                "simple",
+                "groovy"
+            ],
+            "dataformats": [
+                "json"
+            ]
+        },
+        "tags": [
+            "shop",
+            "mqtt",
+            "iot",
+            "telemetry",
+            "time-window",
+            "throttle",
+            "messaging"
+        ],
+        "bundled": false,
+        "requiresDocker": true,
+        "hasCitrusTests": true,
+        "files": [
+            "README.md",
+            "application.properties",
+            "iot-telemetry.camel.yaml",
+            "sensors.camel.yaml"
+        ],
+        "infraServices": [
+            "mosquitto"
+        ],
+        "order": 8
+    },
     {
         "name": "connect-service/jms-request-reply",
         "title": "JMS request-reply",
diff --git a/connect-service/README.md b/connect-service/README.md
index 83d71ab..2775a81 100644
--- a/connect-service/README.md
+++ b/connect-service/README.md
@@ -12,6 +12,7 @@ SQL, JMS, MQTT, Kafka and FTP against a service the Camel CLI 
starts for you wit
 | [Kafka orders](kafka-orders/) | The three orders go through a shared 
validation route to a Kafka topic on a broker started with camel infra; the 
pending one is stopped, the paid ones are dispatched to the fulfilment and 
notifications topics, whose consumers log the picking and the email; camel cmd 
route-topology draws the flow. | `camel infra run kafka` |
 | [FTP courier](ftp/) | Each order on the shipments queue of an Artemis broker 
becomes a file on the courier's FTP server, both started with camel infra; the 
log shows each shipment uploaded and camel infra get ftp shows where the files 
landed. | `camel infra run artemis ftp` |
 | [Camel 1.0 tribute](camel-1-tribute/) | The very first Camel example of 
2007, JMS to file: a timer sends ten messages to a queue on an Artemis broker 
started with camel infra, and the consumer writes each message to a file in 
outbox; the log shows both sides. | `camel infra run artemis` |
+| [IoT telemetry](iot-telemetry/) | Three simulated cold-room sensors publish 
a reading every second to a Mosquitto broker started with camel infra; the shop 
subscribes with a wildcard topic and logs an average per room every ten seconds 
with an aggregator, at most one alert per room per window with a throttle per 
room, and a warning when a sensor goes silent with an aggregator completion 
timeout. | `camel infra run mosquitto` |
 
 Start with [SQL database](sql/); the examples read best in the order above, 
each one building on what the one before it set up.
 
diff --git a/connect-service/iot-telemetry/README.md 
b/connect-service/iot-telemetry/README.md
new file mode 100644
index 0000000..3128fb3
--- /dev/null
+++ b/connect-service/iot-telemetry/README.md
@@ -0,0 +1,103 @@
+# IoT telemetry
+
+The sensors in the warehouse's cold rooms publish a temperature reading every 
second over MQTT. The shop logs an
+average per room for each ten-second window, sends at most one alert per room 
per window when a room is too warm,
+and warns when a sensor goes silent.
+
+## What you will see
+
+```text
+WARN ... iot-telemetry.camel.yaml:77 : cold-room-3: 10 °C, too warm, alert the 
warehouse
+INFO ... iot-telemetry.camel.yaml:49 : cold-room-3: average 10.0 °C over 8 
readings
+INFO ... iot-telemetry.camel.yaml:49 : cold-room-1: average 4.0 °C over 8 
readings
+INFO ... iot-telemetry.camel.yaml:49 : cold-room-2: average 5.0 °C over 5 
readings
+WARN ... iot-telemetry.camel.yaml:77 : cold-room-3: 10 °C, too warm, alert the 
warehouse
+WARN ... iot-telemetry.camel.yaml:102 : cold-room-2: no reading for 10000 ms, 
the sensor is offline
+INFO ... iot-telemetry.camel.yaml:49 : cold-room-3: average 10.0 °C over 10 
readings
+INFO ... iot-telemetry.camel.yaml:49 : cold-room-1: average 4.0 °C over 10 
readings
+WARN ... iot-telemetry.camel.yaml:77 : cold-room-3: 10 °C, too warm, alert the 
warehouse
+...
+```
+
+The first window is shorter, as the sensors start during it. cold-room-3 sends 
ten warm readings per window but gets
+one alert, and cold-room-2 stops after five readings.
+
+## Install Camel CLI
+
+Install [JBang](https://www.jbang.dev/download/) and the Camel CLI as 
described in the
+[root README](../../README.md#install-the-camel-cli); `camel --version` 
confirms the install.
+
+## Run it
+
+Start a Mosquitto MQTT broker on port 1883, in a terminal of its own:
+
+```shell
+camel infra run mosquitto
+```
+
+Then run the example:
+
+```shell
+camel run *
+```
+
+This runs the shop's routes and the three simulated sensors in one app. Stop 
the example with `ctrl` + `c`, and the
+broker with `camel infra stop mosquitto`.
+
+## How it works
+
+- `sensors.camel.yaml` plays the sensors: a `timer` per room publishes a JSON 
reading every second on
+  `warehouse/<room>/temperature`. cold-room-3 runs too warm, and cold-room-2 
stops after five readings
+  (`repeatCount: 5`), as a sensor with a flat battery would.
+- `iot-telemetry.camel.yaml` subscribes to `warehouse/+/temperature`: `+` 
matches any room. A `multicast` sends each
+  reading to three routes.
+- `average`: an `aggregate` per room (`correlationExpression` on the sensor) 
collects the readings and completes every
+  window (`completionInterval`). Groovy computes the average, formatted with 
`Locale.ROOT`, so it prints `10.0` on any
+  machine.
+- `alert`: a `filter` lets readings above `alert.threshold` through, and a 
`throttle` per room (`correlationExpression`)
+  lets one alert through per window. `rejectExecution: true` makes it reject 
the others instead of holding them back,
+  and the `doTry` drops them.
+- `offline`: a second `aggregate` per room completes only when no reading has 
come for `offline.millis`
+  (`completionTimeout`), so it fires for a room that went silent.
+- `application.properties` holds the broker's address, the alert threshold and 
the lengths of the window and the
+  offline timeout.
+
+## Build it step by step
+
+Ask your assistant, or type it yourself, one step at a time, and run after 
each:
+
+1. Sensor routes that publish a reading for three cold rooms every second on 
`warehouse/<room>/temperature`, and a
+   route that subscribes to all rooms and logs each reading.
+2. Log the average temperature per room every ten seconds: "cold-room-1: 
average 4.0 °C over 10 readings".
+3. Alert when a reading is above 8 degrees, but at most once per room per ten 
seconds.
+4. Log "cold-room-2: no reading for 10000 ms, the sensor is offline" when a 
room sends nothing for ten seconds.
+
+## Try changing
+
+- Remove `rejectExecution: true`: the throttle no longer drops the extra 
alerts but holds them back, and they come one
+  per window, later and later.
+- Set `offline.millis=500`: every room seems offline between two readings.
+- Remove `correlationExpression` from the throttle: then one alert per window 
is shared by all rooms, and a second
+  warm room would get no alert.
+
+## Integration testing
+
+The example comes with a test in the [Citrus](https://citrusframework.org/) 
YAML DSL,
+`test/iot-telemetry.citrus.it.yaml`, which the Camel CLI runs:
+
+```shell
+camel test run test/iot-telemetry.citrus.it.yaml
+```
+
+The test starts Mosquitto itself, runs the shop's routes and the sensors as 
two integrations, and verifies an
+alert, the averages and the offline sensor.
+
+## Help and contributions
+
+If you hit any problem using Camel or have some feedback, then please
+[let us know](https://camel.apache.org/community/support/).
+
+We also love contributors, so
+[get involved](https://camel.apache.org/community/contributing/) :-)
+
+The Camel riders!
diff --git a/connect-service/iot-telemetry/application.properties 
b/connect-service/iot-telemetry/application.properties
new file mode 100644
index 0000000..b163fa1
--- /dev/null
+++ b/connect-service/iot-telemetry/application.properties
@@ -0,0 +1,25 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+# the Mosquitto broker that camel infra run mosquitto starts
+mqtt.broker.url=tcp://localhost:1883
+# a reading above this, in degrees, raises an alert
+alert.threshold=8
+# the length of a time window, in milliseconds: one average and at most one 
alert per room per window
+window.millis=10000
+# a room that sends nothing for this long, in milliseconds, is offline
+offline.millis=10000
diff --git a/connect-service/iot-telemetry/iot-telemetry.camel.yaml 
b/connect-service/iot-telemetry/iot-telemetry.camel.yaml
new file mode 100644
index 0000000..aef6449
--- /dev/null
+++ b/connect-service/iot-telemetry/iot-telemetry.camel.yaml
@@ -0,0 +1,104 @@
+# The shop watches the cold rooms of its warehouse: an average per room for 
each time window, at most one alert per
+# room per window when a room is too warm, and a warning when a room goes 
silent.
+- route:
+    id: readings
+    from:
+      uri: paho-mqtt5
+      parameters:
+        # + matches any room
+        topic: warehouse/+/temperature
+        brokerUrl: "{{mqtt.broker.url}}"
+      steps:
+        - unmarshal:
+            json:
+              library: Jackson
+        - multicast:
+            steps:
+              - to:
+                  uri: direct:average
+              - to:
+                  uri: direct:alert
+              - to:
+                  uri: direct:offline
+
+# One average per room per window: the readings of a room are collected and 
completed every window
+- route:
+    id: average
+    from:
+      uri: direct
+      parameters:
+        name: average
+      steps:
+        - aggregate:
+            correlationExpression:
+              simple:
+                expression: "${body[sensor]}"
+            aggregationStrategy: 
"#class:org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy"
+            completionInterval: "{{window.millis}}"
+            steps:
+              - setVariable:
+                  name: sensor
+                  expression:
+                    simple:
+                      expression: "${body[0][sensor]}"
+              - setVariable:
+                  name: average
+                  expression:
+                    groovy:
+                      expression: "String.format(Locale.ROOT, '%.1f', 
body.collect { it.value }.sum() / body.size())"
+              - log:
+                  message: "${variable.sensor}: average ${variable.average} °C 
over ${body.size()} readings"
+
+# At most one alert per room per window: the throttle lets one reading through 
per room and rejects the others
+- route:
+    id: alert
+    from:
+      uri: direct
+      parameters:
+        name: alert
+      steps:
+        - filter:
+            expression:
+              simple:
+                expression: "${body[value]} > {{alert.threshold}}"
+            steps:
+              - doTry:
+                  steps:
+                    - throttle:
+                        expression:
+                          constant:
+                            expression: "1"
+                        mode: TotalRequests
+                        timePeriodMillis: "{{window.millis}}"
+                        correlationExpression:
+                          simple:
+                            expression: "${body[sensor]}"
+                        rejectExecution: true
+                    - log:
+                        loggingLevel: WARN
+                        message: "${body[sensor]}: ${body[value]} °C, too 
warm, alert the warehouse"
+                  doCatch:
+                    # the room got its alert in this window already
+                    - exception:
+                        - 
org.apache.camel.processor.ThrottlerRejectedExecutionException
+                      steps:
+                        - stop: {}
+
+# A room that sends nothing is offline: its group completes when no reading 
has come for a while
+- route:
+    id: offline
+    from:
+      uri: direct
+      parameters:
+        name: offline
+      steps:
+        - aggregate:
+            correlationExpression:
+              simple:
+                expression: "${body[sensor]}"
+            aggregationStrategy: 
"#class:org.apache.camel.processor.aggregate.UseLatestAggregationStrategy"
+            completionTimeout: "{{offline.millis}}"
+            steps:
+              - log:
+                  loggingLevel: WARN
+                  message: "${body[sensor]}: no reading for {{offline.millis}} 
ms, the sensor is offline"
diff --git a/connect-service/iot-telemetry/metadata.json 
b/connect-service/iot-telemetry/metadata.json
new file mode 100644
index 0000000..1e62afc
--- /dev/null
+++ b/connect-service/iot-telemetry/metadata.json
@@ -0,0 +1,46 @@
+{
+    "title": "IoT telemetry",
+    "description": "Three simulated cold-room sensors publish a reading every 
second to a Mosquitto broker started with camel infra; the shop subscribes with 
a wildcard topic and logs an average per room every ten seconds with an 
aggregator, at most one alert per room per window with a throttle per room, and 
a warning when a sensor goes silent with an aggregator completion timeout.",
+    "level": "connect-service",
+    "order": 8,
+    "teaches": {
+        "components": [
+            "paho-mqtt5",
+            "timer",
+            "direct",
+            "log"
+        ],
+        "eips": [
+            "multicast",
+            "aggregate",
+            "throttle",
+            "filter",
+            "doTry",
+            "stop",
+            "setVariable",
+            "setBody",
+            "unmarshal"
+        ],
+        "languages": [
+            "simple",
+            "groovy"
+        ],
+        "dataformats": [
+            "json"
+        ]
+    },
+    "tags": [
+        "shop",
+        "mqtt",
+        "iot",
+        "telemetry",
+        "time-window",
+        "throttle",
+        "messaging"
+    ],
+    "infraServices": [
+        "mosquitto"
+    ],
+    "requiresDocker": true,
+    "bundled": false
+}
diff --git a/connect-service/iot-telemetry/sensors.camel.yaml 
b/connect-service/iot-telemetry/sensors.camel.yaml
new file mode 100644
index 0000000..59f5ced
--- /dev/null
+++ b/connect-service/iot-telemetry/sensors.camel.yaml
@@ -0,0 +1,54 @@
+# Plays the sensors in the three cold rooms: each publishes a reading every 
second on warehouse/<room>/temperature.
+# cold-room-3 runs too warm, and cold-room-2 stops after five readings, as a 
sensor with a flat battery would.
+- route:
+    id: cold-room-1
+    from:
+      uri: timer
+      parameters:
+        timerName: cold-room-1
+        period: 1000
+      steps:
+        - setBody:
+            expression:
+              constant:
+                expression: '{"sensor": "cold-room-1", "value": 4}'
+        - to:
+            uri: paho-mqtt5
+            parameters:
+              topic: warehouse/cold-room-1/temperature
+              brokerUrl: "{{mqtt.broker.url}}"
+- route:
+    id: cold-room-2
+    from:
+      uri: timer
+      parameters:
+        timerName: cold-room-2
+        period: 1000
+        repeatCount: 5
+      steps:
+        - setBody:
+            expression:
+              constant:
+                expression: '{"sensor": "cold-room-2", "value": 5}'
+        - to:
+            uri: paho-mqtt5
+            parameters:
+              topic: warehouse/cold-room-2/temperature
+              brokerUrl: "{{mqtt.broker.url}}"
+- route:
+    id: cold-room-3
+    from:
+      uri: timer
+      parameters:
+        timerName: cold-room-3
+        period: 1000
+      steps:
+        - setBody:
+            expression:
+              constant:
+                expression: '{"sensor": "cold-room-3", "value": 10}'
+        - to:
+            uri: paho-mqtt5
+            parameters:
+              topic: warehouse/cold-room-3/temperature
+              brokerUrl: "{{mqtt.broker.url}}"
diff --git a/connect-service/iot-telemetry/test/iot-telemetry.citrus.it.yaml 
b/connect-service/iot-telemetry/test/iot-telemetry.citrus.it.yaml
new file mode 100644
index 0000000..170c744
--- /dev/null
+++ b/connect-service/iot-telemetry/test/iot-telemetry.citrus.it.yaml
@@ -0,0 +1,49 @@
+name: iot-telemetry-test
+description: The iot-telemetry example logs averages, alerts and an offline 
sensor, as its README shows
+actions:
+  - camel:
+      infra:
+        run:
+          service: mosquitto
+          fixedPort: true
+  - camel:
+      cli:
+        run:
+          integration:
+            name: "iot-telemetry"
+            file: "../iot-telemetry.camel.yaml"
+            systemProperties:
+              file: "../application.properties"
+  - camel:
+      cli:
+        verify:
+          integration: "iot-telemetry"
+          logMessage: "(iot-telemetry) started"
+  - camel:
+      cli:
+        run:
+          integration:
+            name: "sensors"
+            file: "../sensors.camel.yaml"
+            systemProperties:
+              file: "../application.properties"
+  - camel:
+      cli:
+        verify:
+          integration: "iot-telemetry"
+          logMessage: "cold-room-3: 10 °C, too warm, alert the warehouse"
+  - camel:
+      cli:
+        verify:
+          integration: "iot-telemetry"
+          logMessage: "cold-room-1: average 4.0 °C over"
+  - camel:
+      cli:
+        verify:
+          integration: "iot-telemetry"
+          logMessage: "cold-room-3: average 10.0 °C over"
+  - camel:
+      cli:
+        verify:
+          integration: "iot-telemetry"
+          logMessage: "cold-room-2: no reading for 10000 ms, the sensor is 
offline"

Reply via email to