Link Search Menu Expand Document

C-Sharp Script Editor

Dec 6 2023 at 12:00 AM

  1. Introduction
  2. Example Options File
    1. Property Details
  3. Template Script
    1. IPayload Interface
    2. IPayloadValue Interface

Introduction

This filter provides users with the flexibility to code a C# script, enabling them to read or modify the raw source data. Users can add, modify, and remove payload values according to their requirements. The script comprises one or more C# code files (.cs), with one of these files containing a class that implements the IoTnxt.Raptor.Generic.Filter.IPayloadExecutor interface.

During compilation, the C# compiler loads all script files associated with the script project and compiles them collectively. The C# compilation process is customisable, allowing users to set specific options via the C-Sharp Script Options.

Example Options File

{
  "Devices": {
    "Device1": {
      "DeviceType": "Generic",
      ...
      "Filters": [
         {
            "FilterType": "CSharpScript",
            "Order": 1,
            "ProjectId": "Project1",
            "PropertyMaps": {
              "PropertyKey": {
                "DataType": "String"
            },
         }
      ]
    }
  }  
}

Property Details


Here is an overview of the parameters for configuring the C-SharpScript filter:

Property NameDescription
FilterTypeSpecifies that the filter is of type “C-SharpScript”.
OrderUsed to determine the processing order when multiple filters are configured.
ProjectIdThe unique identifier of the C# project that will be compiled and used to process payloads.
PropertyMapsDictionary of properties to create, where the property map key is the property name. These property maps need to be accessed from the payload within a script and will not take effect just by setting the configuration. They are intended to be utilised within the custom C# script during payload processing.

Template Script

using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.Extensions.Logging;

namespace IoTnxt.Raptor.Generic.Filter;

public class PayloadExecutor: IPayloadExecutor
{
    private readonly ILogger _logger;

    public PayloadExecutor(ILogger<PayloadExecutor> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// Executes the filter to process the payload.
    /// </summary>
    /// <param name="payload">The payload to process</param>
    /// <param name="cancellationToken">Cancellation token</param>
    /// <returns>A Task</returns>
    public Task ExecuteAsync(IPayload payload, CancellationToken cancellationToken)
    {
        // TODO: Process the payload here.
        return Task.CompletedTask;
    }
}

IPayload Interface


This is a description of the properties associated with the payload in the C-SharpScript filter:

Property NameData TypeDescription
PayloadIdstringA unique identifier for the payload.
Databyte[]The raw data received, represented as a byte array. It can be modified by a filter.
TimestampUtcDateTimeThe UTC date and time indicating when the payload was created.
AsyncLockIAsyncLockExecutorAn asynchronous lock, allowing for synchronized access in an asynchronous context.
SourceInfoIDictionary<string, string>Additional source information. This is dependent on the Data Source used.
ValuesIDictionary<string, IPayloadValue>The payload properties. These properties will be sent as telemetry once processing is completed.
IsRejectedboolIndicates whether this payload has been rejected. Rejected payloads will not undergo further processing.
RejectReasonstringIf a payload is rejected, this property contains the reason for rejection.

IPayloadValue Interface


The read-only properties form part of the payload key. If there is a need to change these properties, the user is advised to create a new value and remove the old one. This ensures proper handling of changes to the payload properties while maintaining the integrity of the payload key.

Here is an explanation of the properties associated with the payload values:

Property NameData TypeDescription
TimeStampUtcDateTimeRead-only. Represents the UTC date and time when the property value was read.
GatewayIdstringRead-only. The gateway identifier. If this is blank or null, the default will be used.
GroupNamestringRead-only. The group name. If this is blank or null, the default will be used.
DeviceTypestringRead-only. Refers to the device type. If this is blank or null, the default will be used.
DeviceNamestringRead-only. Refers to the device name. If this is blank or null, the default will be used.
PropertyNamestringRead-only. Represents the property name.
ValueobjectRepresents the property value.