@aiot-toolkit/shared-utils 1.0.17-binary-beta.2 → 1.0.17-binary-beta.3

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.
@@ -1,151 +1,2 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getRecords = getRecords;
7
- exports.getProjectClients = getProjectClients;
8
- exports.recordClient = recordClient;
9
- exports.getRecordClient = getRecordClient;
10
- exports.clearProjectRecord = clearProjectRecord;
11
- exports.removeClientBySn = removeClientBySn;
12
- var _fs = _interopRequireDefault(require("fs"));
13
- var _path = _interopRequireDefault(require("path"));
14
- var _index = require("./index");
15
- var _config = _interopRequireDefault(require("../config"));
16
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
17
- /**
18
- * Copyright (C) 2017, hapjs.org. All rights reserved.
19
- */
20
-
21
- /**
22
- * 读取记录文件
23
- * @param {String} clientRecordPath - 记录文件的地址
24
- * @returns {Object} - 记录的全部设备信息
25
- */
26
- function getRecords(clientRecordPath) {
27
- let recordData = {
28
- records: {}
29
- };
30
- try {
31
- recordData = JSON.parse(_fs.default.readFileSync(clientRecordPath).toString());
32
- } catch (err) {
33
- _index.colorconsole.error(`Error reading hap-toolkit-client-records.json file: ${err.message}`);
34
- }
35
- return recordData;
36
- }
37
-
38
- /**
39
- * 将设备记录写入记录文件
40
- * @param {String} clientRecordPath - 记录文件的地址
41
- * @param {Object} recordData - 要写入的全部设备记录
42
- */
43
- function writeRecords(clientRecordPath, recordData) {
44
- _fs.default.writeFileSync(clientRecordPath, JSON.stringify(recordData, null, 2));
45
- }
46
-
47
- /**
48
- * 读取一个项目的设备记录
49
- * @param {Object} recordData - 全部设备信息
50
- * @returns {Array} - 设备记录
51
- */
52
- function getProjectClients(recordData) {
53
- const projectPath = _config.default.projectPath;
54
- let records = recordData.records;
55
- records = records instanceof Object ? records : {};
56
- const clients = records[projectPath];
57
- if (clients && clients instanceof Array) {
58
- return clients;
59
- }
60
- return [];
61
- }
62
-
63
- /**
64
- * 保存设备记录
65
- * @param {String} clientRecordPath - 记录文件的地址
66
- * @param {String} newClient - 新的设备记录
67
- * @param {callback} logHook - log信息的钩子,参数为要打印的信息
68
- */
69
- function recordClient(clientRecordPath, newClient, logHook) {
70
- const pathParentDir = _path.default.dirname(clientRecordPath);
71
- const projectPath = _config.default.projectPath;
72
- let recordData;
73
- (0, _index.mkdirsSync)(pathParentDir);
74
- if (_fs.default.existsSync(clientRecordPath)) {
75
- recordData = getRecords(clientRecordPath);
76
- let clients = getProjectClients(recordData);
77
- logHook && logHook(`writeClientLogFile(): before: ${JSON.stringify(recordData.records)}`);
78
- clients = clients.filter(client => {
79
- return client.ip !== newClient.ip || client.port !== newClient.port;
80
- });
81
- // 保留最后的4条记录,最多记录5条
82
- while (clients.length > 4) {
83
- clients.shift();
84
- }
85
- recordData.records[projectPath] = clients;
86
- } else {
87
- recordData = {
88
- records: {}
89
- };
90
- recordData.records[projectPath] = [];
91
- }
92
- // 写入文件
93
- recordData.records[projectPath].push(newClient);
94
- writeRecords(clientRecordPath, recordData);
95
- logHook && logHook(`writeClientLogFile(): after: ${JSON.stringify(recordData.records)}`);
96
- }
97
-
98
- /**
99
- * 根据设备sn和ip获取设备的记录
100
- * @param {String} clientRecordPath - 记录文件地址
101
- * @param {String} sn - 设备序列号
102
- * @param {String} ip
103
- * @returns {Object} - 匹配的设备记录
104
- */
105
- function getRecordClient(clientRecordPath, sn, ip) {
106
- if (_fs.default.existsSync(clientRecordPath)) {
107
- const recordData = getRecords(clientRecordPath);
108
- const clients = getProjectClients(recordData);
109
- return clients.find(client => {
110
- return client.sn === sn && client.ip === ip && client.port;
111
- });
112
- }
113
- }
114
-
115
- /**
116
- * 清除一个项目的设备记录
117
- * @param {String} clientRecordPath
118
- */
119
- function clearProjectRecord(clientRecordPath) {
120
- if (_fs.default.existsSync(clientRecordPath)) {
121
- const recordData = getRecords(clientRecordPath);
122
- const projectPath = _config.default.projectPath;
123
- recordData.records[projectPath] = [];
124
- writeRecords(clientRecordPath, recordData);
125
- _index.colorconsole.info('### App Server ### Clear debugging equipment records');
126
- } else {
127
- _index.colorconsole.info('### App Server ### There are no debugging equipment records that need to be cleared');
128
- }
129
- }
130
-
131
- /**
132
- * 从端口映射记录文件中移除项目的一个设备记录
133
- * @param {String} clientRecordPath - 记录文件的地址
134
- * @param {String} sn - 设备序列号
135
- * @param {callback} logHook - log信息的钩子,参数为要打印的信息
136
- */
137
- function removeClientBySn(clientRecordPath, sn, logHook) {
138
- if (_fs.default.existsSync(clientRecordPath)) {
139
- const projectPath = _config.default.projectPath;
140
- const recordData = getRecords(clientRecordPath);
141
- const records = recordData.records;
142
- const clients = getProjectClients(recordData);
143
- logHook && logHook(`_removeItemFromClientLogFile(): before: ${JSON.stringify(records)}`);
144
- records[projectPath] = clients.filter(it => {
145
- return it.sn !== sn;
146
- });
147
- writeRecords(clientRecordPath, recordData);
148
- logHook && logHook(`_removeItemFromClientLogFile(): after: ${JSON.stringify(records)}`);
149
- }
150
- }
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.getRecords=getRecords,exports.getProjectClients=getProjectClients,exports.recordClient=recordClient,exports.getRecordClient=getRecordClient,exports.clearProjectRecord=clearProjectRecord,exports.removeClientBySn=removeClientBySn;var _fs=_interopRequireDefault(require("fs")),_path=_interopRequireDefault(require("path")),_index=require("./index"),_config=_interopRequireDefault(require("../config"));function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function getRecords(e){let r={records:{}};try{r=JSON.parse(_fs.default.readFileSync(e).toString())}catch(e){_index.colorconsole.error(`Error reading hap-toolkit-client-records.json file: ${e.message}`)}return r}function writeRecords(e,r){_fs.default.writeFileSync(e,JSON.stringify(r,null,2))}function getProjectClients(e){const r=_config.default.projectPath;let t=e.records;t=t instanceof Object?t:{};const o=t[r];return o&&o instanceof Array?o:[]}function recordClient(e,r,t){const o=_path.default.dirname(e),i=_config.default.projectPath;let n;if((0,_index.mkdirsSync)(o),_fs.default.existsSync(e)){n=getRecords(e);let o=getProjectClients(n);for(t&&t(`writeClientLogFile(): before: ${JSON.stringify(n.records)}`),o=o.filter((e=>e.ip!==r.ip||e.port!==r.port));o.length>4;)o.shift();n.records[i]=o}else n={records:{}},n.records[i]=[];n.records[i].push(r),writeRecords(e,n),t&&t(`writeClientLogFile(): after: ${JSON.stringify(n.records)}`)}function getRecordClient(e,r,t){if(_fs.default.existsSync(e)){return getProjectClients(getRecords(e)).find((e=>e.sn===r&&e.ip===t&&e.port))}}function clearProjectRecord(e){if(_fs.default.existsSync(e)){const r=getRecords(e),t=_config.default.projectPath;r.records[t]=[],writeRecords(e,r),_index.colorconsole.info("### App Server ### Clear debugging equipment records")}else _index.colorconsole.info("### App Server ### There are no debugging equipment records that need to be cleared")}function removeClientBySn(e,r,t){if(_fs.default.existsSync(e)){const o=_config.default.projectPath,i=getRecords(e),n=i.records,c=getProjectClients(i);t&&t(`_removeItemFromClientLogFile(): before: ${JSON.stringify(n)}`),n[o]=c.filter((e=>e.sn!==r)),writeRecords(e,i),t&&t(`_removeItemFromClientLogFile(): after: ${JSON.stringify(n)}`)}}
151
2
  //# sourceMappingURL=record-client.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiot-toolkit/shared-utils",
3
- "version": "1.0.17-binary-beta.2",
3
+ "version": "1.0.17-binary-beta.3",
4
4
  "description": "aiot shared utils",
5
5
  "engines": {
6
6
  "node": ">=8.0.0"
@@ -31,5 +31,5 @@
31
31
  "devDependencies": {
32
32
  "strip-ansi": "^5.2.0"
33
33
  },
34
- "gitHead": "de34d2675573b437ce84aecd64b99c787e97cedb"
34
+ "gitHead": "a775ecf7587c62eae78c09c5446786c02b3dde05"
35
35
  }