Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule TermuxSmsCli.Fetcher do
- use GenServer
- defp fetch_int(filter_fn, minimum) do
- fetch_int(filter_fn, minimum, minimum)
- end
- defp fetch_int(filter_fn, minimum, count) do
- {reply, 0} = System.cmd("ssh", ["192.168.2.8", "-p", "8022", "termux-sms-list", "-l", "#{count}"])
- reply = Jason.decode!(reply)
- reply = Enum.filter(reply, filter_fn)
- if (Enum.count(reply) < minimum) do
- fetch_int(filter_fn, minimum, 2*count)
- else
- reply
- end
- end
- def start_link(opts) do
- GenServer.start_link(__MODULE__, :ok, opts)
- end
- def init(:ok) do
- {:ok, nil}
- end
- def handle_call({:fetch, count}, _From, nil) do
- {:reply, fetch_int(fn _ -> true end, count), nil}
- end
- def handle_call({:fetch_thread, count, tid}, _From, nil) do
- {:reply, fetch_int(fn %{"threadid" => tid_} -> tid == tid_ end, count), nil}
- end
- def fetch(pid, count) do
- GenServer.call(pid, {:fetch, count})
- end
- def fetch_thread(pid, count, tid) do
- GenServer.call(pid, {:fetch_thread, count, tid})
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement