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