-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.ex
51 lines (42 loc) · 987 Bytes
/
serial.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
defmodule Panacea.Serial do
use WebSockex
@ip_address "localhost:8001"
#############
# Interface #
#############
def start_link(_) do
try do
connect()
rescue
_ ->
IO.puts("Serial server not running on #{@ip_address}. Starting it...")
{_, os_type} = :os.type()
python_file =
case os_type do
:linux ->
"./priv/python/webSocketServerForSerialCommunication.py"
_ ->
".\\priv\\python\\webSocketServerForSerialCommunication.py"
end
Task.start(
fn ->
System.cmd("python", [python_file])
end
)
:timer.sleep(1000)
connect()
end
end
def write(message) do
WebSockex.send_frame(__MODULE__, {:text, message})
end
defp connect() do
{:ok, pid} = WebSockex.start_link(
"ws://" <> @ip_address,
__MODULE__,
%{},
name: __MODULE__
)
{:ok, pid}
end
end