- Timestamp:
- 06/23/15 15:52:47 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/FACT++_scripts_refactoring/Handler_Function_class.js
r18242 r18264 1 1 'use strict'; 2 3 function Handler(name) 4 { 5 this.name = name; 6 this.handler_function_list = []; 7 } 2 if (!("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 } 41 else{ 42 console.out("multiple include of 'Handler_Function_class.js'"); 43 } 44 45 8 46 /* 9 47 add a handler_function … … 21 59 for a certain state. 22 60 */ 23 Handler .prototype.add = function(func)61 Handler_Function_class.Handler.prototype.add = function(func) 24 62 { 25 63 this.handler_function_list.push(func); … … 31 69 32 70 */ 33 Handler .prototype.run = function(timeout)71 Handler_Function_class.Handler.prototype.run = function(timeout) 34 72 { 35 73 console.out(this.name+":start"); … … 64 102 // ------------------------------------------------------------------------------ 65 103 66 67 function 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 76 function Set(list){ 77 for (var i=0; i<list.length; i++){ 78 this[list[i]] = true; 79 } 80 } 81 82 function 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 97 Handler_Function.prototype.call = function(){ 104 Handler_Function_class.Handler_Function.prototype.call = function(){ 98 105 this.last_state = state; 99 106 var state = dim.state(this.server_name); … … 125 132 } 126 133 127 Handler_Function .prototype.set_wait_state_undefined = function(){134 Handler_Function_class.Handler_Function.prototype.set_wait_state_undefined = function(){ 128 135 console.log("set_wait_state_undefined: this:"); 129 136 print_object(this); … … 131 138 } 132 139 133 Handler_Function .prototype.wait = function(){140 Handler_Function_class.Handler_Function.prototype.wait = function(){ 134 141 console.log("wait: this:"+this); 135 142 // we do nothing here, … … 137 144 } 138 145 139 Handler_Function .prototype.we_are_done = function(){146 Handler_Function_class.Handler_Function.prototype.we_are_done = function(){ 140 147 this.done = true; 141 148 } 142 149 143 var send_command_expect_state = function(command, expectation) 144 { 150 //--------- bias_voltage_off 151 152 Handler_Function_class.bias_voltage_off = new Handler_Function_class.Handler_Function("BIAS_CONTROL"); 153 Handler_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 174 Handler_Function_class.power_camera_on = new Handler_Function("PWR_CONTROL"); 175 Handler_Function_class.set_camera_power_on = function (wait_for_state){ 145 176 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 153 var throw_string_exception = function(message){ 154 return function(){ 155 throw message; 156 } 157 } 158 159 var throw_error = function(){ 160 throw new Error(this.name+':' 161 +state.name+"["+state.index+"] in error state."); 162 } 163 164 include("scripts/handlePwrCameraOn.js"); 165 include("scripts/handleBiasVoltageOff.js"); 166 include("scripts/handleFtmIdle.js"); 167 include("scripts/handleFscConnected.js"); 168 include("scripts/handleFeedbackConnected.js"); 169 include("scripts/handleRatectrlConnected.js"); 170 include("scripts/handleLidClosed.js"); 171 include("scripts/handleFadConnected.js"); 172 include("scripts/handleDriveArmed.js"); 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 }; 182 Handler_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 195 Handler_Function_class.make_ftm_idle = new Handler_Function_class.Handler_Function("FTM_CONTROL"); 196 Handler_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 220 Handler_Function_class.connect_fsc = new Handler_Function_class.Handler_Function("FSC_CONTROL"); 221 Handler_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 229 var send_stop_expect_calibrated = Handler_Function_class.send_command_expect_state("FEEDBACK/STOP", "Calibrated"); 230 231 Handler_Function_class.connect_feedback = new Handler_Function_class.Handler_Function("FEEDBACK"); 232 Handler_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 246 var send_stop_wait_for_connected = send_command_expect_state("RATE_CONTROL/STOP", "Connected"); 247 248 Handler_Function_class.connect_ratecontrol = new Handler_Function_class.Handler_Function("RATE_CONTROL"); 249 Handler_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 259 var send_close_wait_for_closed = Handler_Function_class.send_command_expect_state( 260 "LID_CONTROL/CLOSE", "Closed"); 261 262 Handler_Function_class.close_lid = new Handler_Function_class.Handler_Function("LID_CONTROL"); 263 Handler_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 275 var send_reset_configure = Handler_Function_class.send_command_expect_state( 276 "FAD_CONTROL/RESET_CONFIGURE", "Connected"); 277 var send_start = Handler_Function_class.send_command_expect_state( 278 "FAD_CONTROL/START", "Connected"); 279 var send_close_files = Handler_Function_class.send_command_expect_state( 280 "FAD_CONTROL/CLOSE_OPEN_FILES", "Connected"); 281 282 var 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 300 Handler_Function_class.connect_fad = new Handler_Function_class.Handler_Function("FAD_CONTROL"); 301 Handler_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 317 var send_stop = Handler_Function_class.send_command_expect_state("DRIVE_CONTROL", "Armed"); 318 319 Handler_Function_class.arm_drive = new Handler_Function_class.Handler_Function("DRIVE_CONTROL") 320 Handler_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 TracChangeset
for help on using the changeset viewer.