Skip to content

Commit 1273f61

Browse files
committed
refactor tutorial desktop part
1 parent c6fc4d1 commit 1273f61

File tree

1 file changed

+89
-114
lines changed

1 file changed

+89
-114
lines changed

hf-data/tutorial.md

Lines changed: 89 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -139,129 +139,104 @@ Data collected from the mouse movement (X and Y positions) will be stored in the
139139
Connection with Pryv is established to store collected measures in the stream "**HF demo**":
140140
```javascript
141141
async function setupStreamStructure(connection) {
142-
var postData;
143-
var resultTreatment = [];
144-
var postData = [];
145-
var streams = (await connection.get('streams', null)).streams;
146-
let [hasHF, hasDesktop, hasMobile] = hasStreams(streams);
147-
if (!hasHF) {
148-
postData.push(
149-
{
150-
method: 'streams.create',
151-
params: {
152-
id: 'hfdemo',
153-
name: 'HF Demo',
154-
parentId: 'hf'
155-
}
156-
},
157-
);
158-
resultTreatment.push(null);
159-
}
160-
```
161-
162-
[HF events](https://api.pryv.com/reference/#create-hf-event) need to be created to hold the mouse position, which will be consisting of points along the x and y-axis of type `count/generic` (see [Event Types Reference](https://api.pryv.com/event-types/)):
163-
```javascript
164-
if (!hasDesktop) {
165-
postData.push(
166-
{
167-
method: 'streams.create',
168-
params: {
169-
id: 'hfdemo-mouse-x',
170-
name: 'Mouse-X',
171-
parentId: 'hfdemo'
172-
}
173-
},
174-
{
175-
method: 'streams.update',
176-
params: {
177-
id: 'hfdemo-mouse-x',
178-
update: {
179-
clientData: buildPlotlyOptions('Mouse', 'count/generic', 'X')
180-
}
181-
}
182-
},
183-
{
184-
method: 'events.create',
185-
params: {
186-
streamId: 'hfdemo-mouse-x',
187-
type: 'series:count/generic',
188-
description: 'Holder for x mouse position',
189-
}
190-
},
191-
{
192-
method: 'streams.create',
193-
params: {
194-
id: 'hfdemo-mouse-y',
195-
name: 'Mouse-Y',
196-
parentId: 'hfdemo'
197-
}
198-
},
199-
{
200-
method: 'streams.update',
201-
params: {
202-
id: 'hfdemo-mouse-y',
203-
update: {
204-
clientData: buildPlotlyOptions('Mouse', 'count/generic', 'Y')
205-
}
206-
}
207-
},
208-
{
209-
method: 'events.create',
210-
params: {
211-
streamId: 'hfdemo-mouse-y',
212-
type: 'series:count/generic',
213-
description: 'Holder for y mouse position',
214-
}
215-
}
216-
);
142+
const resultHandlers = [];
143+
const apiCalls = [];
144+
const streams = (await connection.get('streams', null)).streams;
145+
const [hasRootStream, hasDesktopStream, hasMobileStream] = hasStreams(
146+
streams
147+
);
148+
if (!hasRootStream) {
149+
apiCalls.push({
150+
method: 'streams.create',
151+
params: {
152+
id: 'hfdemo',
153+
name: 'HF Demo',
154+
parentId: 'hf'
155+
}
156+
});
157+
resultHandlers.push(null);
158+
}
217159
```
218160
219-
These events are populated with the X and Y positions of the mouse:
161+
[HF events](https://api.pryv.com/reference/#create-hf-event) and their streams need to be created to hold the mouse position, which will be consisting of points along the x and y-axis of type `count/generic` (see [Event Types Reference](https://api.pryv.com/event-types/)):
220162
```javascript
221-
resultTreatment.push(
222-
null,
223-
null,
224-
function handleCreateEventX(result) {
225-
pryvHF.measures.mouseX.event = result.event;
226-
227-
console.log('handle xEvent set', result.event);
228-
},
229-
null,
230-
null,
231-
function handleCreateEventY(result) {
232-
pryvHF.measures.mouseY.event = result.event;
233-
console.log('handle yEvent set', result.event);
234-
}
235-
);
236-
} else {
237-
postData.push(
163+
if (!hasDesktopStream) {
164+
apiCalls.push(
238165
{
239-
method: 'events.create',
240-
params: {
241-
streamId: 'hfdemo-mouse-x',
242-
type: 'series:count/generic',
243-
description: 'Holder for x mouse position',
244-
}
166+
method: 'streams.create',
167+
params: {
168+
id: 'hfdemo-mouse-x',
169+
name: 'Mouse-X',
170+
parentId: 'hfdemo',
171+
clientData: buildPlotlyOptions('Mouse', 'count/generic', 'X')
172+
}
245173
},
246174
{
247-
method: 'events.create',
248-
params: {
249-
streamId: 'hfdemo-mouse-y',
250-
type: 'series:count/generic',
251-
description: 'Holder for y mouse position',
252-
}
175+
method: 'streams.create',
176+
params: {
177+
id: 'hfdemo-mouse-y',
178+
name: 'Mouse-Y',
179+
parentId: 'hfdemo',
180+
clientData: buildPlotlyOptions('Mouse', 'count/generic', 'Y')
181+
}
253182
}
254-
);
255-
resultTreatment.push(
256-
function handleCreateEventX(result) {
257-
pryvHF.measures.mouseX.event = result.event;
258-
console.log('handle xEvent set', result.event);
259-
},
260-
function handleCreateEventY(result) {
261-
pryvHF.measures.mouseY.event = result.event;
262-
console.log('handle yEvent set', result.event);
183+
);
184+
185+
resultHandlers.push(null, null, null, null);
186+
}
187+
apiCalls.push(
188+
{
189+
method: 'events.create',
190+
params: {
191+
streamId: 'hfdemo-mouse-x',
192+
type: 'series:count/generic',
193+
description: 'Holder for x mouse position'
194+
}
195+
},
196+
{
197+
method: 'events.create',
198+
params: {
199+
streamId: 'hfdemo-mouse-y',
200+
type: 'series:count/generic',
201+
description: 'Holder for y mouse position'
263202
}
203+
}
264204
);
205+
// https://github.com/pryv/lib-js#advanced-usage-of-api-calls-with-optional-individual-result-and-progress-callbacks
206+
resultHandlers.push(
207+
function registerEventX(result) {
208+
pryvHF.measures.mouseX.event = result.event;
209+
console.log('handle xEvent set', result.event);
210+
},
211+
function registerEventY(result) {
212+
pryvHF.measures.mouseY.event = result.event;
213+
console.log('handle yEvent set', result.event);
214+
}
215+
);
216+
```
217+
218+
These events are populated with the X and Y positions of the mouse:
219+
```javascript
220+
function savePoints() {
221+
if (pryvHF.pryvConn) {
222+
sendHfPoints(pryvHF.pryvConn, pryvHF.measures);
223+
}
224+
setTimeout(savePoints, SAMPLE_POST_MS);
225+
}
226+
227+
function sendHfPoints(connection, measures) {
228+
for (let key in measures) {
229+
let bufferLength = measures[key].buffer.length;
230+
if (measures[key].event && bufferLength > 0) {
231+
let points = measures[key].buffer;
232+
connection.addPointsToHFEvent(
233+
measures[key].event.id,
234+
measures[key].event.content.fields,
235+
points
236+
);
237+
measures[key].buffer = [];
238+
}
239+
}
265240
}
266241
```
267242

0 commit comments

Comments
 (0)