1 | #include <iostream>
|
---|
2 | #include <iomanip>
|
---|
3 | #include <fstream>
|
---|
4 |
|
---|
5 | #include <TSystem.h>
|
---|
6 | #include <TApplication.h>
|
---|
7 | #include <TObjectTable.h>
|
---|
8 | #include <TSocket.h>
|
---|
9 |
|
---|
10 | #include "ethernet.h"
|
---|
11 | #include "vmodican.h"
|
---|
12 |
|
---|
13 | #include "MEnv.h"
|
---|
14 | #include "MArgs.h"
|
---|
15 | #include "MCosy.h"
|
---|
16 | #include "MTime.h"
|
---|
17 | #include "MDriveCom.h"
|
---|
18 |
|
---|
19 | #include "MLogManip.h"
|
---|
20 |
|
---|
21 | #include "MStarguider.h"
|
---|
22 |
|
---|
23 | using namespace std;
|
---|
24 |
|
---|
25 | #define EXPERT
|
---|
26 |
|
---|
27 | static void StartUpMessage()
|
---|
28 | {
|
---|
29 | gLog << all << endl;
|
---|
30 |
|
---|
31 | // 1 2 3 4 5 6
|
---|
32 | // 123456789012345678901234567890123456789012345678901234567890
|
---|
33 | gLog << "========================================================" << endl;
|
---|
34 | gLog << " COSY " << endl;
|
---|
35 | gLog << " Magic Drive Control System Software " << endl;
|
---|
36 | gLog << " Compiled with ROOT v" << ROOT_RELEASE << " on <" << __DATE__ << ">" << endl;
|
---|
37 | gLog << "========================================================" << endl;
|
---|
38 | gLog << endl;
|
---|
39 | }
|
---|
40 |
|
---|
41 | static void Usage()
|
---|
42 | {
|
---|
43 | // 1 2 3 4 5 6 7 8
|
---|
44 | // 12345678901234567890123456789012345678901234567890123456789012345678901234567890
|
---|
45 | gLog << all << endl;
|
---|
46 | gLog << "Sorry the usage is:" << endl;
|
---|
47 | gLog << " cosy [options]" << endl << endl;
|
---|
48 | gLog << " Arguments:" << endl;
|
---|
49 | gLog << " n/a" << endl;
|
---|
50 | gLog << endl;
|
---|
51 | gLog << " Root Options:" << endl;
|
---|
52 | gLog << " -b Batch mode (no graphical output to screen)" << endl<<endl;
|
---|
53 | gLog << " Options:" << endl;
|
---|
54 | gLog.Usage();
|
---|
55 | gLog << " --debug-env=0 Disable debugging setting resources <default>" << endl;
|
---|
56 | gLog << " --debug-env[=1] Display untouched resources after program execution" << endl;
|
---|
57 | gLog << " --debug-env=2 Display untouched resources after eventloop setup" << endl;
|
---|
58 | gLog << " --debug-env=3 Debug setting resources from resource file and command line" << endl;
|
---|
59 | gLog << " --debug-mem Debug memory usage" << endl << endl;
|
---|
60 | gLog << " --debug-threads Debug threads" << endl << endl;
|
---|
61 | gLog << " --rc=Name:option Set or overwrite a resource of the resource file." << endl << endl;
|
---|
62 | gLog << " --version, -V Show startup message with version number" << endl;
|
---|
63 | gLog << " -?, -h, --help This help" << endl << endl;
|
---|
64 | }
|
---|
65 |
|
---|
66 | enum Ports_t {
|
---|
67 | kPortTPoint = 4,
|
---|
68 | kPortStarguider = 5,
|
---|
69 | };
|
---|
70 |
|
---|
71 | Bool_t SwitchCamera(const TString ip, Ports_t port, Bool_t on)
|
---|
72 | {
|
---|
73 | if (ip.IsNull())
|
---|
74 | return kTRUE;
|
---|
75 |
|
---|
76 | TSocket s(ip, 80);
|
---|
77 | if (!s.IsValid())
|
---|
78 | {
|
---|
79 | gLog << err << "ERROR - Could not connect to " << ip << " to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd." << endl;
|
---|
80 | return kFALSE;
|
---|
81 | }
|
---|
82 |
|
---|
83 | TString msg = Form("GET /ov.html?cmd=1&p=%d&s=%d HTTP/1.1\r\n", port, on?1:0);
|
---|
84 |
|
---|
85 | if (s.SendRaw(msg.Data(), msg.Length()) == -1)
|
---|
86 | {
|
---|
87 | gLog << err << "ERROR - Could not talk to " << ip << " to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd." << endl;
|
---|
88 | return kFALSE;
|
---|
89 | }
|
---|
90 |
|
---|
91 | char buf[8192];
|
---|
92 |
|
---|
93 | if (s.RecvRaw(buf, 8192) == -1)
|
---|
94 | return kFALSE;
|
---|
95 |
|
---|
96 | // FIXME: Eval buffer
|
---|
97 |
|
---|
98 | gLog << all << "- " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd switched " << (on?"on":"off") << "." << endl;
|
---|
99 |
|
---|
100 | return kTRUE;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /* ---------------------------------------------------------------------- */
|
---|
105 | int main(int argc, char **argv)
|
---|
106 | {
|
---|
107 | if (!MARS::CheckRootVer())
|
---|
108 | return 0xff;
|
---|
109 |
|
---|
110 | MLog::RedirectErrorHandler(MLog::kColor);
|
---|
111 |
|
---|
112 | //
|
---|
113 | // Evaluate arguments
|
---|
114 | //
|
---|
115 | MArgs arg(argc, argv, kTRUE);
|
---|
116 | gLog.Setup(arg);
|
---|
117 |
|
---|
118 | StartUpMessage();
|
---|
119 |
|
---|
120 | if (arg.HasOnly("-V") || arg.HasOnly("--version"))
|
---|
121 | return 0;
|
---|
122 |
|
---|
123 | if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
|
---|
124 | {
|
---|
125 | Usage();
|
---|
126 | return 2;
|
---|
127 | }
|
---|
128 |
|
---|
129 | const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
|
---|
130 | const Bool_t kDebugThreads = arg.HasOnlyAndRemove("--debug-threads");
|
---|
131 | const Int_t ceco_tx = arg.GetIntAndRemove("--cc-tx=", 7314);//7304);
|
---|
132 | const Int_t ceco_rx = arg.GetIntAndRemove("--cc-rx=", 7414);//7404);
|
---|
133 | const TString kConfig = arg.GetStringAndRemove("--config=", ".cosyrc"); // ceco
|
---|
134 | Int_t kDebugEnv = arg.HasOnlyAndRemove("--debug-env") ? 1 : 0;
|
---|
135 | kDebugEnv = arg.GetIntAndRemove("--debug-env=", kDebugEnv);
|
---|
136 |
|
---|
137 | //
|
---|
138 | // check for the right usage of the program (number of arguments)
|
---|
139 | //
|
---|
140 | if (arg.GetNumArguments()>0)
|
---|
141 | {
|
---|
142 | gLog << warn << "WARNING - Wrong number of arguments..." << endl;
|
---|
143 | Usage();
|
---|
144 | return 2;
|
---|
145 | }
|
---|
146 |
|
---|
147 | //
|
---|
148 | // Now we access/read the resource file. This will remove all
|
---|
149 | // --rc= from the list of arguments.
|
---|
150 | //
|
---|
151 | MEnv env(kConfig);
|
---|
152 | if (!env.IsValid())
|
---|
153 | {
|
---|
154 | gLog << err << "ERROR - Reading resource file " << kConfig << "." << endl;
|
---|
155 | return 0xfe;
|
---|
156 | }
|
---|
157 |
|
---|
158 | const Int_t channel = env.GetValue("DefaultCameraChannel", 0);
|
---|
159 | const TString sps = env.GetValue("IpAddressSPS", "sps");
|
---|
160 | const TString ceco = env.GetValue("IpAddressCentralControl", "161.72.130.60");
|
---|
161 | const TString powerswitch = env.GetValue("IpAddressPowerSwitch", "");
|
---|
162 | const TString pointing = env.GetValue("PointingModel", "bending.txt");
|
---|
163 |
|
---|
164 | // And move the resource options from the command line to the MEnv
|
---|
165 | if (!env.TakeEnv(arg, kDebugEnv>2))
|
---|
166 | return 0xfd;
|
---|
167 |
|
---|
168 | //
|
---|
169 | // check for the right usage of the program (number of options)
|
---|
170 | //
|
---|
171 | if (arg.GetNumOptions()>0)
|
---|
172 | {
|
---|
173 | gLog << warn << "WARNING - Unknown commandline options..." << endl;
|
---|
174 | arg.Print("options");
|
---|
175 | gLog << endl;
|
---|
176 | return 2;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (!gLog.IsOutputDeviceEnabled(MLog::eFile))
|
---|
180 | {
|
---|
181 | const TString name = MCosy::GetFileName("log", "cosy", "log");
|
---|
182 | gLog << inf << "Open automatic logfile: " << name << endl;
|
---|
183 | gLog.SetOutputFile(name);
|
---|
184 | gLog.EnableOutputDevice(MLog::eFile);
|
---|
185 | }
|
---|
186 |
|
---|
187 | gLog << all << "Starting Cosy at " << MTime(-1) << " in thread " << TThread::SelfId() << "..." << endl;
|
---|
188 |
|
---|
189 | //
|
---|
190 | // start the main window
|
---|
191 | //
|
---|
192 | gLog << all << "- Initialising Root environment." << endl;
|
---|
193 |
|
---|
194 | //
|
---|
195 | // Initialize root
|
---|
196 | //
|
---|
197 | //MArray::Class()->IgnoreTObjectStreamer();
|
---|
198 | //MParContainer::Class()->IgnoreTObjectStreamer();
|
---|
199 |
|
---|
200 | TApplication app("cosy", &argc, argv);
|
---|
201 | if (gROOT->IsBatch())
|
---|
202 | {
|
---|
203 | gLog << err << "ERROR - Cannot run in Batch mode!" << endl;
|
---|
204 | return 0;
|
---|
205 | }
|
---|
206 | if (!gClient)
|
---|
207 | {
|
---|
208 | gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
|
---|
209 | return 1;
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (kDebugMem)
|
---|
213 | TObject::SetObjectStat(kTRUE);
|
---|
214 |
|
---|
215 | //
|
---|
216 | // Create the Network. Device: /dev/dpm_00, Rate: 500kbps
|
---|
217 | //
|
---|
218 | gLog << all <<"- Constructing MCosy." << endl;
|
---|
219 | /*
|
---|
220 | //
|
---|
221 | // check for the right usage of the program
|
---|
222 | //
|
---|
223 | int mode = 0;
|
---|
224 | if (argc==2 && (argv[1][0]=='-' || argv[1][1]=='m'))
|
---|
225 | switch (argv[1][2])
|
---|
226 | {
|
---|
227 | case '0': // standard
|
---|
228 | mode = 0;
|
---|
229 | break;
|
---|
230 | case '1': // SE mode
|
---|
231 | mode = 1;
|
---|
232 | break;
|
---|
233 | case '2': // GUI demo mode
|
---|
234 | mode = 2;
|
---|
235 | break;
|
---|
236 | }
|
---|
237 | */
|
---|
238 |
|
---|
239 | MDriveCom *com = new MDriveCom(ceco, ceco_tx, ceco_rx);
|
---|
240 | com->SetTelescope(2);
|
---|
241 |
|
---|
242 | MCosy *cosy = new MCosy(env, com, pointing);
|
---|
243 |
|
---|
244 | com->SetMsgQueue(cosy);
|
---|
245 |
|
---|
246 | Interface *interface = new Ethernet(sps, 5357, 5358, cosy);
|
---|
247 | // Interface *interface = new VmodIcan(cosy, "/dev/dpm_00", 125);
|
---|
248 |
|
---|
249 | gLog << all << "- Starting MCosy." << endl;
|
---|
250 |
|
---|
251 | cosy->Start(env);
|
---|
252 |
|
---|
253 | // FIXME: Is this the right position?
|
---|
254 | if (kDebugEnv>0)
|
---|
255 | env.PrintUntouched();
|
---|
256 |
|
---|
257 |
|
---|
258 | MStarguider *client=0;
|
---|
259 | if (channel>=0)
|
---|
260 | {
|
---|
261 | SwitchCamera(powerswitch, kPortTPoint, kTRUE);
|
---|
262 | SwitchCamera(powerswitch, kPortStarguider, kTRUE);
|
---|
263 |
|
---|
264 | gLog << all << "- Starting Starguider." << endl;
|
---|
265 |
|
---|
266 | client=new MStarguider(MObservatory::kMagic1, channel);
|
---|
267 | client->SetupEnv(env);
|
---|
268 | //client.SetDriveCom(&com);
|
---|
269 | cosy->SetStarguider(client);
|
---|
270 | client->SetCosy(cosy);
|
---|
271 | }
|
---|
272 |
|
---|
273 | gLog << all << "- Starting mainloop." << endl;
|
---|
274 |
|
---|
275 | app.Run(kTRUE);
|
---|
276 |
|
---|
277 | if (kDebugThreads)
|
---|
278 | TThread::Ps();
|
---|
279 |
|
---|
280 | if (channel>=0)
|
---|
281 | {
|
---|
282 | client->SetCosy(NULL);
|
---|
283 | cosy->SetStarguider(NULL);
|
---|
284 | gLog << all << "- Stopping starg." << endl;
|
---|
285 | delete client;
|
---|
286 |
|
---|
287 | SwitchCamera(powerswitch, kPortTPoint, kFALSE);
|
---|
288 | SwitchCamera(powerswitch, kPortStarguider, kFALSE);
|
---|
289 | }
|
---|
290 |
|
---|
291 | gLog << all << "- Stopping cosy." << endl;
|
---|
292 | cosy->Stop();
|
---|
293 |
|
---|
294 | gLog << all << MTime(-1) << ": MCosy stopped." << endl;
|
---|
295 |
|
---|
296 | delete interface;
|
---|
297 |
|
---|
298 | gLog << all << "Deleting cosy at " << MTime(-1) << endl;
|
---|
299 |
|
---|
300 | //com.SetMsgQueue(NULL);
|
---|
301 |
|
---|
302 | delete cosy;
|
---|
303 |
|
---|
304 | gLog << all << "Deleting DriveCom at " << MTime(-1) << endl;
|
---|
305 | delete com;
|
---|
306 |
|
---|
307 | if (kDebugThreads)
|
---|
308 | TThread::Ps();
|
---|
309 |
|
---|
310 | //delete app;
|
---|
311 |
|
---|
312 | if (TObject::GetObjectStat())
|
---|
313 | {
|
---|
314 | TObject::SetObjectStat(kFALSE);
|
---|
315 | gObjectTable->Print();
|
---|
316 | }
|
---|
317 |
|
---|
318 | gLog << all << "The End." << endl;
|
---|
319 | }
|
---|