Skip to content

Commit

Permalink
Some more code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
copystring committed Oct 14, 2024
1 parent fedd355 commit 2e917a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 15 deletions.
19 changes: 7 additions & 12 deletions lib/RRMapParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,7 @@ class RRMapParser {
}

BytesToInt(buffer, offset, len) {
let result = 0;

for (let i = 0; i < len; i++) {
result |= (0x000000FF & parseInt(buffer[i + offset])) << 8 * i;
}

return result;
return buffer.slice(offset, offset + len).reduce((acc, byte, i) => acc | (byte << (8 * i)), 0);
}

async parsedata(buf) {
Expand All @@ -90,8 +84,7 @@ class RRMapParser {

let dataPosition = 0x14; // Skip header

const result = {};
result.metaData = metaData;
const result = { metaData };

Check failure on line 87 in lib/RRMapParser.js

View workflow job for this annotation

GitHub Actions / check-and-lint

Expected indentation of 2 tabs but found 8 spaces

while (dataPosition < metaData.data_length) {
const type = buf.readUInt16LE(dataPosition);
Expand All @@ -104,7 +97,6 @@ class RRMapParser {
// this.adapter.log.debug("Known values: type=" + type + ", hlength=" + hlength + ", length=" + length);

if (TYPES_REVERSE[type]) {

// this.adapter.log.debug("Test length: " + TYPES_REVERSE[type] + " " + length);
// if (length < 100) this.adapter.log.debug("Test data type: " + TYPES_REVERSE[type] + " " + buf.toString("hex", dataPosition, dataPosition + length));

Expand Down Expand Up @@ -294,9 +286,12 @@ class RRMapParser {
major: mapBuf.readUInt16LE(0x08),
minor: mapBuf.readUInt16LE(0x0a),
},
map_index: mapBuf.readUInt32LE(0x0C),
map_index: mapBuf.readUInt32LE(0x0c),
map_sequence: mapBuf.readUInt32LE(0x10),
SHA1: crypto.createHash("sha1").update(Uint8Array.prototype.slice.call(mapBuf, 0, mapBuf.length - 20)).digest("hex"),
SHA1: crypto
.createHash("sha1")
.update(Uint8Array.prototype.slice.call(mapBuf, 0, mapBuf.length - 20))
.digest("hex"),
expectedSHA1: Buffer.from(Uint8Array.prototype.slice.call(mapBuf, mapBuf.length - 20)).toString("hex"),
};
} else {
Expand Down
6 changes: 3 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ class Roborock extends utils.Adapter {
this.log.info(`Starting adapter finished. Lets go!!!!!!!`);
} else {
this.log.info(`Most likely failed to login. Deleting UserData to force new login!`);
await this.deleteStateAsync(`UserData`);
await this.delObjectAsync(`UserData`);
}
}
} catch (error) {
Expand Down Expand Up @@ -244,8 +244,8 @@ class Roborock extends utils.Adapter {
return userdata;
} catch (error) {
this.log.error(`Error in getUserData: ${error.message}`);
await this.deleteStateAsync("HomeData");
await this.deleteStateAsync("UserData");
await this.delObjectAsync("HomeData");
await this.delObjectAsync("UserData");
throw error;
}
}
Expand Down

0 comments on commit 2e917a3

Please sign in to comment.