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

Last change on this file since 18242 was 18242, checked in by dneise, 10 years ago
added Handler_Function class
File size: 4.6 KB
Line 
1'use strict';
2
3function Handler(name)
4{
5 this.name = name;
6 this.handler_function_list = [];
7}
8/*
9 add a handler_function
10 to the internal handler_function_list.
11
12 A handler_function tries every time it is invoked,
13 to get a certain subsystem into a certain *fixed* state.
14 When it succeeded, it return an empty string "".
15 if it not (yet) acomplished its goal it will return:
16 * The name of the state it waits for.
17 * undefined, when it does not know, what state to wait for.
18
19 It is important to feed it back, the wait_state_name it returned
20 last time, so it knows, it should not do any action, while waiting
21 for a certain state.
22*/
23Handler.prototype.add = function(func)
24{
25 this.handler_function_list.push(func);
26}
27
28/* run each handler_function in the handler_function_list
29 until either all of them return the empty string ""
30 or the timeout occured.
31
32*/
33Handler.prototype.run = function(timeout)
34{
35 console.out(this.name+":start");
36
37 var rc = [];
38
39 var start_date = new Date();
40 while (!timeout || (new Date()-start_date)<timeout)
41 {
42 var done = true;
43 for (var i=0; i<this.handler_function_list.length; i++)
44 {
45 var current_handler_function = this.handler_function_list[i];
46 if (!current_handler_function.call())
47 done = false;
48 }
49
50 if (done)
51 {
52 console.out(this.name+":success [time="+(new Date()-start_date)+"ms]");
53 return true;
54 }
55
56 v8.sleep();
57 }
58
59 console.out(this.name+":timeout ["+timeout+"ms]");
60 return false;
61}
62
63
64// ------------------------------------------------------------------------------
65
66
67function print_object(obj){
68 for (var x in obj){
69 if (typeof obj[x] != 'function')
70 console.log(x+':'+obj[x]);
71 else
72 console.log(x+': function');
73 }
74}
75
76function Set(list){
77 for (var i=0; i<list.length; i++){
78 this[list[i]] = true;
79 }
80}
81
82function Handler_Function(server_name){
83 this.name = server_name;
84 this.definition = {
85 final_states: new Set([]),
86 just_wait_states: new Set([]),
87 reset_wait_states: new Set([]),
88 action_states: {},
89 };
90 this.wait_state = undefined;
91 this.done = false;
92
93 this.current_state = undefined;
94 this.last_state = undefined;
95}
96
97Handler_Function.prototype.call = function(){
98 this.last_state = state;
99 var state = dim.state(this.server_name);
100 this.current_state = state;
101
102 if (state === undefined){
103 // if the current state is undefined, we cannot be done!
104 this.wait_state=undefined;
105 this.done = false;
106 return false;
107 }
108 else if(this.done){
109 return true;
110 }
111 else if (this.wait_state && state.name!=this.wait_state){
112 // if we *have* a state to actually wait for, we just wait
113 return false;
114 }else if(state.name in this.definition.final_states){
115 this.we_are_done();
116 }else if(state.name in this.definition.just_wait_states){
117 this.wait();
118 }else if(state.name in this.definition.reset_wait_states){
119 this.set_wait_state_undefined();
120 }else if(state.name in this.definition.action_states){
121 this.definition.action_states[state.name]();
122 }
123 else
124 throw new Error(this.name+":"+state.name+"["+state.index+"] unknown or not handled.");
125}
126
127Handler_Function.prototype.set_wait_state_undefined = function(){
128 console.log("set_wait_state_undefined: this:");
129 print_object(this);
130 this.wait_state = undefined;
131}
132
133Handler_Function.prototype.wait = function(){
134 console.log("wait: this:"+this);
135 // we do nothing here,
136 // esp. we do not alter the wait_state
137}
138
139Handler_Function.prototype.we_are_done = function(){
140 this.done = true;
141}
142
143var send_command_expect_state = function(command, expectation)
144{
145 return function(){
146 console.out(this.name+'in '+this.current_state.name
147 +"..."+command+".");
148 dim.send(command);
149 this.wait_state = expectation;
150 }
151}
152
153var throw_string_exception = function(message){
154 return function(){
155 throw message;
156 }
157}
158
159var throw_error = function(){
160 throw new Error(this.name+':'
161 +state.name+"["+state.index+"] in error state.");
162}
163
164include("scripts/handlePwrCameraOn.js");
165include("scripts/handleBiasVoltageOff.js");
166include("scripts/handleFtmIdle.js");
167include("scripts/handleFscConnected.js");
168include("scripts/handleFeedbackConnected.js");
169include("scripts/handleRatectrlConnected.js");
170include("scripts/handleLidClosed.js");
171include("scripts/handleFadConnected.js");
172include("scripts/handleDriveArmed.js");
Note: See TracBrowser for help on using the repository browser.