Skip to content

Tutorial Developer SensorClientFIFOWrite

sqall01 edited this page Oct 8, 2021 · 5 revisions

Tutorial - Developer - AlertR Sensor Client FIFO Write

This tutorial describes how to write your own scripts that writes into the FIFO file of the AlertR Sensor Client FIFO. This tutorial assumes you have set up the AlertR Sensor Client FIFO according to the provided AlertR Sensor Client FIFO Tutorial and only describes the necessary additions.

Table of Contents

Description

The following gives you a short example on how to configure the AlertR Sensor Client FIFO and write a script that writes processable data for AlertR. The script is written in Python, but it can be any programming language you like. I just find Python most convenient to show what has to be done.

Configuration

The corresponding sensor configuration for the AlertR Sensor Client FIFO looks like the following:

[...]

<sensors>

    <sensor>

        <general
            id="0"
            description="Developer Sensor"
            alertDelay="0"
            triggerAlert="True"
            triggerAlertNormal="True" />

        <alertLevel>0</alertLevel>

        <fifo
            umask="0000"
            fifoFile="/home/alertr/sensorClientFIFO/some_notification.fifo"
            dataType="1" />

    </sensor>

</sensors>

[...]

This sensor has a FIFO file at /home/alertr/sensorClientFIFO/some_notification.fifo and the data that is provided by the sensor is of the type integer (1 = integer).

Sensor Script

The following shows the script that writes into the FIFO file. This script does not have any purpose at all. It should just demonstrate how to output data that can be processed by AlertR.

#!/usr/bin/python3

import json
import random
import time

# This function creates a Sensor Alert message dictionary.
# It uses the given "value" as data for the sensor alert,
# the "msg" as message for the optional data,
# and the "state" as state for the sensor alert.
def create_sensor_alert(value, msg, state):
    result = dict()
    result["message"] = "sensoralert"

    optionalData = dict()
    optionalData["message"] = msg

    payload = dict()
    payload["state"] = state
    payload["hasOptionalData"] = True
    payload["optionalData"] = optionalData
    payload["dataType"] = 1
    payload["data"] = {"value": value, "unit": ""}
    payload["hasLatestData"] = True
    payload["changeState"] = True

    result["payload"] = payload
    return result

# This function creates a State Change message dictionary.
# It uses the given "value" as data for the state change,
# and the "state" as state for the state change.
def create_state_change(value, state):
    result = dict()
    result["message"] = "statechange"

    payload = dict()
    payload["state"] = state
    payload["dataType"] = 1
    payload["data"] = {"value": value, "unit": ""}

    result["payload"] = payload
    return result

# This actually does nothing. It just creates a
# random number and triggers a sensor alert if
# it is greater than 10. The created number is used
# as data for the message.
def main():

    fifo_file = "/home/alertr/sensorClientFIFO/some_notification.fifo"

    while True:
        data = random.randint(0, 20)
        msg = None
        if data > 10:
            msg = create_sensor_alert(data, "Oh no, over 10!", 1)
        else:
            msg = create_state_change(data, 0)

        with open(fifo_file, "w") as fp:
            fp.write(json.dumps(msg))
        time.sleep(5)

if __name__ == '__main__':
    main()

Done. This script creates a sensor alert if a random number is greater than 10. Otherwise, a state change message with the state normal is returned. With a FIFO file you can trigger an event in the AlertR system as soon as it occurs (in contrast to the AlertR Sensor Client Executer which executes scripts in an interval). If you want a detailed description of what messages can be passed back to the AlertR system via writing to a FIFO file, please take a look at the Protocol of the AlertR Sensor Client FIFO.

Further Information

If you want additional examples that have actually any purpose, please take a look into the scripts_example folder in the AlertR Sensor Client FIFO installation directory.

Troubleshooting

If you experience problems, please check the log file first. If it is not helpful, change the log level to DEBUG and check again. If no error can be seen, please start the AlertR client manually and check if an error occurs that is not printed into the log file. This can be done by just executing the AlertR client as the user that it normally runs with.

alertr@towel:~/sensorClientFIFO$ ./alertRclient.py

If you still have problems and do not know how to solve them, you can ask on the community page on reddit or you can use the Github Issues.

Clone this wiki locally