Service Payload Scripts
C# scripts help to generate or parse payloads depending on the platform or outside systems. This chapter can be used as development reference.
C# Script Structure
The C# script contains following structure:
- usings
- namespace
- class declaration
- overwritten methods from base class
- your own private methods, properties and fields
- class declaration
Example of DataLogger Payload Script.
//List of usings
using CyberTech;
using HumanOS.Kernel;
using HumanOS.Kernel.DataModel;
using HumanOS.Kernel.PeSeL.DataLogger;
using HumanOS.Kernel.PeSeL.Script;
using HumanOS.Kernel.Utils;
using System;
using System.Collections.Generic;
using System.Text;
//Namespace (can by any)
namespace HumanOS.IoT.Designer.Library.Scripts
{
/// Your script class
public class MyDataLoggerScriptObject : TAbstractDataLoggerScriptObject<string>
{
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void initialize(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
//Your code to initial the processing (first time)
}
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void postProcess(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
//Your code to post process data after successful sending
}
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override string[] processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, List<TDataSet> lstData)
{
//Your code to generate the payload
return new string[] { "MyPayload" };
}
//Some private methods and fields
private void doInternal()
{
//...
}
}
}
Base Class for DataLogger
Each script object must be derived from TAbstractDataLoggerScriptObject<T>
for data logging.
The T
stands for a data type used by the data logger service:
- HumanOS.PeSeL.AzureIoTClient uses
byte[]
. - HumanOS.PeSeL.MQTTClient uses
byte[]
. - HumanOS.PeSeL.NodeSpaceDataLogger uses
string
. - HumanOS.PeSeL.RestDataLogger uses
string
. - HumanOS.PeSeL.RabbitMqClient uses
byte[]
.
The following methods must be overwritten:
Initialize
The initialize
method is only called once at the beginning of the processing. Place your initial code here.
The method requires arguments:
Argument | Description | Data Type |
---|---|---|
Kernel | Access to the HumanOS kernel. | IKernelAccess |
Context | Contains information about the service endpoint and data logger. | TPayloadProcessingContext |
public override void initialize(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
//Your code to initial the processing (first time)
}
ProcessPayload
The processPayload
method is called each time a payload must be sent to the endpoint. Here, you place the code to generate the payload.
The method requires arguments.
Argument | Description | Data Type |
---|---|---|
Kernel | Access to the HumanOS kernel. | IKernelAccess |
Context | Contains information about the service endpoint and data logger. | TPayloadProcessingContext |
lstData | List of data sets to process. | List<TDataSet> |
The method returns a list of payloads. Each item in the list is individually sent to the service endpoint.
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override string[] processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, List<TDataSet> lstData)
{
List<string> lstRetval = new List<string>();
//Your code to generate the payload
return lstRetval;
}
PostProcess
The postProcess
method is called after a successful sending of the payload.
The method requires arguments. See method Initialize.
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void postProcess(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
//Your code to post process data after successful sending
}