# HG changeset patch # User virii # Date 2018-12-28 17:28:26 # Node ID fa0d2b72bc16c40a8e9858d86014a85e1621dd2a # Parent 8955d2ad00d14fb7a04d22bc706493b0d6bfc924 MQTT support added diff --git a/dash.pl b/dash.pl --- a/dash.pl +++ b/dash.pl @@ -6,77 +6,95 @@ use diagnostics; use feature qw(:all); use WWW::Curl::Easy; +use Net::MQTT::Simple; use JSON::Parse qw(:all); use Amazon::Dash::Button; my ($response_body, $info, $postfields, $space_status, $new_space_status); my ($status, $site, $user, $pass, $mac, $curl, $json, $dev, $timeout); +my ($mqtt, $topic, $message, $mqttuser, $mqttpass, $mqttIP, $mqttonly); + + +$status = "https://spaceapi.c3l.lu/status.json"; # SpaceAPI direct link to status json file +$site = "https://spaceapi.c3l.lu/open"; # SpaceAPI direct link to change the state +$user = ""; # SpaceAPI user +$pass = ""; # SpaceAPI pass +$mqttIP = "127.0.0.1"; # MQTT Server IP +$mqttuser = ""; # MQTT User +$mqttpass = ""; # MQTT Pass +$mqttonly = "True"; # MQTT Only modus active = not changing Space Status by myself +$mac = 'de:ad:be:ef:0a:0a'; # MAC of Dash button. +$timeout = 60; # Timeout in seconds. -$status = "https://spaceapi.c3l.lu/status.json"; # SpaceAPI direct link to status json file -$site = "https://spaceapi.c3l.lu/open"; # SpaceAPI direct link to change the state -$user = ""; # SpaceAPI user -$pass = ""; # SpaceAPI pass -$mac = 'de:ad:be:ef:0a:0a'; # MAC of Dash button. -$timeout = 60; # Timeout in seconds. +$ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'} = 1; # Needed for the MQTT Login +$mqtt = Net::MQTT::Simple->new($mqttIP); +$mqtt->last_will("availability/dashbutton", "offline", 1); # You may need to change the Topic +$mqtt->login($mqttuser, $mqttpass); +$mqtt->retain("availability/dashbutton", "online"); # You may need to change the Topic +say "[+] Connection to MQTT server established"; say "[+] Scanning network traffic"; -Amazon::Dash::Button->new( from => { # The author of the module says that you - mac => $mac, # can omit a "dev" option to use another - onClick => \&spaceapi, # network interface but actually it never - name => q{SpaceAPI}, # gets used in the code. See the provided - timeout => $timeout # patch to define a different interface. -} )->listen; # Patch changes default to eth0. +Amazon::Dash::Button->new( from => { # The author of the module says that you + mac => $mac, # can omit a "dev" option to use another + onClick => \&spaceapi, # network interface but actually it never + name => q{SpaceAPI}, # gets used in the code. See the provided + timeout => $timeout # patch to define a different interface. +} )->listen; # Patch changes default to eth0. sub spaceapi { say "[+] Button has been pushed"; +$mqtt->publish("space/status/trigger" => 1); # You may need to change the Topic +say "[+] MQTT message sent"; -$curl = WWW::Curl::Easy->new; # $curl = new WWW::Curl::Easy; +if ($mqttonly eq "False") { + $curl = WWW::Curl::Easy->new; -$curl->setopt(CURLOPT_URL, $status); -$curl->setopt(CURLOPT_VERBOSE,0); -$curl->setopt(CURLOPT_WRITEDATA,\$response_body); -$curl->perform; + $curl->setopt(CURLOPT_URL, $status); + $curl->setopt(CURLOPT_VERBOSE,0); + $curl->setopt(CURLOPT_WRITEDATA,\$response_body); + $curl->perform; -$info = $curl->getinfo(CURLINFO_RESPONSE_CODE); -say "[-] Server returned $info" and exit unless $info == 200; + $info = $curl->getinfo(CURLINFO_RESPONSE_CODE); + say "[-] Server returned $info" and exit unless $info == 200; -$json = valid_json($response_body); -say "[-] Malformed json!" and exit unless $json; -$json = parse_json($response_body); + $json = valid_json($response_body); + say "[-] Malformed json!" and exit unless $json; + $json = parse_json($response_body); -if ($json->{state}{open} and $json->{state}{message} =~ /members only/i) { - $space_status = 2; # Propably needed in the future - $new_space_status = "CLOSED"; - $postfields = "close=close&message="; -} elsif ($json->{state}{open}) { - $space_status = 1; # Propably needed in the future - $new_space_status = "CLOSED"; - $postfields = "close=close&message="; -} else { - $space_status = 0; # Propably needed in the future - $new_space_status = "OPEN"; - $postfields = "open=open&message="; -} + if ($json->{state}{open} and $json->{state}{message} =~ /members only/i) { + $space_status = 2; # Propably needed in the future + $new_space_status = "CLOSED"; + $postfields = "close=close&message="; + } elsif ($json->{state}{open}) { + $space_status = 1; # Propably needed in the future + $new_space_status = "CLOSED"; + $postfields = "close=close&message="; + } else { + $space_status = 0; # Propably needed in the future + $new_space_status = "OPEN"; + $postfields = "open=open&message="; + } -$curl->setopt(CURLOPT_URL, $site); -$curl->setopt(CURLOPT_VERBOSE,0); -$curl->setopt(CURLOPT_USERPWD,"$user:$pass"); -$curl->setopt(CURLOPT_COOKIEFILE, 'cookiescurl'); # Apparently you need this -$curl->setopt(CURLOPT_COOKIEJAR, '/tmp/cookiescurl'); # to make Digest Auth work -$curl->setopt(CURLOPT_HTTPAUTH,CURLAUTH_DIGEST); # ...sad! -$curl->setopt(CURLOPT_POSTFIELDS(), $postfields); + $curl->setopt(CURLOPT_URL, $site); + $curl->setopt(CURLOPT_VERBOSE,0); + $curl->setopt(CURLOPT_USERPWD,"$user:$pass"); + $curl->setopt(CURLOPT_COOKIEFILE, 'cookiescurl'); # Apparently you need this + $curl->setopt(CURLOPT_COOKIEJAR, '/tmp/cookiescurl'); # to make Digest Auth work + $curl->setopt(CURLOPT_HTTPAUTH,CURLAUTH_DIGEST); # ...sad! + $curl->setopt(CURLOPT_POSTFIELDS(), $postfields); -$curl->perform; + $curl->perform; -$info = $curl->getinfo(CURLINFO_RESPONSE_CODE); -say "[-] Server returned $info" and exit unless $info == 302||200; -say "[+] Changed Space Status to $new_space_status"; + $info = $curl->getinfo(CURLINFO_RESPONSE_CODE); + say "[-] Server returned $info" and exit unless $info == 302||200; + say "[+] Changed Space Status to $new_space_status"; +} }