1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.IdentifierCache = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const crypto_1 = tslib_1.__importDefault(require("crypto"));
|
6 | const util_1 = tslib_1.__importDefault(require("util"));
|
7 | const HAPStorage_1 = require("./HAPStorage");
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class IdentifierCache {
|
18 | username;
|
19 | _cache = {};
|
20 | _usedCache = null;
|
21 | _savedCacheHash = "";
|
22 | constructor(username) {
|
23 | this.username = username;
|
24 | }
|
25 | startTrackingUsage() {
|
26 | this._usedCache = {};
|
27 | }
|
28 | stopTrackingUsageAndExpireUnused() {
|
29 |
|
30 | this._cache = this._usedCache || this._cache;
|
31 | this._usedCache = null;
|
32 | }
|
33 | getCache(key) {
|
34 | const value = this._cache[key];
|
35 |
|
36 | if (this._usedCache && typeof value !== "undefined") {
|
37 | this._usedCache[key] = value;
|
38 | }
|
39 | return value;
|
40 | }
|
41 | setCache(key, value) {
|
42 | this._cache[key] = value;
|
43 |
|
44 | if (this._usedCache) {
|
45 | this._usedCache[key] = value;
|
46 | }
|
47 | return value;
|
48 | }
|
49 | getAID(accessoryUUID) {
|
50 | const key = accessoryUUID;
|
51 |
|
52 | this.getCache("|nextAID");
|
53 | return this.getCache(key) || this.setCache(key, this.getNextAID());
|
54 | }
|
55 | getIID(accessoryUUID, serviceUUID, serviceSubtype, characteristicUUID) {
|
56 | const key = accessoryUUID
|
57 | + "|" + serviceUUID
|
58 | + (serviceSubtype ? "|" + serviceSubtype : "")
|
59 | + (characteristicUUID ? "|" + characteristicUUID : "");
|
60 |
|
61 | this.getCache(accessoryUUID + "|nextIID");
|
62 | return this.getCache(key) || this.setCache(key, this.getNextIID(accessoryUUID));
|
63 | }
|
64 | getNextAID() {
|
65 | const key = "|nextAID";
|
66 | const nextAID = this.getCache(key) || 2;
|
67 | this.setCache(key, nextAID + 1);
|
68 | return nextAID;
|
69 | }
|
70 | getNextIID(accessoryUUID) {
|
71 | const key = accessoryUUID + "|nextIID";
|
72 | const nextIID = this.getCache(key) || 2;
|
73 | this.setCache(key, nextIID + 1);
|
74 | return nextIID;
|
75 | }
|
76 | save() {
|
77 | const newCacheHash = crypto_1.default.createHash("sha1").update(JSON.stringify(this._cache)).digest("hex");
|
78 | if (newCacheHash !== this._savedCacheHash) {
|
79 | const saved = {
|
80 | cache: this._cache,
|
81 | };
|
82 | const key = IdentifierCache.persistKey(this.username);
|
83 | HAPStorage_1.HAPStorage.storage().setItemSync(key, saved);
|
84 | this._savedCacheHash = newCacheHash;
|
85 | }
|
86 | }
|
87 | |
88 |
|
89 |
|
90 |
|
91 | static persistKey(username) {
|
92 | return util_1.default.format("IdentifierCache.%s.json", username.replace(/:/g, "").toUpperCase());
|
93 | }
|
94 | static load(username) {
|
95 | const key = IdentifierCache.persistKey(username);
|
96 | const saved = HAPStorage_1.HAPStorage.storage().getItem(key);
|
97 | if (saved) {
|
98 | const info = new IdentifierCache(username);
|
99 | info._cache = saved.cache;
|
100 |
|
101 | info._savedCacheHash = crypto_1.default.createHash("sha1").update(JSON.stringify(info._cache)).digest("hex");
|
102 | return info;
|
103 | }
|
104 | else {
|
105 | return null;
|
106 | }
|
107 | }
|
108 | static remove(username) {
|
109 | const key = this.persistKey(username);
|
110 | HAPStorage_1.HAPStorage.storage().removeItemSync(key);
|
111 | }
|
112 | }
|
113 | exports.IdentifierCache = IdentifierCache;
|
114 |
|
\ | No newline at end of file |