Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified compesys.sh
100644 → 100755
Empty file.
142 changes: 86 additions & 56 deletions iottrain_central.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -466,70 +472,94 @@ 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;
});

};

/**
* Set PWM value for iot train
* @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
Expand Down
Empty file added nohup.out
Empty file.
10 changes: 8 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 8 additions & 8 deletions routes/matchmaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down