Skip to content

Commit

Permalink
Serialize the simultaneous commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
banboobee committed Aug 13, 2022
1 parent 9ec337e commit 7ed70dc
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
3 changes: 3 additions & 0 deletions helpers/getDevice.js
Expand Up @@ -2,6 +2,7 @@ const ping = require('ping');
const broadlink = require('./broadlink');
const delayForDuration = require('./delayForDuration');
const dgram = require('dgram');
const Mutex = require('await-semaphore').Mutex;

const pingFrequency = 5000;
const keepAliveFrequency = 90000;
Expand Down Expand Up @@ -100,6 +101,8 @@ const discoverDevices = (automatic = true, log, logLevel, deviceDiscoveryTimeout
const addDevice = (device) => {
if (!device.isUnitTestDevice && (discoveredDevices[device.host.address] || discoveredDevices[device.host.macAddress])) {return;}

device.mutex = new Mutex();

discoveredDevices[device.host.address] = device;
discoveredDevices[device.host.macAddress] = device;
}
Expand Down
12 changes: 7 additions & 5 deletions helpers/sendData.js
Expand Up @@ -3,7 +3,7 @@ const assert = require('assert')
const { getDevice } = require('./getDevice');
const convertProntoCode = require('./convertProntoCode')

module.exports = ({ host, hexData, log, name, logLevel }) => {
module.exports = async ({ host, hexData, log, name, logLevel }) => {
assert(hexData && typeof hexData === 'string', `\x1b[31m[ERROR]: \x1b[0m${name} sendData (HEX value is missing)`);

// Check for pronto code
Expand All @@ -28,8 +28,10 @@ module.exports = ({ host, hexData, log, name, logLevel }) => {
if (!device.sendData) {return log(`\x1b[31m[ERROR] \x1b[0mThe device at ${device.host.address} (${device.host.macAddress}) doesn't support the sending of IR or RF codes.`);}
if (hexData.includes('5aa5aa555')) {return log(`\x1b[31m[ERROR] \x1b[0mThis type of hex code (5aa5aa555...) is no longer valid. Use the included "Learn Code" accessory to find new (decrypted) codes.`);}

const hexDataBuffer = new Buffer(hexData, 'hex');
device.sendData(hexDataBuffer, logLevel, hexData);

if (logLevel <=2) {log(`${name} sendHex (${device.host.address}; ${device.host.macAddress}) ${hexData}`);}
await device.mutex.use(async () => {
const hexDataBuffer = new Buffer(hexData, 'hex');
device.sendData(hexDataBuffer, logLevel, hexData);

if (logLevel <=2) {log(`${name} sendHex (${device.host.address}; ${device.host.macAddress}) ${hexData}`);}
});
}
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -40,7 +40,8 @@
"node-persist": ">=2.1.0 <3.0.0",
"semver": "^7.3.6",
"node-arp": "^1.0.6",
"fakegato-history": "^0.6.3"
"fakegato-history": "^0.6.3",
"await-semaphore": "^0.1.3"
},
"devDependencies": {
"eslint": "^8.12.0",
Expand Down

0 comments on commit 7ed70dc

Please sign in to comment.