Skip to content

Instantly share code, notes, and snippets.

@jameswhite
Created August 25, 2016 21:21
Show Gist options
  • Save jameswhite/016968e25b2836aef6016a1bf5adae36 to your computer and use it in GitHub Desktop.
Save jameswhite/016968e25b2836aef6016a1bf5adae36 to your computer and use it in GitHub Desktop.
alias off='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"black\"}"'
alias blue='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"blue\"}"'
alias orange='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"orange\"}"'
alias yellow='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"yellow\"}"'
alias red='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"red\"}"'
alias on='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"white\"}"'
alias loop='mqtts bikeshed/hue@mqtt "{\"action\":\"effect\",\"effect\":\"colorloop\",\"lights\":\"all\"}"'
alias stop='mqtts bikeshed/hue@mqtt "{\"action\":\"effect\",\"effect\":\"none\",\"lights\":\"all\"}"'
alias redalert='mqtts bikeshed/hue@mqtt "{\"action\":\"color\",\"lights\":\"all\",\"color\":\"red\",\"alert\":\"lselect\"}"'
!mqtt publish bikeshed/hue {"action":"color","lights":"all","color":"blue"}
!mqtt publish bikeshed/hue {"action":"effect","effect":"colorloop","lights":"all"}
!mqtt publish bikeshed/hue {"action":"effect","effect":"none","lights":"all"}
!mqtt publish bikeshed/hue {"action":"color","lights":"all","color":"black"}
!mqtt publish bikeshed/hue {"action":"color","lights":"all","color":"white"}
!mqtt publish hecuba/hue {"action":"color","lights":"all","color":"blue"}
!mqtt publish hecuba/hue {"action":"effect","effect":"colorloop","lights":"all"}
!mqtt publish hecuba/hue {"action":"effect","effect":"none","lights":"all"}
!mqtt publish hecuba/hue {"action":"color","lights":"all","color":"black"}
!mqtt publish hecuba/hue {"action":"color","lights":"all","color":"white"}
@jameswhite
Copy link
Author

!/usr/bin/env perl

use JSON;
use Net::MQTT::Simple::SSL;

my $json = JSON->new->allow_nonref;
my $mqtt = Net::MQTT::Simple::SSL->new( "mqtt:8883",
{
SSL_ca_file => '/etc/ssl/certs/ca.crt',
SSL_cert_file => '/etc/ssl/certs/localhost.crt',
SSL_key_file => '/etc/ssl/private/localhost.ckey',
}
);

Derive our topic from our some/topic@hostname

my $topic_at_host = shift(@argv);
my ($topic,$host) = split(/@/,$topic_at_host);

Generate a random topic for reply

my @chars = ("A".."Z", "a".."z", "0".."9");
my $respond_topic;
$respond_topic .= $chars[rand @chars] for 1..16;

my $json_text = shift(@argv);
$json_hash = $json->decode($json_text);

print "$topic $host $respond_topic $json_text\n";

append a 'respond_topic':'mqtt/_random_respond_topic' to our user-provided message

$json_hash->{'respond_topic'} = "mqtt/$respond_topic@mqtt.hq.thebikeshed.io" unless(defined( $json_hash{'respond_topic'}));

$json_text = $json->encode($json_hash);

send the command

$mqtt->publish($topic => $json_text);

wait for our response

print "waitfor: mqtt/${respond_topic}\n";

$mqtt->run(

"mqtt/${respond_topic}" => sub{

($topic,$message)=@_;

print "[$topic] $message\n";

exit 0;

}

);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment