source: trunk/Cosy/cosy.cc@ 10305

Last change on this file since 10305 was 10031, checked in by tbretz, 14 years ago
Changed connection from SPS to a client-only connection.
File size: 9.4 KB
Line 
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
23using namespace std;
24
25#define EXPERT
26
27static 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
41static 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
66enum Ports_t {
67 kPortTPoint = 4,
68 kPortStarguider = 5,
69};
70
71Bool_t SwitchCamera(const TString ip, Ports_t port, Bool_t on)
72{
73 if (ip.IsNull())
74 return kTRUE;
75
76 gLog << all << "- Trying to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd via " << ip << endl;
77
78 TSocket s(ip, 80);
79 if (!s.IsValid())
80 {
81 gLog << err << "ERROR - Could not connect to " << ip << " to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd." << endl;
82 return kFALSE;
83 }
84
85 TString msg = Form("GET /ov.html?cmd=1&p=%d&s=%d HTTP/1.1\r\n", port, on?1:0);
86
87 if (s.SendRaw(msg.Data(), msg.Length()) == -1)
88 {
89 gLog << err << "ERROR - Could not talk to " << ip << " to switch " << (on?"on":"off") << " " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd." << endl;
90 return kFALSE;
91 }
92
93 char buf[8192];
94
95 if (s.RecvRaw(buf, 8192) == -1)
96 return kFALSE;
97
98 // FIXME: Eval buffer
99
100 gLog << all << "- " << (port==kPortTPoint?"TPoint":"Starguider") << " ccd switched " << (on?"on":"off") << "." << endl;
101
102 return kTRUE;
103}
104
105
106/* ---------------------------------------------------------------------- */
107int main(int argc, char **argv)
108{
109 if (!MARS::CheckRootVer())
110 return 0xff;
111
112 MLog::RedirectErrorHandler(MLog::kColor);
113
114 //
115 // Evaluate arguments
116 //
117 MArgs arg(argc, argv);
118 gLog.Setup(arg);
119
120 StartUpMessage();
121
122 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
123 return 0;
124
125 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
126 {
127 Usage();
128 return 2;
129 }
130
131 const Bool_t kDebugMem = arg.HasOnlyAndRemove("--debug-mem");
132 const Bool_t kDebugThreads = arg.HasOnlyAndRemove("--debug-threads");
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.2");
161 const Int_t ceco_rx = env.GetValue("PortCentralControlRX", 7404);
162 const Int_t ceco_tx = env.GetValue("PortCentralControlTX", 7304);
163 const TString powerswitch = env.GetValue("IpAddressPowerSwitch", "");
164 const Int_t telescope = env.GetValue("Telescope", 1);
165
166 // And move the resource options from the command line to the MEnv
167 if (!env.TakeEnv(arg, kDebugEnv>2))
168 return 0xfd;
169
170 //
171 // check for the right usage of the program (number of options)
172 //
173 if (arg.GetNumOptions()>0)
174 {
175 gLog << warn << "WARNING - Unknown commandline options..." << endl;
176 arg.Print("options");
177 gLog << endl;
178 return 2;
179 }
180
181 if (!gLog.IsOutputDeviceEnabled(MLog::eFile))
182 {
183 const TString name = MCosy::GetFileName("log", "cosy", "log");
184 gLog << inf << "Open automatic logfile: " << name << endl;
185 gLog.SetOutputFile(name);
186 gLog.EnableOutputDevice(MLog::eFile);
187 }
188
189 gLog << all << "Starting Cosy at " << MTime(-1) << " in thread " << TThread::SelfId() << "..." << endl;
190
191 //
192 // start the main window
193 //
194 gLog << all << "- Initialising Root environment." << endl;
195
196 //
197 // Initialize root
198 //
199 //MArray::Class()->IgnoreTObjectStreamer();
200 //MParContainer::Class()->IgnoreTObjectStreamer();
201
202 TApplication app("cosy", &argc, argv);
203 if (gROOT->IsBatch())
204 {
205 gLog << err << "ERROR - Cannot run in Batch mode!" << endl;
206 return 0;
207 }
208 if (!gClient)
209 {
210 gLog << err << "Bombing... maybe your DISPLAY variable is not set correctly!" << endl;
211 return 1;
212 }
213
214 if (kDebugMem)
215 TObject::SetObjectStat(kTRUE);
216
217 // Check if a connection can be established
218 const Int_t save = gDebug;
219 gDebug = 1;
220 TSocket test(sps, 5357);
221 gDebug = save;
222 test.Close();
223
224 //
225 // Create the Network. Device: /dev/dpm_00, Rate: 500kbps
226 //
227 gLog << all <<"- Constructing MCosy." << endl;
228/*
229 //
230 // check for the right usage of the program
231 //
232 int mode = 0;
233 if (argc==2 && (argv[1][0]=='-' || argv[1][1]=='m'))
234 switch (argv[1][2])
235 {
236 case '0': // standard
237 mode = 0;
238 break;
239 case '1': // SE mode
240 mode = 1;
241 break;
242 case '2': // GUI demo mode
243 mode = 2;
244 break;
245 }
246 */
247
248 MDriveCom *com = new MDriveCom(ceco, ceco_tx, ceco_rx);
249 com->SetTelescope(telescope);
250
251 MCosy *cosy = new MCosy(env, com);
252
253 com->SetMsgQueue(cosy);
254
255 Interface *interface = new Ethernet(sps, 5357, cosy);
256 // Interface *interface = new VmodIcan(cosy, "/dev/dpm_00", 125);
257
258 gLog << all << "- Starting MCosy." << endl;
259
260 cosy->Start(env);
261
262 gLog << all << "- MCosy started." << endl;
263
264 // FIXME: Is this the right position?
265 if (kDebugEnv>0)
266 env.PrintUntouched();
267
268 MStarguider *client=0;
269 if (channel>=0)
270 {
271 SwitchCamera(powerswitch, kPortTPoint, kTRUE);
272 SwitchCamera(powerswitch, kPortStarguider, kTRUE);
273
274 gLog << all << "- Starting Starguider." << endl;
275
276 client=new MStarguider(MObservatory::kMagic1, channel);
277 client->SetupEnv(env);
278 //client.SetDriveCom(&com);
279 cosy->SetStarguider(client);
280 client->SetCosy(cosy);
281 }
282
283 gLog << all << "- Starting mainloop." << endl;
284
285 app.Run(kTRUE);
286
287 if (kDebugThreads)
288 TThread::Ps();
289
290 if (channel>=0)
291 {
292 client->SetCosy(NULL);
293 cosy->SetStarguider(NULL);
294 gLog << all << "- Stopping starg." << endl;
295 delete client;
296
297 SwitchCamera(powerswitch, kPortTPoint, kFALSE);
298 SwitchCamera(powerswitch, kPortStarguider, kFALSE);
299 }
300
301 gLog << all << "- Stopping cosy." << endl;
302 cosy->Stop();
303
304 gLog << all << MTime(-1) << ": MCosy stopped." << endl;
305
306 delete interface;
307
308 gLog << all << "Deleting cosy at " << MTime(-1) << endl;
309
310 //com.SetMsgQueue(NULL);
311
312 delete cosy;
313
314 gLog << all << "Deleting DriveCom at " << MTime(-1) << endl;
315 delete com;
316
317 if (kDebugThreads)
318 TThread::Ps();
319
320 //delete app;
321
322 if (TObject::GetObjectStat())
323 {
324 TObject::SetObjectStat(kFALSE);
325 gObjectTable->Print();
326 }
327
328 gLog << all << "The End." << endl;
329}
Note: See TracBrowser for help on using the repository browser.