@bitpoolos/edge-bacnet 1.6.3 → 1.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/common.js CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  const { randomUUID } = require("crypto");
6
6
  const os = require("os");
7
+ const path = require("path");
7
8
  const baEnum = require("./resources/node-bacstack-ts/dist/index.js").enum;
8
9
  const fs = require("fs");
9
10
  const fs2 = require("fs").promises;
@@ -189,27 +190,16 @@ const roundDecimalPlaces = function (value, decimals) {
189
190
  return value;
190
191
  };
191
192
 
192
- // STORE CONFIG FUNCTION ==========================================================
193
- //
194
- // ================================================================================
195
-
196
- /*
197
-
198
- async function Store_Config(data) {
199
- try {
200
- await fs.writeFile("edge-bacnet-datastore.cfg", data, { encoding: "utf8", flag: "w" }, (err) => {
201
- if (err) {
202
- console.log("Store_Config writeFile error: ", err);
203
- }
204
- });
205
- } catch (e) {
206
- //do nothing
193
+ const getStoragePath = (fileName) => {
194
+ const storagePath = process.env.BACNET_STORAGE_PATH;
195
+ if (storagePath) {
196
+ if (!fs.existsSync(storagePath)) {
197
+ fs.mkdirSync(storagePath, { recursive: true });
198
+ }
199
+ return path.join(storagePath, fileName);
207
200
  }
208
- }
209
-
210
- */
211
-
212
- // refactor:
201
+ return fileName;
202
+ };
213
203
 
214
204
  let storeQueue = [];
215
205
  let isStoreProcessing = false;
@@ -235,9 +225,9 @@ async function queueConfigStore(data) {
235
225
  }
236
226
 
237
227
  async function Store_Config(data) {
238
- const mainFile = "edge-bacnet-datastore.cfg";
239
- const tempFile = "edge-bacnet-datastore.cfg.tmp";
240
- const backupFile = "edge-bacnet-datastore.cfg.bak";
228
+ const mainFile = getStoragePath("edge-bacnet-datastore.cfg");
229
+ const tempFile = getStoragePath("edge-bacnet-datastore.cfg.tmp");
230
+ const backupFile = getStoragePath("edge-bacnet-datastore.cfg.bak");
241
231
 
242
232
  try {
243
233
  // First validate the JSON to ensure it's valid before writing
@@ -295,54 +285,10 @@ async function Store_Config(data) {
295
285
  }
296
286
  }
297
287
 
298
- // READ CONFIG SYNC FUNCTION ======================================================
299
- //
300
- // ================================================================================
301
-
302
- function Read_Config_Sync() {
303
- const mainFile = "edge-bacnet-datastore.cfg";
304
- const backupFile = "edge-bacnet-datastore.cfg.bak";
305
- const defaultData = "{}";
306
-
307
- try {
308
- // Try to read the main file
309
- let data = fsSync.readFileSync(mainFile, { encoding: "utf8" });
310
-
311
- // Validate JSON
312
- try {
313
- JSON.parse(data);
314
- return data;
315
- } catch (jsonError) {
316
- console.error("Main file contains invalid JSON, attempting backup recovery");
317
-
318
- // Try to read backup file
319
- try {
320
- const backupData = fsSync.readFileSync(backupFile, { encoding: "utf8" });
321
- JSON.parse(backupData); // Validate backup JSON
322
-
323
- // Restore from backup
324
- fsSync.copyFileSync(backupFile, mainFile);
325
- console.log("Successfully restored from backup file");
326
- return backupData;
327
- } catch (backupError) {
328
- console.error("Backup recovery failed, creating new file");
329
- fsSync.writeFileSync(mainFile, defaultData, { encoding: "utf8" });
330
- return defaultData;
331
- }
332
- }
333
- } catch (error) {
334
- console.error("Error reading config:", error);
335
- fsSync.writeFileSync(mainFile, defaultData, { encoding: "utf8" });
336
- return defaultData;
337
- }
338
- }
339
-
340
- // refactor:
341
-
342
288
  async function Read_Config_Async() {
343
289
  // todo rename function, not using sync
344
- const mainFile = "edge-bacnet-datastore.cfg";
345
- const backupFile = "edge-bacnet-datastore.cfg.bak";
290
+ const mainFile = getStoragePath("edge-bacnet-datastore.cfg");
291
+ const backupFile = getStoragePath("edge-bacnet-datastore.cfg.bak");
346
292
  const defaultData = "{}";
347
293
 
348
294
  try {
@@ -366,15 +312,11 @@ async function Read_Config_Async() {
366
312
  await fs.copyFile(backupFile, mainFile);
367
313
  console.log("Successfully restored from backup file");
368
314
 
369
- console.log("log2");
370
-
371
315
  return backupData;
372
316
  } catch (backupError) {
373
317
  console.error("Backup recovery failed, creating new file");
374
318
  await Store_Config(defaultData);
375
319
 
376
- console.log("log3");
377
-
378
320
  return defaultData;
379
321
  }
380
322
  }
@@ -382,8 +324,6 @@ async function Read_Config_Async() {
382
324
  console.error("Error reading config:", error);
383
325
  await Store_Config(defaultData);
384
326
 
385
- console.log("log4");
386
-
387
327
  return defaultData;
388
328
  }
389
329
  }
@@ -393,7 +333,7 @@ async function Read_Config_Async() {
393
333
  // ================================================================================
394
334
  async function Store_Config_Server(data) {
395
335
  try {
396
- await fs.writeFile("edge-bacnet-server-datastore.cfg", data, (err) => {
336
+ await fs.writeFile(getStoragePath("edge-bacnet-server-datastore.cfg"), data, (err) => {
397
337
  if (err) {
398
338
  //console.log("Store_Config_Server writeFile error: ", err);
399
339
  }
@@ -407,7 +347,7 @@ async function Store_Config_Server(data) {
407
347
  function Read_Config_Sync_Server() {
408
348
  var data = "{}";
409
349
  try {
410
- data = fs.readFileSync("edge-bacnet-server-datastore.cfg", { encoding: "utf8", flag: "r" });
350
+ data = fs.readFileSync(getStoragePath("edge-bacnet-server-datastore.cfg"), { encoding: "utf8", flag: "r" });
411
351
  } catch (err) {
412
352
  if (err.errno == -4058) {
413
353
  data = "{}";
@@ -485,7 +425,6 @@ module.exports = {
485
425
  roundDecimalPlaces,
486
426
  queueConfigStore,
487
427
  Store_Config,
488
- Read_Config_Sync,
489
428
  Read_Config_Async,
490
429
  Store_Config_Server,
491
430
  Read_Config_Sync_Server,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bitpoolos/edge-bacnet",
3
- "version": "1.6.3",
3
+ "version": "1.6.4",
4
4
  "description": "A bacnet gateway for node-red",
5
5
  "dependencies": {
6
6
  "@plus4nodered/ts-node-bacnet": "^1.0.0-beta.2",
@@ -54,4 +54,4 @@
54
54
  "type": "github",
55
55
  "url": "git+https://github.com/bitpool/edge-bacnet.git"
56
56
  }
57
- }
57
+ }