Skip to content

Commit 11845aa

Browse files
authored
Merge pull request #61 from bewee/bewee/patch-4
Add custom actions to custom things
2 parents 5fdf5b3 + acd83ce commit 11845aa

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

manifest.json

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
"items": {
4747
"type": "object",
4848
"required": [
49-
"name",
50-
"properties"
49+
"name"
5150
],
5251
"properties": {
5352
"name": {
@@ -145,6 +144,38 @@
145144
}
146145
}
147146
}
147+
},
148+
"actions": {
149+
"type": "array",
150+
"items": {
151+
"type": "object",
152+
"required": [
153+
"name",
154+
"title"
155+
],
156+
"properties": {
157+
"name": {
158+
"description": "Machine-readable action name, e.g. \"action1\".",
159+
"type": "string"
160+
},
161+
"title": {
162+
"description": "Human-readable action name, e.g. \"My Action 1\".",
163+
"type": "string"
164+
},
165+
"description": {
166+
"description": "Human-readable description of this action.",
167+
"type": "string"
168+
},
169+
"input": {
170+
"description": "Optional JSON describing the inputs of this action (No inputs if empty)",
171+
"type": "string"
172+
},
173+
"emitEvent": {
174+
"description": "Emit an event whenever this action gets executed",
175+
"type": "boolean"
176+
}
177+
}
178+
}
148179
}
149180
}
150181
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"devDependencies": {
2525
"babel-eslint": "^10.1.0",
26-
"eslint": "^7.14.0"
26+
"eslint": "^7.25.0"
2727
},
2828
"files": [
2929
"LICENSE",

virtual-things-adapter.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1323,6 +1323,16 @@ class VirtualThingsDevice extends Device {
13231323

13241324
action.start();
13251325

1326+
if (this.id.startsWith('virtual-things-custom-')) {
1327+
if (this.events.has(action.name)) {
1328+
this.eventNotify(new Event(this,
1329+
action.name,
1330+
action.input));
1331+
}
1332+
action.finish();
1333+
return Promise.resolve();
1334+
}
1335+
13261336
switch (action.name) {
13271337
case 'basic':
13281338
this.eventNotify(new Event(this,
@@ -1525,7 +1535,7 @@ class VirtualThingsAdapter extends Adapter {
15251535
continue;
15261536
}
15271537

1528-
const properties = descr.properties.map((property) => {
1538+
const properties = (descr.properties || []).map((property) => {
15291539
return Object.assign({}, property);
15301540
});
15311541
for (const property of properties) {
@@ -1583,6 +1593,17 @@ class VirtualThingsAdapter extends Adapter {
15831593
}
15841594
}
15851595

1596+
const actions = (descr.actions || []).map((action) => {
1597+
return Object.assign({}, action);
1598+
});
1599+
for (const action of actions) {
1600+
try {
1601+
action.input = JSON.parse(action.input);
1602+
} catch (ex) {
1603+
delete action.input;
1604+
}
1605+
}
1606+
15861607
const newDescr = {
15871608
type: 'thing',
15881609
'@context': descr['@context'] || 'https://iot.mozilla.org/schemas',
@@ -1638,6 +1659,38 @@ class VirtualThingsAdapter extends Adapter {
16381659
newDescr.properties.push(prop);
16391660
}
16401661

1662+
for (const action of actions) {
1663+
const act = {
1664+
name: action.name,
1665+
metadata: {
1666+
title: action.title,
1667+
},
1668+
};
1669+
1670+
if (action.description) {
1671+
act.metadata.description = action.description;
1672+
}
1673+
1674+
if (action.input) {
1675+
act.metadata.input = action.input;
1676+
}
1677+
1678+
if (action.emitEvent) {
1679+
const event = {
1680+
name: action.name,
1681+
metadata: {
1682+
title: action.title,
1683+
},
1684+
};
1685+
if (action.input) {
1686+
event.type = action.input.type;
1687+
}
1688+
newDescr.events.push(event);
1689+
}
1690+
1691+
newDescr.actions.push(act);
1692+
}
1693+
16411694
new VirtualThingsDevice(this, id, newDescr);
16421695
}
16431696

0 commit comments

Comments
 (0)