update esppilight lib

content of the library is not filtered on 433mhz only
This commit is contained in:
Florian committed 2018-11-28 20:41:37 +01:00
1 parent a275e48ee1
commit 67dd7ac078
1030 files changed
+203335 -116

No files matched your search

+147
View File
@@ -0,0 +1,147 @@
/*
Copyright (C) 2013 CurlyMo
This file is part of pilight.
pilight is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
pilight is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pilight. If not, see <http://www.gnu.org/licenses/>
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ByteArrayInputStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.io.PrintStream;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.util.Collections;
import java.util.Enumeration;
import sun.net.*;
import java.net.*;
import java.util.Date;
public class process {
public static void main(String[] args) {
String line = null;
DatagramSocket ssdp = null;
PrintStream printStream = null;
BufferedReader bufferedReader = null;
int port = 0;
String server = null;
Socket socket = null;
String msg = "M-SEARCH * HTTP/1.1\r\n"
+ "Host:239.255.255.250:1900\r\n"
+ "ST:urn:schemas-upnp-org:service:pilight:1\r\n"
+ "Man:\"ssdp:discover\"\r\n"
+ "MX:3\r\n\r\n";
try {
Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces();
for(NetworkInterface netint : Collections.list(nets)) {
Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for(InetAddress inetAddress : Collections.list(inetAddresses)) {
if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
try {
ssdp = new DatagramSocket(new InetSocketAddress(inetAddress.getHostAddress().toString(), 0));
byte[] buff = msg.getBytes();
DatagramPacket sendPack = new DatagramPacket(buff, buff.length);
sendPack.setAddress(InetAddress.getByName("239.255.255.250"));
sendPack.setPort(1900);
try {
ssdp.send(sendPack);
ssdp.setSoTimeout(10);
boolean loop = true;
while(loop) {
DatagramPacket recvPack = new DatagramPacket(new byte[1024], 1024);
ssdp.receive(recvPack);
byte[] recvData = recvPack.getData();
InputStreamReader recvInput = new InputStreamReader(new ByteArrayInputStream(recvData), Charset.forName("UTF-8"));
StringBuilder recvOutput = new StringBuilder();
for(int value; (value = recvInput.read()) != -1;) {
recvOutput.append((char)value);
}
BufferedReader bufReader = new BufferedReader(new StringReader(recvOutput.toString()));
Pattern pattern = Pattern.compile("Location:([0-9.]+):(.*)");
while((line=bufReader.readLine()) != null) {
Matcher matcher = pattern.matcher(line);
if(matcher.matches()) {
server = matcher.group(1);
port = Integer.parseInt(matcher.group(2));
loop = false;
break;
}
}
}
} catch(SocketTimeoutException e) {
} catch(IOException e) {
System.out.println("no pilight ssdp connections found");
ssdp.close();
System.exit(0);
}
} catch(UnknownHostException e) {
}
}
}
}
} catch(SocketException e) {
}
if(server == null || port == 0) {
System.out.println("no pilight ssdp connections found");
System.exit(0);
}
try {
socket = new Socket();
socket.connect(new InetSocketAddress(server, port), 1000);
try {
if(printStream == null) {
printStream = new PrintStream(socket.getOutputStream(), false);
}
printStream.print("{\"action\":\"identify\",\"options\":{\"receiver\":1}}\n");
printStream.flush();
} catch(IOException e) {
System.out.println("failed to write messages to server");
}
int size = 0;
if(size == 0) {
size = 1025;
}
try {
if(bufferedReader == null) {
bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"), size);
}
int c = 0;
StringBuilder response = new StringBuilder();
if(bufferedReader.ready()) {
while((c = bufferedReader.read()) > 0) {
response.append((char)c);
}
System.out.println(response.toString());
}
} catch(IOException e) {
System.out.println("failed to receive messages from server");
}
} catch (UnknownHostException e) {
System.out.println("don't know about host");
} catch(IOException e) {
System.out.println("could not get I/O for connection");
}
}
}
+69
View File
@@ -0,0 +1,69 @@
#!/usr/bin/env php
<?php
/*
Copyright (C) 2013 CurlyMo
This file is part of pilight.
pilight is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
pilight is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pilight. If not, see <http://www.gnu.org/licenses/>
*/
$sMsg = "M-SEARCH * HTTP/1.1\r\n";
$sMsg .= "Host:239.255.255.250:1900\r\n";
$sMsg .= "ST:urn:schemas-upnp-org:service:pilight:1\r\n";
$sMsg .= "Man:\"ssdp:discover\"\r\n";
$sMsg .= "MX:3\r\n\r\n";
$sBuffer = NULL;
$sTmp = NULL;
$rSocket = socket_create(AF_INET, SOCK_DGRAM, 0);
socket_set_option($rSocket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>0, 'usec'=>10000));
$send_ret = socket_sendto($rSocket, $sMsg, 1024, 0, '239.255.255.250', 1900);
$aHosts = Array();
$x = 0;
while(@socket_recvfrom($rSocket, $sBuffer, 1024, MSG_WAITALL, $sTmp, $sTmp)) {
if(strpos($sBuffer, 'pilight') !== false) {
$aLines = explode("\r\n", $sBuffer);
foreach($aLines as $sLine) {
if(sscanf($sLine, "Location:%d.%d.%d.%d:%d\r\n", $ip1, $ip2, $ip3, $ip4, $port) > 0) {
$ip = $ip1.'.'.$ip2.'.'.$ip3.'.'.$ip4;
$aHosts[$x]['ip'] = $ip;
$aHosts[$x]['port'] = $port;
$x++;
}
}
}
}
socket_close($rSocket);
$sLine = '';
if(count($aHosts) > 0) {
$fp = fsockopen($aHosts[0]['ip'], $aHosts[0]['port'], $errno, $errdesc) or die ("Couldn't connect to server");
fputs($fp, '{"action":"identify","options":{"receiver":1}}');
$sLine = fgets($fp, 1024);
if($sLine == "{\"status\":\"success\"}\n") {
$iLen = 0;
$sLine = '';
while(true) {
$sLine .= fgets($fp, 1024);
$iLen = strlen($sLine);
if($iLen > 2 && ord($sLine[$iLen-1]) === 10 && ord($sLine[$iLen-2]) === 10) {
$sLine = substr($sLine, 0, -2);
echo $sLine."\n";
$sLine = '';
}
}
}
} else {
die("no pilight ssdp connections found\n");
}
?>
+65
View File
@@ -0,0 +1,65 @@
#!/usr/bin/env perl
#
# Copyright (C) 2013 CurlyMo
#
# This file is part of pilight.
#
# pilight is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# pilight is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pilight. If not, see <http://www.gnu.org/licenses/>
#
use IO::Socket;
socket(sock, PF_INET, SOCK_DGRAM, getprotobyname("udp")) or die "socket:$@";
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, 1) or die "setsockopt:$@";
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, pack('l!l!', 0, 10000)) or die "setsockopt:$@";
my $dest = sockaddr_in(1900, inet_aton('239.255.255.250'));
$msg = "M-SEARCH * HTTP/1.1\r\n";
$msg .= "Host:239.255.255.250:1900\r\n";
$msg .= "ST:urn:schemas-upnp-org:service:pilight:1\r\n";
$msg .= "Man:\"ssdp:discover\"\r\n";
$msg .= "MX:3\r\n\r\n";
my $ip = '';
my $port = '';
send(sock, $msg, 1024, $dest);
while(recv(sock, $data, 1024, $dest)) {
if(index($data, "pilight") != -1) {
foreach(split(/\r\n/, $data)) {
$_ =~ /Location:([0-9\.]+):([0-9]+)/ || next;
($ip,$port) = ($1,$2);
}
}
}
$socket = new IO::Socket::INET (
PeerHost => $ip,
PeerPort => $port,
Proto => 'tcp',
) or die "no pilight ssdp connections found\n";
$socket->send('{"action":"identify","options":{"receiver":1}}');
$socket->recv($recv_data, 1024);
my $text = '';
while(1) {
$text = '';
while(1) {
$socket->recv($recv_data, 1024);
$text .= $recv_data;
if($recv_data[(length $recv_data)-1] == '\n' || $recv_data[(length $recv_data)] == '\n') {
$text = substr $text, 0, -1;
last;
}
}
print $text;
}
+109
View File
@@ -0,0 +1,109 @@
#!/usr/bin/env python
#
# Copyright (C) 2017 pilino1234
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
"""Example Python 2 client for pilight daemon socket API
Runs a SSDP discovery for pilight daemons on the network, and connects to it.
Identifies as a "receiver" type client, and prints received messages until
terminated.
"""
import socket
import struct
import re
def discover(service, retries=1):
"""SSDP discovery function
:param service: The name of the SSDP service to run discovery for
:type service: str
:param retries: The number of discovery retries. Defaults to 1.
:type retries: int
"""
group = ("239.255.255.250", 1900)
message = "\r\n".join([
'M-SEARCH * HTTP/1.1',
'HOST: {0}:{1}'.format(*group),
'MAN: "ssdp:discover"',
'ST: {st}', 'MX: 3', '', ''])
ssdp_responses = {}
for i in range(retries):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO,
struct.pack('LL', 0, 10000))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto(message.format(st=service), group)
while True:
try:
ssdp_responses[i] = sock.recv(1024)
break
except socket.timeout:
break
except IOError:
print "no pilight ssdp connections found"
break
sock.close()
return ssdp_responses
if __name__ == "__main__":
RESPONSES = discover("urn:schemas-upnp-org:service:pilight:1")
if len(RESPONSES) > 0:
for resp in RESPONSES:
locationsrc = re.search('Location:([0-9.]+):(.*)',
str(RESPONSES[0]), re.IGNORECASE)
if locationsrc:
location = locationsrc.group(1)
port = locationsrc.group(2)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.setdefaulttimeout(0)
try:
s.connect((location, int(port)))
except ConnectionRefusedError:
# Connection was refused, try next SSDP response
print "connection refused"
continue
s.send('{"action": "identify", "options": {"core": 1}}')
text = ""
while True:
line = s.recv(1024)
text += line
if "\n\n" in line[-2:]:
text = text[:-2]
break
if text == '{"status":"success"}':
text = ""
while True:
line = s.recv(1024)
text += line
if "\n\n" in line[-2:]:
text = text[:-2]
for f in iter(text.splitlines()):
print f
text = ""
elif text == '{"status":"failure"}':
# connection failed, try next SSDP response
print "connection failed"
continue
s.close()
+108
View File
@@ -0,0 +1,108 @@
#!/usr/bin/env python
#
# Copyright (C) 2017 pilino1234
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
"""Example Python 3 client for pilight daemon socket API
Runs a SSDP discovery for pilight daemons on the network, and connects to it.
Identifies as a "receiver" type client, and prints received messages until
terminated.
"""
import socket
import struct
import re
def discover(service, retries=1):
"""SSDP discovery function
:param service: The name of the SSDP service to run discovery for
:type service: str
:param retries: The number of discovery retries. Defaults to 1.
:type retries: int
"""
group = ("239.255.255.250", 1900)
discover_message = "\r\n".join([
'M-SEARCH * HTTP/1.1',
'HOST: {0}:{1}'.format(*group),
'MAN: "ssdp:discover"',
'ST: {st}', 'MX: 3', '', '']).format(st=service)
ssdp_responses = {}
for i in range(retries):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM,
socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO,
struct.pack('LL', 0, 10000))
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto(bytes(discover_message, "utf-8"), group)
while True:
try:
ssdp_responses[i] = sock.recv(1024).decode("utf-8")
break
except socket.timeout:
break
except IOError:
print("no pilight ssdp connections found")
break
sock.close()
return ssdp_responses
if __name__ == "__main__":
RESPONSES = discover("urn:schemas-upnp-org:service:pilight:1")
if len(RESPONSES) > 0:
for resp in RESPONSES: # try all responses until one works
locationsrc = re.search("Location:([0-9.]+):(.*)",
str(RESPONSES[resp]), re.IGNORECASE)
if locationsrc:
location = locationsrc.group(1)
port = locationsrc.group(2)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((location, int(port)))
except ConnectionRefusedError:
# Connection was refused, try next SSDP response
print("connection refused")
continue
s.send(bytes('{"action": "identify", \
"options": {"receiver": 1}}', "utf-8"))
response = ""
while True:
line = s.recv(1024).decode("utf-8")
response += line
if "\n\n" in line[-2:]:
response = response[:-2]
break
if response == '{"status":"success"}':
message = ""
while True:
line = s.recv(1024).decode("utf-8")
message += line
if "\n\n" in line[-2:]:
message = message[:-2]
for f in iter(message.splitlines()):
print(f)
message = ""
elif response == '{"status":"failure"}':
# connection failed, try next SSDP response
print("connection failed")
continue