forked from UncleSamSwiss/dm-utils
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdm-test.ts
172 lines (168 loc) · 5.93 KB
/
dm-test.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {
type ActionContext,
type DeviceDetails,
DeviceManagement,
type DeviceRefresh,
type JsonFormSchema,
} from '../src';
import type * as base from '../src/types/base';
const demoFormSchema: JsonFormSchema = {
type: 'tabs',
items: {
options1: {
type: 'panel',
label: 'Tab1',
icon: 'base64 svg', // optional
items: {
myPort: {
type: 'number',
min: 1,
max: 65565,
label: 'Number',
sm: 6, // 1 - 12
// "validator": "'"!!data.name"'", // else error
hidden: 'data.myType === 1', // hidden if myType is 1
disabled: 'data.myType === 2', // disabled if myType is 2
},
myType: {
// name could support more than one levelhelperText
newLine: true, // must start from new row
type: 'select',
label: 'My Type',
sm: 6, // 1 - 12
options: [
{ label: 'option 0', value: 0 },
{ label: 'option 1', value: 1 },
{ label: 'option 2', value: 2 },
],
},
myBool: {
type: 'checkbox',
label: 'My checkbox',
},
},
},
options2: {
type: 'panel',
label: 'Tab2',
icon: 'base64 svg', // optional
items: {
secondPort: {
type: 'number',
min: 1,
max: 65565,
label: 'Second Number',
sm: 6, // 1 - 12
// "validator": "'"!!data.name"'", // else error
hidden: 'data.secondType === 1', // hidden if myType is 1
disabled: 'data.secondType === 2', // disabled if myType is 2
},
secondType: {
// name could support more than one levelhelperText
newLine: true, // must start from new row
type: 'select',
label: 'Second Type',
sm: 6, // 1 - 12
options: [
{ label: 'option 0', value: 0 },
{ label: 'option 1', value: 1 },
{ label: 'option 2', value: 2 },
],
},
secondBool: {
type: 'checkbox',
label: 'Second checkbox',
},
},
},
},
};
export class DmTestDeviceManagement extends DeviceManagement {
protected listDevices(): Promise<base.DeviceInfo<'adapter'>[]> {
return Promise.resolve([
{ id: 'test-123', name: 'Test 123', status: 'connected' },
{ id: 'test-345', name: 'Test 345', status: 'disconnected', hasDetails: true, actions: [] },
{
id: 'test-789',
name: 'Test 789',
status: 'connected',
actions: [
{
id: 'play',
icon: 'fas fa-play',
},
{
id: 'pause',
icon: 'fa-pause',
description: 'Pause device',
},
{
id: 'forward',
icon: 'forward',
description: 'Forward',
},
],
},
{
id: 'test-ABC',
name: 'Test ABC',
status: 'connected',
actions: [
{
id: 'forms',
icon: 'fab fa-wpforms',
description: 'Show forms flow',
},
],
},
]);
}
protected override async handleDeviceAction(
deviceId: string,
actionId: string,
context: ActionContext,
): Promise<{ refresh: DeviceRefresh }> {
switch (actionId) {
case 'play':
this.log.info(`Play was pressed on ${deviceId}`);
return { refresh: false };
case 'pause': {
this.log.info(`Pause was pressed on ${deviceId}`);
const confirm = await context.showConfirmation('Do you want to refresh the device only?');
return { refresh: confirm ? 'device' : 'instance' };
}
case 'forms': {
this.log.info(`Forms was pressed on ${deviceId}`);
const data = await context.showForm(demoFormSchema, { data: { myPort: 8081, secondPort: 8082 } });
if (!data) {
await context.showMessage('You cancelled the previous form!');
} else {
await context.showMessage(`You entered: ${JSON.stringify(data)}`);
}
return { refresh: false };
}
default:
throw new Error(`Unknown action ${actionId}`);
}
}
protected override getDeviceDetails(id: string): Promise<DeviceDetails> {
const schema: JsonFormSchema = {
type: 'panel',
items: {
text1: {
type: 'staticText',
text: 'This is some description',
sm: 12,
},
button1: {
type: 'sendto',
label: 'Click me to send a message!',
sm: 6,
command: 'send',
data: { hello: 'world' },
},
},
};
return Promise.resolve({ id, schema });
}
}