RestApi Publisher Configuration
The publisher is used to report datasets (values or alarms) to the server. To check the possibilities of publisher configuration, please check the Common DataLogger Configuration manual.
The server address must be set in the property "Connection". Example: "https://abcxyz.pipedream.net"
The following table contains the specific properties of the RestDataLogger Publisher configuration. It extends the general DataLogger publisher configuration documented in Common DataLogger Configuration.
Parameter | Description | Data Type |
---|---|---|
ClientMethod | Http client method, possible values are: (Delete, Get, Head, Options, Patch, Post, Put, Trace). Default is "Post". | System.String |
ContentType | Http content type, e.g. (text/html, application/json) | System.String |
Payload Scripting
The payload definition is done by C# Script. The plugin will not work without the script. For further information on scripting see the Operation Manual.
The Rest payload is a string. Therefore, each script processes strings only.
Publisher Scripts
The publisher script transforms a dataset into a string.
The script object must extend the TAbstractDataLoggerScriptObject<string>
class.
Following methods must be overwritten:
initialize()
: initializes the context of the script. This method is called only once at the beginning.processPayload()
: Each time a dataset is generated, this method is called to create a payload.postProcess()
: After processPayload, this method is called to do some work after successfully generating the payload.
Following example shows a RestDataLogger payload script:
using CyberTech.Diagnostics;
using HumanOS.Kernel;
using HumanOS.Kernel.DataModel;
using HumanOS.Kernel.PeSeL.DataLogger;
using HumanOS.Kernel.PeSeL.DataLogger.Config;
using HumanOS.Kernel.PeSeL.Script;
using HumanOS.Kernel.Utils;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
namespace HumanOS.PeSeL.RestDataLogger
{
/// <summary>
/// Script defining the payload
/// </summary>
public class TLoggerValuePayload : TAbstractDataLoggerScriptObject<string>
{
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void initialize(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
TLogger.writeInfo("Payload script initialized");
}
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override void postProcess(IKernelAccess Kernel, TPayloadProcessingContext Context)
{
}
///<see cref="TAbstractDataLoggerScriptObject{T}"/>
public override string[] processPayload(IKernelAccess Kernel, TPayloadProcessingContext Context, List<TDataSet> lstData)
{
List<string> lstRetval = new List<string>();
StringBuilder Builder = new StringBuilder();
Builder.AppendLine("{\"data\": [");
bool bFirst = true;
foreach(TDataSet DataSet in lstData)
{
EDataState eState = DataSet.getFieldValue<EDataState>("State");
if (eState == EDataState.Good)
{
Guid DeviceId = DataSet.getFieldValue<Guid>("DeviceId");
if (DeviceId != Guid.Empty && Kernel.NodeSpace.tryGetNode(DeviceId, out INode DeviceNode))
{
string strResourceId = DeviceNode.getProperty<string>("ResourceId");
if (!bFirst)
{
Builder.AppendLine(",");
}
bFirst = false;
Builder.Append("{");
object nValue = DataSet.getFieldValue<object>("Value");
string strValue = TValueConverter.convertToString(nValue);
Builder.AppendLine($"\"resource\": \"{strResourceId}\",");
Builder.AppendLine($"\"name\": \"{DataSet.getFieldValue<string>("Name")}\",");
Builder.AppendLine($"\"id\": \"{DataSet.getFieldValue<Guid>("Id")}\",");
Builder.AppendLine($"\"timestamp\": \"{DataSet.getFieldValue<DateTime>("TimeStamp").ToString("O")}\",");
Builder.AppendLine($"\"value\": \"{strValue.Replace("\\", "\\\\")}\"");
Builder.Append("}");
} //DeviceId != Guid.Empty && Kernel.NodeSpace.tryGetNode(DeviceId, out INode DeviceNode)
} //eState == EDataState.Good
} //end foreach
Builder.Append(" ] }");
lstRetval.Add(Builder.ToString());
return lstRetval.ToArray();
}
}
}