Index: trunk/FACT++/scripts/Main.js
===================================================================
--- trunk/FACT++/scripts/Main.js	(revision 18113)
+++ trunk/FACT++/scripts/Main.js	(revision 18114)
@@ -381,7 +381,10 @@
 // ================================================================
 
-function Shutdown(singlepe)
-{
-    dim.log("Starting shutdown.");
+function Shutdown(type)
+{
+    if (!type)
+        type = "default";
+
+    dim.log("Starting shutdown ["+type+"].");
 
     var now1 = new Date();
@@ -395,10 +398,14 @@
     var now2 = new Date();
 
-    dim.send("DRIVE_CONTROL/PARK");
+    if (type=="sleep")
+        dim.send("DRIVE_CONTROL/MOVE_TO", 101, 0);
+    else
+        dim.send("DRIVE_CONTROL/PARK");
 
     console.out("","Waiting for telescope to park. This may take a while.");
 
     // FIXME: This might not work is the drive is already close to park position
-    dim.wait("DRIVE_CONTROL", "Locked", 3000);
+    if (type!="sleep")
+        dim.wait("DRIVE_CONTROL", "Locked", 3000);
 
     var sub = new Subscription("DRIVE_CONTROL/POINTING_POSITION");
@@ -424,5 +431,4 @@
         dim.log('Park position not reached? Telescope at Zd='+p.obj['Zd']+' Az='+p.obj['Az']);
     }
-
     /*
     // Check if DRS calibration is necessary
@@ -435,5 +441,5 @@
     }*/
 
-    if (singlepe)
+    if (type=="singlepe")
     {
         dim.log("Taking single-pe run.");
@@ -455,17 +461,21 @@
     var now3 = new Date();
 
-    //dim.send("FEEDBACK/STOP");
     dim.send("FTM_CONTROL/STOP_TRIGGER");
-
     dim.wait("FTM_CONTROL",  "Valid",        3000);
-    dim.wait("FEEDBACK",     "Calibrated",   3000);
-
-    dim.send("BIAS_CONTROL/DISCONNECT");
-    dim.wait("BIAS_CONTROL", "Disconnected", 3000);
+
+    if (bias!="Disconnected")
+        dim.wait("FEEDBACK", "Calibrated",   3000);
+
+    if (type!="sleep")
+    {
+        dim.send("BIAS_CONTROL/DISCONNECT");
+        dim.wait("BIAS_CONTROL", "Disconnected", 3000);
+
+    }
 
     var report = sub.get();
 
     console.out("");
-    console.out("Shutdown procedure seems to be finished...");
+    console.out("Shutdown procedure ["+type+"] seems to be finished...");
     console.out("  "+new Date().toUTCString());
     console.out("  Telescope at Zd=%.1fdeg Az=%.1fdeg".$(report.obj['Zd'], report.obj['Az']));
@@ -484,4 +494,51 @@
     sub.close();
 }
+
+
+// ================================================================
+//  Function to set the system to sleep-mode
+// ================================================================
+// FIXME: do not repeat code from shutdown-function
+/*
+function GoToSleep()
+{
+    CloseLid();
+
+    var isArmed = dim.state("DRIVE_CONTROL").name=="Armed";
+    if (!isArmed)
+    {
+        dim.log("Drive not ready to move. -> send STOP");
+        dim.send("DRIVE_CONTROL/STOP");
+        dim.wait("DRIVE_CONTROL", "Armed", 5000);
+    }
+
+    dim.send("DRIVE_CONTROL/MOVE_TO 101 0");//park position
+    var sub = new Subscription("DRIVE_CONTROL/POINTING_POSITION");
+    sub.get(5000);  // FIXME: Proper error message in case of failure
+
+    function func()
+    {
+        var report = sub.get();
+
+        var zd = report.obj['Zd'];
+        var az = report.obj['Az'];
+
+        if (zd>100 && Math.abs(az)<1)
+            return true;
+
+        return undefined;
+    }
+
+    try { v8.timeout(150000, func); }
+    catch (e)
+    {
+        var p = sub.get();
+        dim.log('Park position not reached? Telescope at Zd='+p.obj['Zd']+' Az='+p.obj['Az']);
+    }
+    var p2 = sub.get();
+    dim.log('Telescope at Zd=%.1fdeg Az=%.1fdeg'.$(p2.obj['Zd'], p2.obj['Az']));
+    sub.close();
+}
+*/
 
 // ================================================================
@@ -574,5 +631,4 @@
 
     var list = obj.data.split('\n').map(map);
-//     [A
     function check(name)
     {
@@ -930,4 +986,12 @@
         continue;
 
+    case "SLEEP":
+        Shutdown("sleep"); //GoToSleep();
+
+        sub++;
+        dim.log("Task finished [SLEEP].");
+        console.out("");
+        continue;
+
     case "STARTUP":
         CloseLid();
@@ -954,5 +1018,5 @@
 
     case "SHUTDOWN":
-        Shutdown(true);
+        Shutdown("singlepe");
         system_on = false;
 
@@ -1121,4 +1185,14 @@
         dim.wait("DRIVE_CONTROL", "Armed", 15000);
 
+        if (obs[sub].rstype=="dark-bias-off")
+            service_feedback.voltageOff();
+        else
+        {
+            // Switch the voltage to a reduced level (Ubd)
+            var bias = dim.state("BIAS_CONTROL").name;
+            if (bias=="VoltageOn" || bias=="Ramping")
+                service_feedback.voltageOn(0);
+        }
+
         // Open the lid if required
         if (!obs[sub].lidclosed)
@@ -1127,7 +1201,4 @@
             CloseLid();
 
-        // Switch the voltage to a reduced level (Ubd)
-        service_feedback.voltageOn(0);
-
         // track source/position or move to position
         if (obs[sub].lidclosed)
@@ -1135,5 +1206,5 @@
             dim.log("Moving telescope to zd="+obs[sub].zd+" az="+obs[sub].az);
             dim.send("DRIVE_CONTROL/MOVE_TO", obs[sub].zd, obs[sub].az);
-            v8.sleep(1000);
+            v8.sleep(3000);
             dim.wait("DRIVE_CONTROL", "Armed", 150000); // 110s for turning and 30s for stabilizing
         }
@@ -1156,12 +1227,9 @@
         // Now tracking stable, switch voltage to nominal level and wait
         // for stability.
-        if (obs[sub].rstype=="dark-bias-off")
-            service_feedback.voltageOff();
-        else
+        if (obs[sub].rstype!="dark-bias-off")
         {
             service_feedback.voltageOn();
             service_feedback.waitForVoltageOn();
         }
-        dim.log("rs-type: "+obs[sub].rstype);
 
         if (!irq)
@@ -1169,5 +1237,5 @@
             var tm2 = new Date();
 
-            dim.log("Starting ratescan 2/1");
+            dim.log("Starting ratescan 2/1 ["+obs[sub].rstype+"]");
 
             // Start rate scan
@@ -1178,5 +1246,7 @@
             // it to be configured.
             dim.wait("RATE_SCAN", "InProgress", 10000);
-            dim.wait("RATE_SCAN", "Connected", 2700000);
+            //FIXME: discuss what best value is here
+            dim.wait("RATE_SCAN", "Connected", 2700000);//45min
+            //dim.wait("RATE_SCAN", "Connected", 1200000);//3.3h
 
             // Here one could implement a watchdog for the feedback as well, but what is the difference
@@ -1196,5 +1266,5 @@
             var tm2 = new Date();
 
-            dim.log("Starting ratescan 2/2");
+            dim.log("Starting ratescan 2/2 ["+obs[sub].rstype+"]");
 
             // Start rate scan
