ThingWorx Flow > ThingWorx Flow SDK > ThingWorx Flow SDK 概觀
ThingWorx Flow SDK 概觀
ThingWorx Flow 連接器 SDK (例如 ptc-flow-sdk) 需要包括在每個連接器模組中。執行 init 指令可產生已包括此模組的 package.json 檔案。
ThingWorx Flow 伺服器會使用 flow-sdk 來載入連接器及存取內含的「連線」、「動作」、「觸發器」與「查詢」。
ThingWorx Flow 連接器 SDK 也提供下列 API,以供連接器使用:
記錄
連接器內的所有記錄都必須使用由 SDK 提供的記錄 API 進行。這樣有助於有效偵錯問題。欲存取記錄 API,請將下面一行新增至您來源檔案最上方的 node js 程式碼:
const logger = require('ptc-flow-sdk').getLogger('module-name')
將上面的模組名稱取代為「動作」、「觸發器」或「查詢」等項目的名稱。
記錄器有 4 種方法:debugerrorwarninfo。可根據具體原因使用適當的方法。
記錄陳述式可以記錄字串與 JSON 物件。
CustomFilters
CustomFilters 是一個協助程式 API,可用來篩選 JSON、字串或數值的陣列。自訂篩選器可用於篩選提供至動作或觸發器的輸入。使用 customFilters 是一個兩個步驟的流程。第一步是在輸入結構描述中建立 customFilters 內容,如下所示。建立之後,使用者便可在 GUI 中挑選運算子與運算元。
"input": {
"type": "object",
"title": "Select Trigger",
"properties": {
"customFilters": {
"type": "array",
"title": "Custom Filters",
"items": {
"type": "object",
"title": "filter",
"properties": {
"input": {
"type": "string",
"title": "Input",
"minLength": 1
},
"operator": {
"type": "string",
"title": "Condition",
"enum": [
"Equals",
"Equals(Number)",
"GreaterThan",
"LessThan",
"Contains",
"DoesNotContains",
"matches",
"isNull",
"isEmpty",
"isNumber",
"isObject",
"isArray",
"isBoolean",
"isDate",
"isUndefined"
]
},
"expected": {
"type": "string",
"title": "Expected",
"minLength": 1
}
}
}
}
}
第二步是使用 CustomFilters API 的 filter 方法。使用 CustomFilters 的程式碼片段如下:
//add the first line of the code to the top of the node js script
const customFilters = require('ptc-flow-sdk').CustomFilters;
function execute (input, options, output) {
run(input, options, function (err, data) {
if (err || !data) {
return output(err || 'empty')
}
customFilters.filter(input.customFilters, data, output
})
}
在上述程式碼片段中,run 方法會執行部份程式碼,這可能是會傳回資料陣列的 http 呼叫。input.customFilters 是您之前在設計工作流程時選取的篩選條件。output 是要以篩選結果呼叫的回撥函數。
協助程式
協助程式物件具有一些公用程式 JavaScript 方法。
toStr(obj) - 可將任何物件轉換為字串
toISO(str) - 可將字串或字串的毫秒表示轉換為 ISO 格式的字串。
toNumber(obj) - 可嘗試將字串轉換為數值。
clone() - 可對物件執行淺層複製
deepClone() - 可對物件執行深層複製
mix(a,b) - 可合併物件 ab,以傳回包含來自 ab 之內容的新物件
這是否有幫助?