@capturebridge/sdk 0.12.1 → 0.14.2
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/CHANGELOG.md +93 -58
- package/Camera.js +63 -63
- package/CanonCamera.js +25 -25
- package/CaptureBridge.js +160 -160
- package/CapturePlugin.js +68 -68
- package/Common.js +258 -252
- package/Device.js +588 -595
- package/ICAO.js +86 -86
- package/IFace.js +237 -237
- package/ImageSource.js +110 -110
- package/Logs.js +129 -129
- package/MockCamera.js +17 -17
- package/Plugin.js +299 -299
- package/README.md +3 -3
- package/Scanner.js +55 -55
- package/SignatureTablet.js +86 -108
- package/SignatureTabletWidgets.js +164 -164
- package/StreamingCapturePlugin.js +129 -129
- package/TopazSigGem.js +68 -25
- package/Verifone.js +239 -192
- package/VerifoneControls.js +484 -0
- package/Version.js +7 -7
- package/WindowsScanner.js +25 -25
- package/package.json +1 -1
package/Plugin.js
CHANGED
|
@@ -1,299 +1,299 @@
|
|
|
1
|
-
import Device from './Device.js'
|
|
2
|
-
|
|
3
|
-
import { BASE_URL, DELETE, checkResponse, asyncToCallback } from './Common.js'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @classdesc CaptureBridge utilizes a plugin system for interacting with
|
|
7
|
-
* hardware devices and vendor SDKs. Clients will interact with plugins via the
|
|
8
|
-
* REST API using their unique plugin ID. This class provides basic methods
|
|
9
|
-
* for working with these plugins such as obtaining a list of compatible devices
|
|
10
|
-
* and managing the plugin's configuration.
|
|
11
|
-
*
|
|
12
|
-
* @see {@link CaptureBridge#plugins} Get all installed plugin.
|
|
13
|
-
* @see {@link CaptureBridge#plugin} Get a single plugin by ID.
|
|
14
|
-
*/
|
|
15
|
-
class Plugin {
|
|
16
|
-
/**
|
|
17
|
-
* @property {string} id - ID of the plugin, used when building REST endpoint paths.
|
|
18
|
-
* @property {string} name - Human friendly name for the plugin.
|
|
19
|
-
* @property {string} description - Human friendly description of plugin.
|
|
20
|
-
* @property {string} version - {@link https://semver.org/|Semantic version} version of the plugin.
|
|
21
|
-
* @property {string[]} methods - A plugin's list of supported RPC methods.
|
|
22
|
-
* @property {boolean} ready - True if plugin has been loaded into memory and is ready to receive requests.
|
|
23
|
-
* @property {object} defaultDevice - Default device to use for the plugin.
|
|
24
|
-
* @property {string[]} configMethods - Methods required to implement configuration.
|
|
25
|
-
*/
|
|
26
|
-
baseUrl
|
|
27
|
-
id
|
|
28
|
-
name
|
|
29
|
-
description
|
|
30
|
-
version
|
|
31
|
-
methods = []
|
|
32
|
-
ready = false
|
|
33
|
-
defaultDevice = null
|
|
34
|
-
configMethods = ['config_schema', 'get_config', 'set_config']
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Instantiate a plugin.
|
|
38
|
-
* @constructor
|
|
39
|
-
* @param {object|string} plugin - plugin object as received from the API or a plugin ID string.
|
|
40
|
-
* @param {string} plugin.id - ID of the plugin, used when building endpoint paths to call methods on the plugin.
|
|
41
|
-
* @param {string} plugin.name - Human friendly name of plugin.
|
|
42
|
-
* @param {string} plugin.description - Human friendly description of plugin.
|
|
43
|
-
* @param {string} plugin.version - {@link https://semver.org/|Semantic version} version of the plugin.
|
|
44
|
-
* @param {string[]} plugin.methods - plugin's capabilities.
|
|
45
|
-
* @param {boolean} plugin.ready - True if plugin has been loaded into memory and is ready.
|
|
46
|
-
* @param {string} [baseURL=BASE_URL] - Override the default protocol, domain, and port for the service.
|
|
47
|
-
* @throws Will throw an Error the plugin argument is not a plugin ID (String) or an object.
|
|
48
|
-
*/
|
|
49
|
-
constructor (plugin, baseUrl = BASE_URL) {
|
|
50
|
-
this.baseUrl = baseUrl
|
|
51
|
-
if (typeof plugin === 'string') {
|
|
52
|
-
this.id = plugin
|
|
53
|
-
} else if (typeof plugin === 'object') {
|
|
54
|
-
Object.assign(this, {}, plugin)
|
|
55
|
-
} else {
|
|
56
|
-
throw new Error('Invalid properties supplied to Plugin constructor')
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Get all devices for this plugin
|
|
62
|
-
*
|
|
63
|
-
* @method
|
|
64
|
-
* @async
|
|
65
|
-
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
66
|
-
* @returns {object[]} Array of {@link Device} objects.
|
|
67
|
-
* @example
|
|
68
|
-
* const plugin = await captureBridge.plugin('capture_camera_canon')
|
|
69
|
-
* const devices = await plugin.devices()
|
|
70
|
-
*/
|
|
71
|
-
async devices () {
|
|
72
|
-
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/device`)
|
|
73
|
-
await checkResponse(response)
|
|
74
|
-
const devices = await response.json()
|
|
75
|
-
return devices.map(d => new Device(d, this, this.baseUrl))
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* Get all Devices for this plugin
|
|
80
|
-
*
|
|
81
|
-
* @method
|
|
82
|
-
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
83
|
-
* @param {function} callback - Invoked with two arguments. The first argument
|
|
84
|
-
* is an Error object (if an error occurred) or null. The second argument is
|
|
85
|
-
* an Array of {@link Device} objects. If no devices are available the second
|
|
86
|
-
* argument will be an empty Array.
|
|
87
|
-
* @example
|
|
88
|
-
* captureBridge.pluginHandler('capture_camera_canon', (error, plugin) => {
|
|
89
|
-
* plugin.devicesHandler((error, devices) => console.log(devices))
|
|
90
|
-
* })
|
|
91
|
-
*/
|
|
92
|
-
devicesHandler (callback) {
|
|
93
|
-
asyncToCallback(this, this.devices, callback)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Get a device by ID for this plugin
|
|
98
|
-
*
|
|
99
|
-
* @method
|
|
100
|
-
* @async
|
|
101
|
-
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
102
|
-
* @param {string} id - Device ID
|
|
103
|
-
* @returns {object?} Requested {@link Device} or null
|
|
104
|
-
* @example
|
|
105
|
-
* const plugin = await captureBridge.plugin('capture_camera_canon')
|
|
106
|
-
* const device = await plugin.device('AWOOO56709')
|
|
107
|
-
*/
|
|
108
|
-
async device (id) {
|
|
109
|
-
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/device`)
|
|
110
|
-
const devices = await response.json()
|
|
111
|
-
const device = devices.find(d => d.id === id)
|
|
112
|
-
return device ? new Device(device, this, this.baseUrl) : null
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get a Device by ID for this plugin
|
|
117
|
-
* @method
|
|
118
|
-
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
119
|
-
* @param {string} id - Device ID
|
|
120
|
-
* @param {function} callback - Invoked with two arguments. The first argument
|
|
121
|
-
* is an Error object (if an error occurred) or null. The second argument is
|
|
122
|
-
* a {@link Device} object. If the requested device is not available the
|
|
123
|
-
* second argument will be null.
|
|
124
|
-
* @example
|
|
125
|
-
* captureBridge.pluginHandler('capture_camera_canon', (error, plugin) => {
|
|
126
|
-
* plugin.deviceHandler('AWOOO56709', (error, device) => console.log(device))
|
|
127
|
-
* })
|
|
128
|
-
*/
|
|
129
|
-
deviceHandler (id, callback) {
|
|
130
|
-
asyncToCallback(this, this.device, callback, id)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async configSchema () {
|
|
134
|
-
if (!await this.supportsConfig()) {
|
|
135
|
-
throw new Error('Plugin does not support config')
|
|
136
|
-
}
|
|
137
|
-
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/config/schema`)
|
|
138
|
-
return await response.json()
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
configSchemaHandler (callback) {
|
|
142
|
-
asyncToCallback(this, this.configSchema, callback)
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
async config () {
|
|
146
|
-
if (!await this.supportsConfig()) {
|
|
147
|
-
throw new Error('Plugin does not support config')
|
|
148
|
-
}
|
|
149
|
-
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/config`)
|
|
150
|
-
return await response.json()
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
configHandler (callback) {
|
|
154
|
-
asyncToCallback(this, this.config, callback)
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async saveConfig (config) {
|
|
158
|
-
if (!await this.supportsConfig()) {
|
|
159
|
-
throw new Error('Plugin does not support config')
|
|
160
|
-
}
|
|
161
|
-
const options = {
|
|
162
|
-
method: 'POST',
|
|
163
|
-
mode: 'cors',
|
|
164
|
-
headers: {
|
|
165
|
-
'Content-Type': 'application/json'
|
|
166
|
-
},
|
|
167
|
-
body: JSON.stringify(config)
|
|
168
|
-
}
|
|
169
|
-
const url = `${this.baseUrl}/plugin/${this.id}/config`
|
|
170
|
-
const response = await fetch(url, options)
|
|
171
|
-
return await response.json()
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
async saveConfigHandler (config, callback) {
|
|
175
|
-
asyncToCallback(this, this.saveConfig, callback, config)
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
async supportsConfig () {
|
|
179
|
-
await this.update()
|
|
180
|
-
return this.configMethods.every(m => this.methods.includes(m))
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
supportsConfigHandler (callback) {
|
|
184
|
-
asyncToCallback(this, this.configHandler, callback)
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
async shutdown () {
|
|
188
|
-
const url = `${this.baseUrl}/plugin/${this.id}`
|
|
189
|
-
const response = await fetch(url, DELETE)
|
|
190
|
-
return await response.json()
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
shutdownHandler (callback) {
|
|
194
|
-
asyncToCallback(this, this.shutdown, callback)
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
async startup () {
|
|
198
|
-
// See: ValidRD/wa_capture_bridge/issues/48
|
|
199
|
-
// We need an explicit endpoint for this instead
|
|
200
|
-
const devices = await this.devices()
|
|
201
|
-
if (devices) {
|
|
202
|
-
await this.update(true)
|
|
203
|
-
return { status: 'starting' }
|
|
204
|
-
}
|
|
205
|
-
return { status: 'failed' }
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
startupHandler (callback) {
|
|
209
|
-
asyncToCallback(this, this.startup, callback)
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* This class can be instantiated from either a plugin ID or a full plugin
|
|
214
|
-
* object from an API request.
|
|
215
|
-
* Because constructors cannot be async we use this method to hydrate the
|
|
216
|
-
* plugin properties if needed.
|
|
217
|
-
*
|
|
218
|
-
* @private
|
|
219
|
-
* @method
|
|
220
|
-
*/
|
|
221
|
-
async update (force = false) {
|
|
222
|
-
if (force || this.methods.length === 0) {
|
|
223
|
-
const response = await fetch(`${this.baseUrl}/plugin`)
|
|
224
|
-
const plugins = await response.json()
|
|
225
|
-
const plugin = plugins.find(p => p.id === this.id)
|
|
226
|
-
Object.assign(this, plugin)
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
updateHandler (force = false, callback) {
|
|
231
|
-
asyncToCallback(this, this.update, callback, force)
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async rpc (request) {
|
|
235
|
-
const options = {
|
|
236
|
-
method: 'POST',
|
|
237
|
-
mode: 'cors',
|
|
238
|
-
headers: {
|
|
239
|
-
'Content-Type': 'application/json'
|
|
240
|
-
},
|
|
241
|
-
body: JSON.stringify(request)
|
|
242
|
-
}
|
|
243
|
-
const url = `${this.baseUrl}/plugin/${this.id}/rpc`
|
|
244
|
-
const response = await fetch(url, options)
|
|
245
|
-
return await response.json()
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
rpcHandler (request, callback) {
|
|
249
|
-
asyncToCallback(this, this.rpc, callback, request)
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async ping () {
|
|
253
|
-
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/ping`)
|
|
254
|
-
return await response.json()
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
pingHandler (callback) {
|
|
258
|
-
asyncToCallback(this, this.ping, callback)
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* Classes that extend this one will add their own methods to perform
|
|
263
|
-
* operations on a device. They will call this method first to get a device
|
|
264
|
-
* object to use for making calls to the appropriate endpoint.
|
|
265
|
-
*
|
|
266
|
-
* This method really should be "protected" in classical OOP terms but
|
|
267
|
-
* Javascript does not currently have support for such scopes, and it's not
|
|
268
|
-
* marked using the ES6 private modifier so that it can be inherited by it's
|
|
269
|
-
* children.
|
|
270
|
-
* @private
|
|
271
|
-
* @method
|
|
272
|
-
*/
|
|
273
|
-
async setupDevice (deviceOpt) {
|
|
274
|
-
await this.update()
|
|
275
|
-
|
|
276
|
-
if (deviceOpt instanceof Device) {
|
|
277
|
-
this.defaultDevice = deviceOpt
|
|
278
|
-
return this.defaultDevice
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
if (typeof deviceOpt === 'string') {
|
|
282
|
-
this.defaultDevice = this.device(deviceOpt)
|
|
283
|
-
return this.defaultDevice
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
if (!this.defaultDevice) {
|
|
287
|
-
const devices = await this.devices()
|
|
288
|
-
if (!devices.length) {
|
|
289
|
-
throw new Error('No devices found for the plugin')
|
|
290
|
-
}
|
|
291
|
-
this.defaultDevice = devices[0]
|
|
292
|
-
return this.defaultDevice
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return this.defaultDevice
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
export default Plugin
|
|
1
|
+
import Device from './Device.js'
|
|
2
|
+
|
|
3
|
+
import { BASE_URL, DELETE, checkResponse, asyncToCallback } from './Common.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @classdesc CaptureBridge utilizes a plugin system for interacting with
|
|
7
|
+
* hardware devices and vendor SDKs. Clients will interact with plugins via the
|
|
8
|
+
* REST API using their unique plugin ID. This class provides basic methods
|
|
9
|
+
* for working with these plugins such as obtaining a list of compatible devices
|
|
10
|
+
* and managing the plugin's configuration.
|
|
11
|
+
*
|
|
12
|
+
* @see {@link CaptureBridge#plugins} Get all installed plugin.
|
|
13
|
+
* @see {@link CaptureBridge#plugin} Get a single plugin by ID.
|
|
14
|
+
*/
|
|
15
|
+
class Plugin {
|
|
16
|
+
/**
|
|
17
|
+
* @property {string} id - ID of the plugin, used when building REST endpoint paths.
|
|
18
|
+
* @property {string} name - Human friendly name for the plugin.
|
|
19
|
+
* @property {string} description - Human friendly description of plugin.
|
|
20
|
+
* @property {string} version - {@link https://semver.org/|Semantic version} version of the plugin.
|
|
21
|
+
* @property {string[]} methods - A plugin's list of supported RPC methods.
|
|
22
|
+
* @property {boolean} ready - True if plugin has been loaded into memory and is ready to receive requests.
|
|
23
|
+
* @property {object} defaultDevice - Default device to use for the plugin.
|
|
24
|
+
* @property {string[]} configMethods - Methods required to implement configuration.
|
|
25
|
+
*/
|
|
26
|
+
baseUrl
|
|
27
|
+
id
|
|
28
|
+
name
|
|
29
|
+
description
|
|
30
|
+
version
|
|
31
|
+
methods = []
|
|
32
|
+
ready = false
|
|
33
|
+
defaultDevice = null
|
|
34
|
+
configMethods = ['config_schema', 'get_config', 'set_config']
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Instantiate a plugin.
|
|
38
|
+
* @constructor
|
|
39
|
+
* @param {object|string} plugin - plugin object as received from the API or a plugin ID string.
|
|
40
|
+
* @param {string} plugin.id - ID of the plugin, used when building endpoint paths to call methods on the plugin.
|
|
41
|
+
* @param {string} plugin.name - Human friendly name of plugin.
|
|
42
|
+
* @param {string} plugin.description - Human friendly description of plugin.
|
|
43
|
+
* @param {string} plugin.version - {@link https://semver.org/|Semantic version} version of the plugin.
|
|
44
|
+
* @param {string[]} plugin.methods - plugin's capabilities.
|
|
45
|
+
* @param {boolean} plugin.ready - True if plugin has been loaded into memory and is ready.
|
|
46
|
+
* @param {string} [baseURL=BASE_URL] - Override the default protocol, domain, and port for the service.
|
|
47
|
+
* @throws Will throw an Error the plugin argument is not a plugin ID (String) or an object.
|
|
48
|
+
*/
|
|
49
|
+
constructor (plugin, baseUrl = BASE_URL) {
|
|
50
|
+
this.baseUrl = baseUrl
|
|
51
|
+
if (typeof plugin === 'string') {
|
|
52
|
+
this.id = plugin
|
|
53
|
+
} else if (typeof plugin === 'object') {
|
|
54
|
+
Object.assign(this, {}, plugin)
|
|
55
|
+
} else {
|
|
56
|
+
throw new Error('Invalid properties supplied to Plugin constructor')
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Get all devices for this plugin
|
|
62
|
+
*
|
|
63
|
+
* @method
|
|
64
|
+
* @async
|
|
65
|
+
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
66
|
+
* @returns {object[]} Array of {@link Device} objects.
|
|
67
|
+
* @example
|
|
68
|
+
* const plugin = await captureBridge.plugin('capture_camera_canon')
|
|
69
|
+
* const devices = await plugin.devices()
|
|
70
|
+
*/
|
|
71
|
+
async devices () {
|
|
72
|
+
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/device`)
|
|
73
|
+
await checkResponse(response)
|
|
74
|
+
const devices = await response.json()
|
|
75
|
+
return devices.map(d => new Device(d, this, this.baseUrl))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get all Devices for this plugin
|
|
80
|
+
*
|
|
81
|
+
* @method
|
|
82
|
+
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
83
|
+
* @param {function} callback - Invoked with two arguments. The first argument
|
|
84
|
+
* is an Error object (if an error occurred) or null. The second argument is
|
|
85
|
+
* an Array of {@link Device} objects. If no devices are available the second
|
|
86
|
+
* argument will be an empty Array.
|
|
87
|
+
* @example
|
|
88
|
+
* captureBridge.pluginHandler('capture_camera_canon', (error, plugin) => {
|
|
89
|
+
* plugin.devicesHandler((error, devices) => console.log(devices))
|
|
90
|
+
* })
|
|
91
|
+
*/
|
|
92
|
+
devicesHandler (callback) {
|
|
93
|
+
asyncToCallback(this, this.devices, callback)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get a device by ID for this plugin
|
|
98
|
+
*
|
|
99
|
+
* @method
|
|
100
|
+
* @async
|
|
101
|
+
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
102
|
+
* @param {string} id - Device ID
|
|
103
|
+
* @returns {object?} Requested {@link Device} or null
|
|
104
|
+
* @example
|
|
105
|
+
* const plugin = await captureBridge.plugin('capture_camera_canon')
|
|
106
|
+
* const device = await plugin.device('AWOOO56709')
|
|
107
|
+
*/
|
|
108
|
+
async device (id) {
|
|
109
|
+
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/device`)
|
|
110
|
+
const devices = await response.json()
|
|
111
|
+
const device = devices.find(d => d.id === id)
|
|
112
|
+
return device ? new Device(device, this, this.baseUrl) : null
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Get a Device by ID for this plugin
|
|
117
|
+
* @method
|
|
118
|
+
* @see {@link https://local.capturebridge.net:9001/doc/#api-Plugin-GetPluginDevices|API Endpoint (/plugin/:pluginId/device)}
|
|
119
|
+
* @param {string} id - Device ID
|
|
120
|
+
* @param {function} callback - Invoked with two arguments. The first argument
|
|
121
|
+
* is an Error object (if an error occurred) or null. The second argument is
|
|
122
|
+
* a {@link Device} object. If the requested device is not available the
|
|
123
|
+
* second argument will be null.
|
|
124
|
+
* @example
|
|
125
|
+
* captureBridge.pluginHandler('capture_camera_canon', (error, plugin) => {
|
|
126
|
+
* plugin.deviceHandler('AWOOO56709', (error, device) => console.log(device))
|
|
127
|
+
* })
|
|
128
|
+
*/
|
|
129
|
+
deviceHandler (id, callback) {
|
|
130
|
+
asyncToCallback(this, this.device, callback, id)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
async configSchema () {
|
|
134
|
+
if (!await this.supportsConfig()) {
|
|
135
|
+
throw new Error('Plugin does not support config')
|
|
136
|
+
}
|
|
137
|
+
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/config/schema`)
|
|
138
|
+
return await response.json()
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
configSchemaHandler (callback) {
|
|
142
|
+
asyncToCallback(this, this.configSchema, callback)
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async config () {
|
|
146
|
+
if (!await this.supportsConfig()) {
|
|
147
|
+
throw new Error('Plugin does not support config')
|
|
148
|
+
}
|
|
149
|
+
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/config`)
|
|
150
|
+
return await response.json()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
configHandler (callback) {
|
|
154
|
+
asyncToCallback(this, this.config, callback)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async saveConfig (config) {
|
|
158
|
+
if (!await this.supportsConfig()) {
|
|
159
|
+
throw new Error('Plugin does not support config')
|
|
160
|
+
}
|
|
161
|
+
const options = {
|
|
162
|
+
method: 'POST',
|
|
163
|
+
mode: 'cors',
|
|
164
|
+
headers: {
|
|
165
|
+
'Content-Type': 'application/json'
|
|
166
|
+
},
|
|
167
|
+
body: JSON.stringify(config)
|
|
168
|
+
}
|
|
169
|
+
const url = `${this.baseUrl}/plugin/${this.id}/config`
|
|
170
|
+
const response = await fetch(url, options)
|
|
171
|
+
return await response.json()
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async saveConfigHandler (config, callback) {
|
|
175
|
+
asyncToCallback(this, this.saveConfig, callback, config)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async supportsConfig () {
|
|
179
|
+
await this.update()
|
|
180
|
+
return this.configMethods.every(m => this.methods.includes(m))
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
supportsConfigHandler (callback) {
|
|
184
|
+
asyncToCallback(this, this.configHandler, callback)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async shutdown () {
|
|
188
|
+
const url = `${this.baseUrl}/plugin/${this.id}`
|
|
189
|
+
const response = await fetch(url, DELETE)
|
|
190
|
+
return await response.json()
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
shutdownHandler (callback) {
|
|
194
|
+
asyncToCallback(this, this.shutdown, callback)
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async startup () {
|
|
198
|
+
// See: ValidRD/wa_capture_bridge/issues/48
|
|
199
|
+
// We need an explicit endpoint for this instead
|
|
200
|
+
const devices = await this.devices()
|
|
201
|
+
if (devices) {
|
|
202
|
+
await this.update(true)
|
|
203
|
+
return { status: 'starting' }
|
|
204
|
+
}
|
|
205
|
+
return { status: 'failed' }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
startupHandler (callback) {
|
|
209
|
+
asyncToCallback(this, this.startup, callback)
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* This class can be instantiated from either a plugin ID or a full plugin
|
|
214
|
+
* object from an API request.
|
|
215
|
+
* Because constructors cannot be async we use this method to hydrate the
|
|
216
|
+
* plugin properties if needed.
|
|
217
|
+
*
|
|
218
|
+
* @private
|
|
219
|
+
* @method
|
|
220
|
+
*/
|
|
221
|
+
async update (force = false) {
|
|
222
|
+
if (force || this.methods.length === 0) {
|
|
223
|
+
const response = await fetch(`${this.baseUrl}/plugin`)
|
|
224
|
+
const plugins = await response.json()
|
|
225
|
+
const plugin = plugins.find(p => p.id === this.id)
|
|
226
|
+
Object.assign(this, plugin)
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
updateHandler (force = false, callback) {
|
|
231
|
+
asyncToCallback(this, this.update, callback, force)
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async rpc (request) {
|
|
235
|
+
const options = {
|
|
236
|
+
method: 'POST',
|
|
237
|
+
mode: 'cors',
|
|
238
|
+
headers: {
|
|
239
|
+
'Content-Type': 'application/json'
|
|
240
|
+
},
|
|
241
|
+
body: JSON.stringify(request)
|
|
242
|
+
}
|
|
243
|
+
const url = `${this.baseUrl}/plugin/${this.id}/rpc`
|
|
244
|
+
const response = await fetch(url, options)
|
|
245
|
+
return await response.json()
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
rpcHandler (request, callback) {
|
|
249
|
+
asyncToCallback(this, this.rpc, callback, request)
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async ping () {
|
|
253
|
+
const response = await fetch(`${this.baseUrl}/plugin/${this.id}/ping`)
|
|
254
|
+
return await response.json()
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
pingHandler (callback) {
|
|
258
|
+
asyncToCallback(this, this.ping, callback)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Classes that extend this one will add their own methods to perform
|
|
263
|
+
* operations on a device. They will call this method first to get a device
|
|
264
|
+
* object to use for making calls to the appropriate endpoint.
|
|
265
|
+
*
|
|
266
|
+
* This method really should be "protected" in classical OOP terms but
|
|
267
|
+
* Javascript does not currently have support for such scopes, and it's not
|
|
268
|
+
* marked using the ES6 private modifier so that it can be inherited by it's
|
|
269
|
+
* children.
|
|
270
|
+
* @private
|
|
271
|
+
* @method
|
|
272
|
+
*/
|
|
273
|
+
async setupDevice (deviceOpt) {
|
|
274
|
+
await this.update()
|
|
275
|
+
|
|
276
|
+
if (deviceOpt instanceof Device) {
|
|
277
|
+
this.defaultDevice = deviceOpt
|
|
278
|
+
return this.defaultDevice
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (typeof deviceOpt === 'string') {
|
|
282
|
+
this.defaultDevice = this.device(deviceOpt)
|
|
283
|
+
return this.defaultDevice
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (!this.defaultDevice) {
|
|
287
|
+
const devices = await this.devices()
|
|
288
|
+
if (!devices.length) {
|
|
289
|
+
throw new Error('No devices found for the plugin')
|
|
290
|
+
}
|
|
291
|
+
this.defaultDevice = devices[0]
|
|
292
|
+
return this.defaultDevice
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return this.defaultDevice
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export default Plugin
|
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
# Capture Bridge JavaScript SDK
|
|
2
|
-
|
|
3
|
-
See `CHANGELOG.md` for new features and breaking changes.
|
|
1
|
+
# Capture Bridge JavaScript SDK
|
|
2
|
+
|
|
3
|
+
See `CHANGELOG.md` for new features and breaking changes.
|