diff --git a/src/main/lemondronor/advisorycircular.cljs b/src/main/lemondronor/advisorycircular.cljs index 2c67e3d..78308fd 100644 --- a/src/main/lemondronor/advisorycircular.cljs +++ b/src/main/lemondronor/advisorycircular.cljs @@ -110,14 +110,22 @@ (defn adsbx-url [{:keys [url lat lon radius-nm rapid-api?]}] - (let [url-pieces [url - "lat" lat - "lon" lon - "dist" (.toFixed radius-nm (if rapid-api? 0 1))] - url-str (->> url-pieces - (map str) - (string/join "/"))] - url-str)) + (if (or lat lon radius-nm) + (do + (when-not (and lat lon radius-nm) + (throw (js/Error. "Need all of :lat, :lon, and :radius-nm"))) + (let [url-pieces [url + "lat" lat + "lon" lon + "dist" (.toFixed radius-nm (if rapid-api? 0 1))] + url-str (->> url-pieces + (map str) + (string/join "/"))] + url-str)) + (do + (when rapid-api? + (throw (js/Error. "Must specify :lat, :lon, and :radius-nm with RapidAPI"))) + (str url "/all")))) (defn adsbx-headers [{:keys [url api-key api-whitelist rapid-api?]}] @@ -153,6 +161,20 @@ updated)) +;; Filters out aircraft that aren't in the keep list--unless the keep +;; list is empty, in which case we don't do any filtering. +(defn keep-specified-icaos [live-data keep-list] + (if (seq keep-list) + (let [keep-list (set (map string/upper-case keep-list))] + (update + live-data + :aircraft + (fn [aircraft] + (filter #(keep-list (string/upper-case (:icao %))) + aircraft)))) + live-data)) + + ;; Given a vector of position history, removes old entries. (defn prune-history [history now config] @@ -872,6 +894,7 @@ :radius-nm (* (:radius-km config) 0.539957) :rapid-api? (:rapid-api? config)})) filtered-api-data (remove-blocked-icaos api-data (get config :icao-blocklist '())) + filtered-api-data (keep-specified-icaos filtered-api-data (get config :icaos '())) now (current-time) [new-db potential-circles] (-> db (update-history-db (:aircraft filtered-api-data) now config) diff --git a/src/test/lemondronor/advisorycircular_test.cljs b/src/test/lemondronor/advisorycircular_test.cljs index 3ee52b9..f1100a0 100644 --- a/src/test/lemondronor/advisorycircular_test.cljs +++ b/src/test/lemondronor/advisorycircular_test.cljs @@ -473,7 +473,21 @@ :lon 1 :radius-nm 2 :rapid-api? true}) - "http://adsbx/lat/0/lon/1/dist/2"))) + "http://adsbx/lat/0/lon/1/dist/2")) + (is (thrown-with-msg? + js/Error #"Need all of :lat, :lon, and :radius-nm" + (advisorycircular/adsbx-url + {:url "http://adsbx" + :lat 0 + :rapid-api? true}))) + (is (= (advisorycircular/adsbx-url + {:url "http://adsbx"}) + "http://adsbx/all")) + (is (thrown-with-msg? + js/Error #"Must specify.*lat.*RapidAPI" + (advisorycircular/adsbx-url + {:url "http://adsbx" + :rapid-api? true})))) (deftest adsbx-headers @@ -494,3 +508,21 @@ {:x-rapidapi-key "key" :x-rapidapi-host "adsbx" :useQueryString true}))) + + +(deftest remove-blocked-icaos + (let [aircraft {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}] + (is (= (advisorycircular/remove-blocked-icaos aircraft '()) + {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]})) + (is (= (advisorycircular/remove-blocked-icaos aircraft '("a.*")) + {:aircraft [{:icao "123"} {:icao "124"}]})) + (is (= (advisorycircular/remove-blocked-icaos aircraft '("a.*" "123")) + {:aircraft [{:icao "124"}]})))) + + +(deftest keep-specified-icaos + (let [aircraft {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]}] + (is (= (advisorycircular/keep-specified-icaos aircraft '()) + {:aircraft [{:icao "abc"} {:icao "ADEF"} {:icao "123"} {:icao "124"}]})) + (is (= (advisorycircular/keep-specified-icaos aircraft '("123" "adef")) + {:aircraft [{:icao "ADEF"} {:icao "123"}]}))))