开发者中心

函数

概述

QuarkIoE事件语言支持使用函数。本节覆盖可以在语句中直接使用的内建函数。编写自己的函数请参阅 此节

Java函数

每个模块自动导入下面的库:


    java.lang.*
    java.math.*
    java.text.*
    java.util.*
                        

你可以使用包含在这些库中的任何函数。

示例:

使用 java.util.Random


create variable Random generator = new Random();

insert into CreateMeasurement
select
  "12345" as source,
  "c8y_TemperatureMeasurement" as type,
  current_timestamp().toDate() as time,
  {
    "c8y_TemperatureMeasurement.T1.value", generator.nextInt(12) + 18,
    "c8y_TemperatureMeasurement.T1.unit", "C",
    "c8y_TemperatureMeasurement.T2.value", generator.nextInt(12) + 18,
    "c8y_TemperatureMeasurement.T2.unit", "C",
    "c8y_TemperatureMeasurement.T3.value", generator.nextInt(12) + 18,
    "c8y_TemperatureMeasurement.T3.unit", "C",
    "c8y_TemperatureMeasurement.T4.value", generator.nextInt(12) + 18,
    "c8y_TemperatureMeasurement.T4.unit", "C",
    "c8y_TemperatureMeasurement.T5.value", generator.nextInt(12) + 18,
    "c8y_TemperatureMeasurement.T5.unit", "C"
  } as fragments
from pattern[every timer:at(*, *, *, *, *, */30)];
                        

使用 java.math.BigDecimal


select
  getNumber(m, "c8y_TemperatureMeasurement.T.value").divide(new BigDecimal(3),2,RoundingMode.HALF_UP)
from MeasurementCreated m;
                        

数据库函数

要和历史数据交互,可以使用以下函数之一直接查询数据库 大多数函数有几个变体:

  • findOne...(...): 函数需要刚好一个对象作为查询结果,否则失败。
  • findFirst...(...): 函数返回查询结果的第一个对象,如果结果为空返回"null"。
  • findAll...(...): 函数返回查询结果的所有对象。

这里是可用函数的完整列表。用 "findOne", "findFirst" 或 "findAll"替换省略号("...")。

函数名称 (包括变体) 返回类型 参数列表
findManagedObjectById ManagedObject id:String
id:GId
findFirstManagedObjectParent
findOneManagedObjectParent
ManagedObject managedObjectId:String
managedObjectId:GId
...ManagedObjectByFragmentType List fragmentType:String
...ManagedObjectByType List type:String
findEventById Event id:String
id:GId
...EventByFragmentType List fragmentType:String
...EventByFragmentTypeAndSource List fragmentType:String, source:String
...EventByFragmentTypeAndSourceAndTimeBetween List fragmentType:String, source:String, from:Date, to:Date
...EventByFragmentTypeAndSourceAndTimeBetweenAndType List fragmentType:String, source:String, from:Date, to:Date, type:String
...EventByFragmentTypeAndSourceAndType List fragmentType:String, source:String, type:String
...EventByFragmentTypeAndTimeBetween List fragmentType:String, from:Date, to:Date
...EventByFragmentTypeAndTimeBetweenAndType List fragmentType:String, from:Date, to:Date, type:String
...EventByFragmentTypeAndType List fragmentType:String, type:String
...EventBySource List source:String
findMeasurementById Measurement id:String
id:GId
...MeasurementByFragmentType List fragmentType:String
...MeasurementByFragmentTypeAndSource List fragmentType:String, source:String
...MeasurementByFragmentTypeAndSourceAndTimeBetween List fragmentType:String, source:String, from:Date, to:Date
...MeasurementByFragmentTypeAndSourceAndTimeBetweenAndType List fragmentType:String, source:String, from:Date, to:Date, type:String
...MeasurementByFragmentTypeAndSourceAndType List fragmentType:String, source:String, type:String
...MeasurementByFragmentTypeAndTimeBetween List fragmentType:String, from:Date, to:Date
...MeasurementByFragmentTypeAndTimeBetweenAndType List fragmentType:String, from:Date, to:Date, type:String
...MeasurementByFragmentTypeAndType List fragmentType:String, type:String
...MeasurementBySource List source:String
findLastMeasurementByFragmentTypeAndSourceAndTimeBetween Measurement fragmentType:String, source:String, from:Date, to:Date
findLastMeasurementByFragmentTypeAndSourceAndTimeBetweenAndType Measurement fragmentType:String, source:String, from:Date, to:Date, type:String
findLastMeasurementByFragmentTypeAndTimeBetween Measurement fragmentType:String, from:Date, to:Date
findLastMeasurementByFragmentTypeAndTimeBetweenAndType Measurement fragmentType:String, from:Date, to:Date, type:String
findOperationById Operation id:String
id:GId
...OpererationByAgent List agentId:String
...OpererationByAgentAndStatus List agentId:String, status:String
...OpererationByDevice List deviceId:String
...OpererationByDeviceAndStatus List deviceId:String, status:String
...OpererationByStatus List status:String
...OpererationByCreationTimeBetween List from:Date, to:Date
findAlarmById Alarm id:String
id:GId
...AlarmBySource List sourceId:String
...AlarmBySourceAndStatus List sourceId:String, status:String
...AlarmBySourceAndStatusAndType List sourceId:String, status:String, type:String
...AlarmBySourceAndStatusAndTimeBetween List sourceId:String, status:String, from:Date, to:Date
...AlarmBySourceAndTimeBetween List sourceId:String, from:Date, to:Date
...AlarmByStatus List status:String
...AlarmByStatusAndTimeBetween List status:String, from:Date, to:Date
...AlarmByTimeBetween List from:Date, to:Date

