@iobroker/js-controller-cli 4.0.0-alpha.47-20220120-7bad8e08 → 4.0.0-alpha.50-20220123-bedb0512

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.
@@ -7,7 +7,7 @@ const ALIAS_STARTS_WITH = 'alias.';
7
7
 
8
8
  /** Command iobroker state ... */
9
9
  module.exports = class CLIStates extends CLICommand {
10
- /** @param {import('./cliCommand').CLICommandOptions} options */
10
+ /** @param {CLICommandOptions} options */
11
11
  constructor(options) {
12
12
  super(options);
13
13
  }
@@ -39,6 +39,9 @@ module.exports = class CLIStates extends CLICommand {
39
39
  case 'getvalue':
40
40
  resultTransform = obj => (obj ? formatValue(obj.val, pretty) : 'null');
41
41
  return this.get_(args, resultTransform);
42
+ case 'getBinary':
43
+ case 'getbinary':
44
+ return this._getBinary(args);
42
45
  case 'set':
43
46
  return this.set_(args);
44
47
  case 'chmod':
@@ -102,6 +105,53 @@ module.exports = class CLIStates extends CLICommand {
102
105
  });
103
106
  }
104
107
 
108
+ /**
109
+ * Checks if state is a binary state
110
+ * @param {string} id id of the state
111
+ * @param {object} objects the objects db
112
+ * @param {ioBroker.OtherObject?} obj cached object
113
+ * @return {Promise<boolean>}
114
+ * @private
115
+ */
116
+ async _isBinary(id, objects, obj) {
117
+ obj = obj || (await objects.getObjectAsync(id));
118
+
119
+ return !!(obj && (obj.binary || (obj.common && obj.common.type === 'file')));
120
+ }
121
+
122
+ /**
123
+ * Get and show binary state
124
+ *
125
+ * @param {any[]} args
126
+ * @private
127
+ */
128
+ _getBinary(args) {
129
+ const { callback, dbConnect } = this.options;
130
+ const id = args[1];
131
+ dbConnect(async (objects, states) => {
132
+ try {
133
+ /** @type Buffer | null */
134
+ const state = await states.getBinaryState(id);
135
+
136
+ if (!state) {
137
+ CLI.error.stateNotFound(id);
138
+ return void callback(1);
139
+ }
140
+
141
+ if (Buffer.isBuffer(state)) {
142
+ console.log(state.toString(this.options.encoding));
143
+ return void callback();
144
+ } else {
145
+ CLI.error.stateNotBinary(id);
146
+ return void callback(1);
147
+ }
148
+ } catch (e) {
149
+ CLI.error.unknown(e);
150
+ return void callback(1);
151
+ }
152
+ });
153
+ }
154
+
105
155
  /**
106
156
  * Returns the value of a state
107
157
  * @param {any[]} args
@@ -111,7 +161,7 @@ module.exports = class CLIStates extends CLICommand {
111
161
  const { callback, dbConnect } = this.options;
112
162
  const id = args[1];
113
163
 
114
- dbConnect((objects, states) => {
164
+ dbConnect(async (objects, states) => {
115
165
  if (id.startsWith(ALIAS_STARTS_WITH)) {
116
166
  objects.getObject(id, (err, targetObj) => {
117
167
  // alias
@@ -120,17 +170,25 @@ module.exports = class CLIStates extends CLICommand {
120
170
  typeof targetObj.common.alias.id.read === 'string'
121
171
  ? targetObj.common.alias.id.read
122
172
  : targetObj.common.alias.id;
123
- objects.getObject(aliasId, (err, sourceObj) => {
124
- // write target
125
- states.getState(aliasId, (err, state) => {
126
- if (err || !targetObj) {
127
- CLI.error.unknown(err);
173
+ objects.getObject(aliasId, async (err, sourceObj) => {
174
+ // read target
175
+ try {
176
+ if (await this._isBinary(aliasId, objects, targetObj)) {
177
+ CLI.error.stateBinaryGetUnsupported(aliasId);
178
+ return void callback(1);
179
+ }
180
+
181
+ const state = await states.getStateAsync(aliasId);
182
+ if (!state) {
183
+ CLI.error.stateNotFound(id);
128
184
  } else {
129
185
  tools.formatAliasValue(sourceObj.common, targetObj.common, state, console, '');
130
186
  console.log(resultTransform(state));
131
187
  }
132
- return void callback();
133
- });
188
+ } catch (e) {
189
+ CLI.error.unknown(e);
190
+ }
191
+ return void callback();
134
192
  });
135
193
  } else {
136
194
  CLI.error.unknown(err || `Alias ${id} has no target`);
@@ -138,14 +196,21 @@ module.exports = class CLIStates extends CLICommand {
138
196
  }
139
197
  });
140
198
  } else {
141
- states.getState(id, (err, state) => {
142
- if (err || !state) {
143
- CLI.error.stateNotFound(id, err);
199
+ try {
200
+ if (await this._isBinary(id, objects)) {
201
+ CLI.error.stateBinaryGetUnsupported(id);
202
+ return void callback(1);
203
+ }
204
+ const state = await states.getStateAsync(id);
205
+ if (!state) {
206
+ CLI.error.stateNotFound(id);
144
207
  } else {
145
208
  console.log(resultTransform(state));
146
209
  }
147
- return void callback();
148
- });
210
+ } catch (e) {
211
+ CLI.error.unknown(e);
212
+ }
213
+ return void callback();
149
214
  }
150
215
  });
151
216
  }
@@ -173,7 +238,11 @@ module.exports = class CLIStates extends CLICommand {
173
238
  const newVal = ack === undefined ? { val, ack: false } : { val, ack: !!ack };
174
239
 
175
240
  if (id.startsWith(ALIAS_STARTS_WITH)) {
176
- objects.getObject(id, (err, obj) => {
241
+ objects.getObject(id, async (err, obj) => {
242
+ if (await this._isBinary(id, objects, obj)) {
243
+ CLI.error.stateBinarySetUnsupported(id);
244
+ return void callback(1);
245
+ }
177
246
  // alias
178
247
  if (obj && obj.common && obj.common.alias && obj.common.alias.id) {
179
248
  const aliasId =
@@ -227,11 +296,17 @@ module.exports = class CLIStates extends CLICommand {
227
296
  }
228
297
  });
229
298
  } else {
230
- objects.getObject(id, (err, obj) => {
299
+ objects.getObject(id, async (err, obj) => {
231
300
  if (err) {
232
301
  CLI.error.unknown(err);
233
302
  return void callback(1); // access error
234
303
  }
304
+
305
+ if (await this._isBinary(id, objects, obj)) {
306
+ CLI.error.stateBinarySetUnsupported(id);
307
+ return void callback(1);
308
+ }
309
+
235
310
  if (!obj && !force) {
236
311
  CLI.error.objectNotFound(id, 'null');
237
312
  return void callback(1); // object not exists
@@ -4,6 +4,11 @@ const { tools } = require('@iobroker/js-controller-common');
4
4
  const errorMessages = Object.freeze({
5
5
  stateNotFound: (/** @type {string} */ stateID, /** @type {string?} */ error) =>
