Skip to content

Commit 8f1a11e

Browse files
committed
[#19] Enhance device communication API
* Add support for device states * Add support for request/response commands * Add support for ack-required command type and change configs to use ack-required type * Add retry mechanism for sending configs * Add automatic Pub/Sub tenant topic & subscription creation and deletion * Add automatically sending of a config in case a config is requested or a new one created * Add possibility to change log level * Update Quarkus dependency and migrated javax dependencies to jakarta * Improve SQL connection handling * Format and clean up device communication api code Signed-off-by: Matthias Kaemmer <[email protected]>
1 parent c048ec4 commit 8f1a11e

File tree

96 files changed

+5097
-1401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+5097
-1401
lines changed

device-communication/README.md

+95-60
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Device Communication API
22

33
Device communication API enables users and applications to send configurations and commands to devices via HTTP(S)
4-
endpoints.
4+
endpoints as well as retrieve states of devices.
55

66
![img.png](img.png)
77

88
### Application
99

10-
The application is reactive and uses Quarkus Framework for the application and Vertx tools for the HTTP server.
10+
The application is reactive and uses the Quarkus framework for the application and Vert.x tools for the HTTP server.
1111

1212
### Hono internal communication
1313

14-
API uses [Google's PubSub](https://cloud.google.com/pubsub/docs/overview?hl=de) service to communicate with the command
15-
router.
14+
The application uses [Google's PubSub](https://cloud.google.com/pubsub/docs/overview?hl=de) service to communicate with
15+
the command router.
1616

1717
## API endpoints
1818

@@ -32,32 +32,41 @@ router.
3232

3333
- POST: create a device config version
3434

35-
For more information please see resources/api/openApi file.
35+
For more information please see resources/api/hono-endpoint.yaml file.
3636

3737
## Pub/Sub - Internal Messaging
3838

39-
API communicates with hono components via the internal messaging interface (implemented from Google's PubSub).
40-
All the settings for the InternalMessaging component are in the application.yaml file. By publish/subscribe to a topic
41-
application sends or expects some message attributes.
39+
The application communicates with hono components via the internal messaging interface (implemented for Google PubSub).
40+
The settings for the internal messaging component can be found in the application.yaml file.
4241

4342
### Events
4443

45-
API will subscribe to all tenants' event topic at startup.
44+
The application subscribes to all tenants' event topic at startup to listen for config requests.
4645

47-
Expected message Attributes:
46+
#### MQTT config request (empty notification)
47+
48+
Expected message attributes:
4849

4950
- deviceId
5051
- tenantId
51-
- content-type
52+
- content-type (must be "application/vnd.eclipse-hono-empty-notification")
53+
- ttd (must be -1)
5254

53-
Application will <b>proceed only empty Notifications events (content-type is
54-
application/vnd.eclipse-hono-empty-notification)</b>.
55+
#### HTTP config request
56+
57+
Expected message attributes:
58+
59+
- deviceId
60+
- tenantId
61+
- ttd (must not be blank or empty)
62+
- orig_adapter (must be "hono-http")
63+
- orig_address (must contain "config")
5564

5665
### States
5766

58-
API will subscribe to all tenants' state topic at startup.
67+
The application subscribes to all tenants' state topic at startup.
5968

60-
Expected message Attributes:
69+
Expected message attributes:
6170

6271
- deviceId
6372
- tenantId
@@ -66,49 +75,71 @@ States are read only.
6675

6776
### Configs
6877

69-
Application will publish the latest device configuration when:
78+
The application publishes the latest device configuration when:
7079

71-
- an empty Notifications event was received
72-
- a new device config was created
80+
- An empty notification event is received (MQTT device subscribed to the command topic)
81+
- A config request from an HTTP device is received
82+
- A new device config is created
7383

74-
Message will be published with the following attributes:
84+
Message attributes:
7585

7686
- deviceId
7787
- tenantId
78-
- config-version
88+
- correlation-id (the config version)
89+
- subject (always set to "config")
90+
- ack-required (always set to true)
91+
92+
Body:
7993

80-
The Body will be a JSON object with the device config object.
94+
A JSON object with the device config object.
8195

82-
After publishing device configs, application subscribes to config_response topic and waits for the device to ack the
83-
configs.
96+
After publishing a device config, the application waits internally for an acknowledgement from the device on the
97+
command_response topic to update the "device_ack_time" in the database.
8498

8599
### Config ACK
86100

87101
Expected message attributes:
88102

89103
- deviceId
90104
- tenantId
91-
- configVersion (the config version received from device)
92-
93-
If configVersion is not set, application will ack always the latest config.
105+
- correlation-id (the config version)
106+
- content-type (must be "application/vnd.eclipse-hono-delivery-success-notification+json")
107+
- subject (must be "config")
94108

95109
### Commands
96110

97-
A command will be published from API to the command topic.
111+
A command will be published from the application to the command topic.
98112

99113
Attributes:
100114

101115
- deviceId
102116
- tenantId
103-
- subject (always set to "command")
117+
- subject (if not specified set to "command")
118+
- response-required (optional)
119+
- ack-required (optional)
120+
- correlation-id (optional)
121+
- timeout (optional)
104122

105123
Body:
106124

107125
The command as string.
108126

127+
If ack-required is set to "true", the request will wait for the duration of the specified timeout (in milliseconds)
128+
for an acknowledgement from the device on the command_response topic.
129+
130+
### Command ACK
131+
132+
Expected message attributes:
133+
134+
- deviceId
135+
- tenantId
136+
- correlation-id
137+
- content-type (must be "application/vnd.eclipse-hono-delivery-success-notification+json")
138+
- subject (must not be "config")
139+
109140
## Database
110141

111-
Application uses PostgresSQL database. All the database configurations can be found in application.yaml file.
142+
The application uses a PostgreSQL database. All the database configurations can be found in the application.yaml file.
112143

113144
### Tables
114145

@@ -121,29 +152,29 @@ Application uses PostgresSQL database. All the database configurations can be fo
121152

122153
### Migrations
123154

124-
When Applications starts tables will be created by the DatabaseSchemaCreator service.
155+
When the application starts the tables will be created by the DatabaseSchemaCreator service.
125156

126-
### Running postgresSQL container local
157+
### Running PostgresSQL container locally
127158

128-
For running the PostgresSQL Database local with docker run:
159+
The docker run command for running the PostgreSQL database locally:
129160

130161
``````
131162
132163
docker run -p 5432:5432 --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
133164
134165
``````
135166

136-
After the container is running, log in to the container and with psql create the database. Then we have
137-
to set the application settings.
167+
After the container is running, log in to the container and with psql create the database. Then we have to set the
168+
application settings.
138169

139-
Default postgresSQl values:
170+
Default PostgreSQl values:
140171

141172
- userName = postgres
142173
- password = mysecretpassword
143174

144-
## Build and Push API Docker Image
175+
## Build and push the application's Docker image
145176

146-
Mavens auto build and push functionality can be enabled from application.yaml settings:
177+
Mavens auto build and push functionality can be enabled in the application.yaml settings:
147178

148179
````
149180
quarkus:
@@ -154,45 +185,49 @@ quarkus:
154185
image: "eclipse/hono-device-communication"
155186
````
156187

157-
By running maven package, install or deploy will automatically build the docker image and if push is enabled it will
158-
push the image
159-
to the given registry.
188+
By running maven package, install or deploy the docker image will automatically be built and if push is enabled it will
189+
push the image to the registry specified in the image path.
160190

161191
## OpenApi Contract-first
162192

163-
For creating the endpoints, Vertx takes the openApi definition file and maps every endpoint operation-ID with a specific
164-
Handler
165-
function.
193+
For creating the endpoints, Vert.x takes the openApi definition file and maps every endpoint operation-ID with a
194+
specific handler function.
166195

167196
## Handlers
168197

169-
Handlers are providing callBack functions for every endpoint. Functions are going to be called automatically from vertx
170-
server every time a request is received.
198+
Handlers are providing callBack functions for every endpoint. Functions are going to be called automatically from the
199+
Vert.x server every time a request is received.
171200

172-
## Adding a new Endpoint
201+
## Adding a new endpoint
173202

174-
Adding new Endpoint steps:
203+
Adding new endpoint steps:
175204

176-
1. Add Endpoint in openApi file and set an operationId
177-
2. Use an existing const Class or create a new one under /config and set the operation id name
178-
3. Implement an HttpEndpointHandler and set the Routes
205+
1. Add endpoint in openApi (hono-device-communication-v1.yaml) and swagger (hono-endpoint.yaml) file and set an
206+
operationId in the openApi file
207+
2. Depending on the endpoint, create a new HttpEndpointHandler or use an existing one (in case it's a new one, add it to
208+
the availableHandlerServices within the VertxHttpHandlerManagerService)
209+
3. Implement the method and set the routes within the handler
179210

180211
## PubSub Events
181212

182-
Application subscribes and uses to the following topics:
213+
The application publishes and subscribes to the following topics:
183214

184-
1. TENANT_ID.command
185-
2. TENANT_ID.command_response
186-
3. TENANT_ID.event
187-
4. TENANT_ID.event.state
188-
5. registry-tenant.notification
215+
publish:
216+
* TENANT_ID.command
189217

190-
## Automatically create PubSub topics and subscriptions
218+
subscribe:
219+
* TENANT_ID.command_response
220+
* TENANT_ID.event
221+
* TENANT_ID.event.state
222+
* registry-tenant.notification
191223

192-
Application creates all tenants topics and subscriptions when:
224+
## Automatically create and delete PubSub topics and subscriptions
193225

194-
1. Application starts if are not exist
195-
2. New tenant is created
226+
The application creates tenant topics and subscriptions when:
196227

228+
* The application starts, if they do not exist already
229+
* A new tenant is created
197230

231+
The application deletes tenant topics and subscriptions when:
198232

233+
* A tenant is deleted

device-communication/pom.xml

+17-9
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1414
<quarkus.platform.artifact-id>quarkus-bom</quarkus.platform.artifact-id>
1515
<quarkus.platform.group-id>io.quarkus.platform</quarkus.platform.group-id>
16-
<quarkus.platform.version>2.15.3.Final</quarkus.platform.version>
16+
<quarkus.platform.version>3.2.6.Final</quarkus.platform.version>
1717
<skipITs>true</skipITs>
1818
<surefire-plugin.version>3.0.0-M9</surefire-plugin.version>
1919
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
2020
<hono.version>2.1.0</hono.version>
21-
<vertx-pg-client.version>4.3.7</vertx-pg-client.version>
21+
<vertx-pg-client.version>4.4.6</vertx-pg-client.version>
2222
<jakarta.persistence-api.version>2.2.3</jakarta.persistence-api.version>
2323
<com.ongres.scram.client.version>2.1</com.ongres.scram.client.version>
24-
<quarkus-container-image-docker.version>2.16.0.Final</quarkus-container-image-docker.version>
25-
<quarkus-google-cloud-pubsub.version>1.3.0</quarkus-google-cloud-pubsub.version>
24+
<quarkus-container-image-docker.version>3.2.6.Final</quarkus-container-image-docker.version>
25+
<quarkus-google-cloud-pubsub.version>2.6.0</quarkus-google-cloud-pubsub.version>
2626
<maven-surefire-plugin.version>3.0.0-M9</maven-surefire-plugin.version>
2727
<com.google.guava.version>31.1-jre</com.google.guava.version>
28+
<mockito-inline.version>5.2.0</mockito-inline.version>
2829
</properties>
2930
<dependencyManagement>
3031
<dependencies>
@@ -84,6 +85,7 @@
8485
<dependency>
8586
<groupId>org.mockito</groupId>
8687
<artifactId>mockito-inline</artifactId>
88+
<version>${mockito-inline.version}</version>
8789
<scope>test</scope>
8890
</dependency>
8991

@@ -97,6 +99,14 @@
9799
<artifactId>jakarta.persistence-api</artifactId>
98100
<version>${jakarta.persistence-api.version}</version>
99101
</dependency>
102+
<dependency>
103+
<groupId>jakarta.inject</groupId>
104+
<artifactId>jakarta.inject-api</artifactId>
105+
</dependency>
106+
<dependency>
107+
<groupId>jakarta.enterprise</groupId>
108+
<artifactId>jakarta.enterprise.cdi-api</artifactId>
109+
</dependency>
100110
<dependency>
101111
<groupId>com.ongres.scram</groupId>
102112
<artifactId>client</artifactId>
@@ -152,25 +162,23 @@
152162
<dependency>
153163
<groupId>org.eclipse.hono</groupId>
154164
<artifactId>hono-core</artifactId>
155-
<version>2.4.0-SNAPSHOT</version>
165+
<version>2.5.0-SNAPSHOT</version>
156166
</dependency>
157167
<dependency>
158168
<groupId>org.eclipse.hono</groupId>
159169
<artifactId>hono-client-pubsub-common</artifactId>
160-
<version>2.4.0-SNAPSHOT</version>
170+
<version>2.5.0-SNAPSHOT</version>
161171
</dependency>
162172
<dependency>
163173
<groupId>org.eclipse.hono</groupId>
164174
<artifactId>hono-client-notification</artifactId>
165-
<version>2.4.0-SNAPSHOT</version>
175+
<version>2.5.0-SNAPSHOT</version>
166176
</dependency>
167177
<dependency>
168178
<groupId>commons-codec</groupId>
169179
<artifactId>commons-codec</artifactId>
170180
<version>1.15</version>
171181
</dependency>
172-
173-
174182
</dependencies>
175183
<build>
176184
<plugins>

device-communication/src/main/docker/Dockerfile.jvm

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ COPY --chown=185 target/quarkus-app/quarkus/ /deployments/quarkus/
8888

8989
EXPOSE 8080
9090
USER 185
91-
ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
91+
ENV JAVA_OPTS="-Djava.util.logging.manager=org.jboss.logmanager.LogManager"
9292
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"
9393

device-communication/src/main/docker/Dockerfile.legacy-jar

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ COPY target/*-runner.jar /deployments/quarkus-run.jar
9090

9191
EXPOSE 8080
9292
USER 185
93-
ENV JAVA_OPTS="-Dquarkus.http.host=0.0.0.0 -Djava.util.logging.manager=org.jboss.logmanager.LogManager"
93+
ENV JAVA_OPTS="-Djava.util.logging.manager=org.jboss.logmanager.LogManager"
9494
ENV JAVA_APP_JAR="/deployments/quarkus-run.jar"

device-communication/src/main/docker/Dockerfile.native

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ COPY --chown=1001:root target/*-runner /work/application
2424
EXPOSE 8080
2525
USER 1001
2626

27-
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
27+
CMD ["./application"]

device-communication/src/main/docker/Dockerfile.native-micro

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ COPY --chown=1001:root target/*-runner /work/application
2727
EXPOSE 8080
2828
USER 1001
2929

30-
CMD ["./application", "-Dquarkus.http.host=0.0.0.0"]
30+
CMD ["./application"]

0 commit comments

Comments
 (0)