实用函数

访问片段

通过以下辅助函数可以访问片段:

  • Object getObject(Object event, String path[, Object defaultValue])
  • String getString(Object event, String path[, String defaultValue])
  • Number getNumber(Object event, String path[, Number defaultValue])
  • Boolean getBoolean(Object event, String path[, Boolean defaultValue])
  • Date getDate(Object event, String path[, Date defaultValue])
  • List getList(Object event, String path[, List defaultValue])

你可以使用jsonpath(没有根元素$)在对象结构中导航 示例:


  select
    getNumber(m, "c8y_TemperatureMeasurement.T.value")
    from MeasurementCreated m;

    select
    e.event as event
    from EventCreated e
    where getObject(e, "c8y_Position") is not null;
  
类型转换

cast() 函数让你可以将数据转换为正确的数据类型,如对象。 转换为基本Java类型:

cast(myVariable, long)
                          

对于其他类型,你需要指定完全限定的类名称

cast(event.managedObject.childAssets[0], com.cumulocity.model.ID)
                          

current_timestamp

current_timestamp() 函数提供当前服务器时间。 你可以很容易地用toDate()函数转换为QuarkIoE流中使用的日期数据类型。

示例:


  insert into CreateAlarm
    select
    "c8y_HighTemperatureAlarm" as type,
    current_timestamp().toDate() as time,
    event.event.source as source,
    QuarkioeSeverities.WARNING as severity,
    QuarkioeAlarmStatuses.ACTIVE as status,
    "The device has high temperature" as text
    from EventCreated event;
                          
inMaintenanceMode

inMaintenaceMode() 函数是检查设备当前是否在维护模式的一个快速方法。它需哟啊一个 ID 作为参数并返回一个布尔值。

示例:


  insert into SendEmail
    select
    "receiver1@cumulocity.com,receiver2@cumulocity.com" as receiver,
    "cc@cumulocity.com" as cc,
    "bcc@cumulocity.com" as bcc,
    "reply@cumulocity.com" as replyTo,
    "Example mail" as subject,
    "This mail was sent to test the SendEmail stream in Quarkioe" as text
    from EventCreated e
    where not inMaintenanceMode(e.event.source);
                          
replaceAllPlaceholders

为了丰富文本可以使用连接

insert into SendEmail
                            select
                            "receiver1@cumulocity.com,receiver2@cumulocity.com" as receiver,
                            "cc@cumulocity.com" as cc,
                            "bcc@cumulocity.com" as bcc,
                            "reply@cumulocity.com" as replyTo,
                            "Example mail" as subject,
                            "An event with the text " || e.event.text || " has been created." as text
                            from EventCreated e;
                          

如果文本变长并有很多值动态生成,可以使用replaceAllPlaceholders()函数。 这个函数的另一个好处是你不仅可以用在当前对象也可以访问创建报警,测量值,事件的设备的所有信息。 在文本字符串用JsonPath标记占位符(无根元素 $) 并由 {}包含。如果想从设备访问数据 从源开始JsonPath。

用包含占位符的字符串以及填充占位符的对象调用这个函数。 源设备被自动查询。

示例:


  create variable string myMailText =
    "The device {source.name} with the serial number {source.c8y_Hardware.serialNumber} created an event with the text {text} at {time}. The device is located at {source.c8y_Address.street} in {source.c8y_Address.city}.";

    insert into SendEmail
    select
    "receiver1@cumulocity.com,receiver2@cumulocity.com" as receiver,
    "cc@cumulocity.com" as cc,
    "bcc@cumulocity.com" as bcc,
    "reply@cumulocity.com" as replyTo,
    "Example mail" as subject,
    replaceAllPlaceholders(myMailText, e.event) as text
    from EventCreated e;
                          

toNumberSetParameter

toNumberSetParameter() 函数帮助你设定本模块之外的定时器模式匹配。 当部署一个带定时器模式匹配的模块,模式匹配必须在部署阶段固定并且不能在重新部署模块时变更。 但可以用变量配置定时器模式匹配,部署时变量立即赋值。这使你可以在ManagedObject中保存定时器模式匹配并在部署时加载。 toNumberSetParameter() 函数将字符串转换为用于定时器模式匹配输入的NumberSetParameter类型。 更多定时器模式匹配信息请参阅 这里

示例:


  create variable ManagedObject device = findManagedObjectById("12345");
    create variable string minuteVal = getString(device, "config.minute");
    create variable string hourVal = getString(device, "config.hour");
    create variable string dayOfMonthVal = getString(device, "config.day");
    create variable string monthVal = getString(device, "config.month");
    create variable string dayOfWeekVal = getString(device, "config.weekday");

    insert into CreateOperation
    select
    "PENDING" as status,
    "12345" as deviceId,
    { "c8y_Restart", {} } as fragments
    from
    pattern [every timer:at(toNumberSetParameter(minuteVal),
    toNumberSetParameter(hourVal),
    toNumberSetParameter(dayOfMonthVal),
    toNumberSetParameter(monthVal),
    toNumberSetParameter(dayOfWeekVal))];