Skip to content

Commit 28b196d

Browse files
jjcarstensLivInAbsurdism
authored andcommitted
Add Jeff.file_transfer/3
This adds pieces to send file data to a device. It uses a functional core in `Jeff.Command.FileTransfer` to check each reply after a FILETRANSFER attempt to determine next steps for continuing the data transfer. The functional core allows for simpler testing
1 parent 91777da commit 28b196d

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/jeff.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ defmodule Jeff do
99
alias Jeff.MFG.Encoder
1010
alias Jeff.Reply
1111
alias Jeff.Reply.ErrorCode
12+
alias Jeff.{ACU, Command, Command.FileTransfer, Device, Reply}
1213

1314
@type acu() :: GenServer.server()
1415
@type device_opt() :: ACU.device_opt()
@@ -206,4 +207,31 @@ defmodule Jeff do
206207

207208
defp handle_reply({:ok, %{data: data}}), do: data
208209
defp handle_reply(err), do: err
210+
211+
@doc """
212+
Send file data to a PD
213+
"""
214+
@spec file_transfer(acu(), osdp_address(), binary()) ::
215+
Reply.FileTransferStatus.t() | Reply.ErrorCode.t()
216+
def file_transfer(acu, address, data) when is_binary(data) do
217+
with %{name: PDCAP, data: caps} <- ACU.send_command(acu, address, CAP) do
218+
max = caps[:receive_buffer_size] || 128
219+
220+
FileTransfer.command_set(data, max)
221+
|> run_file_transfer(acu, address)
222+
end
223+
end
224+
225+
defp run_file_transfer([cmd | rem], acu, address) do
226+
ACU.send_command(acu, address, FILETRANSFER, Map.to_list(cmd))
227+
|> FileTransfer.adjust_from_reply(rem)
228+
|> case do
229+
{:cont, next, delay} ->
230+
:timer.sleep(delay)
231+
run_file_transfer(next, acu, address)
232+
233+
{:halt, data} ->
234+
data
235+
end
236+
end
209237
end

0 commit comments

Comments
 (0)