-
Notifications
You must be signed in to change notification settings - Fork 17
/
fakegato-storage.js
173 lines (155 loc) · 5.22 KB
/
fakegato-storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*jshint esversion: 6,node: true,-W041: false */
'use strict';
const DEBUG = true;
var fs = require('fs');
var os = require('os');
var path = require('path');
var hostname = os.hostname().split(".")[0];
var googleDrive = require('./lib/googleDrive').drive;
var fileSuffix = '_persist.json';
var thisStorage;
class FakeGatoStorage {
constructor(params) {
if (!params)
params = {};
this.writers = [];
this.log = params.log || {};
if (!this.log.debug) {
this.log.debug = DEBUG ? console.log : function () { };
}
thisStorage = this;
this.addingWriter = false;
}
addWriter(service, params) {
if (!this.addingWriter) {
this.addingWriter = true;
if (!params)
params = {};
this.log.debug("** Fakegato-storage AddWriter :", service.accessoryName);
let newWriter = {
'service': service,
'callback': params.callback,
'storage': params.storage || 'fs',
'fileName': params.filename || hostname + "_" + service.accessoryName + fileSuffix // Unique filename per homebridge server. Allows test environments on other servers not to break prod.
};
var onReady = typeof (params.onReady) == 'function' ? params.onReady : function () { }.bind(this);
switch (newWriter.storage) {
case 'fs':
newWriter.storageHandler = fs;
newWriter.path = params.path || path.join(os.homedir(), '.homebridge');
this.writers.push(newWriter);
this.addingWriter = false;
onReady();
break;
case 'googleDrive':
newWriter.path = params.path || 'fakegato';
newWriter.keyPath = params.keyPath || path.join(os.homedir(), '.homebridge');
newWriter.storageHandler = new googleDrive({
keyPath: newWriter.keyPath, callback: function () {
this.addingWriter = false;
onReady(arguments);
}.bind(this), folder: newWriter.path
});
this.writers.push(newWriter);
break;
/*
case 'memcached' :
break;
*/
}
} else {
setTimeout(function () {
this.addWriter(service, params);
}.bind(this), 100);
}
}
getWriter(service) {
let findServ = function (element) {
return element.service === service;
};
return this.writers.find(findServ);
}
_getWriterIndex(service) {
let findServ = function (element) {
return element.service === service;
};
return this.writers.findIndex(findServ);
}
getWriters() {
return this.writers;
}
delWriter(service) {
let index = this._getWriterIndex(service);
this.writers.splice(index, 1);
}
write(params) { // must be asynchronous
if (!this.writing) {
this.writing = true;
let writer = this.getWriter(params.service);
let callBack = typeof (params.callback) == 'function' ? params.callback : (typeof (writer.callback) == 'function' ? writer.callback : function () { }); // use parameter callback or writer callback or empty function
switch (writer.storage) {
case 'fs':
this.log.debug("** Fakegato-storage write FS file:", path.join(writer.path, writer.fileName), params.data.substr(1, 80));
writer.storageHandler.writeFile(path.join(writer.path, writer.fileName), params.data, 'utf8', function () {
this.writing = false;
callBack(arguments);
}.bind(this));
break;
case 'googleDrive':
this.log.debug("** Fakegato-storage write googleDrive file:", writer.path, writer.fileName, params.data.substr(1, 80));
writer.storageHandler.writeFile(writer.path, writer.fileName, params.data, function () {
this.writing = false;
callBack(arguments);
}.bind(this));
break;
/*
case 'memcached' :
break;
*/
}
} else {
setTimeout(function () { // retry in 100ms
this.write(params);
}.bind(this), 100);
}
}
read(params) {
let writer = this.getWriter(params.service);
let callBack = typeof (params.callback) == 'function' ? params.callback : (typeof (writer.callback) == 'function' ? writer.callback : function () { }); // use parameter callback or writer callback or empty function
switch (writer.storage) {
case 'fs':
this.log.debug("** Fakegato-storage read FS file:", path.join(writer.path, writer.fileName));
writer.storageHandler.readFile(path.join(writer.path, writer.fileName), 'utf8', callBack);
break;
case 'googleDrive':
this.log.debug("** Fakegato-storage read googleDrive file: %s/%s", writer.path, writer.fileName);
writer.storageHandler.readFile(writer.path, writer.fileName, callBack);
break;
/*
case 'memcached' :
break;
*/
}
}
remove(params) {
let writer = this.getWriter(params.service);
let callBack = typeof (params.callback) == 'function' ? params.callback : (typeof (writer.callback) == 'function' ? writer.callback : function () { }); // use parameter callback or writer callback or empty function
switch (writer.storage) {
case 'fs':
this.log.debug("** Fakegato-storage delete FS file:", path.join(writer.path, writer.fileName));
writer.storageHandler.unlink(path.join(writer.path, writer.fileName), callBack);
break;
case 'googleDrive':
this.log.debug("** Fakegato-storage delete googleDrive file:", writer.path, writer.fileName);
writer.storageHandler.deleteFile(writer.path, writer.fileName, callBack);
break;
/*
case 'memcached' :
break;
*/
}
}
}
module.exports = {
FakeGatoStorage: FakeGatoStorage
};