dataUtil.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. "use strict";
  2. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  3. function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
  4. return new (P || (P = Promise))(function (resolve, reject) {
  5. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  6. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  7. function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
  8. step((generator = generator.apply(thisArg, _arguments || [])).next());
  9. });
  10. };
  11. var __generator = (this && this.__generator) || function (thisArg, body) {
  12. var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
  13. return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
  14. function verb(n) { return function (v) { return step([n, v]); }; }
  15. function step(op) {
  16. if (f) throw new TypeError("Generator is already executing.");
  17. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  18. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  19. if (y = 0, t) op = [op[0] & 2, t.value];
  20. switch (op[0]) {
  21. case 0: case 1: t = op; break;
  22. case 4: _.label++; return { value: op[1], done: false };
  23. case 5: _.label++; y = op[1]; op = [0]; continue;
  24. case 7: op = _.ops.pop(); _.trys.pop(); continue;
  25. default:
  26. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
  27. if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
  28. if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
  29. if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
  30. if (t[2]) _.ops.pop();
  31. _.trys.pop(); continue;
  32. }
  33. op = body.call(thisArg, _);
  34. } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
  35. if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
  36. }
  37. };
  38. var __importDefault = (this && this.__importDefault) || function (mod) {
  39. return (mod && mod.__esModule) ? mod : { "default": mod };
  40. };
  41. Object.defineProperty(exports, "__esModule", { value: true });
  42. exports.setGwData = exports.getGwData = exports.initLibHaFiles = exports.clearData = exports.appendData = exports.saveData = exports.getDataSync = void 0;
  43. var fs_1 = __importDefault(require("fs"));
  44. var promises_1 = __importDefault(require("fs/promises"));
  45. var path_1 = __importDefault(require("path"));
  46. var lodash_1 = __importDefault(require("lodash"));
  47. var config_1 = require("../config/config");
  48. var const_1 = require("../lib-ha/const");
  49. var logger_1 = require("./logger");
  50. var basePath = path_1.default.join('/data');
  51. if (config_1.debugMode || !config_1.isSupervisor) {
  52. basePath = path_1.default.join(__dirname, '../../data');
  53. }
  54. if (!fs_1.default.existsSync(basePath)) {
  55. fs_1.default.mkdirSync(basePath);
  56. }
  57. var getDataSync = function (fileName, namePath) {
  58. if (namePath === void 0) { namePath = []; }
  59. try {
  60. var data = fs_1.default.readFileSync(path_1.default.join(basePath, "/".concat(fileName)), { encoding: 'utf-8' });
  61. return namePath.reduce(function (cur, path) { return cur[path]; }, JSON.parse(data));
  62. }
  63. catch (err) {
  64. logger_1.logger.error("getDataSync: ".concat(fileName, " -> ").concat(namePath, " no data"));
  65. return null;
  66. }
  67. };
  68. exports.getDataSync = getDataSync;
  69. var saveData = function (fileName, data) { return __awaiter(void 0, void 0, void 0, function () {
  70. return __generator(this, function (_a) {
  71. try {
  72. return [2, new Promise(function (resolve, reject) {
  73. fs_1.default.writeFile(path_1.default.join(basePath, "/".concat(fileName)), data, function (err) {
  74. if (err) {
  75. logger_1.logger.error("fs.writeFile error: ".concat(err));
  76. resolve(-1);
  77. }
  78. resolve(0);
  79. });
  80. })];
  81. }
  82. catch (err) {
  83. logger_1.logger.error("saveData -> no data");
  84. return [2, -1];
  85. }
  86. return [2];
  87. });
  88. }); };
  89. exports.saveData = saveData;
  90. var appendData = function (fileName, namePath, data) { return __awaiter(void 0, void 0, void 0, function () {
  91. var fileData;
  92. return __generator(this, function (_a) {
  93. fileData = getDataSync(fileName) || {};
  94. lodash_1.default.set(fileData, namePath, data);
  95. return [2, saveData(fileName, JSON.stringify(fileData))];
  96. });
  97. }); };
  98. exports.appendData = appendData;
  99. var clearData = function (fileName) { return __awaiter(void 0, void 0, void 0, function () {
  100. return __generator(this, function (_a) {
  101. return [2, saveData(fileName, '{}')];
  102. });
  103. }); };
  104. exports.clearData = clearData;
  105. function initLibHaFiles() {
  106. return __awaiter(this, void 0, void 0, function () {
  107. var FILEPATH, err_1;
  108. return __generator(this, function (_a) {
  109. switch (_a.label) {
  110. case 0:
  111. FILEPATH = path_1.default.join(basePath, const_1.GW_DATA_FILENAME);
  112. _a.label = 1;
  113. case 1:
  114. _a.trys.push([1, 3, , 6]);
  115. return [4, promises_1.default.stat(FILEPATH)];
  116. case 2:
  117. _a.sent();
  118. return [3, 6];
  119. case 3:
  120. err_1 = _a.sent();
  121. if (!(err_1.code === 'ENOENT')) return [3, 5];
  122. return [4, promises_1.default.writeFile(FILEPATH, JSON.stringify([]))];
  123. case 4:
  124. _a.sent();
  125. _a.label = 5;
  126. case 5: return [3, 6];
  127. case 6: return [2];
  128. }
  129. });
  130. });
  131. }
  132. exports.initLibHaFiles = initLibHaFiles;
  133. function getGwData() {
  134. return __awaiter(this, void 0, void 0, function () {
  135. var data;
  136. return __generator(this, function (_a) {
  137. switch (_a.label) {
  138. case 0: return [4, promises_1.default.readFile(path_1.default.join(basePath, const_1.GW_DATA_FILENAME), { encoding: 'utf-8' })];
  139. case 1:
  140. data = _a.sent();
  141. return [2, JSON.parse(data)];
  142. }
  143. });
  144. });
  145. }
  146. exports.getGwData = getGwData;
  147. function setGwData(data) {
  148. return __awaiter(this, void 0, void 0, function () {
  149. return __generator(this, function (_a) {
  150. switch (_a.label) {
  151. case 0: return [4, promises_1.default.writeFile(path_1.default.join(basePath, const_1.GW_DATA_FILENAME), JSON.stringify(data))];
  152. case 1:
  153. _a.sent();
  154. return [2];
  155. }
  156. });
  157. });
  158. }
  159. exports.setGwData = setGwData;