From e491cdb48129752324c4e3764f99bd9203c56dec Mon Sep 17 00:00:00 2001
From: lgq <1015864684@qq.com>
Date: 星期二, 31 三月 2026 09:48:44 +0800
Subject: [PATCH] 1.新增VF205门禁机代码

---
 vf205_access/dxmodules/dxWorkerPool.js |  167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 167 insertions(+), 0 deletions(-)

diff --git a/vf205_access/dxmodules/dxWorkerPool.js b/vf205_access/dxmodules/dxWorkerPool.js
new file mode 100644
index 0000000..91cbf5d
--- /dev/null
+++ b/vf205_access/dxmodules/dxWorkerPool.js
@@ -0,0 +1,167 @@
+//build:20240717
+//绾跨▼姹狅紝閲岄潰鍔犺浇澶氫釜worker锛岀嚎绋嬫睜鎺ユ敹浠诲姟鎴栦簨鍔″悗鐒跺悗娲惧彂缁欑嚎绋嬫睜閲岄潰绌洪棽鐨剋orker鏉ユ墽琛屼换鍔★紝鐢ㄤ簬瑙e喅澶氫簨鍔″鐞嗙殑鐡堕
+//璁惧璧勬簮鏈夐檺锛岀嚎绋嬫暟閲忎笉瀹滃お澶氾紝鍙﹀涔熶笉鑰冭檻澶氫釜绾跨▼姹犵殑鎯呭喌锛屽叏灞�鍙竴涓�
+//缁勪欢渚濊禆 dxLogger,dxCommon锛宒xStd
+import std from './dxStd.js'
+import logger from './dxLogger.js'
+import * as os from "os";
+//-------------------------variable--------------------
+const pool = {}
+const isMain = (os.Worker.parent === undefined)
+let queueSize = 100
+const queue = []
+const all = {}
+pool.os = os
+/**
+ * 鍒濆鍖栫嚎绋嬫睜锛岃缃畐orker涓暟鍜岀紦瀛橀槦鍒楀ぇ灏忥紝鏈夊彲鑳藉涓獁orker閮芥病鏈夌┖闂诧紝缂撳瓨闃熷垪鍙互缂撳瓨鏉ヤ笉鍙婂鐞嗙殑浜嬪姟
+ * 鍥犱负worker鍙兘閫氳繃涓荤嚎绋嬪垱寤猴紝鎵�浠nit鍑芥暟涔熷彧鑳藉湪涓荤嚎绋嬮噷鎵ц
+ * 娉ㄦ剰: worker瀵瑰簲鐨勬枃浠堕噷涓嶈兘鍖呭惈while(true)杩欑姝诲惊鐜紝鍙互鐢╯etInteval鏉ュ疄鐜板惊鐜�
+ * @param {string} file worker瀵瑰簲鐨勬枃浠跺悕锛屽繀濉紝缁濆璺緞锛岄�氬父浠�'/app/code/src'寮�濮�
+ * @param {Object} bus EventBus瀵硅薄 蹇呭~
+ * @param {Array} topics 瑕佽闃呯殑涓婚缁� 蹇呭~
+ * @param {number} count 绾跨▼鐨勪釜鏁帮紝闈炲繀濉紝涓嶈兘灏忎簬1锛岀己鐪�2,
+ * @param {number} maxsize 浜嬪姟缂撳瓨鐨勫ぇ灏忥紝闈炲繀濉紝缂虹渷100锛屽鏋滆秴杩�100锛屾渶鑰佺殑浜嬪姟琚姏寮�
+ */
+pool.init = function (file, bus, topics, count = 2, maxsize = 100) {
+    if (!file) {
+        throw new Error("pool init:'file' should not be empty")
+    }
+    if (!bus) {
+        throw new Error("pool init:'bus' should not be empty")
+    }
+    if (!topics) {
+        throw new Error("pool init:'topics' should not be empty")
+    }
+    if (!isMain) {
+        throw new Error("pool init should be invoked in main thread")
+    }
+    if (!std.exist(file)) {
+        throw new Error("pool init: file not found:" + file)
+    }
+    queueSize = maxsize
+    if (count <= 1) {
+        count = 1
+    }
+    for (let i = 0; i < count; i++) {
+        const id = 'pool__id' + i
+        let content = std.loadFile(file) + `
+import __pool from '/app/code/dxmodules/dxWorkerPool.js'
+__pool.id = '${id}'
+const __parent = __pool.os.Worker.parent
+__parent.onmessage = function (e) {
+    if (!e.data) {
+        return
+    }
+    let fun = __pool.callbackFunc
+    if (fun) {
+        try {
+            fun(e.data)
+            __parent.postMessage({ id: __pool.id })//閫氱煡澶勭悊瀹屼簡idle
+        } catch (err) {
+            __parent.postMessage({ id: __pool.id, error: err.stack })//閫氱煡澶勭悊瀹屼簡idle,浣嗘槸澶辫触浜�    
+        }
+    }
+}
+            `
+        let newfile = file + '_' + id + '.js'
+        std.saveFile(newfile, content)
+        let worker = new os.Worker(newfile)
+        all[id] = { isIdle: true, worker: worker }
+        worker.onmessage = function (data) {
+            if (!data.data) {
+                return
+            }
+            const id = data.data.id
+            if (id) {//閫氱煡澶勭悊瀹屾垚鐨勬秷鎭�
+                all[id].isIdle = true
+                if (data.data.error) {
+                    logger.error(`worker ${id} callback error:${data.data.error}`)
+                }
+            } else {
+                const topic = data.data.topic
+                if (topic) {//bus.fire鍑烘潵鐨勬秷鎭�
+                    bus.fire(topic, data.data.data)
+                }
+            }
+        }
+    }
+    for (let topic of topics) {
+        bus.on(topic, function (d) {
+            push({ topic: topic, data: d })
+        })
+    }
+
+    std.setInterval(function () {
+        Object.keys(all).forEach(key => {
+            const obj = all[key]
+            if (obj.isIdle) {
+                let event = take()
+                if (event) {
+                    obj.isIdle = false
+                    obj.worker.postMessage(event)
+                }
+            }
+        });
+    }, 5)
+}
+/**
+ * 杩斿洖绾跨▼鐨勫敮涓�鏍囪瘑id
+ * @returns worker鍞竴鏍囪瘑
+ */
+pool.getWorkerId = function () {
+    if (isMain) {
+        return 'main'
+    } else {
+        return pool.id
+    }
+}
+/**
+ * 璁㈤槄EventBus 涓婄殑浜嬪姟涓婚锛屽彲浠ヨ闃呭涓富棰�,杩欎釜鍑芥暟涔熷彧鑳藉湪涓荤嚎绋嬮噷鎵ц
+ * @param {Object} bus EventBus瀵硅薄
+ * @param {Array} topics 瑕佽闃呯殑涓婚缁�
+ */
+pool.on = function (bus, topics) {
+    if (!bus) {
+        throw new Error("pool onEventBus:'bus' should not be empty")
+    }
+    if (!topics) {
+        throw new Error("pool onEventBus:'topics' should not be empty")
+    }
+    if (!isMain) {
+        throw new Error("pool onEventBus should be invoked in main thread")
+    }
+
+}
+
+pool.callbackFunc = null
+/**
+ * worker绾跨▼璁㈤槄绾跨▼姹犵殑浜嬩欢锛屼笉鐢ㄩ�夋嫨鐗瑰畾鐨勪富棰橈紝绾跨▼姹犲叧娉ㄧ殑鎵�鏈変簨浠堕兘浼氬鐞�,
+ * 杩欎釜鍑芥暟蹇呴』鍦╳orker绾跨▼閲屾墽琛岋紝涓嶈兘鍦ㄤ富绾跨▼鎵ц
+ * @param {function} cb 浜嬩欢澶勭悊鐨勫洖璋冨嚱鏁帮紝蹇呭~
+ */
+pool.callback = function (cb) {
+    if (!cb || (typeof cb) != 'function') {
+        throw new Error("pool on :The 'callback' should be a function");
+    }
+    if (isMain) {
+        throw new Error("pool on should not be invoked in main thread")
+    }
+    pool.callbackFunc = cb
+}
+
+function push(item) {
+    if (queue.length >= queueSize) {
+        const first = JSON.stringify(queue[0])
+        logger.error(`pool queue is full,removing oldest element: ${first}`)
+        queue.shift(); // 绉婚櫎鏈�鑰佺殑鍏冪礌
+    }
+    queue.push(item);
+}
+
+function take() {
+    if (queue.length === 0) {
+        return null; // 闃熷垪涓虹┖鏃惰繑鍥� null
+    }
+    return queue.shift(); // 绉婚櫎骞惰繑鍥炴渶鏃╂坊鍔犵殑鍏冪礌
+}
+export default pool

--
Gitblit v1.9.3