-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path30_SNS.pm
79 lines (62 loc) · 1.7 KB
/
30_SNS.pm
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main;
use strict;
use warnings;
use Amazon::SNS;
sub SNS_Initialize($$)
{
my ($hash) = @_;
$hash->{SetFn} = "SNS_Set";
$hash->{DefFn} = "SNS_Define";
$hash->{AttrList} = "setList key secret";
$hash->{AttrFn} = "SNS_Attr";
}
sub SNS_Define($$)
{
my ($hash, $def) = @_;
my ($name, $type, $arn) = split("[ \t]+", $def);
if (! $arn =~ m/^arn:aws:sns:.*/) {
my $error = "SNS ARN must be of the form 'arn:aws:sns:<region>:<account>:<topic>'";
Log(1, $error);
return $error;
}
my @arn_parts = split(':', $arn);
my $region = $arn_parts[3];
my $sns = Amazon::SNS->new({'key' => undef, 'secret' => undef});
$sns->service('http://sns.'.$region.'.amazonaws.com');
$hash->{fhem}{arn} = $arn;
$hash->{fhem}{sns} = $sns;
$hash->{fhem}{topic} = $arn;
$hash->{STATE} = 'Defined';
return undef;
}
sub SNS_Set($$@)
{
my ($hash, $name, @msg) = @_;
my $sns = $hash->{fhem}{sns};
my $topic = $sns->GetTopic($hash->{fhem}{topic});
my $msg = join(" ", @msg);
my $ok = $topic->Publish($msg);
if (!defined($ok)) {
Log(1, "$name: Failed to Publish '".$msg."' to ".$topic->arn);
Log(1, "$name: ".$sns->error);
}
return undef;
}
sub SNS_Attr(@)
{
my ($cmd, $name, $attrName, $attrVal) = @_;
if($cmd eq "set") {
my $hash = $defs{$name};
my $sns = $hash->{fhem}{sns};
if ($attrName eq 'key') {
$sns->key($attrVal);
}
elsif ($attrName eq 'secret') {
$sns->secret($attrVal);
}
if (defined($sns->secret) && defined($sns->secret)) {
$hash->{STATE} = 'Ready to Send';
}
}
}
1;