Global

Methods

(async) WDwdataBuilder(optopt) → {Object}

Description:
  • 基於檔案之下載數據變更驅動任務建構器

    外部調用僅須提供下載數據,並須放置於fdDwAttime,前次數據程序會於結束前自動備份,並放置於fdDwCurrent

Source:
Example
import fs from 'fs'
import _ from 'lodash-es'
import w from 'wsemi'
import WDwdataBuilder from './src/WDwdataBuilder.mjs'

//fdResult, 額外創建供另產結果之用
let fdResult = `./_result`
w.fsCleanFolder(fdResult)

//fdTagRemove
let fdTagRemove = `./_tagRemove`
w.fsCleanFolder(fdTagRemove)

//fdDwAttime
let fdDwAttime = `./_dwAttime`
w.fsCleanFolder(fdDwAttime)

//fdDwCurrent
let fdDwCurrent = `./_dwCurrent`
w.fsCleanFolder(fdDwCurrent)

//fdTaskCpActualSrc
let fdTaskCpActualSrc = `./_taskCpActualSrc`
w.fsCleanFolder(fdTaskCpActualSrc)

//fdTaskCpSrc
let fdTaskCpSrc = `./_taskCpSrc`
w.fsCleanFolder(fdTaskCpSrc)

//funDownload
let funDownload = async() => {

    //items
    let items = [
        {
            'id': '114115',
            'tag': '2025082116374751115',
            'number': '115',
            'time': '2025-08-21T16:37:47+08:00',
            'timeRec': '2025-08-21 16:37:47',
            'timeTag': '20250821163747',
            'ml': '5.1',
        },
        {
            'id': '114116',
            'tag': '2025082214061554116',
            'number': '116',
            'time': '2025-08-22T14:06:15+08:00',
            'timeRec': '2025-08-22 14:06:15',
            'timeTag': '20250822140615',
            'ml': '5.4',
        },
    ]

    _.each(items, (v) => {

        let fp = `${fdDwAttime}/${v.id}.json`

        fs.writeFileSync(fp, JSON.stringify(v), 'utf8')

    })

    return items
}

//funGetCurrent
let funGetCurrent = async() => {

    //vfps
    let vfps = w.fsTreeFolder(fdDwCurrent, 1)
    // console.log('vfps', vfps)

    //items
    let items = []
    _.each(vfps, (v) => {

        let j = fs.readFileSync(v.path, 'utf8')
        let item = JSON.parse(j)

        items.push(item)

    })

    return items
}

//funRemove
let funRemove = async(v) => {

    let fd = `${fdResult}/${v.id}`

    if (w.fsIsFolder(fd)) {
        w.fsDeleteFolder(fd)
    }

}

//funAdd
let funAdd = async(v) => {

    let fd = `${fdResult}/${v.id}`

    if (w.fsIsFolder(fd)) {
        w.fsCleanFolder(fd)
    }

    //do somethings

}

//funModify
let funModify = async(v) => {

    let fd = `${fdResult}/${v.id}`

    if (w.fsIsFolder(fd)) {
        w.fsCleanFolder(fd)
    }

    //do somethings

}

let opt = {
    fdTagRemove,
    fdDwAttime,
    fdDwCurrent,
    fdTaskCpActualSrc,
    fdTaskCpSrc,
    funDownload,
    funGetCurrent,
    funRemove,
    funAdd,
    funModify,
}
let ev = await WDwdataBuilder(opt)
    .catch((err) => {
        console.log(err)
    })
ev.on('change', (msg) => {
    delete msg.type
    delete msg.timeRunStart
    delete msg.timeRunEnd
    delete msg.timeRunSpent
    console.log('change', msg)
})
// change { event: 'start', msg: 'running...' }
// change { event: 'proc-callfun-afterStart', msg: 'start...' }
// change { event: 'proc-callfun-afterStart', msg: 'done' }
// change { event: 'proc-callfun-download', msg: 'start...' }
// change { event: 'proc-callfun-download', num: 2, msg: 'done' }
// change { event: 'proc-callfun-getCurrent', msg: 'start...' }
// change { event: 'proc-callfun-getCurrent', num: 0, msg: 'done' }
// change { event: 'proc-compare', msg: 'start...' }
// change {
//   event: 'proc-compare',
//   numRemove: 0,
//   numAdd: 2,
//   numModify: 0,
//   numSame: 0,
//   msg: 'done'
// }
// change { event: 'proc-add-callfun-add', id: '114115', msg: 'start...' }
// change { event: 'proc-add-callfun-add', id: '114115', msg: 'done' }
// change { event: 'proc-add-callfun-add', id: '114116', msg: 'start...' }
// change { event: 'proc-add-callfun-add', id: '114116', msg: 'done' }
// change { event: 'proc-callfun-beforeEnd', msg: 'start...' }
// change { event: 'proc-callfun-beforeEnd', msg: 'done' }
// change { event: 'end', msg: 'done' }
Parameters:
Name Type Attributes Default Description
opt Object <optional>
{}

輸入設定物件,預設{}

Properties
Name Type Attributes Default Description
keyId String <optional>
'keyId'

輸入各筆數據之主鍵字串,預設'keyId'

fdTagRemove String <optional>
'./_tagRemove'

輸入暫存標記為刪除數據資料夾字串,預設'./_tagRemove'

fdDwAttime String <optional>
'./_dwAttime'

輸入儲存下載數據資料夾字串,預設'./_dwAttime'

fdDwCurrent String <optional>
'./_dwCurrent'

輸入儲存前次數據資料夾字串,預設'./_dwCurrent'

fdTaskCpActualSrc String <optional>
`./_taskCpActualSrc`

輸入任務狀態之來源端完整資料夾字串,預設./_taskCpActualSrc

fdTaskCpSrc String <optional>
`./_taskCpSrc`

輸入任務狀態之來源端資料夾字串,預設./_taskCpSrc

fdLog String <optional>
'./_logs'

輸入儲存log資料夾字串,預設'./_logs'

funDownload function <optional>
null

輸入取得下載數據之函數,回傳資料陣列,預設null

funGetCurrent function <optional>
null

輸入取得前次數據之函數,回傳資料陣列,預設null

funAdd function <optional>
null

輸入當有新資料時,需要連動處理之函數,預設null

funModify function <optional>
null

輸入當有資料需更新時,需要連動處理之函數,預設null

funRemove function <optional>
null

輸入當有資料需刪除時,需要連動處理之函數,預設null

funAfterStart function <optional>
null

輸入偵測程序剛開始啟動時,需要處理之函數,預設null

funBeforeEnd function <optional>
null

輸入偵測程序要結束前,需要處理之函數,預設null

timeToleranceRemove Number <optional>
0

輸入刪除任務之防抖時長,單位ms,預設0,代表不使用

Returns:

回傳事件物件,可呼叫函數on監聽change事件,可呼叫函數srlog額外進行事件紀錄

Type
Object