Skip to content

Commit bfe965c

Browse files
Refactor CPU info handling and validation
Refactor CPU info retrieval and improve Raspberry Pi check.
1 parent cde477d commit bfe965c

File tree

1 file changed

+17
-36
lines changed

1 file changed

+17
-36
lines changed

index.js

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,8 @@
33
const rpio = require("rpio");
44
const fs = require("fs");
55

6-
let Service;
7-
let Characteristic;
8-
9-
// ======================
10-
// Get cpuinfo
11-
// ======================
12-
13-
let cpuinfo = "";
14-
try {
15-
cpuinfo = fs.readFileSync("/proc/cpuinfo", "utf8");
16-
} catch {
17-
cpuinfo = "";
18-
}
6+
let Service, Characteristic;
7+
const cpuInfoCache = fs.readFileSync("/proc/cpuinfo", "utf8");
198

209
module.exports = (homebridge) => {
2110
Service = homebridge.hap.Service;
@@ -29,23 +18,17 @@ module.exports = (homebridge) => {
2918
);
3019
};
3120

32-
// ======================
33-
// Utility
34-
// ======================
35-
21+
// Get Raspberry Pi serial number
3622
function getSerial() {
37-
try {
38-
const match = cpuinfo.match(/Serial\s*:\s*([a-f0-9]+)/i);
39-
return match ? match[1] : "unknown";
40-
} catch {
41-
return "unknown";
42-
}
23+
try {
24+
const serialLine = cpuInfoCache.split("\n").find((line) => line.startsWith("Serial"));
25+
return serialLine ? serialLine.split(":")[1].trim() : "UNKNOWN";
26+
} catch {
27+
return "UNKNOWN";
28+
}
4329
}
4430

45-
// ======================
46-
// Platform
47-
// ======================
48-
31+
// Platform class
4932
class ElectricRimLockPlatform {
5033

5134
constructor(log, config, api) {
@@ -110,14 +93,10 @@ class ElectricRimLockPlatform {
11093
}
11194
}
11295

113-
// ======================
114-
// Accessory
115-
// ======================
116-
96+
// Electric Rim Lock Accessory
11797
class ElectricRimLockAccessory {
118-
11998
constructor(platform, accessory) {
120-
99+
121100
this.platform = platform;
122101
this.log = platform.log;
123102
this.accessory = accessory;
@@ -130,15 +109,17 @@ class ElectricRimLockAccessory {
130109
this.version = require("./package.json").version;
131110
this.busy = false;
132111

112+
// Validate name and pin
133113
if (!this.name || !this.pin) {
134114
this.log.warn("⚠ RimLock not configured correctly: name or pin missing. Plugin disabled.");
135115
this.disabled = true;
136116
return;
137117
}
138118

139-
if (!/Raspberry Pi/i.test(cpuinfo)) {
140-
this.log.warn("⚠ This plugin is intended to run only on Raspberry Pi. Some features may not work.");
141-
}
119+
// Raspberry Pi check
120+
if (!/Raspberry Pi/i.test(cpuInfoCache)) {
121+
this.log.warn("⚠ This plugin is intended for Raspberry Pi. Some features may not work.");
122+
}
142123

143124
platform.initPin(this.pin);
144125

0 commit comments

Comments
 (0)