forked from WebThingsIO/webthing-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mastodon-thing.js
65 lines (58 loc) · 1.87 KB
/
mastodon-thing.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
// -*- mode: js; js-indent-level:2; -*-
// SPDX-License-Identifier: MPL-2.0
/**
*
* Copyright 2018-present Samsung Electronics France SAS, and other contributors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
const {
Property,
SingleThing,
Thing,
Value,
WebThingServer,
} = require('webthing');
const fs = require('fs');
const Mastodon = require('mastodon-lite');
const conf = '.mastodon-lite.json';
const config = JSON.parse(fs.readFileSync(conf, 'utf8'));
const mastodon = Mastodon(config);
function handleLevelUpdate(value)
{
var message = value;
message = `https://s-opensource.org/tag/wot/# #MultiLevelSwitch is \"${value}\" (#MastodonLite #WebThing Actuator)`
console.log(message);
mastodon.post(message);
}
function makeThing() {
const thing = new Thing('MastodonMultiLevelSwitchExample', 'multiLevelSwitch', 'An actuator example that just blog');
thing.addProperty(
new Property(thing,
'level',
new Value(0, handleLevelUpdate),
{
label: 'Level',
type: 'number',
description: 'Whether the output is changed',
}));
return thing;
}
function runServer() {
const port = process.argv[2] ? Number(process.argv[2]) : 8888;
const url = `http://localhost:${port}/properties/multiLevelSwitch`;
console.log('Usage:\n'
+ process.argv[0] + ' ' + process.argv[1] + ' [port]\n'
+ 'Try:\ncurl -H "Content-Type: application/json" '
+ url + '\n');
const thing = makeThing();
const server = new WebThingServer(new SingleThing(thing), port);
process.on('SIGINT', () => {
server.stop();
process.exit();
});
server.start();
}
runServer();