source: branches/FACT++_lidctrl_usb/scripts/getSchedule.js@ 19127

Last change on this file since 19127 was 18713, checked in by tbretz, 8 years ago
The conversion key to name is now done directly with the query by joining the tables (the results are not that big anyways), added a sanity check for the new suspend/resume feature
File size: 2.8 KB
Line 
1// ======================================================================================
2
3function getSchedule()
4{
5 // Get current time
6 var start = new Date();//new Date("2013-04-07 19:00:00 UTC");
7
8 // Because Main.js could start a new observations just in the moment between 'now'
9 // and entering the new data in the database, we have to use the unique id
10 // in Main.js to check if the current observation should be changed (and sub resetted)
11 start = new Date(start.getTime()-10*3600000);
12
13 // ----------------------------------------------------------------------
14
15 // Connect to database
16 var db = new Database($['schedule-database']);
17
18 // Get the current schedule
19 var rows = db.query("SELECT * FROM Schedule "+
20 "LEFT JOIN MeasurementType USING (fMeasurementTypeKey) "+
21 "LEFT JOIN Source USING (fSourceKey) "+
22 "WHERE fStart>'"+start.toISOString()+"' "+
23 "ORDER BY fStart, fMeasurementID");
24
25 // Close db connection
26 db.close();
27
28 // ----------------------------------------------------------------------
29
30 var schedule = [];
31 var entry = -1;
32 var sub = 0;
33
34 for (var i=0; i<rows.length; i++)
35 {
36 var id = rows[i]['fScheduleID'];
37 var sub = rows[i]['fMeasurementID'];
38 if (sub==0)
39 entry++;
40
41 var m = { }
42
43 m.task = rows[i]['fMeasurementTypeName'];
44 if (!m.task)
45 throw new Error("No valid measurement type for id=("+id+":"+sub+")");
46
47 // For simplicity, measurements suspend and resume must be unique in an observation
48 if ((m.task=="suspend" || m.task=="resume") && sub>0)
49 throw new Error("Measurement "+m.task+" not the only one in the observation (id="+id+")");
50
51 m.source = rows[i]['fSourceName'];
52
53 var data = rows[i]['fData'];
54 if (data)
55 {
56 var obj = JSON.parse(("{"+data+"}").replace(/\ /g, "").replace(/(\w+):/gi, "\"$1\":"));
57 for (var key in obj)
58 m[key] = obj[key];
59 }
60
61 if (!schedule[entry])
62 schedule[entry] = { };
63
64 schedule[entry].id = id;
65 schedule[entry].date = new Date(rows[i]['fStart']+" UTC");
66
67 if (!schedule[entry].measurements)
68 schedule[entry].measurements = [];
69
70 schedule[entry].measurements[sub] = m;
71 }
72
73 for (var i=0; i<schedule.length; i++)
74 schedule[i] = new Observation(schedule[i]);
75
76 return schedule;
77}
78
79// -------------------------------------------------------------------------------------
80
81/*
82 // remove "
83 conv = conv.replace(/\"(\w+)\":/ig, "$1:");
84 // must contain one , less than :
85 // must not contain " and '
86 //var test = "{ra:12,dec:13}".replace(/\ /g, "").replace(/(\w+):/gi, "\"$1\":");
87*/
Note: See TracBrowser for help on using the repository browser.