22import { dataDefs } from './common-data-defs.js' ;
33
44export const drPatientLib = {
5- getPatientData
5+ setRefresh
66}
77
8+ let connection ;
9+ async function setRefresh ( patientApiEndoint , questionaryId , refreshCallBack ) {
10+ connection = new Pryv . Connection ( patientApiEndoint ) ;
11+ const infos = await connection . accessInfo ( ) ;
12+
13+ async function doRefresh ( ) {
14+ const lines = await getPatientData ( questionaryId ) ;
15+ refreshCallBack ( lines ) ;
16+ }
17+
18+ await connection . socket . open ( ) ;
19+ connection . socket . on ( 'eventsChanged' , async ( ) => {
20+ console . log ( '>> refresh event' ) ;
21+ await doRefresh ( ) ;
22+ } ) ;
23+
24+ // do it once
25+ doRefresh ( ) ;
26+ return infos ;
27+ }
28+
29+
830// prepare data for easy lookup
931// map streamId/EventType with form data
1032const eventMapByQuestionnary = { } ;
@@ -19,19 +41,17 @@ for (const [questionaryId, questionary] of Object.entries(dataDefs.questionnaire
1941 }
2042 }
2143}
22- console . log ( '>> eventMapByQuestionnary' , eventMapByQuestionnary ) ;
2344
24- async function getPatientData ( patientApiEndoint , questionaryId ) {
45+ async function getPatientData ( questionaryId ) {
2546 const patientData = [ ] ;
2647 const queryParams = { limit : 10000 } ;
2748 function forEachEvent ( event ) {
2849 patientData . push ( getLineForEvent ( event , questionaryId ) ) ;
2950 }
3051
31- const connection = new Pryv . Connection ( patientApiEndoint ) ;
32- const infos = await connection . accessInfo ( ) ;
52+
3353 await connection . getEventsStreamed ( queryParams , forEachEvent ) ;
34- return { infos , lines : patientData } ;
54+ return patientData ;
3555}
3656
3757
@@ -46,15 +66,17 @@ function getLineForEvent (event, questionaryId) {
4666 }
4767
4868 const field = eventMapByQuestionnary [ questionaryId ] [ event . streamId + ':' + event . type ] ;
49- console . log ( '>> field' , field , event ) ;
5069 if ( field ) {
5170 Object . assign ( line , field ) ;
5271 if ( field . type === 'date' ) {
5372 line . value = ( new Date ( event . time * 1000 ) ) . toISOString ( ) . split ( 'T' ) [ 0 ] ;
5473 }
5574 if ( field . type === 'select' ) {
5675 line . value = event . content ;
57-
76+ if ( field . eventType === 'ratio/generic' ) {
77+ line . value = event . content . value ;
78+ }
79+
5880 const selected = field . options . find ( ( o ) => ( o . value === line . value ) ) ;
5981 line . description = selected ?. label ;
6082 }
0 commit comments