Wiki source code of wink.pl
Last modified by Richard Johnson on 2018/08/01 00:46
Show last authors
| author | version | line-number | content |
|---|---|---|---|
| 1 | {{{#!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | use Time::Local "timelocal"; | ||
| 4 | #use XML::Parser; | ||
| 5 | #use XML::Simple; | ||
| 6 | #use IO::Handle; | ||
| 7 | use Data::Dumper; | ||
| 8 | #use URI::Escape; | ||
| 9 | use lib "/opt/local/lib/perl5/site_perl/5.12.4"; | ||
| 10 | use JSON; | ||
| 11 | #require HTTP::Request; | ||
| 12 | require LWP::UserAgent; | ||
| 13 | |||
| 14 | my $host = ''; | ||
| 15 | |||
| 16 | # | ||
| 17 | # Get my token | ||
| 18 | # | ||
| 19 | my $URL = $host . '/oauth2/token'; | ||
| 20 | my %data = ('client_id' => 'my_client_id_from wink', | ||
| 21 | 'client_secret' => 'my_client_secret_from_wink', | ||
| 22 | 'username' => 'my_wink_account_email', | ||
| 23 | 'password' => 'my_wink_account_password', | ||
| 24 | 'grant_type' => 'password'); | ||
| 25 | |||
| 26 | my $json = encode_json \%data; | ||
| 27 | |||
| 28 | my $h = HTTP::Headers->new( | ||
| 29 | Content_Length => length($json), | ||
| 30 | Content_Type => 'application/json' | ||
| 31 | ); | ||
| 32 | my $browser = LWP::UserAgent->new(); | ||
| 33 | my $r = HTTP::Request->new('POST', $URL, $h, $json); | ||
| 34 | my $response = $browser->request($r); | ||
| 35 | #die 'Error ' unless $response->is_success; | ||
| 36 | |||
| 37 | # $browser->default_header(content_type => "application/json"); | ||
| 38 | |||
| 39 | #print $response; | ||
| 40 | #print $response->content_type; | ||
| 41 | #print $response->as_string; | ||
| 42 | #print $response->content; | ||
| 43 | |||
| 44 | # Parse the JSON response | ||
| 45 | $resp = decode_json $response->content; | ||
| 46 | #print Dumper($resp); | ||
| 47 | #print $resp->{access_token}, "\n"; | ||
| 48 | #print $resp->{data}->{access_token}, "\n"; | ||
| 49 | |||
| 50 | # | ||
| 51 | # Have my token, now get device list | ||
| 52 | # | ||
| 53 | |||
| 54 | $token = $resp->{access_token}; | ||
| 55 | #$refresh = $resp->{refresh_token}; | ||
| 56 | |||
| 57 | # Now create my new query with this access token | ||
| 58 | #$URL = $host . '/users/me/wink_devices'; | ||
| 59 | $URL = $host . '/users/me/light_bulbs'; | ||
| 60 | # my %data = ('Authorization' => 'Bearer ' . $resp->{access_token}); | ||
| 61 | # my $json = encode_json \%data; | ||
| 62 | #print "Getting device list, query is\n"; | ||
| 63 | # print $json; | ||
| 64 | #print "\n"; | ||
| 65 | |||
| 66 | $h = HTTP::Headers->new( | ||
| 67 | Content_Type => 'application/json', | ||
| 68 | Authorization => 'Bearer ' . $resp->{access_token} | ||
| 69 | ); | ||
| 70 | $browser = LWP::UserAgent->new(); | ||
| 71 | $r = HTTP::Request->new('GET', $URL, $h); | ||
| 72 | $response = $browser->request($r); | ||
| 73 | |||
| 74 | #print "\n", "List of all devices:\n"; | ||
| 75 | #print "\n", $response->as_string; | ||
| 76 | $resp = decode_json $response->content; | ||
| 77 | #print Dumper($resp); | ||
| 78 | |||
| 79 | $count = $resp->{pagination}->{count}; | ||
| 80 | #print "Count = ", $count; | ||
| 81 | |||
| 82 | my $i = 0; | ||
| 83 | for ($j = 0 ; $j <= $#ARGV ; $j++) { | ||
| 84 | for ($i = 0 ; $i < $count ; $i++) { | ||
| 85 | if ("$resp->{data}[$i]->{name}" eq "$ARGV[$j]") { | ||
| 86 | # | ||
| 87 | # Set "OFF" as 0 and "ON" as 1 | ||
| 88 | # | ||
| 89 | $state = 2; | ||
| 90 | $val = uc $ARGV[$j+1]; | ||
| 91 | if ($val eq "OFF") {$state = 0;} | ||
| 92 | if ($val eq "ON") {$state = 1;} | ||
| 93 | if ($state == 2) { | ||
| 94 | print "Must use 'ON' or 'OFF' specifically\n"; | ||
| 95 | exit; | ||
| 96 | } | ||
| 97 | $id = $resp->{data}[$i]->{light_bulb_id}; | ||
| 98 | # | ||
| 99 | # Set the powered state | ||
| 100 | # | ||
| 101 | |||
| 102 | $h = HTTP::Headers->new( | ||
| 103 | Content_Type => 'application/json', | ||
| 104 | Authorization => 'Bearer ' . $token | ||
| 105 | ); | ||
| 106 | $browser = LWP::UserAgent->new(); | ||
| 107 | $URL = $host . '/light_bulbs/' . $id; | ||
| 108 | %power = ('powered' => $state, 'brightness' => 1); | ||
| 109 | %data2 = ('desired_state' => \%power); | ||
| 110 | $json = encode_json \%data2; | ||
| 111 | |||
| 112 | $r = HTTP::Request->new('PUT', $URL, $h, $json); | ||
| 113 | $response = $browser->request($r); | ||
| 114 | |||
| 115 | system("/bin/echo -n $ARGV[$j] $ARGV[$j+1] \" : \" >> /usr/local/etc/wink.log"); | ||
| 116 | system("date >> /usr/local/etc/wink.log"); | ||
| 117 | |||
| 118 | goto next_arg; | ||
| 119 | } # end "if" | ||
| 120 | } # end "for $j" | ||
| 121 | next_arg: | ||
| 122 | $j++; | ||
| 123 | } # end "for $i" | ||
| 124 | exit 0;}}} |