6
6
  `The state ${stateID} was not found!` + (error ? ` Reason: ${error}` : ''),
7
+ stateNotBinary: (/** @type {string} */ stateId) => `State "${stateId}" is not binary.`,
8
+ stateBinaryGetUnsupported: (/** @type {string} */ stateId) =>
9
+ `State "${stateId}" is a binary state, please use getBinary.`,
10
+ stateBinarySetUnsupported: (/** @type {string} */ stateId) =>
11
+ `State "${stateId}" is a binary state and cannot be set via cli.`,
7
12
  objectNotFound: (/** @type {string} */ objectID, /** @type {string?} */ error) =>
8
13
  `The object ${objectID} was not found!` + (error ? ` Reason: ${error}` : ''),
9
14
  cannotUpdateObject: (/** @type {string} */ objectID, /** @type {string?} */ error) =>
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@iobroker/js-controller-cli",
3
- "version": "4.0.0-alpha.47-20220120-7bad8e08",
3
+ "version": "4.0.0-alpha.50-20220123-bedb0512",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
7
7
  "dependencies": {
8
- "@iobroker/js-controller-common": "4.0.0-alpha.47-20220120-7bad8e08",
9
- "@iobroker/js-controller-common-db": "4.0.0-alpha.47-20220120-7bad8e08",
8
+ "@iobroker/js-controller-common": "4.0.0-alpha.50-20220123-bedb0512",
9
+ "@iobroker/js-controller-common-db": "4.0.0-alpha.50-20220123-bedb0512",
10
10
  "chokidar": "^3.5.2",
11
11
  "daemonize2": "^0.4.2",
12
12
  "debug": "^4.3.3",
@@ -38,5 +38,5 @@
38
38
  "lib/",
39
39
  "index.js"
40
40
  ],
41
- "gitHead": "1b9bfb430be9ce5a823a9dcd91f612a216281f4f"
41
+ "gitHead": "6c23f1520df68de8eb5450e81ee94ec286063720"
42
42
  }