From e68715ff4f23eb94a975aca7c7cbf5b0a6e3f992 Mon Sep 17 00:00:00 2001 From: Masayuki Hikichi Date: Sat, 23 Sep 2023 10:14:07 +0900 Subject: [PATCH] Async-lock module test. --- compesys.sh | 0 iottrain_central.js | 142 ++++++++++++++++++++++++++----------------- nohup.out | 0 package-lock.json | 10 ++- package.json | 1 + routes/matchmaker.js | 16 ++--- 6 files changed, 103 insertions(+), 66 deletions(-) mode change 100644 => 100755 compesys.sh create mode 100644 nohup.out diff --git a/compesys.sh b/compesys.sh old mode 100644 new mode 100755 diff --git a/iottrain_central.js b/iottrain_central.js index 46e673a..2b0f7f2 100644 --- a/iottrain_central.js +++ b/iottrain_central.js @@ -5,6 +5,10 @@ const loggerChild = logger.child({ domain: "iottrain_central" }); const sleep = (msec) => new Promise((resolve) => setTimeout(resolve, msec)); +var AsyncLock = require('async-lock'); +var lock = new AsyncLock({timeout:5000}); + + // BLE peripheral GATT profile: XIAO side const GATT_PROFILE = { services: { @@ -246,7 +250,9 @@ noble.on("discover", async (peripheral) => { if (key === 'voltage') { loggerChild.info('[noble]subscribe: ' + key); await instance.subscribeAsync(); - instance.on('data', async (data, isNotification) => { + //loggerChild.info('wait [noble]subscribe: ' + key); + await sleep(100); + instance.on('data', async (data, isNotification) => { noble.inbox.mabeee["voltage"].value = data.readUInt8(0) * Math.sqrt(2) / 100.0; if ( noble.inbox.mabeee["voltage"].value <= 1.2 && @@ -466,40 +472,51 @@ const fetchGyroscope = () => { * fetch voltage for iot train * @returns */ -const fetchVoltage = () => { - return new Promise((resolve, reject) => { - noble.mabeee.characteristics["voltage"].instance.write( - new Buffer.from([0x00]), - true, - (error) => { - if (error !== null) { - return reject(error); +const fetchVoltage = () => { + new Promise((resolve, reject) => { + lock.acquire('my-lock', async (resolve, reject) => { + + noble.mabeee.characteristics["voltage"].instance.write( + new Buffer.from([0x00]), + true, + (error) => { + if (error !== null) { + return reject(error); + } + //return resolve(); + }); + await sleep(100); + }, + (err,result) => { + if(err) { + return reject(err); + } + return resolve(); + } - return resolve(); - } - ); - }) - .then(() => { - // if (data.readFloatLE(4) !== 0) { - // noble.inbox.mabeee["voltage"].timestamp = data.readFloatLE(0); - // noble.inbox.mabeee["voltage"].value = data.readFloatLE(4); - // if ( - // noble.inbox.mabeee["voltage"].value <= 1.2 && - // noble.inbox.mabeee["voltage"].value > 0 - // ) { - // loggerChild.warn( - // "battery voltage is low!! :" + noble.inbox["voltage"].value + "V" - // ); - // } - // } - return; - }) - .catch((error) => { - loggerChild.error(error); - noble.inbox.mabeee["voltage"].timestamp = null; - noble.inbox.mabeee["voltage"].value = null; - return; - }); + ); // lock + }).then(() => { + // if (data.readFloatLE(4) !== 0) { + // noble.inbox.mabeee["voltage"].timestamp = data.readFloatLE(0); + // noble.inbox.mabeee["voltage"].value = data.readFloatLE(4); + // if ( + // noble.inbox.mabeee["voltage"].value <= 1.2 && + // noble.inbox.mabeee["voltage"].value > 0 + // ) { + // loggerChild.warn( + // "battery voltage is low!! :" + noble.inbox["voltage"].value + "V" + // ); + // } + // } + // return; + }) + .catch((error) => { + loggerChild.error(error); + noble.inbox.mabeee["voltage"].timestamp = null; + noble.inbox.mabeee["voltage"].value = null; + //return; + }); + }; /** @@ -507,29 +524,42 @@ const fetchVoltage = () => { * @param {number} pwm * @returns */ +prev_pwm=0; const setPwm = (pwm) => { - return new Promise((resolve, reject) => { - setTimeout(() => reject('timeout'), 1000); - noble.inbox.mabeee["pwm"].targetValue = pwm; - noble.mabeee.characteristics["pwm"].instance.write( - new Buffer.from([0x01, pwm, 0x0, 0x0, 0x0]), - false, - (error) => { - if (error !== null) { - return reject(error); - } - return resolve(); - } - ); - }) - .then(() => { - return null; - }) - .catch((error) => { - loggerChild.error(error); - return error; - }); -}; + return new Promise((resolve, reject) => { + setTimeout(() => reject('timeout'), 1000); + noble.inbox.mabeee["pwm"].targetValue = pwm; + lock.acquire('my-lock', async (resolve, reject) => { + if(prev_pwm!=pwm) { + prev_pwm=pwm; + noble.mabeee.characteristics["pwm"].instance.write( + new Buffer.from([0x01, pwm, 0x0, 0x0, 0x0]), + false, + (error) => { + if (error !== null) { + return reject(error); + } + //return resolve(); + } + ) + await sleep(100); + } + } + ,(err,result) => { + if(err) { + loggerChild.error(err); + return reject(err); + } + return resolve(); + }); + }).then((result) => { + return null; + }).catch((error) => { + loggerChild.error(error); + return error; + }); +} + /** * fetch pwm diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000..e69de29 diff --git a/package-lock.json b/package-lock.json index ebb75f5..b5b3c1a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "compesys", - "version": "0.0.1", + "version": "1.1.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "compesys", - "version": "0.0.1", + "version": "1.1.3", "license": "MIT", "dependencies": { "@abandonware/noble": "^1.9.2-21", "archiver": "^5.3.1", + "async-lock": "^1.4.0", "body-parser": "^1.20.2", "dotenv": "^16.3.1", "express": "^4.18.2", @@ -293,6 +294,11 @@ "resolved": "https://registry.npmjs.org/async/-/async-3.2.4.tgz", "integrity": "sha512-iAB+JbDEGXhyIUavoDl9WP/Jj106Kz9DEn1DPgYw5ruDn0e3Wgi3sKFm55sASdGBNOQB8F59d9qQ7deqrHA8wQ==" }, + "node_modules/async-lock": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/async-lock/-/async-lock-1.4.0.tgz", + "integrity": "sha512-coglx5yIWuetakm3/1dsX9hxCNox22h7+V80RQOu2XUUMidtArxKoZoOtHUPuR84SycKTXzgGzAUR5hJxujyJQ==" + }, "node_modules/atomic-sleep": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/atomic-sleep/-/atomic-sleep-1.0.0.tgz", diff --git a/package.json b/package.json index 67d0dd1..e1ef619 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "dependencies": { "@abandonware/noble": "^1.9.2-21", "archiver": "^5.3.1", + "async-lock": "^1.4.0", "body-parser": "^1.20.2", "dotenv": "^16.3.1", "express": "^4.18.2", diff --git a/routes/matchmaker.js b/routes/matchmaker.js index 3b595f6..e00bff1 100644 --- a/routes/matchmaker.js +++ b/routes/matchmaker.js @@ -8,14 +8,14 @@ const { STATE, MATCHMAKER_IP } = require("../constants"); const { logger } = require("../logger.js"); const loggerChild = logger.child({ domain: "matchmaker" }); -router.use(error); -router.use((req, res, next) => { - if (req.ip !== MATCHMAKER_IP) { - const error = new RequestError(403, "Request not currently allowed"); - return res.status(error.statusCode).error(error); - } - next(); -}); +//router.use(error); +//router.use((req, res, next) => { +// if (req.ip !== MATCHMAKER_IP) { +// const error = new RequestError(403, "Request not currently allowed"); +// return res.status(error.statusCode).error(error); +// } +// next(); +//}); router.put("/state/:trigger", (req, res) => { try {