1 'use strict';
  2 
  3 // ==========================================================================
  4 // Reset Crate
  5 // ==========================================================================
  6 
  7 // call it with: .js doCrateReset.js crate0=true crate3=true
  8 //           or: DIM_CONTROL/START doCrateReset.js crate0=true crate3=true
  9 
 10 // -------------------------------------------------------------------------
 11 
 12 include('scripts/CheckStates.js');
 13 
 14 function crateReset(crate)
 15 {
 16     var msg = "Starting crate reset:";
 17 
 18     var cnt = 0;
 19     for (var i=0; i<4; i++)
 20         if (crate[i])
 21         {
 22             cnt++;
 23             msg += " "+i;
 24         }
 25 
 26     if (cnt==0)
 27     {
 28         console.out("No crate to reset.");
 29         return;
 30     }
 31 
 32     dim.log(msg);
 33 
 34     console.out("Checking availability of servers...");
 35 
 36     var table =
 37         [
 38          [ "MCP" ],
 39          [ "FTM_CONTROL" ],
 40          [ "FAD_CONTROL" ],
 41         ];
 42     if (!checkStates(table, 3000))
 43         throw new Error("Either MCP, FTM_CONTROL or FAD_CONTROL not online.");
 44 
 45     // No data taking should be in progress
 46     // Trigger must be switched off
 47     checkSend(["MCP", "FAD_CONTROL", "FTM_CONTROL" ]);
 48 
 49 
 50     var mcp = dim.state("MCP");
 51     if (mcp.name=="TriggerOn" || mcp.state=="TakingData")
 52         dim.send("MCP/STOP");
 53     if (mcp.name.substr(0,7)=="kConfig")
 54         dim.send("MCP/RESET");
 55     if (dim.state("FTM_CONTROL").name=="TriggerOn")
 56         dim.send("FTM_CONTROL/STOP_TRIGGER");
 57     if (dim.state("FTM_CONTROL").name.indexOf("Config")==0)
 58         dim.send("FTM_CONTROL/RESET_CONFIGURE");
 59 
 60     console.out("Checking status of servers...");
 61 
 62     var table =
 63         [
 64          [ "MCP", [ "Idle", "Connected" ]],
 65          [ "FTM_CONTROL", [ "Valid" ] ],
 66          [ "FAD_CONTROL", [ "Disengaged", "Disconnected", "Connecting", "Connected" ] ],
 67         ];
 68     if (!checkStates(table, 3000, true))
 69         throw new Error("Either MCP, FTM_CONTROL or FAD_CONTROL not in a state in which it ought to be.");
 70 
 71     // FTUs must be switched off
 72 
 73     console.out("Disable FTUs...");
 74 
 75     //checkSend(["FTM_CONTROL"]);
 76     dim.send("FTM_CONTROL/ENABLE_FTU", -1, false);
 77     v8.sleep(1000);
 78 
 79     // Boards in the crates must be disconnected
 80 
 81     //checkSend(["FAD_CONTROL"]);
 82 
 83     dim.log("Disconnecting crates.");
 84 
 85     if (dim.state("FAD_CONTROL").name=="Connecting" || dim.state("FAD_CONTROL").name=="Connected")
 86         for (var i=0; i<10; i++)
 87         {
 88             for (var j=0; j<4; j++)
 89                 if (crate[j])
 90                 {
 91                     console.out("Sending DISCONNECT "+(j*10+i));
 92                     dim.send("FAD_CONTROL/DISCONNECT", j*10+i);
 93                 }
 94         }
 95 
 96     v8.sleep(2000);
 97     if (!checkStates([[ "FAD_CONTROL", [ "Disengaged", "Disconnected", "Connected" ] ]]))
 98         throw new Error("FAD_CONTROL neither Disengaged, Disconnected not Connected.");
 99 
100 
101     // Reset crates
102 
103     dim.log("Sending reset.");
104 
105     if (cnt==4)
106         dim.send("FTM_CONTROL/RESET_CAMERA");
107     else
108     {
109         for (var i=0; i<4; i++)
110             if (crate[i])
111             {
112                 console.out("Sending RESET_CRATE "+i);
113                 dim.send("FTM_CONTROL/RESET_CRATE", i);
114             }
115     }
116 
117     // We have to wait a bit
118 
119     v8.sleep(3200);
120 
121     // Reconnect all boards
122 
123     dim.log("Waiting for connection.");
124 
125     if (dim.state("FAD_CONTROL").name=="Disengaged")
126     {
127         dim.send("Waiting 38s for crates to finish reset.");
128         v8.sleep(38000);
129         dim.send("FAD_CONTROL", "START");
130     }
131     else
132         for (var i=0; i<10; i++)
133         {
134             v8.sleep(3200);
135             for (var j=0; j<4; j++)
136                 if (crate[j])
137                 {
138                     console.out("Sending CONNECT "+(j*10+i));
139                     dim.send("FAD_CONTROL/CONNECT", j*10+i);
140                 }
141         }
142 
143 
144     // Reconnect all FTUs
145 
146     console.out("Enable FTUs...");
147 
148     v8.sleep(1000);
149     dim.send("FTM_CONTROL/ENABLE_FTU", -1, true);
150     v8.sleep(3000);
151     dim.send("FTM_CONTROL/PING");
152     v8.sleep(1000);
153 
154     dim.wait("FAD_CONTROL", "Connected", 3000);
155 
156     // Done
157 
158     dim.log("Crate reset finished.");
159 }
160