Skip to content

Complex configurations

Overview

Passing configurations to an application is, in most cases, key to flexibility. In this tutorial, we will cover custom configurations and how to inject them into an application.

Prerequisite:

  1. Complete the Getting Started Guide.

Source Code

The source code for this application is available at GitHub.

The Use case

  • The application use case will be quite simple and concise:
    • The application will receive a random value.
    • This random value must respect both the min and max values. By default, 0 and 100 respectively.
    • If the random_value variable complies with the pre-established min and max thresholds, a success_message is printed.
    • If not, print a random entry from rejection_messages.

Creating the application

We start of by creating the application:

kelvin app create min-max-configuration

Configuring the application

Next, specify all the required configurations under app/kelvin/configuration

app:
  kelvin:
    data_types: [ ]
    inputs:
      - data_type: raw.float32
        name: temperature
        sources:
          - asset_names: [ emulation ]
            workload_names: [ ]
    configuration:
      min_threshold: 10
      max_threshold: 100
      rejection_messages:
        - "The values are not correct"
        - "Please provide and adequate value"
        - "The provided value does not comply with the thresholds"
      success_message: "The received value was accepted!"
    language:
      python:
        entry_point: min_max_configuration.min_max_configuration:App
        requirements: requirements.txt
      type: python
  type: kelvin
info:
  description: min-max-configuration
  name: min-max-configuration
  title: min-max-configuration
  version: 1.0.0
spec_version: 2.0.0

Handling configurations in the code

The configuration will now be available in code with self.config variable:

import random

from kelvin.app import DataApplication

class App(DataApplication):
    """Application."""

    def init(self) -> None:
        """
        Initialisation method
        """
        # Custom initialisation logic

    def process(self):
        temperature = self.data.get('temperature')
        min_threshold = self.config.complex_config.min_threshold
        max_threshold = self.config.complex_config.max_threshold
        if temperature and min_threshold <= temperature <= max_threshold:
            print(self.config.complex_config.success_message)
        else:
            print(random.choice(self.config.complex_config.rejection_messages))

The result

It is now time to run the application and see the results:

kelvin app build
kelvin emulation start --show-logs

The output:

[ExtremeDbBackend.cpp: 258:I] - eXtremeDB is running in PAID LICENSE MODE                                                                                         
[ExtremeDbBackend.cpp: 260:I] - eXtremeDB version: interim build eXtremeDB_8.1_1800, rev.25973                                                                    
[ExtremeDbBackend.cpp:  30:I] - eXtremeDB started successfully.                                                                                                   
[   UploadManager.cpp:  27:I] - The UploadManager's worker will not start because it's either disabled or has no address configured                               
[          Poller.cpp:  70:I] - Successful poller: 1.000000 secs (fd: 8)                                                                                          
[  RPCApplication.cpp:  72:I] - min-max-configuration initialize.                                                                                                 
OPCUA tree for min-max-configuration processing pipeline:                                                                                                         
ROOT                                                                                                                                                              

[          Server.cpp:  24:I] - OPCUA server opened endpoints for following URLs:                                                                                 
[          Server.cpp:  29:I] -   opc.tcp://96ee7abc7a70:48010                                                                                                    
[         Network.hpp:  51:E] - Failed get addr info: Unknown error                                                                                               
[         Runtime.cpp: 227:I] - Establishing OPCUA client connection:  opc.tcp://producer.app:48010                                                               
Please provide and adequate value                                                                                                                                 
Please provide and adequate value                                                                                                                                 
The provided value does not comply with the thresholds