1 | <?php
|
---|
2 |
|
---|
3 | if (!extension_loaded('v8js'))
|
---|
4 | die("V8Js missing");
|
---|
5 |
|
---|
6 | if (isset($_POST['editor1']) || isset($_POST['editor2']))
|
---|
7 | {
|
---|
8 | $isOne = isset($_POST['editor1']);
|
---|
9 |
|
---|
10 | $source = $isOne ? $_POST['editor1'] : $_POST['editor2'];
|
---|
11 | if (!isset($_POST['files']))
|
---|
12 | $name = $isOne ? "proc.js" : "main.js";
|
---|
13 | else
|
---|
14 | $name = $_POST['files'][0];
|
---|
15 |
|
---|
16 | header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
|
---|
17 | header("Cache-Control: public"); // needed for i.e.
|
---|
18 | header("Content-Type: text/plain");
|
---|
19 | header("Content-Transfer-Encoding: Text");
|
---|
20 | header("Content-Disposition: attachment; filename=\"".$name."\"");
|
---|
21 | print($source);
|
---|
22 | return;
|
---|
23 | }
|
---|
24 |
|
---|
25 | if (!isset($_POST['file']) || !isset($_POST['event']) || !isset($_POST['pixel']))
|
---|
26 | {
|
---|
27 | /*
|
---|
28 | function getList($path)
|
---|
29 | {
|
---|
30 | $hasdir = false;
|
---|
31 |
|
---|
32 | $list = array();
|
---|
33 | foreach (new DirectoryIterator($path) as $file)
|
---|
34 | {
|
---|
35 | if ($file->isDot())
|
---|
36 | continue;
|
---|
37 |
|
---|
38 | $name = $file->getFilename();
|
---|
39 |
|
---|
40 |
|
---|
41 | if ($file->isDir())
|
---|
42 | {
|
---|
43 | $list[$name] = getList($path."/".$name);
|
---|
44 | $hasdir = true;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if ($file->isFile() && $file->isReadable())
|
---|
48 | {
|
---|
49 | if (substr($name, 12)!=".fits.fz")
|
---|
50 | continue;
|
---|
51 |
|
---|
52 | array_push($list, substr($name, 9, 3));
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | if (!$hasdir)
|
---|
57 | sort($list);
|
---|
58 | return $list;
|
---|
59 | }
|
---|
60 | */
|
---|
61 |
|
---|
62 | function getList($path, $sub)
|
---|
63 | {
|
---|
64 | $hasdir = false;
|
---|
65 |
|
---|
66 | $list = array();
|
---|
67 | foreach (new DirectoryIterator($path."/".$sub) as $file)
|
---|
68 | {
|
---|
69 | if ($file->isDot())
|
---|
70 | continue;
|
---|
71 |
|
---|
72 | $name = $file->getFilename();
|
---|
73 |
|
---|
74 | if ($file->isDir())
|
---|
75 | $list = array_merge($list, getList($path, $sub."/".$name));
|
---|
76 |
|
---|
77 | if ($file->isFile() && $file->isReadable())
|
---|
78 | {
|
---|
79 | if (substr($name, 12)!=".fits.fz")
|
---|
80 | continue;
|
---|
81 |
|
---|
82 | $rc = substr($name, 0, 4)."/".substr($name, 4, 2)."/".substr($name, 6, 2)."-".substr($name, 9, 3);
|
---|
83 |
|
---|
84 | array_push($list, $rc);
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 | return $list;
|
---|
89 | }
|
---|
90 |
|
---|
91 | try
|
---|
92 | {
|
---|
93 | $list = getList("/daq/raw", "");
|
---|
94 | sort($list);
|
---|
95 | }
|
---|
96 | catch (Exception $e)
|
---|
97 | {
|
---|
98 | return header('HTTP/1.0 400 '.$e->getMessage());
|
---|
99 | }
|
---|
100 |
|
---|
101 | print(json_encode($list));
|
---|
102 | return;
|
---|
103 | }
|
---|
104 |
|
---|
105 | $event = intval($_POST['event']);
|
---|
106 | $pixel = intval($_POST['pixel']);
|
---|
107 |
|
---|
108 | function get($handle, $format, $count = 1)
|
---|
109 | {
|
---|
110 | $size = 0;
|
---|
111 | switch ($format)
|
---|
112 | {
|
---|
113 | case 'L': $size = 4; break;
|
---|
114 | case 'S': $size = 2; break;
|
---|
115 | case 's': $size = 2; break;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if ($size==0)
|
---|
119 | return;
|
---|
120 |
|
---|
121 | $data = fread($handle, $size*$count);
|
---|
122 | $data = unpack($format.$count, $data);
|
---|
123 |
|
---|
124 | return $count==1 ? $data[1] : $data;
|
---|
125 | }
|
---|
126 |
|
---|
127 | //define('E_FATAL', E_ERROR | E_USER_ERROR | E_PARSE | E_CORE_ERROR |
|
---|
128 | // E_COMPILE_ERROR | E_RECOVERABLE_ERROR);
|
---|
129 |
|
---|
130 | $rc = array();
|
---|
131 | $rc['startPhp'] = microtime(true);
|
---|
132 |
|
---|
133 | // ============================ Read data from file ==========================
|
---|
134 | $file = $_POST['file'];
|
---|
135 |
|
---|
136 | $y = substr($file, 0, 4);
|
---|
137 | $m = substr($file, 5, 2);
|
---|
138 | $d = substr($file, 8, 2);
|
---|
139 | $r = substr($file, 11, 3);
|
---|
140 |
|
---|
141 | $filename = "/daq/raw/".$y."/".$m."/".$d."/".$y.$m.$d."_".$r.".fits.fz";
|
---|
142 |
|
---|
143 | $file = popen("/home/fact/FACT++/getevent ".$filename." ".$event." 2> /dev/null", "r");
|
---|
144 | if (!$file)
|
---|
145 | return header('HTTP/1.0 400 Could not open file.');
|
---|
146 |
|
---|
147 | $evt = array();
|
---|
148 |
|
---|
149 | $evt['numEvents'] = get($file, "L");
|
---|
150 | $evt['numRoi'] = get($file, "L");
|
---|
151 | $evt['numPix'] = get($file, "L");
|
---|
152 | $evt['eventNumber'] = get($file, "L");
|
---|
153 | $evt['triggerNumber'] = get($file, "L");
|
---|
154 | $evt['triggerType'] = get($file, "S");
|
---|
155 | $evt['unixTime'] = get($file, "L", 2);
|
---|
156 |
|
---|
157 | if (isset($_POST['source1']))
|
---|
158 | {
|
---|
159 | // Read the data and copy it from an associative array to an Array
|
---|
160 | // (this is just nicer and seems more logical)
|
---|
161 | $data = array();
|
---|
162 | for ($i=0; $i<$evt['numPix']; $i++)
|
---|
163 | {
|
---|
164 | //$data[$i] = get($file, "s", $evt['numRoi']);
|
---|
165 | $var = get($file, "s", $evt['numRoi']);
|
---|
166 |
|
---|
167 | $data[$i] = array();
|
---|
168 | for ($j=0; $j<$evt['numRoi']; $j++)
|
---|
169 | $data[$i][$j] = $var[$j+1]*0.48828125; // dac -> mV
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | //if (feof($file))
|
---|
174 | // return;
|
---|
175 |
|
---|
176 | pclose($file);
|
---|
177 |
|
---|
178 | if ($evt['numEvents']==0)
|
---|
179 | return header('HTTP/1.0 400 Could not read event.');
|
---|
180 |
|
---|
181 | // =============================== Copy waveform =============================
|
---|
182 |
|
---|
183 | $rc['event'] = $evt;
|
---|
184 | $rc['event']['index'] = $event;
|
---|
185 | $rc['event']['pixel'] = $pixel;
|
---|
186 | //$rc['waveform'] = $data[$pixel];
|
---|
187 |
|
---|
188 | // Get execution times
|
---|
189 | $now = microtime(true);
|
---|
190 |
|
---|
191 | if (isset($_POST['source1']))
|
---|
192 | {
|
---|
193 | // =============================== Run Javascript ============================
|
---|
194 |
|
---|
195 | /*
|
---|
196 | $JS = <<< EOT
|
---|
197 | require("path/to/module1");
|
---|
198 | EOT;
|
---|
199 |
|
---|
200 | $v8 = new V8Js();
|
---|
201 | $v8->setModuleLoader(function($module) {
|
---|
202 | switch ($module) {
|
---|
203 | case 'path/to/module1':
|
---|
204 | return 'print(' . json_encode($module . PHP_EOL) . ');require("./module2");';
|
---|
205 |
|
---|
206 | case 'path/to/module2':
|
---|
207 | return 'print(' . json_encode($module . PHP_EOL) . ');require("../../module3");';
|
---|
208 |
|
---|
209 | default:
|
---|
210 | return 'print(' . json_encode($module . PHP_EOL) . ');';
|
---|
211 | }
|
---|
212 | });
|
---|
213 | */
|
---|
214 |
|
---|
215 | // V8Js global methods: exit, print, sleep, var_dump, require
|
---|
216 | //$v8 = new V8Js("$", array("data"=>"data"), extensions, flags, millisecond, bytes);
|
---|
217 | $v8 = new V8Js("$"/*, array("data"=>"data")*/);
|
---|
218 |
|
---|
219 | //$v8 = new V8Js("$", array("data"=>"data"));
|
---|
220 |
|
---|
221 | //V8Js::registerExtension("exit", "1");
|
---|
222 | //$v8 = new V8Js("$", array(), array("exit"));
|
---|
223 |
|
---|
224 | //$v8->func = function($a) { echo "Closure with param $a\n"; };
|
---|
225 | //$v8->greeting = "From PHP with love!";
|
---|
226 |
|
---|
227 | // This is much faster than the variables option in the constructor
|
---|
228 | $v8->data = $data;
|
---|
229 | $v8->event = $evt;
|
---|
230 | $v8->clone = function($data) { return $data; };
|
---|
231 |
|
---|
232 | $rc['startJs'] = microtime(true);
|
---|
233 |
|
---|
234 | // Buffer output from javascript
|
---|
235 | ob_start();
|
---|
236 |
|
---|
237 | try
|
---|
238 | {
|
---|
239 | $JS = "function proc(pixel){\n".$_POST['source1']."\n};proc(".$pixel.");";
|
---|
240 | $rc['waveform'] = $v8->executeString($JS, 'proc');
|
---|
241 |
|
---|
242 | if (isset($_POST['source2']))
|
---|
243 | {
|
---|
244 | $JS = "(function main(){\n".$_POST['source2']."\n})();";
|
---|
245 | $rc['ret'] = $v8->executeString($JS, 'main');
|
---|
246 | }
|
---|
247 |
|
---|
248 | // This is supposed to work, but it seems it does not...
|
---|
249 | //$v8->proc = $rc['ret']->proc;
|
---|
250 | //$rc['ret'] = $v8->executeString('PHP.proc();', 'proc');
|
---|
251 | }
|
---|
252 | catch (V8JsException $e)
|
---|
253 | {
|
---|
254 | $rc['err'] = array();
|
---|
255 | $rc['err']['message'] = $e->getMessage();
|
---|
256 | $rc['err']['file'] = $e->getJsFileName();
|
---|
257 | $rc['err']['sourceLine'] = $e->getJsSourceLine();
|
---|
258 | $rc['err']['lineNumber'] = $e->getJsLineNumber()-1;
|
---|
259 | $rc['err']['trace'] = $e->getJsTrace();
|
---|
260 | }
|
---|
261 |
|
---|
262 | // Copy output buffer and clean it
|
---|
263 | $rc['debug'] = ob_get_contents();
|
---|
264 | ob_end_clean();
|
---|
265 |
|
---|
266 | // Get execution times
|
---|
267 | $now = microtime(true);
|
---|
268 |
|
---|
269 | $rc['timeJs'] = ($now - $rc['startJs']);
|
---|
270 | }
|
---|
271 |
|
---|
272 | $rc['timePhp'] = ($now - $rc['startPhp']);
|
---|
273 |
|
---|
274 | // Output result as JSON object
|
---|
275 | print(json_encode($rc));
|
---|
276 |
|
---|
277 | ?>
|
---|