source: branches/fscctrl_safety_limits/scripts/getSchedule.js@ 18605

Last change on this file since 18605 was 18173, checked in by dneise, 10 years ago
Also this file has been changed by Daniela Dorner in February, while introducing the new CUSTOM run feature. It has been tested now since quite a while, so I take the liberty to check it in.
File size: 2.9 KB
Line 
1// ======================================================================================
2
3function getSchedule()
4{
5 // List of all available measurement types (see also Observation_class.js)
6 var measurementType = [ "STARTUP", "IDLE", "DRSCALIB", "SINGLEPE", "DATA", "RATESCAN", "SHUTDOWN", "OVTEST", "RATESCAN2", "SLEEP", "CUSTOM" ];
7
8 // Get current time
9 var start = new Date();//new Date("2013-04-07 19:00:00 UTC");
10
11 // Because Main.js could start a new observations just in the moment between 'now'
12 // and entering the new data in the database, we have to use the unique id
13 // in Main.js to check if the current observation should be changed (and sub resetted)
14 start = new Date(start.getTime()-10*3600000);
15
16 // ----------------------------------------------------------------------
17
18 // Connect to database
19 var db = new Database($['schedule-database']);
20
21 // get all sources from database
22 var sources = db.query("SELECT * from Source");
23
24 // Get the current schedule
25 var rows = db.query("SELECT * FROM Schedule WHERE fStart>'"+start.toISOString()+"' ORDER BY fStart, fMeasurementID");
26
27 // Close db connection
28 db.close();
29
30 // ----------------------------------------------------------------------
31
32 var schedule = [];
33 var entry = -1;
34 var sub = 0;
35
36 for (var i=0; i<rows.length; i++)
37 {
38 var sub = rows[i]['fMeasurementID'];
39 if (sub==0)
40 entry++;
41
42 var m = { }
43
44 var task = rows[i]['fMeasurementTypeKey'];
45 m.task = measurementType[task];
46
47 var src = rows[i]['fSourceKey'];
48 if (src)
49 {
50 // Convert SourceKey to SourceName
51 var arr = sources.filter(function(e) { return e['fSourceKEY']==src; });
52 if (arr.length==0)
53 throw new Error("SourceKey "+src+" unknown.");
54
55 m.source = arr[0]['fSourceName'];
56 }
57
58 var data = rows[i]['fData'];
59 if (data)
60 {
61 var obj = JSON.parse(("{"+data+"}").replace(/\ /g, "").replace(/(\w+):/gi, "\"$1\":"));
62 for (var key in obj)
63 m[key] = obj[key];
64 }
65
66 if (!schedule[entry])
67 schedule[entry] = { };
68
69 schedule[entry].id = rows[i]['fScheduleID'];
70 schedule[entry].date = new Date(rows[i]['fStart']+" UTC");
71
72 if (!schedule[entry].measurements)
73 schedule[entry].measurements = [];
74
75 schedule[entry].measurements[sub] = m;
76 }
77
78 for (var i=0; i<schedule.length; i++)
79 schedule[i] = new Observation(schedule[i]);
80
81 return schedule;
82}
83
84// -------------------------------------------------------------------------------------
85
86/*
87 // remove "
88 conv = conv.replace(/\"(\w+)\":/ig, "$1:");
89 // must contain one , less than :
90 // must not contain " and '
91 //var test = "{ra:12,dec:13}".replace(/\ /g, "").replace(/(\w+):/gi, "\"$1\":");
92*/
Note: See TracBrowser for help on using the repository browser.