99_andnotify.pm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package main;
  2. use JSON;
  3. use HTTP::Request;
  4. use LWP::UserAgent;
  5. use IO::Socket::SSL;
  6. use utf8;
  7. my @gets = ('dummy');
  8. sub
  9. andnotify_Initialize($)
  10. {
  11. my ($hash) = @_;
  12. $hash->{DefFn} = "andnotify_Define";
  13. $hash->{StateFn} = "andnotify_SetState";
  14. $hash->{SetFn} = "andnotify_Set";
  15. $hash->{AttrList} = "loglevel:0,1,2,3,4,5";
  16. }
  17. sub
  18. andnotify_SetState($$$$)
  19. {
  20. my ($hash, $tim, $vt, $val) = @_;
  21. $val = $1 if($val =~ m/^(.*) \d+$/);
  22. return "Undefined value $val" if(!defined($it_c2b{$val}));
  23. return undef;
  24. }
  25. sub
  26. andnotify_Define($$)
  27. {
  28. my ($hash, $def) = @_;
  29. my @args = split("[ \t]+", $def);
  30. if (int(@args) < 1)
  31. {
  32. return "energy_Define: too much arguments. Usage:\n" .
  33. "define <name> andnotify <apikey>";
  34. }
  35. $hash->{APIKEY} = $args[2];
  36. $hash->{REGIDS}= $args[3];
  37. $hash->{STATE} = 'Initialized';
  38. Log 3, "$hash->{NAME} APIKEY: $hash->{APIKEY} REGIDS: $hash->{REGIDS}";
  39. return undef;
  40. }
  41. sub andnotify_Set($@)
  42. {
  43. my $json = JSON->new->allow_nonref;
  44. my ($hash, @a) = @_;
  45. my $name = $hash->{NAME};
  46. my $apikey = $hash->{APIKEY};
  47. my $count = @a;
  48. my $arg = lc($a[1]);
  49. my $cont1 = ucfirst($arg);
  50. my $cont2 = "";
  51. my $cont3 = "";
  52. my $cont4 = "";
  53. my $cont5 = "";
  54. my $cont6 = "";
  55. my $cont7 = "";
  56. my $cont8 = "";
  57. my $cont9 = "";
  58. if (defined $a[2]) { $cont2 = $a[2]}
  59. if (defined $a[3]) { $cont3 = $a[3]}
  60. if (defined $a[4]) { $cont4 = $a[4]}
  61. if (defined $a[5]) { $cont5 = $a[5]}
  62. if (defined $a[6]) { $cont6 = $a[6]}
  63. if (defined $a[7]) { $cont7 = $a[7]}
  64. if (defined $a[8]) { $cont8 = $a[8]}
  65. if (defined $a[9]) { $cont9 = $a[9]}
  66. my $fullcmd="$a[2]";
  67. for (my $i=3;$i<$count;$i+=1){ $fullcmd="$fullcmd $a[$i]";}
  68. my @param = split(/\|/, $fullcmd);
  69. if ( $arg eq "regids" )
  70. {
  71. $hash->{REGIDS}=$fullcmd;
  72. Log 3, "AndNotify $hash->{NAME} SET REGIDS: $hash->{REGIDS}";
  73. }
  74. if ( $arg eq "send" )
  75. {
  76. my $client = LWP::UserAgent->new();
  77. my @registration_ids = split(/\|/, $hash->{REGIDS});
  78. my $unix_timestamp = time*1000;
  79. if ($param[5]<1)
  80. {
  81. $param[5]="0";
  82. }
  83. if (substr($param[0],0,5) eq "file:")
  84. {
  85. my @msg = split(/\ /, $param[0]);
  86. my @temp_string;
  87. my $document;
  88. my $iii=0;
  89. foreach (@msg){
  90. @temp_string = split(/\:/, $_);
  91. if ($iii eq 0)
  92. {
  93. my $file = $temp_string[1];
  94. $document = do {
  95. local $/ = undef;
  96. open my $fh, "<", $file or Log 3, "Notify -> could not open $file";
  97. <$fh>;
  98. };
  99. $iii=$iii+1;
  100. }
  101. else
  102. {
  103. $document =~ s/$temp_string[0]/$temp_string[1]/g;
  104. }
  105. }
  106. $param[0]=$document;
  107. }
  108. my $data = {
  109. registration_ids => [ "$registration_ids[0]", "$registration_ids[1]", "$registration_ids[2]", "$registration_ids[3]", "$registration_ids[4]", "$registration_ids[5]", "$registration_ids[6]", "$registration_ids[7]", "$registration_ids[8]", "$registration_ids[9]" ],
  110. data => {
  111. message => $param[0],
  112. tickerText => $param[1],
  113. contentTitle => $param[2],
  114. contentText => $param[3],
  115. timestamp => $unix_timestamp,
  116. icon => $param[4],
  117. customid => $param[5]
  118. }
  119. };
  120. my $req = HTTP::Request->new(POST => "https://android.googleapis.com/gcm/send");
  121. $req->header(Authorization => 'key='.$apikey);
  122. $req->header('Content-Type' => 'application/json; charset=UTF-8');
  123. $req->content($json->encode($data));
  124. $client->request($req);
  125. Log 3, "Notify gesendet";
  126. }
  127. if ( $arg eq "wake" )
  128. {
  129. my $client = LWP::UserAgent->new();
  130. my @registration_ids = split(/\|/, $hash->{REGIDS});
  131. my $unix_timestamp = time*1000;
  132. my $data = {
  133. registration_ids => [ "$registration_ids[0]", "$registration_ids[1]", "$registration_ids[2]", "$registration_ids[3]", "$registration_ids[4]", "$registration_ids[5]", "$registration_ids[6]", "$registration_ids[7]", "$registration_ids[8]", "$registration_ids[9]" ],
  134. data => {
  135. wake => 1
  136. }
  137. };
  138. my $req = HTTP::Request->new(POST => "https://android.googleapis.com/gcm/send");
  139. $req->header(Authorization => 'key='.$apikey);
  140. $req->header('Content-Type' => 'application/json; charset=UTF-8');
  141. $req->content($json->encode($data));
  142. $client->request($req);
  143. Log 3, "Notify wake gesendet";
  144. }
  145. if ( $arg eq "sleep" )
  146. {
  147. my $client = LWP::UserAgent->new();
  148. my @registration_ids = split(/\|/, $hash->{REGIDS});
  149. my $unix_timestamp = time*1000;
  150. my $data = {
  151. registration_ids => [ "$registration_ids[0]", "$registration_ids[1]", "$registration_ids[2]", "$registration_ids[3]", "$registration_ids[4]", "$registration_ids[5]", "$registration_ids[6]", "$registration_ids[7]", "$registration_ids[8]", "$registration_ids[9]" ],
  152. data => {
  153. sleep => 1
  154. }
  155. };
  156. my $req = HTTP::Request->new(POST => "https://android.googleapis.com/gcm/send");
  157. $req->header(Authorization => 'key='.$apikey);
  158. $req->header('Content-Type' => 'application/json; charset=UTF-8');
  159. $req->content($json->encode($data));
  160. $client->request($req);
  161. Log 3, "Notify sleep gesendet";
  162. }
  163. }