Skip to content

Commit cc583ba

Browse files
committed
Updates example consumer script.
1 parent 6eaa1a3 commit cc583ba

File tree

1 file changed

+12
-245
lines changed

1 file changed

+12
-245
lines changed

src/consumer.ts

Lines changed: 12 additions & 245 deletions
Original file line numberDiff line numberDiff line change
@@ -1,259 +1,28 @@
1-
/* eslint-disable prettier/prettier */
2-
// import { hexToUint8Array } from './utils';
1+
import { Capabilities, Command, CommandType } from '.';
32

4-
import { Capabilities, Command, CommandType, Values } from '.';
3+
/**
4+
* Get / set commands
5+
*/
56

6-
// Test case(s) 1
7-
const commands = [
8-
[
9-
0x0b,
10-
0x00, 0x23, // charging
11-
0x01, // set
12-
0x0c, // charge_mode
13-
0x00, 0x04,
14-
0x01,
15-
0x00, 0x01,
16-
0x00, // immediate
17-
],
18-
[
19-
0x0b,
20-
0x00, 0x36, // lights
21-
0x01, // set
22-
0x01, // front_exterior_light
23-
0x00, 0x04,
24-
0x01,
25-
0x00, 0x01,
26-
0x02, // active_with_full_beam
27-
0x0a, // switch_position
28-
0x00, 0x04,
29-
0x01,
30-
0x00, 0x01,
31-
0x03, // parking_light_left
32-
],
33-
[
34-
0x0b,
35-
0x00, 0x36, // lights
36-
0x00, // get all
37-
],
38-
[
39-
0x0b,
40-
0x00, 0x36, // lights
41-
0x00, // get
42-
0x05, // reverse_light
43-
0x07, // fog_lights
44-
0xa3, // vin (universal prop)
45-
],
46-
[
47-
12,
48-
0, 2, // failure_message
49-
1, // set
50-
1, // prop failed_message_id
51-
0, 5,
52-
1, 0, 2, 0, 87,
53-
2, // prop failed_message_type
54-
0, 4,
55-
1, 0, 1, 0,
56-
3, // prop failure_reason
57-
0, 4,
58-
1,
59-
0, 1,
60-
0 // unsupported_capability
61-
],
62-
[
63-
12,
64-
0, 52, // maintenance
65-
1, // set
66-
1, // days_to_next_service
67-
0, 15,
68-
5, 0, 12, 0, 14,
69-
0, 64, 80, 0, 0, 0, 0, 0, 0, 1,
70-
2, // kilometers_to_next_service
71-
0, 15, 5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0,
72-
0, 0, 0, 1,
73-
3,
74-
0, 15,
75-
5, 0, 12, 0, 14,
76-
0, 64, 80, 0, 0, 0, 0, 0, 0, 1,
77-
4,
78-
0, 15,
79-
5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0,
80-
0, 0, 0, 1,
81-
5,
82-
0, 15,
83-
5, 0, 12, 0, 14,
84-
0, 64, 80, 0, 0, 0, 0, 0, 0, 1,
85-
7,
86-
0, 15, 5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0,
87-
0, 0, 0, 1,
88-
10,
89-
0, 15,
90-
5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0, 0, 0, 0, 1,
91-
11,
92-
0, 15,
93-
5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0, 0, 0, 0, 1,
94-
12,
95-
0, 15,
96-
5, 0, 12, 0, 14, 0, 64, 80, 0, 0, 0, 0, 0, 0, 1
97-
],
98-
[
99-
12,
100-
0, 105, // engine
101-
1, // set
102-
1, // status
103-
0, 4,
104-
1, // data
105-
0, 1,
106-
0, // value
107-
],
108-
[
109-
12,
110-
0, 16, // capabilities
111-
1, // set
112-
1, // capabilities
113-
0, 18,
114-
1,
115-
0, 15,
116-
0, 35, 0, 11, 2, 3, 10, 11, 12, 14, 15, 16, 17, 22, 23,
117-
1,
118-
0, 9,
119-
1,
120-
0, 6,
121-
0, 36, 0, 2, 2, 5,
122-
1,
123-
0, 8,
124-
1, 0, 5, 0, 97, 0, 1, 1,
125-
1,
126-
0, 18,
127-
1,
128-
0, 15, 0, 51, 0, 11, 1, 3, 5, 6, 11, 15, 17, 24, 25, 26, 29,
129-
1,
130-
0, 10,
131-
1, 0, 7, 0, 32, 0, 3, 3, 4, 6,
132-
1,
133-
0, 8,
134-
1, 0, 5, 0, 105, 0, 1, 1,
135-
1,
136-
0, 8,
137-
1, 0, 5, 0, 64, 0, 1, 2,
138-
1,
139-
0, 8,
140-
1, 0, 5, 0, 103, 0, 1, 1,
141-
1,
142-
0, 9,
143-
1, 0, 6, 0, 53, 0, 2, 1, 2,
144-
1,
145-
0, 10,
146-
1, 0, 7, 0, 54, 0, 3, 1, 8, 9,
147-
1,
148-
0, 16,
149-
1, 0, 13, 0, 52, 0, 9, 1, 2, 3, 4, 5, 7, 10, 11, 12,
150-
1,
151-
0, 8,
152-
1, 0, 5, 0, 102, 0, 1, 1,
153-
1,
154-
0, 10,
155-
1, 0, 7, 0, 87, 0, 3, 11, 12, 18,
156-
1,
157-
0, 11,
158-
1, 0, 8, 0, 37, 0, 4, 2, 3, 4, 5,
159-
1,
160-
0, 9,
161-
1, 0, 6, 0, 33, 0, 2, 1, 2,
162-
1,
163-
0, 20,
164-
1, 0, 17, 0, 104, 0, 13, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
165-
1, 0,
166-
10, 1, 0, 7, 0, 48, 0, 3, 4, 5, 6,
167-
1,
168-
0, 8,
169-
1, 0, 5, 0, 69, 0, 1, 3
170-
]
171-
];
7+
const request = new Command(CommandType.Get, new Capabilities.Race()).encode();
1728

