Index: README.md ================================================================== --- README.md +++ README.md @@ -16,11 +16,11 @@ Since I wrote this to be semi-useful for me, the result returned depends on the time of the day. If it's run before 8am it looks for the 6am weather data (since data is in 3 hour periods) and assumes the route is being traversed normally, run between 8am and 7pm it looks for the 6pm data for going home and therefore also traverses the saved route in reverse, and run after that time it looks again for the 6am data, but for the next day, and thus the route is back to being traversed in the normal direction. _Hint:_ I have a shell function defined as follows: function headsilose { - erl -pa /home/simon/Code/github/atomicules/heads-I-lose /home/simon/Code/github/atomicules/heads-I-lose/ebin -run headsilose headsilose XXXXXX -noshell; + erl -pa /home/simon/Code/github/atomicules/heads-I-lose /home/simon/Code/github/atomicules/heads-I-lose/ebin -run headsilose headsilose XXXXXX alternative_geometries -noshell; } So I can just call headsilose Index: headsilose.erl ================================================================== --- headsilose.erl +++ headsilose.erl @@ -1,11 +1,11 @@ -module(headsilose). -export([get_locations/0, get_locations/1, headsilose/1]). -include_lib("xmerl/include/xmerl.hrl"). -import(weather_types, [weather_type/1]). -import(polyline, [decode/1]). --import(osrm, [read_route/0]). +-import(osrm, [read_route/1]). %Supply a direction and location and work out if head wind or not %For now "know the location id" upfront, but ideally need to search for it at some point or present a choice. %Initially based on: http://pragdave.pragprog.com/pragdave/2007/04/a_first_erlang_.html @@ -229,17 +229,26 @@ tailwind end. %Something like that? -headsilose(Location) -> +%First two for command line usage +headsilose([Location]) -> + headsilose_(Location); +headsilose([Location, Route_choice]) -> + headsilose_(Location, Route_choice). +headsilose_(Location) -> + %If route choice not specified default to default! + %The other choice is "alternative_geometries", for now I think there is only ever one alternative so pick this first. + headsilose_(Location, "route_geometry"). +headsilose_(Location, Route_choice) -> Date_today = erlang:localtime(), { Date_formatted, Rep } = date_and_rep(Date_today), {Direction, Speed, Gust, Weather, Temperature} = get_weather(Location, { Date_formatted, Rep }), Weather_type = weather_types:weather_type(erlang:list_to_integer(Weather)), [Headwinds, Sidewinds, Tailwinds] = build_list_of_wind_directions(Direction), - {_Checksum, Polyline} = osrm:read_route(), + {_Checksum, Polyline} = osrm:read_route(Route_choice), Polyline_decoded = polyline:decode(Polyline), Distances_and_headings_list = convert_lats_longs_to_distance_heading(Polyline_decoded), %A better representation than 360 or 1080 would be better now this is used here as well. Journey = if Rep =:= "360" -> journey(Distances_and_headings_list); Index: osrm.erl ================================================================== --- osrm.erl +++ osrm.erl @@ -1,7 +1,7 @@ -module(osrm). --export([get_route/1, get_route/2, read_route/0]). +-export([get_route/1, get_route/2, read_route/1]). -import(polyline, [decode/1]). %https://github.com/Project-OSRM/osrm-backend/wiki/Server-api %For now, get weather for one location (probably good enough as relatively short distances weather wise; ultimately consider time as well?) %To get lats and longs could also do a query for here: http://www.uk-postcodes.com/ (json again) @@ -36,16 +36,22 @@ after maybe_quit() end. -read_route() -> +read_route(Route_choice) -> {_Status, Route} = file:read_file(os:getenv("HOME") ++ "/.headsilose_route"), %Use jiffy %http://www.snip2code.com/Snippet/51463/how-to-support-chinese-in-http-request-b/ { Props } = jiffy:decode(Route), - Route_geometry = proplists:get_value(<<"route_geometry">>, Props), + Route_geometry = if Route_choice =:= "route_geometry" -> + proplists:get_value(binary:list_to_bin(Route_choice), Props); + Route_choice =:= "alternative_geometries" -> + %hd only if alternative though! + %for now I think there is only ever one alternative so that is why we pick hd + hd(proplists:get_value(binary:list_to_bin(Route_choice), Props)) + end, %That is all for now? Because... %And these don't seem to actually be returned, hence having to go down the route of polyline decoding %Route_Instructions = proplists:get_value(<<"route_instructions">>, Props), %Total_Distance = proplists:get_value(<<"total_distance">>, proplists:get_value(<<"route_summary">>, Props)), %And need to figure out how to get nested values. I.e like xpath. Just nest the queries? Nope that doesn't work