6.4 示例

6.4.1 将某个变量结果+1

function calc(current,last)
    current=current+1
    return current
end

6.4.2 将某个变量结果 追加一个字符串

function calc(current,last)
    current=current .. "woody"
    return current
end

6.4.3 联合两个变量计算出新的变量

function calc(value)
    if(value.cnc_runstatus==1 and #value.cnc_alarm>0)
    then
        value.custom=2
    end
    return value
end

6.4.4 删除某个变量

function calc(value)
    value.cnc_runstatus=nil
    return value
end

6.4.5 将Alarm的Array[Object]展开成普通变量

{
  "cnc_alarm": [
    {
      "alarm_no": 1,
      "alarm_type": "T01",
      "alarm_msg": "待机中"
    },
    {
      "alarm_no": 204,
      "alarm_type": "XXX",
      "alarm_msg": "急停"
    }
  ]
}

转换成

{
  "cnc_alarm_1_msg": "待机中",
  "cnc_alarm_1_no": 1,
  "cnc_alarm_1_type": "T01",
  "cnc_alarm_2_msg": "急停",
  "cnc_alarm_2_no": 204,
  "cnc_alarm_2_type": "XXX"
}
function calc(value)
    for i, v in ipairs(value.cnc_alarm) do
        value['cnc_alarm_' .. i .. '_msg'] = v.alarm_msg
        value['cnc_alarm_' .. i .. '_no'] = v.alarm_no
        value['cnc_alarm_' .. i .. '_type'] = v.alarm_type
    end
    value.cnc_alarm = nil
    return value
end

6.4.6 将坐标的Array[Object]展开成普通变量

{
  "cnc_mecpos": [
    {
      "axis": "X",
      "value": -84.4
    },
    {
      "axis": "Y",
      "value": 2.986
    },
    {
      "axis": "Z",
      "value": -108.181
    }
  ]
}

转换成

{
  "cnc_mecpos_X": -84.4,
  "cnc_mecpos_Y": 2.986,
  "cnc_mecpos_Z": -108.181
}
function calc(value)
    for i, v in ipairs(value.cnc_mecpos) do
        value['cnc_mecpos_'..v.axis]=v.value
    end
    value.cnc_mecpos=nil
    return value
end