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/dxEventBus.js |  167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 167 insertions(+), 0 deletions(-)

diff --git a/vf205_access/dxmodules/dxEventBus.js b/vf205_access/dxmodules/dxEventBus.js
new file mode 100644
index 0000000..4e450dd
--- /dev/null
+++ b/vf205_access/dxmodules/dxEventBus.js
@@ -0,0 +1,167 @@
+//build:20240628
+//浜嬩欢鎬荤嚎锛屽埄鐢╭uickjs鐨剋orker闂存暟鎹�氫俊鏉ュ疄鐜扮嚎绋嬩箣闂村彂閫佷簨浠堕�氱煡銆�
+//worker鍜寃orker涔嬮棿涓嶈兘鐩存帴閫氫俊锛岄渶瑕侀�氳繃parent(涓荤嚎绋�)鏉ヨ浆鍙戯紝鎵�浠ラ渶瑕佸疄鐜�5绉嶅彲鑳芥�х殑浜嬩欢閫氱煡
+//1. worker1--->parent--->worker2
+//2. worker3--->parent
+//3. parent--->worker4
+//4. parent<-->parent 
+//5. worker5<--->worker5,涔熶細閫氳繃parent杞�
+//缁勪欢渚濊禆 dxLogger,dxCommon
+import std from './dxStd.js'
+import logger from './dxLogger.js'
+import * as os from "os";
+//-------------------------variable--------------------
+const bus = {}
+const all = {}
+const subs = {}
+const isMain = (os.Worker.parent === undefined)
+bus.id = isMain ? '__main' : null
+/**
+ * 鍦ㄦ�荤嚎涓婂惎鍔ㄤ竴涓獁orker锛岀粰瀹冨畾涔変竴涓敮涓�鐨刬d鏍囪瘑
+ * 鍥犱负worker鍙兘閫氳繃涓荤嚎绋嬪垱寤猴紝鎵�浠ewWorker鍑芥暟涔熷彧鑳藉湪涓荤嚎绋嬮噷鎵ц
+ * 娉ㄦ剰: worker瀵瑰簲鐨勬枃浠堕噷涓嶈兘鍖呭惈while(true)杩欑姝诲惊鐜紝鍚﹀垯灏辨敹涓嶅埌message锛屽彲浠ョ敤setInteval鏉ュ疄鐜板惊鐜�
+ * @param {string} id worker鐨勫敮涓�鏍囪瘑锛屼笉鑳戒负绌�
+ * @param {object} file worker瀵瑰簲鐨勬枃浠跺悕锛岀粷瀵硅矾寰勶紝閫氬父浠�'/app/code/src'寮�濮�
+ */
+bus.newWorker = function (id, file) {
+    if (!id) {
+        throw new Error("eventbus newWorker:'id' should not be empty")
+    } if (!file) {
+        throw new Error("eventbus newWorker:'file' should not be empty")
+    }
+    if (!isMain) {
+        throw new Error("evnetbus newWorker should be invoke in main thread")
+    }
+    if (!std.exist(file)) {
+        throw new Error("eventbus newWorker: file not found:" + file)
+    }
+    let content = std.loadFile(file) + `
+import __bus from '/app/code/dxmodules/dxEventBus.js'
+__bus.id='${id}'
+Object.keys(__bus.handlers).forEach(key => {
+    __bus.os.Worker.parent.postMessage({ __sub: key, id: __bus.id })
+})
+__bus.os.Worker.parent.onmessage = function (e) {
+    if(!e.data){
+        return
+    }
+    e = e.data
+    if (!e || !e.topic) {
+        return
+    }
+    let fun = __bus.handlers[e.topic]
+    if (fun) {
+        fun(e.data)
+    }
+}
+    `
+    let newfile = file + '_' + id + '.js'
+    std.saveFile(newfile, content)
+    let worker = new os.Worker(newfile)
+    all[id] = worker
+    worker.onmessage = function (data) {
+        if (data.data) {
+            if (data.data.__sub) {
+                sub(data.data.__sub, data.data.id)
+                return
+            }
+            //worker鍙戦�佽繃鏉ョ殑鏁版嵁鍐嶈皟鐢ㄤ竴娆′富绾跨▼鐨刦ire锛岃涔堜富绾跨▼鑷繁娑堣垂锛岃涔堣浆鍙戝埌鍏跺畠worker
+            bus.fire(data.data.topic, data.data.data)
+        }
+    }
+}
+/**
+ * 鏍规嵁id鍒犻櫎瀵瑰簲鐨剋orker锛岃繖鏍穡orker绾跨▼灏辫兘姝e父缁撴潫
+ * @param {string} id 
+ */
+bus.delWorker = function (id) {
+    delete all[id]
+}
+/**
+ * 瑙﹀彂涓�涓簨浠讹紝杩欎釜浜嬩欢浼氱珛鍒诲彂閫佺粨鏉燂紝鎺ユ敹鍒版秷鎭殑澶勭悊濡傛灉姣旇緝鑰楁椂涓嶄細褰卞搷浜嬩欢鍙戦�佺殑椤哄簭鎴栧嚭鐜颁簨浠朵涪澶�
+ * 鍚屾牱涓�涓簨浠跺彲浠ユ湁澶氫釜璁㈤槄鑰咃紝鍙互鍚屾椂閫氱煡澶氫釜璁㈤槄鑰咃紝鍚屼竴涓猼opic鍗曚綅鏃堕棿鍐呭彧澶勭悊涓�涓簨浠讹紝
+ * 鍙湁褰撳墠topic琚墍鏈夌殑璁㈤槄鑰呭鐞嗗畬涔嬪悗鎵嶅厑璁稿鐞嗗悓涓�topic涓嬩竴涓簨浠�
+ * 
+ * @param {string} topic 浜嬩欢鐨勬爣璇嗐�佷富棰� 
+ * @param {*} data 浜嬩欢闄勫甫鐨勬暟鎹�
+ */
+bus.fire = function (topic, data) {
+    if (!topic || (typeof topic) != 'string') {
+        throw new Error("eventbus :'topic' should not be null");
+    }
+    if (isMain) {
+        if (subs[topic] && subs[topic].length > 0) {
+            for (let i = 0; i < subs[topic].length; i++) {
+                const id = subs[topic][i]
+                if (id === '__main' && bus.handlers[topic]) {
+                    if (Array.isArray(bus.handlers[topic])) {
+                        // 鎵ц鎵�鏈夋敞鍐岀殑澶勭悊鍑芥暟
+                        for (let j = 0; j < bus.handlers[topic].length; j++) {
+                            try {
+                                bus.handlers[topic][j](data)
+                            } catch (error) {
+                                logger.error('Error in event handler for topic ' + topic + ': ' + error.message)
+                            }
+                        }
+                    } else {
+                        // 鍏煎鏃х増鏈紝鎵ц鍗曚釜澶勭悊鍑芥暟
+                        try {
+                            bus.handlers[topic](data)
+                        } catch (error) {
+                            logger.error('Error in event handler for topic ' + topic + ': ' + error.message)
+                        }
+                    }
+                } else {
+                    const worker = all[id]
+                    if (worker) {
+                        worker.postMessage({ topic: topic, data: data })
+                    }
+                }
+            }
+        }
+    } else {
+        os.Worker.parent.postMessage({ topic: topic, data: data })
+    }
+}
+
+
+bus.handlers = {}
+/**
+ * 璁㈤槄涓�涓簨浠�
+ * @param {string} topic 浜嬩欢鐨勬爣璇嗐�佷富棰� 锛屽繀濉�
+ * @param {function} callback 浜嬩欢澶勭悊鐨勫洖璋冨嚱鏁帮紝蹇呭~
+ */
+bus.on = function (topic, callback) {
+    if (!topic || (typeof topic) != 'string') {
+        throw new Error("The 'topic' should not be null");
+    }
+    if (!callback || (typeof callback) != 'function') {
+        throw new Error("The 'callback' should be a function");
+    }
+    sub(topic, bus.id)
+    // 鏀寔澶氫釜浜嬩欢澶勭悊鍑芥暟
+    if (!this.handlers[topic]) {
+        this.handlers[topic] = []
+    }
+    if (!Array.isArray(this.handlers[topic])) {
+        // 鍏煎鏃х増鏈紝灏嗗崟涓嚱鏁拌浆鎹负鏁扮粍
+        this.handlers[topic] = [this.handlers[topic]]
+    }
+    this.handlers[topic].push(callback)
+}
+function sub(topic, id) {
+    if (isMain) {
+        if (!subs[topic]) {
+            subs[topic] = []
+        }
+        if (!subs[topic].includes(id)) {
+            subs[topic].push(id)
+        }
+    } else {
+        if (id != null) {
+            os.Worker.parent.postMessage({ __sub: topic, id: id })
+        }
+    }
+}
+bus.os = os
+export default bus

--
Gitblit v1.9.3