source: branches/FACT++_scripts_refactoring/Handler_Function_class.js@ 19425

Last change on this file since 19425 was 18264, checked in by dneise, 9 years ago
some more steps towards different handler functions
File size: 12.7 KB
Line 
1'use strict';
2if (!("Handler_Function_class" in this)){
3 var Handler_Function_class = {
4 Handler : function(name){
5 this.name = name;
6 this.handler_function_list = [];
7 },
8 Handler_Function: function(server_name){
9 this.name = server_name;
10 this.definition = {
11 final_states: new Set([]),
12 just_wait_states: new Set([]),
13 reset_wait_states: new Set([]),
14 action_states: {},
15 };
16 this.wait_state = undefined;
17 this.done = false;
18
19 this.current_state = undefined;
20 this.last_state = undefined;
21 },
22 send_command_expect_state: function(command, expectation){
23 return function(){
24 console.out(this.name+'in '+this.current_state.name
25 +"..."+command+".");
26 dim.send(command);
27 this.wait_state = expectation;
28 }
29 },
30 throw_string_exception: function(message){
31 return function(){
32 throw message;
33 }
34 },
35 throw_error: function(){
36 throw new Error(this.name+':'
37 +state.name+"["+state.index+"] in error state.");
38 },
39 };
40}
41else{
42 console.out("multiple include of 'Handler_Function_class.js'");
43}
44
45
46/*
47 add a handler_function
48 to the internal handler_function_list.
49
50 A handler_function tries every time it is invoked,
51 to get a certain subsystem into a certain *fixed* state.
52 When it succeeded, it return an empty string "".
53 if it not (yet) acomplished its goal it will return:
54 * The name of the state it waits for.
55 * undefined, when it does not know, what state to wait for.
56
57 It is important to feed it back, the wait_state_name it returned
58 last time, so it knows, it should not do any action, while waiting
59 for a certain state.
60*/
61Handler_Function_class.Handler.prototype.add = function(func)
62{
63 this.handler_function_list.push(func);
64}
65
66/* run each handler_function in the handler_function_list
67 until either all of them return the empty string ""
68 or the timeout occured.
69
70*/
71Handler_Function_class.Handler.prototype.run = function(timeout)
72{
73 console.out(this.name+":start");
74
75 var rc = [];
76
77 var start_date = new Date();
78 while (!timeout || (new Date()-start_date)<timeout)
79 {
80 var done = true;
81 for (var i=0; i<this.handler_function_list.length; i++)
82 {
83 var current_handler_function = this.handler_function_list[i];
84 if (!current_handler_function.call())
85 done = false;
86 }
87
88 if (done)
89 {
90 console.out(this.name+":success [time="+(new Date()-start_date)+"ms]");
91 return true;
92 }
93
94 v8.sleep();
95 }
96
97 console.out(this.name+":timeout ["+timeout+"ms]");
98 return false;
99}
100
101
102// ------------------------------------------------------------------------------
103
104Handler_Function_class.Handler_Function.prototype.call = function(){
105 this.last_state = state;
106 var state = dim.state(this.server_name);
107 this.current_state = state;
108
109 if (state === undefined){
110 // if the current state is undefined, we cannot be done!
111 this.wait_state=undefined;
112 this.done = false;
113 return false;
114 }
115 else if(this.done){
116 return true;
117 }
118 else if (this.wait_state && state.name!=this.wait_state){
119 // if we *have* a state to actually wait for, we just wait
120 return false;
121 }else if(state.name in this.definition.final_states){
122 this.we_are_done();
123 }else if(state.name in this.definition.just_wait_states){
124 this.wait();
125 }else if(state.name in this.definition.reset_wait_states){
126 this.set_wait_state_undefined();
127 }else if(state.name in this.definition.action_states){
128 this.definition.action_states[state.name]();
129 }
130 else
131 throw new Error(this.name+":"+state.name+"["+state.index+"] unknown or not handled.");
132}
133
134Handler_Function_class.Handler_Function.prototype.set_wait_state_undefined = function(){
135 console.log("set_wait_state_undefined: this:");
136 print_object(this);
137 this.wait_state = undefined;
138}
139
140Handler_Function_class.Handler_Function.prototype.wait = function(){
141 console.log("wait: this:"+this);
142 // we do nothing here,
143 // esp. we do not alter the wait_state
144}
145
146Handler_Function_class.Handler_Function.prototype.we_are_done = function(){
147 this.done = true;
148}
149
150//--------- bias_voltage_off
151
152Handler_Function_class.bias_voltage_off = new Handler_Function_class.Handler_Function("BIAS_CONTROL");
153Handler_Function_class.bias_voltage_off.definition = {
154 final_states: new Set(["VoltageOff"]),
155 just_wait_states: new Set(["Connecting", "Initializing", "Connected", "Ramping"]),
156 reset_wait_states: new Set([]),
157 action_states: {
158 "Locked": function(){
159 console.out("WARNING - Bias is LOCKED. Please report, "
160 +"this is serious, unlock manually and go on.");
161 this.done=true;
162 },
163 "Disconnected": Handler_Function_class.send_command_expect_state(
164 "BIAS_CONTROL/RECONNECT", "VoltageOff"),
165 "NotReferenced": Handler_Function_class.send_command_expect_state(
166 "BIAS_CONTROL/SET_ZERO_VOLTAGE", "VoltageOff"),
167 "VoltageOn": Handler_Function_class.send_command_expect_state(
168 "BIAS_CONTROL/SET_ZERO_VOLTAGE", "VoltageOff"),
169 "OverCurrent" : Handler_Function_class.throw_string_exception("BIAS_CONTROL in OverCurrent"),
170 "ExpertMode": Handler_Function_class.throw_string_exception("BIAS_CONTROL in expert mode"),
171 },
172};
173//--------- power_camera_on
174Handler_Function_class.power_camera_on = new Handler_Function("PWR_CONTROL");
175Handler_Function_class.set_camera_power_on = function (wait_for_state){
176 return function(){
177 this.wait_state=wait_for_state;
178 console.out(this.name+" in "+this.current_state.name
179 +"... sending CAMERA_POWER ON... waiting for "+this.wait_state+".");
180 };
181};
182Handler_Function_class.power_camera_on.definition ={
183 final_states: new Set(["DriveOff", "SystemOn"]),
184 just_wait_states: new Set(["CameraOn", "BiasOn", "CameraOff", "BiasOff"]),
185 reset_wait_states: new Set(["Disconnected", "Connected", "NoConnection"]),
186 action_states: {
187 "PowerOff" : Handler_Function_class.set_camera_power_on("DriveOff"),
188 "DriveOn": Handler_Function_class.set_camera_power_on("SystemOn"),
189 "CoolingFailure": function (){
190 throw new Error("Cooling unit reports failure... please check.");
191 },
192 },
193};
194//--------- make_ftm_idle
195Handler_Function_class.make_ftm_idle = new Handler_Function_class.Handler_Function("FTM_CONTROL");
196Handler_Function_class.make_ftm_idle.definitions = {
197 final_states = new Set(["Valid"]),
198 action_states = {
199 "Disconnected": Handler_Function_class.send_command_expect_state(
200 "FTM_CONTROL/RECONNECT", "Valid"),
201 "Idle": Handler_Function_class.send_command_expect_state(
202 "FTM_CONTROL/DISCONNECT", "Disconnected"),
203 "TriggerOn": Handler_Function_class.send_command_expect_state(
204 "FTM_CONTROL/STOP_TRIGGER", "Valid"),
205 "Configuring1": Handler_Function_class.send_command_expect_state(
206 "FTM_CONTROL/RESET_CONFIGURE", "Valid"),
207 "Configuring2": Handler_Function_class.send_command_expect_state(
208 "FTM_CONTROL/RESET_CONFIGURE", "Valid"),
209 "Configured1": Handler_Function_class.send_command_expect_state(
210 "FTM_CONTROL/RESET_CONFIGURE", "Valid"),
211 "Configured2": function(){
212 this.wait_state = "TriggerOn";
213 },
214 "ConfigError1": Handler_Function_class.throw_error,
215 "ConfigError2": Handler_Function_class.throw_error,
216 "ConfigError3": Handler_Function_class.throw_error,
217 }
218};
219//--------- connect_fsc
220Handler_Function_class.connect_fsc = new Handler_Function_class.Handler_Function("FSC_CONTROL");
221Handler_Function_class.connect_fsc.definition = {
222 final_states : new Set(["Connected"]),
223 action_states : {
224 "Disconnected" : Handler_Function_class.send_command_expect_state(
225 "FSC_CONTROL/RECONNECT", "Connected"),
226 },
227};
228//--------- connect_feedback
229var send_stop_expect_calibrated = Handler_Function_class.send_command_expect_state("FEEDBACK/STOP", "Calibrated");
230
231Handler_Function_class.connect_feedback = new Handler_Function_class.Handler_Function("FEEDBACK");
232Handler_Function_class.connect_feedback.definition = {
233 final_states: new Set(["Connected", "Calibrated"]),
234 reset_wait_states: new Set(["Disconnected", "Connecting"]),
235 action_states: {
236 "Calibrating" : Handler_Function_class.send_command_expect_state(
237 "FEEDBACK/STOP", "Connected"),
238 "WaitingForData": send_stop_expect_calibrated,
239 "OnStandby": send_stop_expect_calibrated,
240 "InProgress": send_stop_expect_calibrated,
241 "Warning": send_stop_expect_calibrated,
242 "Critical": send_stop_expect_calibrated,
243 },
244};
245//--------- connect_ratecontrol
246var send_stop_wait_for_connected = send_command_expect_state("RATE_CONTROL/STOP", "Connected");
247
248Handler_Function_class.connect_ratecontrol = new Handler_Function_class.Handler_Function("RATE_CONTROL");
249Handler_Function_class.connect_ratecontrol.definition = {
250 final_states: new Set(["Connected"]),
251 reset_wait_states: new Set(["DimNetworkNotAvailable", "Disconnected"]),
252 action_states: {
253 "Calibrating": send_stop_wait_for_connected,
254 "GlobalThresholdSet": send_stop_wait_for_connected,
255 "InProgress": send_stop_wait_for_connected,
256 }
257};
258//--------- close_lid
259var send_close_wait_for_closed = Handler_Function_class.send_command_expect_state(
260 "LID_CONTROL/CLOSE", "Closed");
261
262Handler_Function_class.close_lid = new Handler_Function_class.Handler_Function("LID_CONTROL");
263Handler_Function_class.close_lid.definition = {
264 final_states: new Set(["Closed"]),
265 just_wait_states: new Set(["NotReady", "Ready",
266 "NoConnection", "Connected", "Moving"]),
267 action_states: {
268 "Unknown": send_close_wait_for_closed,
269 "Inconsistent": send_close_wait_for_closed,
270 "PowerProblem": send_close_wait_for_closed,
271 "Open": send_close_wait_for_closed,
272 }
273};
274//--------- connect_fad
275var send_reset_configure = Handler_Function_class.send_command_expect_state(
276 "FAD_CONTROL/RESET_CONFIGURE", "Connected");
277var send_start = Handler_Function_class.send_command_expect_state(
278 "FAD_CONTROL/START", "Connected");
279var send_close_files = Handler_Function_class.send_command_expect_state(
280 "FAD_CONTROL/CLOSE_OPEN_FILES", "Connected");
281
282var check_final_state = function(){
283 var sub_con = new Subscription("FAD_CONTROL/CONNECTIONS");
284 var con = sub_con.get(5000);
285 var all = true;
286 for (var i=0; i<40; i++)
287 if (con.obj['status'][i]&66!=66)
288 {
289 console.out("Board "+i+" not connected... sending CONNECT... waiting for 'Connected'.");
290 dim.send("FAD_CONTROL/CONNECT", i);
291 all = false;
292 }
293 sub_con.close();
294 if (all)
295 this.done = true;
296 else
297 this.wait_state = "Connected";
298}
299
300Handler_Function_class.connect_fad = new Handler_Function_class.Handler_Function("FAD_CONTROL");
301Handler_Function_class.connect_fad.definitions = {
302 reset_wait_states: new Set(["Offline"]),
303 just_wait_states: new Set(["Connecting"]),
304 action_states: {
305 "Disconnected": Handler_Function_class.send_command_expect_state(
306 "FAD_CONTROL/START", "Connected"),
307 "Configuring1": send_reset_configure,
308 "Configuring2": send_reset_configure,
309 "Configuring3": send_reset_configure,
310 "Configured": send_reset_configure,
311 "Disengaged": send_start,
312 "RunInProgress": send_close_files,
313 "Connected": check_final_state,
314 },
315};
316//--------- arm_drive
317var send_stop = Handler_Function_class.send_command_expect_state("DRIVE_CONTROL", "Armed");
318
319Handler_Function_class.arm_drive = new Handler_Function_class.Handler_Function("DRIVE_CONTROL")
320Handler_Function_class.arm_drive.definitions = {
321 final_states: new Set(["Armed"]),
322 reset_wait_states: new Set(["Disconnected", "Connected", "Ready"]),
323 action_states: {
324 "Moving" : send_stop,
325 "Tracking": send_stop,
326 "OnTrack": send_stop,
327 "NotReady": function(){
328 send_stop(),
329 dim.wait("DRIVE_CONTROL", "Armed", 60000);
330 this.wait_state = undefined;
331 },
332 "ERROR": function(){
333 send_stop(),
334 dim.wait("DRIVE_CONTROL", "Armed", 60000);
335 this.wait_state = undefined;
336 },
337 "Locked": function(){
338 console.out("WARNING - Drive is LOCKED. Please unlock manually.");
339 this.done = true;
340 },
341 },
342};
Note: See TracBrowser for help on using the repository browser.