Skip to content

Commit

Permalink
Improve discovery endpoint cookie limitation (#540)
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <jeremy.setton@gmail.com>
  • Loading branch information
jsetton authored Dec 26, 2022
1 parent 04f5625 commit cdf7455
Show file tree
Hide file tree
Showing 32 changed files with 519 additions and 445 deletions.
7 changes: 5 additions & 2 deletions docs/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ If paired with [`TiltAngle`](#tiltangle), the primary controls (open/close/stop)
* defaults to `position`
* presets=`<presets>`
* each preset formatted as `<presetValue>=<@assetIdOrName1>:...` (e.g. `presets="20=Morning,60=Afternoon,80=Evening:@Setting.Night"`)
* limited to a maximum of 150 presets
* predefined [asset ids](#asset-catalog)
* defaults to item state description options `presets="value1=label1,..."` if defined, otherwise no presets
* language=`<code>`
Expand Down Expand Up @@ -681,6 +682,7 @@ If paired with [`PositionState`](#positionstate), the primary controls (open/clo
* defaults to `position`
* presets=`<presets>`
* each preset formatted as `<presetValue>=<@assetIdOrName1>:...` (e.g. `presets="20=Morning,60=Afternoon,80=Evening:@Setting.Night"`)
* limited to a maximum of 150 presets
* predefined [asset ids](#asset-catalog)
* defaults to item state description options `presets="value1=label1,..."` if defined, otherwise no presets
* language=`<code>`
Expand Down Expand Up @@ -767,7 +769,7 @@ Items that represent an input source (e.g. "HDMI 1" or "TUNER" on a stereo).
* Supported metadata parameters:
* supportedInputs=`<inputs>`
* each input formatted as `<inputValue>=<inputName1>:...` (e.g. `supportedInputs="HDMI1=Cable:Comcast,HDMI2=Kodi"`)
* requires at least two inputs to be specified
* requires at least two inputs to be specified with a maximum of 150
* input value used as name if not provided (e.g. `supportedInputs="HDMI1,DVD"` <=> `supportedInputs="HDMI1=HDMI1,DVD=DVD`)
* defaults to item state description options `supportedInputs="value1=label1,..."`, if defined, otherwise no supported inputs
* language=`<code>`
Expand Down Expand Up @@ -1656,7 +1658,7 @@ Items that represent components of a device that have more than one setting. Mul
* defaults to item state description read only property if defined, otherwise false
* supportedModes=`<modes>`
* each mode formatted as `<mode>=<@assetIdOrName1>:...` (e.g. `supportedModes="0=Cold:Cool,1=Warm,2=Hot"`)
* requires at least two modes to be specified
* requires at least two modes to be specified with a maximum of 150
* shortened format available for string-based modes by either leaving the first element empty or not providing the mode name at all (e.g. `supportedModes="Normal=:Cottons,Whites"` <=> `supportedModes="Normal=Normal:Cottons,Whites=Whites`)
* defaults to item state description options `supportedModes="value1=label1,..."`, if defined, otherwise no supported modes
* ordered=`<boolean>`
Expand Down Expand Up @@ -1729,6 +1731,7 @@ Items that represent components of a device that are characterized by numbers wi
* defaults to item state description min, max & step values, if defined, otherwise `"0:100:1"` (Dimmer/Rollershutter); `"0:10:1"` (Number)
* presets=`<presets>`
* each preset formatted as `<presetValue>=<@assetIdOrName1>:...` (e.g. `presets="1=@Value.Low:Lowest,10=@Value.High:Highest"`)
* limited to a maximum of 150 presets
* requires to be a multiple of the supported range precision
* defaults to item state description options `presets="value1=label1,..."` if defined, otherwise no presets
* unitOfMeasure=`<unitId>`
Expand Down
3 changes: 2 additions & 1 deletion lambda/alexa/smarthome/directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* SPDX-License-Identifier: EPL-2.0
*/

const { decompressJSON } = require('@root/utils');
const { Interface } = require('./constants');
const AlexaEndpoint = require('./endpoint');
const AlexaResponse = require('./response');
Expand Down Expand Up @@ -136,7 +137,7 @@ class AlexaDirective {
...(this.hasEndpoint && {
endpointId: this._directive.endpoint.endpointId,
cookie: Object.entries(this._directive.endpoint.cookie || {}).reduce(
(cookie, [key, value]) => ({ ...cookie, [key]: JSON.parse(value) }),
(cookie, [key, value]) => ({ ...cookie, [key]: decompressJSON(value) }),
{}
),
payload: this.payload
Expand Down
6 changes: 3 additions & 3 deletions lambda/alexa/smarthome/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

const { v5: uuidv5 } = require('uuid');
const { stripPunctuation } = require('@root/utils');
const { compressJSON, decompressJSON, stripPunctuation } = require('@root/utils');
const { ItemType } = require('@openhab/constants');
const AlexaCapabilities = require('./capabilities');
const AlexaDisplayCategory = require('./category');
Expand Down Expand Up @@ -270,7 +270,7 @@ class AlexaEndpoint {
*/
getCookie() {
const capabilities = this.capabilities.map((capability) => capability.toJSON()).flat();
return { ...(capabilities.length > 0 && { capabilities: JSON.stringify(capabilities) }) };
return { ...(capabilities.length > 0 && { capabilities: compressJSON(capabilities) }) };
}

/**
Expand Down Expand Up @@ -513,7 +513,7 @@ class AlexaEndpoint {
// Load directive endpoint cookie capabilities if defined,
// otherwise load property map for backward compatibility if present
if (cookie.capabilities) {
endpoint.loadCapabilities(JSON.parse(cookie.capabilities));
endpoint.loadCapabilities(decompressJSON(cookie.capabilities));
} else if (cookie.propertyMap) {
endpoint.loadPropertyMap(JSON.parse(cookie.propertyMap));
}
Expand Down
7 changes: 7 additions & 0 deletions lambda/alexa/smarthome/properties/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const Generic = require('./generic');
* @extends Generic
*/
class Mode extends Generic {
/**
* Defines supported modes limit
* @type {Number}
*/
static #SUPPORTED_MODES_LIMIT = 150;

/**
* Returns supported item types
* @return {Array}
Expand Down Expand Up @@ -199,6 +205,7 @@ class Mode extends Generic {
)
])
.filter(([, labels]) => labels.length > 0)
.slice(0, Mode.#SUPPORTED_MODES_LIMIT)
.reduce((modes, [mode, labels]) => ({ ...modes, [mode]: labels }), undefined);

// Delete supported commands parameter
Expand Down
7 changes: 7 additions & 0 deletions lambda/alexa/smarthome/properties/rangeValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ const Generic = require('./generic');
* @extends Generic
*/
class RangeValue extends Generic {
/**
* Defines presets limit
* @type {Number}
*/
static #PRESETS_LIMIT = 150;

/**
* Returns supported item types
* @return {Array}
Expand Down Expand Up @@ -206,6 +212,7 @@ class RangeValue extends Generic {
preset % this.supportedRange[2] === 0 &&
labels.length > 0
)
.slice(0, RangeValue.#PRESETS_LIMIT)
.reduce((presets, [preset, labels]) => ({ ...presets, [preset]: labels }), undefined);

// Define unit of measure as follow:
Expand Down
Loading

0 comments on commit cdf7455

Please sign in to comment.