173-
for (const bytes of commands) {
174-
const command = Command.parse(bytes);
175-
const commandAsJSON = JSON.stringify(command);
176-
const fromJSONString = JSON.stringify(Command.fromJSON(JSON.parse(commandAsJSON)));
177-
178-
console.log(commandAsJSON, fromJSONString === commandAsJSON);
179-
}
180-
181-
// Test case 2
182-
183-
// Client
184-
const capability = new Capabilities.Race();
185-
capability.createProperty('gear_mode');
186-
const request = new Command(CommandType.Get, capability).encode();
187-
188-
// Consumer populates command with state and responds with set command
1899
const command = Command.parse(request);
19010

19111
if (command.type === CommandType.Get) {
19212
const { capability } = command;
19313

194-
// (or iterate over all props...)
195-
if (capability.hasProperty('gear_mode')) {
196-
const property = capability.getProperty('gear_mode');
197-
198-
property!.createComponent('data', 'reverse');
199-
property!.createComponent('timestamp', new Date());
14+
if (!capability.hasProperty('gear_mode')) {
15+
capability.createProperty('gear_mode', 'reverse').createComponent('timestamp', new Date());
20016
}
201-
202-
const response = command.setType(CommandType.Set);
203-
204-
console.log(JSON.stringify(response), response.encode());
20517
}
20618

207-
// Test case 3
208-
209-
const customValueDefinition = {
210-
name: 'test',
211-
name_cased: 'Test',
212-
type: 'custom',
213-
items: [
214-
{
215-
name: 'a',
216-
name_cased: 'A',
217-
type: 'enum',
218-
size: 1,
219-
enum_values: [
220-
{
221-
name: 'v1',
222-
id: 0,
223-
},
224-
{
225-
name: 'v2',
226-
id: 1,
227-
},
228-
],
229-
},
230-
{
231-
name: 'b',
232-
name_cased: 'B',
233-
type: 'string',
234-
},
235-
{
236-
name: 'c',
237-
name_cased: 'C',
238-
type: 'integer',
239-
size: 1,
240-
},
241-
{
242-
name: 'd',
243-
name_cased: 'D',
244-
type: 'double',
245-
size: 8,
246-
},
247-
],
248-
};
249-
const encodedCustomValue = new Values.CustomValue(customValueDefinition)
250-
.setValue({ a: 1, b: 'Long string....', c: 1, d: 0.5400000214576721 })
251-
.encode();
252-
const decodedCustomValue = new Values.CustomValue(customValueDefinition).decode(encodedCustomValue);
19+
const response = command.setType(CommandType.Set);
25320

254-
console.log(encodedCustomValue, JSON.stringify(decodedCustomValue));
21+
console.log(JSON.stringify(response));
25522

256-
// Test case 4
23+
/**
24+
* Multi-command
25+
*/
25726

25827
const multiCommand = new Command(CommandType.Set, new Capabilities.MultiCommand());
25928

@@ -277,5 +46,3 @@ console.log(
27746
multiCommandAsJSON,
27847
multiCommandAsJSON === JSON.stringify(Command.parse(multiCommandEncoded)),
27948
);
280-
281-

0 commit comments

Comments
 (0)