// Module included in the following assemblies:
//
// assembly-iot-guide.adoc
// assembly-IoT.adoc

[id='iot-{api}-{context}']

ifeval::["{cmdcli}" == "oc"]
:http-adapter: $(oc -n enmasse-infra get routes iot-http-adapter --template='{{ .spec.host }}')
:mqtt-adapter: $(oc -n enmasse-infra get routes iot-mqtt-adapter --template='{{ .spec.host }}')
:mqtt-port: 443
endif::[]
ifeval::["{cmdcli}" == "kubectl"]
:http-adapter: $(kubectl -n enmasse-infra get service iot-http-adapter-external -o jsonpath={.status.loadBalancer.ingress[0].hostname}):30443
:mqtt-adapter: $(kubectl -n enmasse-infra get service iot-mqtt-adapter-external -o jsonpath={.status.loadBalancer.ingress[0].hostname})
:mqtt-port: 30883
endif::[]

= Sending and receiving {api} data

== Starting the {api} consumer

.Prerequisites
* link:{BookUrlBase}{BaseProductVersion}{BookNameUrl}#installing-services-iot[The IoT services are installed].
* link:{BookUrlBase}{BaseProductVersion}{BookNameUrl}#iot-creating-project-iot[An IoT project is created].
* link:{BookUrlBase}{BaseProductVersion}{BookNameUrl}#iot-creating-device-iot[An IoT device is created].

.Procedure

. In the Hono project, run:
+
[options="nowrap",subs="attributes"]
----
cd cli
----
. Run the following command at least once:
+
[options="nowrap",subs="attributes"]
----
mvn package -am
----
. Get the messaging endpoint certificate:
+
[options="nowrap",subs="attributes"]
----
{cmdcli} -n myapp get addressspace iot -o jsonpath={.status.caCert} | base64 --decode > target/config/hono-demo-certs-jar/tls.crt
----

. Export the messaging endpoint host and port:
+
[options="nowrap",subs="attributes"]
----
export MESSAGING_HOST=$({cmdcli} -n myapp get addressspace iot -o jsonpath={.status.endpointStatuses[?\(@.name==\'messaging\'\)].externalHost})
export MESSAGING_PORT=443
----
ifeval::["{cmdcli}" == "kubectl"]
+
[NOTE]
====
If you are running Kubernetes in a development environment without a proper load balancer, you need to use the IP address of your local cluster and the port of the appropriate service; for example:
[options="nowrap",subs="attributes"]
----
export MESSAGING_HOST=localhost
export MESSAGING_PORT=5671
----
====
endif::[]

. Run the consumer application:
+
[options="nowrap",subs="attributes"]
----
mvn spring-boot:run -Drun.arguments=--hono.client.host=$MESSAGING_HOST,--hono.client.port=$MESSAGING_PORT,--hono.client.username=consumer,--hono.client.password=foobar,--tenant.id=myapp.iot,--hono.client.trustStorePath=target/config/hono-demo-certs-jar/tls.crt,--message.type={api}
----

== Sending {api} data

.Procedure

. Send a message using HTTP protocol:
+
[options="nowrap",subs="attributes"]
----
curl --insecure -X POST -i -u sensor1@myapp.iot:hono-secret -H 'Content-Type: application/json' --data-binary '{"temp": 5}' https://{http-adapter}/{api}
----

. Send a message using MQTT protocol:
+
[options="nowrap",subs="attributes"]
----
mosquitto_pub -d -h {mqtt-adapter} -p {mqtt-port} -u 'sensor1@myapp.iot' -P hono-secret -t {api} -m '{"temp": 5}' -i 4711 --cafile install/components/iot/examples/k8s-tls/build/iot-mqtt-adapter-fullchain.pem
----

