C-Sharp Script Editor
Dec 6 2023 at 12:00 AM
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 Name | Description |
---|---|
FilterType | Specifies that the filter is of type “C-SharpScript”. |
Order | Used to determine the processing order when multiple filters are configured. |
ProjectId | The unique identifier of the C# project that will be compiled and used to process payloads. |
PropertyMaps | Dictionary 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 Name | Data Type | Description |
---|---|---|
PayloadId | string | A unique identifier for the payload. |
Data | byte[] | The raw data received, represented as a byte array. It can be modified by a filter. |
TimestampUtc | DateTime | The UTC date and time indicating when the payload was created. |
AsyncLock | IAsyncLockExecutor | An asynchronous lock, allowing for synchronized access in an asynchronous context. |
SourceInfo | IDictionary<string, string> | Additional source information. This is dependent on the Data Source used. |
Values | IDictionary<string, IPayloadValue> | The payload properties. These properties will be sent as telemetry once processing is completed. |
IsRejected | bool | Indicates whether this payload has been rejected. Rejected payloads will not undergo further processing. |
RejectReason | string | If 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 Name | Data Type | Description |
---|---|---|
TimeStampUtc | DateTime | Read-only. Represents the UTC date and time when the property value was read. |
GatewayId | string | Read-only. The gateway identifier. If this is blank or null, the default will be used. |
GroupName | string | Read-only. The group name. If this is blank or null, the default will be used. |
DeviceType | string | Read-only. Refers to the device type. If this is blank or null, the default will be used. |
DeviceName | string | Read-only. Refers to the device name. If this is blank or null, the default will be used. |
PropertyName | string | Read-only. Represents the property name. |
Value | object | Represents the property value. |