Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(finders_lib).
- -export([
- get_finders/0,
- set_finder_configuration/1,
- batch_export_finders/0,
- batch_export_finders/1,
- batch_import_finders/0,
- batch_import_finders/1
- ]).
- -define(DB_FINDERS_TABLE, "finders").
- -define(USB_PATH, "/media/usb0/").
- -define(FINDERNAME_START, "finder_").
- -define(FINDERNAME_END, ".fnd").
- % Retrieve finders data from the database
- get_finders() ->
- Finders = boss_db:find_by_sql(finder, "SELECT * FROM " ++ ?DB_FINDERS_TABLE),
- prepare_finders_data_for_output(Finders).
- % Save finder configuration
- % Example input:
- % finders_lib:set_finder_configuration(
- % {finder_type, <<"onh">>},
- % {eye, <<"od">>},
- % {fixation_label, <<"central">>},
- % {x_pix, 900},
- % {y_pix, 700},
- % {radius_pix, 300},
- % {color, [
- % {red, 255},
- % {green, 255},
- % {blue, 0}
- % ]
- % }
- % }
- set_finder_configuration(InputConfig) ->
- lager:notice("~p: Setting finder configuration = ~p", [?MODULE, InputConfig]),
- Type = proplists:get_value(finder_type, InputConfig),
- Eye = proplists:get_value(eye, InputConfig),
- FixationLabel = proplists:get_value(fixation_label, InputConfig),
- FixationTargetId = case boss_db:find(fixation_target, [{label, equals, FixationLabel}], [{limit, 1}]) of
- [FixationTarget] ->
- lager:notice("~p: Fixation target ID = ~p", [?MODULE, FixationTarget:id()]),
- FixationTarget:id();
- _ ->
- lager:notice("~p: default fixation target id", [?MODULE]),
- default_finder_fixation_target_id()
- end,
- InputFinder = case boss_db:find(finder, [{finder_type, equals, Type}, {eye, equals, Eye}, {fixation_target_id, equals, FixationTargetId}]) of
- [Finder] ->
- Finder;
- _ ->
- undefined
- end,
- FinderId = update_or_create_finder(InputConfig, InputFinder),
- {set_finder_configuration, ok}.
- batch_export_finders() ->
- batch_export_finders(?USB_PATH).
- batch_export_finders(BasePath) ->
- ExportPath = format_path_with_slash(BasePath),
- Finders = boss_db:find_by_sql(finder, "SELECT * FROM " ++ ?DB_FINDERS_TABLE),
- lager:notice("~p: Exporting on ~p finders ~p", [?MODULE, BasePath, Finders]),
- Result = lists:map(fun(Finder) ->
- FixationLabel = get_fixation_label(Finder:fixation_target_id()),
- ExportFile = ExportPath ++ ?FINDERNAME_START ++ lists:flatten(io_lib:format("~s", [gsd_utils:sanitize(Finder:finder_type())])) ++ "_" ++
- lists:flatten(io_lib:format("~s", [gsd_utils:sanitize(Finder:eye())])) ++ "_" ++
- lists:flatten(io_lib:format("~s", [gsd_utils:sanitize(FixationLabel)])) ++ ?FINDERNAME_END,
- lager:notice("~p: File name = ~p", [?MODULE, ExportFile]),
- OutputData = serialize_finder_data(Finder),
- case file:write_file(ExportFile, OutputData) of
- ok -> ok;
- _ -> error
- end
- end, Finders),
- os:cmd("sync"),
- lager:notice("~p: Export completed", [?MODULE]),
- {export_protocols, Result}.
- batch_import_finders() ->
- batch_import_finders(?USB_PATH).
- batch_import_finders(BasePath) ->
- FileNames = gsd_file_lib:list_filter_files(?USB_PATH, ?FINDERNAME_START, ?FINDERNAME_END),
- lager:notice("~p: Importing from ~p finders ~p", [?MODULE, BasePath, FileNames]),
- Result = lists:map( fun(FileName) ->
- case file:read_file(FileName) of
- {ok, FileContent} ->
- try deserialize_data(FileContent) of
- _ ->
- case set_finder_configuration(deserialize_data(FileContent)) of
- {set_finder_configuration, ok} ->
- ok;
- _ ->
- error
- end
- catch
- Exception:Reason ->
- lager:notice("~p: Exception ~p, with the following reason ~p", [?MODULE, Exception, Reason]),
- error
- end;
- _ ->
- error
- end
- end, FileNames),
- lager:notice("~p: Import completed", [?MODULE]),
- {batch_import_finders, Result}.
- %%%%%%%%%%%%%%%%%%%%%%%%%
- % PRIVATE SECTION %
- %%%%%%%%%%%%%%%%%%%%%%%%%
- % Updates an existing finder with the provided data or creates a new one if InputFinder is undefined
- update_or_create_finder(FinderConfig, InputFinder) ->
- RecordData = prepare_finder_input_data(FinderConfig),
- Id = case InputFinder of
- undefined ->
- lager:notice("~p: New finder will be created", [?MODULE]),
- NewRecord = boss_record:new(finder, RecordData),
- case NewRecord:save() of
- {ok, Record} ->
- Record:id(),
- ok;
- Error ->
- lager:notice("~p: Error saving record ~p", [?MODULE, Error]),
- error
- end;
- _ ->
- lager:notice("~p: Finder ~p will be updated", [?MODULE, InputFinder]),
- UpdatedFinder = InputFinder:set(RecordData),
- {ok, _} = UpdatedFinder:save(),
- InputFinder:id(),
- ok
- end,
- Id.
- % Parses input data to be later used to create or update a finder
- prepare_finder_input_data(Data) ->
- FixationLabel = proplists:get_value(fixation_label, Data),
- lager:notice("~p: Fixation label = ~p", [?MODULE, FixationLabel]),
- FixationTargetId = case boss_db:find(fixation_target, [{label, equals, FixationLabel}], [{limit, 1}]) of
- [FixationTarget] ->
- lager:notice("~p: Fixation target ID = ~p", [?MODULE, FixationTarget:id()]),
- FixationTarget:id();
- _ ->
- lager:notice("~p: default fixation target id", [?MODULE]),
- default_finder_fixation_target_id()
- end,
- Color = proplists:get_value(color, Data),
- [
- {finder_type, proplists:get_value(finder_type, Data)},
- {eye, proplists:get_value(eye, Data)},
- {fixation_target_id, FixationTargetId},
- {x_pix, proplists:get_value(x_pix, Data)},
- {y_pix, proplists:get_value(y_pix, Data)},
- {radius_pix, proplists:get_value(radius_pix, Data)},
- {red, proplists:get_value(red, Color)},
- {green, proplists:get_value(green, Color)},
- {blue, proplists:get_value(blue, Color)}
- ].
- % Prepares the finders' data for output
- prepare_finders_data_for_output(Finders) ->
- lists:foldl(fun(F, Acc) ->
- FixationLabel = get_fixation_label(F:fixation_target_id()),
- Acc ++
- [[
- {id, F:id()},
- {finder_type, F:finder_type()},
- {eye, F:eye()},
- {fixation_label, FixationLabel},
- {x_pix, F:x_pix()},
- {y_pix, F:y_pix()},
- {radius_pix, F:radius_pix()},
- {color, [
- {red, F:red()},
- {green, F:green()},
- {blue, F:blue()}
- ]}
- ]] end, [], Finders).
- % Retrieves default fixation target for finder (central fixation)
- default_finder_fixation_target_id() ->
- FixationTarget = boss_db:find_first(fixation_target, [{label, equals, "central"}]),
- FixationTarget:id().
- % Retrieves default fixation label for finder (central fixation)
- default_finder_fixation_label() ->
- FixationTarget = boss_db:find_first(fixation_target, [{label, equals, "central"}]),
- FixationTarget:label().
- % Retrieves fixation label from the fixation target id
- get_fixation_label(FixationTargetId) ->
- FixationLabel = case boss_db:find(fixation_target, [{id, equals, FixationTargetId}], [{limit, 1}]) of
- [FixationTarget] ->
- FixationTarget:label();
- _ ->
- default_finder_fixation_label()
- end.
- format_path_with_slash(InputPath) ->
- case string:right(InputPath, 1) of
- "/" ->
- InputPath;
- _ ->
- InputPath ++ "/"
- end.
- % Returnes binary-serialized finder data
- serialize_finder_data(F) ->
- FixationLabel = get_fixation_label(F:fixation_target_id()),
- Data =
- [[
- {finder_type, F:finder_type()},
- {eye, F:eye()},
- {fixation_label, FixationLabel},
- {x_pix, F:x_pix()},
- {y_pix, F:y_pix()},
- {radius_pix, F:radius_pix()},
- {color, [
- {red, F:red()},
- {green, F:green()},
- {blue, F:blue()}
- ]}
- ]],
- serialize_data(Data).
- % Generic serialization of data
- serialize_data(Data) ->
- erlang:term_to_binary(Data).
- % Receives binary-serialized input and returns erlang-deserialized data
- deserialize_data(Input) ->
- erlang:binary_to_term(Input).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement