Skip to content

Manage Device Changed Notifications

When the Alerts and Notifications Service is running it receives data about device changes from the Core Metadata. The Notifications Service can then send a notification to show the device has changed. An example of the notification is as follows:

Metadata notice:  {"deviceName":"test-device","deviceServiceName":"device-modbus","protocols":{"modbus-tcp":{"Address":"192.168.68.112","Port":"1502","UnitID":"1"}},"actionType":"Device creation","operatingState":"UP","adminState":"UNLOCKED","datetime":"Wed, 29 Mar 2023 20:36:24 CST"}

Configuring the Device Changed Notification.

When PostDeviceChanges configuration property is set to true, Core Metadata sends a DEVICE_CHANGED notification with a label of metadata to the Alerts and Notifications when a device is changed.

The Device Changed Notifications are configured in the Core Metadata configuration properties, see Core Metadata Configuration Properties

Note

The PostDeviceChanges configuration property of the Core Metadata can be changed via the environment variable NOTIFICATIONS_POSTDEVICECHANGES before Edge Xpert is started. To configure the PostDeviceChanges configuration property to true, use the following command:

export NOTIFICATIONS_POSTDEVICECHANGES=true
The default value of NOTIFICATIONS_POSTDEVICECHANGES is false by default.

The content sent in the device notification update can be changed as well as the description.

To do this you need to use the following environment variables in a Docker Compose file to update the default notification configuration:

  • NOTIFICATIONS_CONTENT
  • NOTIFICATIONS_DESCRIPTION
  • NOTIFICATIONS_SENDER
  • NOTIFICATIONS_LABEL

An example of using a local Docker Compose file to update notification configuration is shown below:

version: '3.7'
services:
  core-metadata:
    environment:
      NOTIFICATIONS_CONTENT: IOTech Metadata notification
      NOTIFICATIONS_DESCRIPTION: Notification when a device is added, modified, or removed.
      NOTIFICATIONS_SENDER: IOTech
      NOTIFICATIONS_LABEL: DeviceChanged

For further information on using a local Docker Compose file, see Using a Local Docker Compose File

Subscribing to Device Changed Notifications

You can subscribe to the device changed notifications from the Alerts and Notifications microservice by creating a subscription with a category of DEVICE_CHANGED and a label of metadata

To subscribe to the notifications using email you need to configure your email client, see Mail Server Configurable

To subscribe to notifications using ZeroMQ or MQTT, see the following examples:

For information on managing subscriptions using the Edge Xpert Manager, see Manage Subscriptions Using Edge Xpert Manager

Device Changed Notifcation Example

The examples require the Alerts and Notifications Service and the Virtual Device Service to be running. A suitable command to start the required Edge Xpert Services is as follows:

edgexpert up support-notifications device-virtual

Using Unencrypted Transport

In the following example, we are going to send the notification to an external MQTT broker (e.g. test.mosquitto.org) using unencrypted transport. To create a subscription to the device changed notification, use the following command:

Info

If Edge Xpert is running in secure mode, you will need to replace localhost in the URL with the IP address for the service. See CLI Service Ports for details.

curl -X POST \
'http://localhost:59860/api/v2/subscription' \
-H 'Content-Type: application/json' \
-d '[
{
  "apiVersion": "V2",
  "subscription": {
    "name": "DEVICE_CHANGED",
    "channels": [{
      "type": "MQTT",
      "host": "test.mosquitto.org",
      "port": 1883,
      "publisher": "notification-service",
      "topic": "iotech/notification",
      "authmode": "none"
      }
    ],
    "categories": [
      "DEVICE_CHANGED"
     ],
    "labels": [
      "metadata"
      ],
    "receiver": "test-mqtt",
    "adminState": "UNLOCKED"
    }
  }
]'

Note

The MQTT broker test.mosquitto.org is a publicly available for testing, but it may sometimes unable to be connected. Please replace it with your own available MQTT broker.

Creating the Notification

To create an example device changed notification you can change the adminState of the Random-Boolean-Device using the Xpert Manager.

Start the Xpert Manager using the command:

edgexpert up xpert-manager
Change the adminState of the Random-Boolean-Device from locked to unlocked by clicking the padlock icon in the Device list view as illustrated below:

Device lock adminState padlock

Click the Lock button to confirm, as illustrated below: Device lock adminState confirmation

Receiving the Notifications

These examples use the mosquitto_sub utility to demonstrate how to receive notifications from the host test.mosquitto.org

To see the notifications, use the following command. Note that the host test.mosquitto.org should be changed if a different MQTT broker was used for the subscription in the previous step.

mosquitto_sub -h test.mosquitto.org -p 1883 -t iotech/notification
Now you should see a notification as follows:
Metadata notice:  {"deviceName":"Random-Boolean-Device","deviceServiceName":"device-virtual","protocols":{"other":{"None":""}},"actionType":"Device update","operatingState":"UP","adminState":"LOCKED","datetime":"Wed, 29 Mar 2023 20:43:04 CST"}