source: BAORadio/libindi/libindi/BAOTest/BAOtest_main.cpp @ 695

Last change on this file since 695 was 695, checked in by frichard, 12 years ago
File size: 7.8 KB
Line 
1/**************************************************************************************
2** Petit simulateur d'antenne
3** BAORadio
4** Franck RICHARD
5***************************************************************************************/
6
7#include "../communs/ClientSocket.h"
8#include "../communs/SocketException.h"
9#include <iostream>
10#include <string>
11#include <string.h>
12#include <stdio.h>
13#include <stdlib.h>
14#include <net/if.h>
15#include <sys/types.h>
16#include <sys/socket.h>
17#include <sys/ioctl.h>
18#include <netinet/in.h>
19
20//#define POSITIONS_DIRECT 
21
22using namespace std;
23
24
25
26/**************************************************************************************
27** Extraction de la position de l'antenne
28**
29***************************************************************************************/
30
31Position ExtractPosition(std::string str)
32{
33    Position result;
34
35    std::string str2, str3;
36
37    result.x=-1;
38    result.y=-1;
39
40    int pos = str.find("#");
41
42    if (pos!=string::npos)
43    {
44        str2 = str.substr(pos+2);
45
46        pos = str2.find("Az");
47
48        result.x=atoi(str2.substr(0, pos-1).c_str());
49
50        pos = str2.find("#");
51
52        if (pos!=string::npos)
53        {
54            str3 = str2.substr(pos+2);
55
56            pos = str3.find("Alt");
57
58            result.y=atoi(str3.substr(0, pos-1).c_str());;
59
60        }
61    }
62
63    return result;
64}
65
66Position ExtractPosition2(std::string str)
67{
68    Position result;
69
70    result.x=0;
71    result.y=0;
72
73    if (str.length() > 2)
74    {
75        result.x=atoi(str.substr(2, 4).c_str());
76
77        result.y=atoi(str.substr(7, 4).c_str());
78
79        if (str[1]=='f') result.x=-result.x;
80
81        if (str[6]=='f') result.y=-result.y;
82    }
83
84    return result;
85}
86
87/**************************************************************************************
88** Création de l'identifiant de l'antenne à partir de l'IP
89**
90***************************************************************************************/
91/*
92int NumeroServeur()
93{
94    std::string str, str2;
95
96    int fd;
97    struct ifreq ifr;
98
99    fd=socket(AF_INET, SOCK_DGRAM, 0);
100
101    ifr.ifr_addr.sa_family=AF_INET;
102
103    strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
104
105    ioctl(fd, SIOCGIFADDR, &ifr);
106
107    close(fd);
108
109    str=(std::string)inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
110
111    int pos = str.find_last_of(".");
112
113    if (pos!=string::npos)
114    {
115        str2 = str.substr(pos+1);
116
117        return atoi(str2.c_str());
118    }
119
120    return -1;
121}
122*/
123
124
125/**************************************************************************************
126** MAIN
127**
128***************************************************************************************/
129
130#define ERREURS 10
131
132int main ( int argc, char *argv[] )
133{
134    Position Pos, Pos2, NouvPos;
135    char cIP;
136    char IP[20];
137    bool Aleat;
138
139    // Parametres sups
140    bool ErreursAleatoires=false;
141    bool Affichage=true;
142
143    // Position PARK
144    Pos.x=0;
145    Pos.y=0;
146
147    NouvPos.x=100;
148    NouvPos.y=100;
149
150    //init nbs aléatoires
151    if (ErreursAleatoires) srand(time(NULL));
152
153
154
155    std::cout <<  "******************\n";
156    std::cout <<  "* Simulateur BAO *\n";
157    std::cout <<  "******************\n\n";
158    std::cout <<  "Adresse IP du serveur :\n";
159    std::cout <<  "1)-  192.168.0.1\n";
160    std::cout <<  "2)-  127.0.0.1\n\n? ";
161    std::cin >> cIP;
162
163    if (cIP=='1') strcpy(IP, "192.168.0.1");
164    else strcpy(IP, "127.0.0.1");
165
166    try
167    {
168        ClientSocket client_socket ( IP, 8000 );
169
170        // ServerSocket socket ( 8000 );
171        // ServerSocket client_socket;
172        // socket.accept ( client_socket );
173
174        std::string reply;
175        std::string asks,memasks;
176        char chaine[5000];
177
178        while (true)
179        {
180
181            client_socket.recv(asks);
182
183            int pos=asks.find("\n");
184            memasks=asks;
185
186            while ((pos!=string::npos) && (asks.length()>1))
187            {
188                memasks=asks.substr(pos+1);
189                asks=asks.substr(0,pos);
190
191                strcpy(chaine, "");
192
193#ifndef POSITIONS_DIRECT
194                if (abs(Pos.x - NouvPos.x) < 5 )
195                {
196                    if (Pos.x - NouvPos.x < 0) Pos.x--;
197                    if (Pos.x - NouvPos.x > 0) Pos.x++;
198                }
199                else
200                {
201                    Pos.x -= (NouvPos.x - Pos.x) / 6.0;
202                }
203
204                if (abs(Pos.y - NouvPos.y) < 5 )
205                {
206                    if (Pos.y - NouvPos.y < 0) Pos.y--;
207                    if (Pos.y - NouvPos.y > 0) Pos.y++;
208                }
209                else
210                {
211                    Pos.y -= (NouvPos.y - Pos.y) / 6.0;
212                }
213#endif
214               
215
216                if (ErreursAleatoires) Aleat=rand()%ERREURS != 1;
217                else Aleat=1;
218
219                if (Affichage) std::cout <<  "Pilote BAO : " << asks << "\n" ;
220
221
222                if (asks.find("P")!=string::npos)
223                {
224
225                    strcpy(chaine,"ACK/POSITION\n");
226                    client_socket << chaine;
227
228                    if (Affichage) std::cout <<  "Microcontrôleur : " << chaine;
229
230                    if (Aleat)
231                    {
232                        sprintf(chaine, "POSITION/%05i/%05i/\n", Pos.x, Pos.y);
233                    }
234                    else
235                    {
236                        strcpy(chaine, "NACK/HALT/POSITION/\n");   //erreur moteur !
237                    }
238                }
239
240                if (asks.find("G")!=string::npos)
241                {
242#ifndef POSITIONS_DIRECT
243                 NouvPos = ExtractPosition2(asks);
244
245                        NouvPos.x += Pos.x;
246                        NouvPos.y += Pos.y;
247#else
248               
249               Pos2 = ExtractPosition2(asks);
250               Pos.x-=Pos2.x;
251               Pos.y-=Pos2.y;
252#endif
253                    strcpy(chaine,"ACK/GOTO/\n");
254                    client_socket << chaine;
255
256                    if (Affichage)
257                    {
258                        std::cout <<  "Microcontrôleur : " << chaine;
259
260                        std::cout <<  "Positionnement de l antenne...\n";
261                    }
262
263                    if (Aleat)
264                    {
265                        strcpy(chaine, "OK/GOTO/\n");
266                    }
267                    else
268                    {
269                        strcpy(chaine, "NACK/HALT/GOTO/\n");   //erreur moteur !
270                    }
271                }
272
273                if (asks.find("Z")!=string::npos)
274                {
275
276                    strcpy(chaine, "ACK/PARK\n");
277                    client_socket << chaine;
278
279                    if (Affichage) std::cout <<  "Microcontrôleur : " << chaine;
280
281                    if (Aleat)
282                    {
283                        strcpy(chaine, "OK/PARK/\n");
284                    }
285                    else
286                    {
287                        strcpy(chaine, "NACK/HALT/PARK/\n");   //erreur
288                    }
289                }
290
291                if (asks.find("A")!=string::npos)
292                {
293
294                    strcpy(chaine,"ACK/ABORT\n");
295                    client_socket << chaine;
296
297                    if (Affichage) std::cout <<  "Microcontrôleur : " << chaine;
298
299                    if (Aleat)
300                    {
301                        strcpy(chaine, "OK/ABORT/\n");
302                    }
303                    else
304                    {
305                        strcpy(chaine, "NACK/HALT/ABORT/\n");   //erreur
306                    }
307                }
308
309                if (strlen(chaine)>1)
310                {
311                    client_socket << chaine;
312
313                    if (Affichage)
314                    {
315                        std::cout <<  "Microcontrôleur : " << chaine;
316
317                        std::cout <<  "---------\n";
318                    }
319                }
320
321                asks=memasks;
322                pos=asks.find("\n");
323            }
324
325        }
326    }
327    catch ( SocketException& e )
328    {
329        std::cout << "Exception was caught:" << e.description() << "\n";
330    }
331
332    return 0;
333}
Note: See TracBrowser for help on using the repository browser.