1   /* Copyright 2002-2021 CS GROUP
2    * Licensed to CS GROUP (CS) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * CS licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.orekit.gnss.metric.ntrip;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.regex.Matcher;
22  import java.util.regex.Pattern;
23  import java.util.stream.Collectors;
24  import java.util.stream.Stream;
25  
26  /** Data stream record in source table.
27   * @author Luc Maisonobe
28   * @since 11.0
29   */
30  public class DataStreamRecord extends Record {
31  
32      /** Pattern for delimiting messages. */
33      private static final Pattern SEPARATOR = Pattern.compile(",");
34  
35      /** Message pattern. */
36      private static final Pattern PATTERN = Pattern.compile("^([^()]+)(?:\\(([0-9]+)\\))?$");
37  
38      /** Data format. */
39      private final DataFormat format;
40  
41      /** Streamed messages. */
42      private final List<StreamedMessage> formatDetails;
43  
44      /** Carrier phase. */
45      private final CarrierPhase carrierPhase;
46  
47      /** Navigation systems. */
48      private final List<NavigationSystem> systems;
49  
50      /** Latitude (rad). */
51      private final double latitude;
52  
53      /** Longitude (rad). */
54      private final double longitude;
55  
56      /** Indicator for required NMEA. */
57      private final boolean nmeaRequired;
58  
59      /** Indicator for networked streams. */
60      private final boolean networked;
61  
62      /** Authentication method. */
63      private final Authentication authentication;
64  
65      /** Indicator for required fees. */
66      private final boolean fees;
67  
68      /** Bit rate. */
69      private int bitRate;
70  
71      /** Build a data stream record by parsing a source table line.
72       * @param line source table line
73       */
74      public DataStreamRecord(final String line) {
75          super(line);
76          this.format         = DataFormat.getDataFormat(getField(3));
77  
78          final String[] detailsFields = SEPARATOR.split(getField(4));
79          this.formatDetails           = new ArrayList<>(detailsFields.length);
80          for (final String field : detailsFields) {
81              if (!field.isEmpty()) {
82                  final Matcher matcher = PATTERN.matcher(field);
83                  if (matcher.matches() && matcher.start(2) >= 0) {
84                      formatDetails.add(new StreamedMessage(matcher.group(1),
85                                                            Integer.parseInt(matcher.group(2))));
86                  } else {
87                      formatDetails.add(new StreamedMessage(field, -1));
88                  }
89              }
90          }
91  
92          this.carrierPhase   = CarrierPhase.getCarrierPhase(getField(5));
93          this.systems        = Stream.
94                                of(getField(6).split("\\+")).
95                                map(k -> NavigationSystem.getNavigationSystem(k)).
96                                collect(Collectors.toList());
97          this.latitude       = Math.toRadians(Double.parseDouble(getField(9)));
98          this.longitude      = Math.toRadians(Double.parseDouble(getField(10)));
99          this.nmeaRequired   = Integer.parseInt(getField(11)) != 0;
100         this.networked      = Integer.parseInt(getField(12)) != 0;
101         this.authentication = Authentication.getAuthentication(getField(15));
102         this.fees           = getField(16).equals("Y");
103         this.bitRate        = Integer.parseInt(getField(17));
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public RecordType getRecordType() {
109         return RecordType.STR;
110     }
111 
112     /** Get the mount point.
113      * @return mount point
114      */
115     public String getMountPoint() {
116         return getField(1);
117     }
118 
119     /** Get the source identifier.
120      * @return source identifier
121      */
122     public String getSourceIdentifier() {
123         return getField(2);
124     }
125 
126     /** Get the data format.
127      * @return data format
128      */
129     public DataFormat getFormat() {
130         return format;
131     }
132 
133     /** Get the format details.
134      * @return format details
135      */
136     public List<StreamedMessage> getFormatDetails() {
137         return formatDetails;
138     }
139 
140     /** Get the carrier phase.
141      * @return carrier phase
142      */
143     public CarrierPhase getCarrierPhase() {
144         return carrierPhase;
145     }
146 
147     /** Get the navigation systems.
148      * @return navigation systems
149      */
150     public List<NavigationSystem> getNavigationSystems() {
151         return systems;
152     }
153 
154     /** Get the network.
155      * @return network
156      */
157     public String getNetwork() {
158         return getField(7);
159     }
160 
161     /** Get the country.
162      * @return country
163      */
164     public String getCountry() {
165         return getField(8);
166     }
167 
168     /** Get the latitude.
169      * @return latitude (rad)
170      */
171     public double getLatitude() {
172         return latitude;
173     }
174 
175     /** Get the longitude.
176      * @return longitude (rad)
177      */
178     public double getLongitude() {
179         return longitude;
180     }
181 
182     /** Check if NMEA message must be sent to caster.
183      * @return true if NMEA message must be sent to caster
184      */
185     public boolean isNMEARequired() {
186         return nmeaRequired;
187     }
188 
189     /** Check if the stream is generated from a network of stations.
190      * @return true if stream  is generated from a network of stations
191      */
192     public boolean isNetworked() {
193         return networked;
194     }
195 
196     /** Get the hardware or software generator.
197      * @return hardware or software generator
198      */
199     public String getGenerator() {
200         return getField(13);
201     }
202 
203     /** Get the compression/encryption algorithm applied.
204      * @return compression/encryption algorithm applied
205      */
206     public String getCompressionEncryption() {
207         return getField(14);
208     }
209 
210     /** Get the authentication method.
211      * @return authentication method
212      */
213     public Authentication getAuthentication() {
214         return authentication;
215     }
216 
217     /** Check if fees are required.
218      * @return true if fees are required
219      */
220     public boolean areFeesRequired() {
221         return fees;
222     }
223 
224     /** Get the bit rate.
225      * @return bit rate
226      */
227     public int getBitRate() {
228         return bitRate;
229     }
230 
231 }