Index: /branches/FACT++_scripts_refactoring/Handler_Function_class.js
===================================================================
--- /branches/FACT++_scripts_refactoring/Handler_Function_class.js	(revision 18242)
+++ /branches/FACT++_scripts_refactoring/Handler_Function_class.js	(revision 18242)
@@ -0,0 +1,172 @@
+'use strict';
+
+function Handler(name)
+{
+    this.name  = name;
+    this.handler_function_list = [];
+}
+/* 
+    add a handler_function 
+    to the internal handler_function_list.
+
+    A handler_function tries every time it is invoked,
+    to get a certain subsystem into a certain *fixed* state.
+    When it succeeded, it return an empty string "".
+    if it not (yet) acomplished its goal it will return:
+        * The name of the state it waits for.
+        * undefined, when it does not know, what state to wait for.
+
+    It is important to feed it back, the wait_state_name it returned
+    last time, so it knows, it should not do any action, while waiting
+    for a certain state.
+*/
+Handler.prototype.add = function(func)
+{
+    this.handler_function_list.push(func);
+}
+
+/* run each handler_function in the handler_function_list
+   until either all of them return the empty string ""
+   or the timeout occured.
+
+*/
+Handler.prototype.run = function(timeout)
+{
+    console.out(this.name+":start");
+
+    var rc = [];
+
+    var start_date = new Date();
+    while (!timeout || (new Date()-start_date)<timeout)
+    {
+        var done = true;
+        for (var i=0; i<this.handler_function_list.length; i++)
+        {
+            var current_handler_function = this.handler_function_list[i];
+            if (!current_handler_function.call())
+                done = false;
+        }
+
+        if (done)
+        {
+            console.out(this.name+":success [time="+(new Date()-start_date)+"ms]");
+            return true;
+        }
+
+        v8.sleep();
+    }
+
+    console.out(this.name+":timeout ["+timeout+"ms]");
+    return false;
+}
+
+
+// ------------------------------------------------------------------------------
+
+
+function print_object(obj){
+    for (var x in obj){
+        if (typeof obj[x] != 'function')
+            console.log(x+':'+obj[x]);
+        else
+            console.log(x+': function');
+    }
+}
+
+function Set(list){
+    for (var i=0; i<list.length; i++){
+        this[list[i]] = true;
+    }
+}
+
+function Handler_Function(server_name){
+    this.name = server_name;
+    this.definition = {
+        final_states: new Set([]),
+        just_wait_states: new Set([]),
+        reset_wait_states: new Set([]),
+        action_states: {}, 
+    };
+    this.wait_state = undefined;
+    this.done = false;
+
+    this.current_state = undefined;
+    this.last_state = undefined;
+}
+
+Handler_Function.prototype.call = function(){
+    this.last_state = state;
+    var state = dim.state(this.server_name);
+    this.current_state = state;
+
+    if (state === undefined){
+        // if the current state is undefined, we cannot be done!
+        this.wait_state=undefined;
+        this.done = false;
+        return false;
+    }
+    else if(this.done){
+        return true;
+    }
+    else if (this.wait_state && state.name!=this.wait_state){
+        // if we *have* a state to actually wait for, we just wait
+        return false;
+    }else if(state.name in this.definition.final_states){
+        this.we_are_done();
+    }else if(state.name in this.definition.just_wait_states){
+        this.wait();
+    }else if(state.name in this.definition.reset_wait_states){
+        this.set_wait_state_undefined();
+    }else if(state.name in this.definition.action_states){
+        this.definition.action_states[state.name]();
+    }
+    else
+        throw new Error(this.name+":"+state.name+"["+state.index+"] unknown or not handled.");
+}
+
+Handler_Function.prototype.set_wait_state_undefined = function(){
+    console.log("set_wait_state_undefined: this:");
+    print_object(this);
+    this.wait_state = undefined;
+}
+
+Handler_Function.prototype.wait = function(){
+    console.log("wait: this:"+this);
+    // we do nothing here, 
+    // esp. we do not alter the wait_state
+}
+
+Handler_Function.prototype.we_are_done = function(){
+    this.done = true;
+}
+
+var send_command_expect_state = function(command, expectation)
+{
+    return function(){
+        console.out(this.name+'in '+this.current_state.name
+            +"..."+command+".");    
+        dim.send(command);
+        this.wait_state = expectation;
+    }
+}
+
+var throw_string_exception = function(message){
+    return function(){
+        throw message;
+    }
+}
+
+var throw_error = function(){
+    throw new Error(this.name+':'
+        +state.name+"["+state.index+"] in error state.");
+}
+
+include("scripts/handlePwrCameraOn.js");
+include("scripts/handleBiasVoltageOff.js");
+include("scripts/handleFtmIdle.js");
+include("scripts/handleFscConnected.js");
+include("scripts/handleFeedbackConnected.js");
+include("scripts/handleRatectrlConnected.js");
+include("scripts/handleLidClosed.js");
+include("scripts/handleFadConnected.js");
+include("scripts/handleDriveArmed.js");
