Skip to content

Commit

Permalink
Merge pull request #416 from martijnpoppen/develop
Browse files Browse the repository at this point in the history
OPT: persistentData via config instead of JSON file
  • Loading branch information
bropat authored Dec 2, 2023
2 parents d581182 + 0dfd78e commit dc7df49
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/eufysecurity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,15 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
} else if (!fse.existsSync(this.config.persistentDir)) {
this.config.persistentDir = path.resolve(__dirname, "../../..");
}
this.persistentFile = path.join(this.config.persistentDir, "persistent.json");

if (this.config.persistentData) {
this.persistentData = JSON.parse(this.config.persistentData) as EufySecurityPersistentData;
} else {
this.persistentFile = path.join(this.config.persistentDir, "persistent.json");
}

try {
if (fse.statSync(this.persistentFile).isFile()) {
if (!this.config.persistentData && fse.statSync(this.persistentFile).isFile()) {
const fileContent = fse.readFileSync(this.persistentFile, "utf8");
this.persistentData = JSON.parse(fileContent) as EufySecurityPersistentData;
}
Expand Down Expand Up @@ -862,7 +867,11 @@ export class EufySecurity extends TypedEmitter<EufySecurityEvents> {
this.persistentData.httpApi = this.api?.getPersistentData();
this.persistentData.country = this.api?.getCountry();
try {
fse.writeFileSync(this.persistentFile, JSON.stringify(this.persistentData));
if(this.config.persistentData) {
this.emit("persistent data", JSON.stringify(this.persistentData));
} else {
fse.writeFileSync(this.persistentFile, JSON.stringify(this.persistentData));
}
} catch (err) {
const error = ensureError(err);
this.log.error("WritePersistentData Error", { error: getError(error) });
Expand Down
2 changes: 2 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface EufySecurityConfig {
language?: string;
trustedDeviceName?: string;
persistentDir?: string;
persistentData?: string;
p2pConnectionSetup: number;
pollingIntervalMinutes: number;
eventDurationSeconds: number;
Expand Down Expand Up @@ -109,6 +110,7 @@ export interface EufySecurityEvents {
"connection error": (error: Error) => void;
"tfa request": () => void;
"captcha request": (id: string, captcha: string) => void;
"persistent data": (data: string) => void;
"mqtt connect": () => void;
"mqtt close": () => void;
"mqtt lock message": (message: DeviceSmartLockMessage) => void;
Expand Down

0 comments on commit dc7df49

Please sign in to comment.