Subversion Repositories HomeAutomation

Rev

Rev 2101 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 2101 Rev 2158
Line 13... Line 13...
13
        )
13
        )
14
        or die "Error: Cannot connect to port $port at $host\n";
14
        or die "Error: Cannot connect to port $port at $host\n";
15
 
15
 
16
    return $socket;
16
    return $socket;
17
}
17
}
-
 
18
 
-
 
19
# ----------------- Atom JS functions -----------------
-
 
20
sub atomjs_read_line
-
 
21
{
-
 
22
    ($socket) = @_;
-
 
23
    # read line
-
 
24
    defined( $result = <$socket> ) or die "Readline failed: $! \n";
-
 
25
   
-
 
26
    return $result;
-
 
27
}
-
 
28
 
-
 
29
sub atomjs_write
-
 
30
{
-
 
31
    ($socket, $command) = @_;
-
 
32
    print $socket $command;
-
 
33
}
-
 
34
 
-
 
35
#function atomjs_data_available($socket)
-
 
36
#{
-
 
37
#   $read   = array($socket);
-
 
38
#   $write  = NULL;
-
 
39
#   $except = NULL;
-
 
40
   
-
 
41
#   if (false === ($num_changed_streams = stream_select($read, $write, $except, 0)))
-
 
42
#   {
-
 
43
#       throw new Exception("could not do select on socket");
-
 
44
#   }
-
 
45
 
-
 
46
#   return $num_changed_streams > 0;
-
 
47
#}
-
 
48
 
-
 
49
 
-
 
50
# ----------------- Atomic functions -----------------
18
 
51
 
19
sub atomd_read_packet
52
sub atomd_read_packet
20
{
53
{
21
    ($socket) = @_;
54
    ($socket) = @_;
22
    $command="";
55
    $command="";
Line 71... Line 104...
71
    return $has_data;
104
    return $has_data;
72
}
105
}
73
 
106
 
74
sub atomd_kill_promt
107
sub atomd_kill_promt
75
{
108
{
76
    ($socket) = @_;
109
    ($socket) = @_;
77
 
110
 
78
    while (atomd_data_available($socket))
111
    while (atomd_data_available($socket))
79
    {
112
    {
80
        $packet = atomd_read_packet($socket); # Read prompt
113
        $packet = atomd_read_packet($socket); # Read prompt
81
    }
114
    }
82
}
115
}
83
 
116
 
84
sub atomd_initialize
117
sub atomd_initialize
85
{
118
{
86
    ($host, $port) = @_;
119
    ($host, $port) = @_;
87
    $socket = atomd_connect($host, $port);
120
    $socket = atomd_connect($host, $port);
88
   
121
   
89
    atomd_kill_promt($socket);
122
    atomd_kill_promt($socket);
90
   
123
   
91
    return $socket;
124
    return $socket;
92
}
125
}
93
 
126
 
94
 
127
 
95
sub atomd_send_command
128
sub atomd_send_command
96
{
129
{
97
    ($socket, $command) = @_;
130
    ($socket, $command) = @_;
98
    atomd_write_packet($socket, "RESP", $command);
131
    atomd_write_packet($socket, "RESP", $command);
99
}
132
}
100
 
133
 
101
 
134
 
102
sub atomd_read_command_response
135
sub atomd_read_command_response
103
{
136
{
104
    ($socket) = @_;
137
    ($socket) = @_;
105
 
138
 
106
    $response = "";
139
    $response = "";
107
 
140
 
108
    while (1)
141
    while (1)
109
    {
142
    {
110
        $packet = atomd_read_packet($socket);
143
        $packet = atomd_read_packet($socket);
111
 
-
 
112
        if (substr($packet, 0, 4) ne "TEXT")
144
        if (substr($packet, 0, 4) ne "TEXT")
113
        {
145
        {
114
            last;
146
            last;
115
        }
147
        }
116
 
148