]> git.mxchange.org Git - flightgear.git/blob - scripts/example/fgfsscript
Merge branch 'jmt/gps' into next
[flightgear.git] / scripts / example / fgfsscript
1 #!/usr/bin/perl -w
2 # USAGE: fgfsscript [host [port]]
3 # $Id$
4 # Public Domain
5
6 use strict;
7 use IO::Socket;
8
9 my $host = (shift || 'localhost');
10 my $port = (shift || 5501);
11 my ($fgfs, $i);
12
13
14
15 # main()
16 {
17         chdir;
18         $fgfs = &connect($host, $port, 120) || die " can't open socket\n";
19         &send("data");
20
21         # wait for random altitude (0--3000 ft.) to be reached
22         my $alt = int(rand(3000));
23         print "disaster begins at $alt ft. AGL\n";
24         while (1) {
25                 sleep(1);
26                 $i = &get("/position/altitude-agl-ft");
27                 print "\r" . int($i) . " ft.";
28                 print "\n" and last if $i > $alt;
29         }
30
31         print "start fuel dumping  :-)\n";
32         for ($i = 0; $i < 4; $i++) {
33                 sleep(rand(60));
34                 &set("/consumables/fuel/tank[$i]/level-gal_us", 0);
35                 print "tank $i empty\n";
36         }
37         
38         &send("quit");
39         close $fgfs;
40 }
41
42
43
44
45 sub get()
46 {       
47         &send("get " . shift);
48         eof $fgfs and die "\nconnection closed by host";
49         $_ = <$fgfs>;
50         s/\015?\012$//;
51         /^-ERR (.*)/ and die "\nfgfs error: $1\n";
52         return $_;
53 }
54
55
56 sub set()
57 {       
58         my $prop = shift;
59         my $value = shift;
60         &send("set $prop $value");
61 }
62
63
64 sub send()
65 {       
66         print $fgfs shift, "\015\012";
67 }
68
69
70 sub connect()
71 {
72         my $host = shift;
73         my $port = shift;
74         my $timeout = (shift || 120);
75         my $socket;
76         STDOUT->autoflush(1);
77         print "connect ";
78         while ($timeout--) {
79                 if ($socket = IO::Socket::INET->new(
80                                 Proto => 'tcp',
81                                 PeerAddr => $host,
82                                 PeerPort => $port)) {
83                         print ".. done.\n";
84                         $socket->autoflush(1);
85                         sleep 1;
86                         return $socket;
87                 }       
88                 print ".";
89                 sleep(1);
90         }
91         return 0;
92 }
93
94
95 # vi:ts=8:sw=8:noet:nowrap:cindent