1 /*
2 * Copyright (C) 2018 Alberto Irurueta Carro (alberto@irurueta.com)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package com.irurueta.navigation.indoor;
17
18 /**
19 * Contains a ranging reading associated to a given radio source (e.g. Wi-Fi
20 * access point or bluetooth beacon), indicating the distance to such source.
21 *
22 * @param <S> a {@link RadioSource} type.
23 */
24 public class RangingReading<S extends RadioSource> extends Reading<S> {
25
26 /**
27 * Default number of measurements.
28 */
29 public static final int DEFAULT_NUM_MEASUREMENTS = 1;
30
31 /**
32 * Distance in meters to the radio source.
33 */
34 private double distance;
35
36 /**
37 * Standard deviation of distance, if available.
38 */
39 private Double distanceStandardDeviation;
40
41 /**
42 * Number of attempted measurements used in the RTT exchange.
43 */
44 private int numAttemptedMeasurements = DEFAULT_NUM_MEASUREMENTS;
45
46 /**
47 * Number of successful measurements used to calculate the distance and standard
48 * deviation.
49 */
50 private int numSuccessfulMeasurements = DEFAULT_NUM_MEASUREMENTS;
51
52 /**
53 * Constructor.
54 *
55 * @param source radio source associated to this reading.
56 * @param distance distance in meters to the radio source.
57 * @throws IllegalArgumentException if radio source data is null or distance is negative.
58 */
59 public RangingReading(final S source, final double distance) {
60 super(source);
61
62 if (distance < 0.0) {
63 throw new IllegalArgumentException();
64 }
65
66 this.distance = distance;
67 }
68
69 /**
70 * Constructor.
71 *
72 * @param source radio source associated to this reading.
73 * @param distance distance in meters to the radio source.
74 * @param numAttemptedMeasurements number of attempted measurements used in the RTT exchange.
75 * @param numSuccessfulMeasurements number of successful measurements used to calculate the
76 * distance and standard deviation.
77 * @throws IllegalArgumentException if radio source data is null, distance is negative,
78 * number of attempted measures is less than 1 or number of successful measures is negative.
79 */
80 public RangingReading(
81 final S source, final double distance, final int numAttemptedMeasurements,
82 final int numSuccessfulMeasurements) {
83 this(source, distance);
84
85 if (numAttemptedMeasurements < DEFAULT_NUM_MEASUREMENTS || numSuccessfulMeasurements < 0) {
86 throw new IllegalArgumentException();
87 }
88
89 this.numAttemptedMeasurements = numAttemptedMeasurements;
90 this.numSuccessfulMeasurements = numSuccessfulMeasurements;
91 }
92
93 /**
94 * Constructor.
95 *
96 * @param source radio source associated to this reading.
97 * @param distance distance in meters to the radio source.
98 * @param distanceStandardDeviation standard deviation of distance, if available.
99 * @throws IllegalArgumentException if radio source data is null, distance is negative or
100 * standard deviation is zero or negative.
101 */
102 public RangingReading(
103 final S source, final double distance, final Double distanceStandardDeviation) {
104 this(source, distance);
105
106 if (distanceStandardDeviation != null && distanceStandardDeviation <= 0.0) {
107 throw new IllegalArgumentException();
108 }
109
110 this.distanceStandardDeviation = distanceStandardDeviation;
111 }
112
113 /**
114 * Constructor.
115 *
116 * @param source radio source associated to this reading.
117 * @param distance distance in meters to the radio source.
118 * @param distanceStandardDeviation standard deviation of distance, if available.
119 * @param numAttemptedMeasurements number of attempted measurements used in the RTT exchange.
120 * @param numSuccessfulMeasurements number of successful measurements used to calculate the
121 * distance and standard deviation.
122 * @throws IllegalArgumentException if radio source data is null, distance is negative or
123 * standard deviation is zero or negative, number of attempted measures is less
124 * than 1 or number of successful measures is negative.
125 */
126 public RangingReading(
127 final S source, final double distance, final Double distanceStandardDeviation,
128 final int numAttemptedMeasurements, final int numSuccessfulMeasurements) {
129 this(source, distance, distanceStandardDeviation);
130
131 if (numAttemptedMeasurements < DEFAULT_NUM_MEASUREMENTS || numSuccessfulMeasurements < 0) {
132 throw new IllegalArgumentException();
133 }
134
135 this.numAttemptedMeasurements = numAttemptedMeasurements;
136 this.numSuccessfulMeasurements = numSuccessfulMeasurements;
137 }
138
139 /**
140 * Empty constructor.
141 */
142 protected RangingReading() {
143 super();
144 }
145
146 /**
147 * Contains radio source reading type.
148 *
149 * @return reading type.
150 */
151 @Override
152 public ReadingType getType() {
153 return ReadingType.RANGING_READING;
154 }
155
156 /**
157 * Gets distance in meters to the radio source.
158 *
159 * @return distance in meters to the radio source.
160 */
161 public double getDistance() {
162 return distance;
163 }
164
165 /**
166 * Gets standard deviation of distance, if available.
167 *
168 * @return standard deviation of distance or null.
169 */
170 public Double getDistanceStandardDeviation() {
171 return distanceStandardDeviation;
172 }
173
174 /**
175 * Gets number of attempted measurements used in the RTT exchange.
176 *
177 * @return number of attempted measurements used in the RTT exchange.
178 */
179 public int getNumAttemptedMeasurements() {
180 return numAttemptedMeasurements;
181 }
182
183 /**
184 * Gets number of successful measurements used to calculate the distance and
185 * standard deviation.
186 *
187 * @return number of successful measurements used to calculate the distance and
188 * standard deviation.
189 */
190 public int getNumSuccessfulMeasurements() {
191 return numSuccessfulMeasurements;
192 }
193 }