1 /*
2 Encode an ATIS into spoken words
3 Copyright (C) 2014 Torsten Dreyer
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 as published by the Free Software Foundation; either version 2
8 of the License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19 
20 #ifndef __ATIS_ENCODER_HXX
21 #define __ATIS_ENCODER_HXX
22 
23 #include <string>
24 #include <Airports/airport.hxx>
25 #include <simgear/props/props.hxx>
26 #include <map>
27 
28 class ATCSpeech {
29 public:
30     static std::string getSpokenDigit( int i );
31     static std::string getSpokenNumber( std::string number );
32     static std::string getSpokenNumber( int number, bool leadingZero = false, int digits = 1 );
33     static std::string getSpokenAltitude( int altitude );
34 };
35 
36 class ATISInformationProvider {
37 public:
~ATISInformationProvider()38     virtual ~ATISInformationProvider() {}
39     virtual bool isValid() = 0;
40     virtual std::string airportId() = 0;
41 
makeAtisTime(int day,int hour,int minute)42     static long makeAtisTime( int day, int hour, int minute ) {
43       return  100*100l* day + 100l * hour + minute;
44     }
getAtisTimeDay(long atisTime)45     inline int getAtisTimeDay( long atisTime ) { return atisTime / (100l*100l); }
getAtisTimeHour(long atisTime)46     inline int getAtisTimeHour( long atisTime ) { return (atisTime % (100l*100l)) / 100l; }
getAtisTimeMinute(long atisTime)47     inline int getAtisTimeMinute( long atisTime ) { return atisTime % 100l; }
48     virtual long getTime() = 0; // see makeAtisTime
49 
50     virtual int getWindDeg() = 0;
51     virtual int getWindMinDeg() = 0;
52     virtual int getWindMaxDeg() = 0;
53     virtual int getWindSpeedKt() = 0;
54     virtual int getGustsKt() = 0;
55     virtual int getQnh() = 0;
56     virtual double getQnhInHg() = 0;
57     virtual bool isCavok() = 0;
58     virtual int getVisibilityMeters() = 0;
59     virtual std::string getPhenomena() = 0;
60 
61     typedef std::map<int,std::string> CloudEntries;
62     virtual CloudEntries getClouds() = 0;
63     virtual int getTemperatureDeg() = 0;
64     virtual int getDewpointDeg() = 0;
65     virtual std::string getTrend() = 0;
66 };
67 
68 class ATISEncoder : public ATCSpeech {
69 public:
70   ATISEncoder();
71   virtual ~ATISEncoder();
72   virtual std::string encodeATIS( ATISInformationProvider * atisInformationProvider );
73 
74 protected:
75   virtual std::string getAtisId( SGPropertyNode_ptr );
76   virtual std::string getAirportName( SGPropertyNode_ptr );
77   virtual std::string getTime( SGPropertyNode_ptr );
78   virtual std::string getApproachType( SGPropertyNode_ptr );
79   virtual std::string getLandingRunway( SGPropertyNode_ptr );
80   virtual std::string getTakeoffRunway( SGPropertyNode_ptr );
81   virtual std::string getTransitionLevel( SGPropertyNode_ptr );
82   virtual std::string getWindDirection( SGPropertyNode_ptr );
83   virtual std::string getWindMinDirection( SGPropertyNode_ptr );
84   virtual std::string getWindMaxDirection( SGPropertyNode_ptr );
85   virtual std::string getWindspeedKnots( SGPropertyNode_ptr );
86   virtual std::string getGustsKnots( SGPropertyNode_ptr );
87   virtual std::string getCavok( SGPropertyNode_ptr );
88   virtual std::string getVisibilityMetric( SGPropertyNode_ptr );
89   virtual std::string getVisibilityMiles( SGPropertyNode_ptr );
90   virtual std::string getPhenomena( SGPropertyNode_ptr );
91   virtual std::string getClouds( SGPropertyNode_ptr );
92   virtual std::string getCloudsBrief( SGPropertyNode_ptr );
93   virtual std::string getTemperatureDeg( SGPropertyNode_ptr );
94   virtual std::string getDewpointDeg( SGPropertyNode_ptr );
95   virtual std::string getQnh( SGPropertyNode_ptr );
96   virtual std::string getInhgInteger( SGPropertyNode_ptr );
97   virtual std::string getInhgFraction( SGPropertyNode_ptr );
98   virtual std::string getInhg( SGPropertyNode_ptr );
99   virtual std::string getTrend( SGPropertyNode_ptr );
100 
101   typedef std::string (ATISEncoder::*handler_t)( SGPropertyNode_ptr baseNode );
102   typedef std::map<std::string, handler_t > HandlerMap;
103   HandlerMap handlerMap;
104 
105   SGPropertyNode_ptr atisSchemaNode;
106 
107   std::string processTokens( SGPropertyNode_ptr baseNode );
108   std::string processToken( SGPropertyNode_ptr baseNode );
109 
110   std::string processTextToken( SGPropertyNode_ptr baseNode );
111   std::string processTokenToken( SGPropertyNode_ptr baseNode );
112   std::string processIfToken( SGPropertyNode_ptr baseNode );
113 
114   bool checkEmptyCondition( SGPropertyNode_ptr node, bool isEmpty );
115 
116   // Wrappers that can be passed as function pointers to checkCondition
117   // @see simgear::strutils::starts_with
118   // @see simgear::strutils::ends_with
contains(const string & s,const string & substring)119   static bool contains(const string &s, const string &substring)
120     { return s.find(substring) != std::string::npos; };
equals(const string & s1,const string & s2)121   static bool equals(const string &s1, const string &s2)
122     { return s1 == s2; };
123 
124   bool checkCondition( SGPropertyNode_ptr node, bool notInverted,
125       bool (*fp)(const std::string &, const std::string &),
126       const std::string &name );
127 
128   FGAirportRef airport;
129   ATISInformationProvider * _atis;
130 
131 };
132 
133 #endif
134