diff --git a/src/main/lemondronor/circlebot.cljs b/src/main/lemondronor/circlebot.cljs index c378344..6e4ac5d 100644 --- a/src/main/lemondronor/circlebot.cljs +++ b/src/main/lemondronor/circlebot.cljs @@ -36,7 +36,7 @@ :lat (numstr (e "lat")) :lon (numstr (e "lon")) :icao (e "icao") - :registration (e "reg") + :registration (nilstr (e "reg")) :alt (numstr (e "alt")) :mlat? (= (e "mlat") "1") :speed (numstr (e "spd")) @@ -308,7 +308,16 @@ (defn to-fixed [n d] (.toFixed n d)) -(defn generate-description [ac sqb reverse wiki-nearby nearby] + +;; Creates a template expansion map from the following: +;; +;; * ac - The ADSBX API entry +;; * sqb - The baseseation.sqb record +;; * reverse - reverse geocoder record +;; * wiki-nearby - nearby landmarks w/ Wikipedia pages +;; * nearby - nearby landmarks. + +(defn template-data [ac sqb reverse wiki-nearby nearby] (let [rev-props (:properties reverse) nearby (:properties (first nearby)) wiki-nearby (:properties (first wiki-nearby)) @@ -331,7 +340,12 @@ (= (:registration ac) (:callsign ac)) (dissoc :callsign) ;; TODO: If layer is "county", find the nearest city. - ) + )] + info)) + + +(defn generate-description [ac sqb reverse wiki-nearby nearby] + (let [info (template-data ac sqb reverse wiki-nearby nearby) expansion (expand-template info)] (:text expansion))) diff --git a/src/test/lemondronor/circlebot_test.cljs b/src/test/lemondronor/circlebot_test.cljs index 9ac125b..dde19f1 100644 --- a/src/test/lemondronor/circlebot_test.cljs +++ b/src/test/lemondronor/circlebot_test.cljs @@ -94,7 +94,7 @@ :history [{:time 3500 :lat 1 :lon 1}]}}))))) -(deftest generation +(deftest expand-template (let [data {:locality "Palmdale", :continent "North America", :military? true, :alt 3850, :speed "209", :normalized-curviness 14.768651250300287, :accuracy "centroid", :country_a "USA", :continent_gid "whosonfirst:continent:102191575", :name "Palmdale", :squawk "5330", :icao "AE1482", :county_a "LO", :county "Los Angeles County", :source "whosonfirst", :gid "whosonfirst:locality:85923493", :curviness 1269.8089810739468, :locality_gid "whosonfirst:locality:85923493", :region "California", :militaryicao "AE1482", :region_a "CA", :nearbydistance 8.167, :callsign "RAIDR49", :layer "locality", :mlat? false, :country_gid "whosonfirst:country:85633793", :label "Palmdale, CA, USA", :id "85923493", :lon -118.00375, :region_gid "whosonfirst:region:85688637", :lat 34.661074, :militaryregistration "166765", :county_gid "whosonfirst:county:102086957", :started-circling-time 1576266715691, :distance 6.855, :source_id "85923493", :registration "166765", :confidence 0.5, :country "United States", :postime 1576266689756, :nearbylandmark "Living Faith Foursquare Church"}] (is (re-find #"military" (-> (circlebot/expand-template data) :text)))) (let [data {:locality "Palmdale", :continent "North America", :military? true, :alt 3200, :speed "161", :normalized-curviness 15.783422690487765, :accuracy "centroid", :country_a "USA", :continent_gid "whosonfirst:continent:102191575", :name "Palmdale", :squawk "5330", :icao "AE1482", :county_a "LO", :county "Los Angeles County", :source "whosonfirst", :gid "whosonfirst:locality:85923493", :curviness 1098.803548060181, :locality_gid "whosonfirst:locality:85923493", :region "California", :militaryicao "AE1482", :region_a "CA", :nearbydistance 7.828, :callsign "RAIDR49", :layer "locality", :mlat? false, :country_gid "whosonfirst:country:85633793", :label "Palmdale, CA, USA", :id "85923493", :lon -118.049183, :region_gid "whosonfirst:region:85688637", :lat 34.649808, :militaryregistration "166765", :county_gid "whosonfirst:county:102086957", :started-circling-time 1576267564959, :distance 6.336, :source_id "85923493", :registration "166765", :confidence 0.5, :country "United States", :postime 1576267555709, :nearbylandmark "Living Faith Foursquare Church"}] @@ -104,3 +104,34 @@ (is (re-find #"an Airbus" (:text (circlebot/expand-template data))))) (let [data {:registration "TEST" :type "Yoyo 380" :locality "Test City"}] (is (re-find #"a Yoyo" (:text (circlebot/expand-template data))))))) + + +(deftest generate-description + (testing "Basic generation" + (let [ac {:icao "B00B00" + :registration "NBADB0Y"} + sqb {:Registration "NGOODB0Y" + :Type "B52"} + reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}} + wiki-nearby {} + nearby [] + desc (circlebot/generate-description ac sqb reverse wiki-nearby nearby)] + (is (= desc "NBADB0Y, a B52, is circling over Silver Lake, Los Angeles #NBADB0Y")))) + (testing "Missing ADSBX registration" + (let [ac {:icao "B00B00"} + sqb {:Registration "NGOODB0Y" + :Type "B52"} + reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}} + wiki-nearby {} + nearby {:name "Disneyland" :distance 2} + desc (circlebot/generate-description ac sqb reverse wiki-nearby nearby)] + (is (= desc "NGOODB0Y, a B52, is circling over Silver Lake, Los Angeles #NGOODB0Y")))) + (testing "foo" + (let [ac {:military? false :alt 1300 :speed 72.1 :squawk "1200" + :icao "AAE0C2" :type nil, :callsign "N80NT", :registration nil} + sqb {:Registration "N80NT", :Type "Eurocopter Squirrel AS 350 B2"} + reverse {:properties {:neighbourhood "Silver Lake" :locality "Los Angeles"}} + wiki-nearby {} + nearby {:name "Disneyland" :distance 2} + desc (circlebot/generate-description ac sqb reverse wiki-nearby nearby)] + (is (= desc "N80NT, a Eurocopter Squirrel AS 350 B2, (callsign N80NT) is circling over Silver Lake, Los Angeles at 1300 feet, speed 83 MPH, squawking 1200, #N80NT")))))