# wink.pl

Last modified by Richard Johnson on 2018/08/01 00:46
Export
Office Formats
Export as PDF
Other Formats

Select the pages to export:

select all/ none

Legend:
  Created Page
  Modified Extension Page
  Clean Extension Page

Export as HTML

    #!/usr/bin/perl -w
    #
    use Time::Local "timelocal";
    #use XML::Parser;
    #use XML::Simple;
    #use IO::Handle;
    use Data::Dumper;
    #use URI::Escape;
    use lib "/opt/local/lib/perl5/site_perl/5.12.4";
    use JSON;
    #require HTTP::Request;
    require LWP::UserAgent;

    my $host = '';

    #
    # Get my token
    #
    my $URL = $host . '/oauth2/token';
    my %data = ('client_id' => 'my_client_id_from wink',
                'client_secret' => 'my_client_secret_from_wink',
                'username' => 'my_wink_account_email',
                'password' => 'my_wink_account_password',
                'grant_type' => 'password');

    my $json = encode_json \%data;

    my $h = HTTP::Headers->new(
        Content_Length      => length($json),
        Content_Type        => 'application/json'
        );
    my $browser = LWP::UserAgent->new();
    my $r = HTTP::Request->new('POST', $URL, $h, $json);
    my $response = $browser->request($r);
    #die 'Error ' unless $response->is_success;

    # $browser->default_header(content_type => "application/json");

    #print $response;
    #print $response->content_type;
    #print $response->as_string;
    #print $response->content;

    # Parse the JSON response
    $resp = decode_json $response->content;
    #print Dumper($resp);
    #print $resp->{access_token}, "\n";
    #print $resp->{data}->{access_token}, "\n";

    #
    # Have my token, now get device list
    #

    $token = $resp->{access_token};
    #$refresh = $resp->{refresh_token};

    # Now create my new query with this access token
    #$URL = $host . '/users/me/wink_devices';
    $URL = $host . '/users/me/light_bulbs';
    # my %data = ('Authorization' => 'Bearer ' . $resp->{access_token});
    # my $json = encode_json \%data;
    #print "Getting device list, query is\n";
    # print $json;
    #print "\n";

    $h = HTTP::Headers->new(
        Content_Type        => 'application/json',
        Authorization => 'Bearer ' . $resp->{access_token}
        );
    $browser = LWP::UserAgent->new();
    $r = HTTP::Request->new('GET', $URL, $h);
    $response = $browser->request($r);

    #print "\n", "List of all devices:\n";
    #print "\n", $response->as_string;
    $resp = decode_json $response->content;
    #print Dumper($resp);

    $count = $resp->{pagination}->{count};
    #print "Count = ", $count;

    my $i = 0;
    for ($j = 0 ; $j <= $#ARGV ; $j++) {
        for ($i = 0 ; $i < $count ; $i++) {
            if ("$resp->{data}[$i]->{name}" eq "$ARGV[$j]") {
    #
    # Set "OFF" as 0 and "ON" as 1
    #
                $state = 2;
                $val = uc $ARGV[$j+1];
                if ($val eq "OFF") {$state = 0;}
                if ($val eq "ON") {$state = 1;}
                if ($state == 2) {
                    print "Must use 'ON' or 'OFF' specifically\n";
                    exit;
                }
                $id = $resp->{data}[$i]->{light_bulb_id};
    #
    # Set the powered state
    #

                $h = HTTP::Headers->new(
                    Content_Type        => 'application/json',
                    Authorization => 'Bearer ' . $token
                    );
                $browser = LWP::UserAgent->new();
                $URL = $host . '/light_bulbs/' . $id;
                %power = ('powered' => $state, 'brightness' => 1);
                %data2 = ('desired_state' => \%power);
                $json = encode_json \%data2;

                $r = HTTP::Request->new('PUT', $URL, $h, $json);
                $response = $browser->request($r);

    system("/bin/echo -n $ARGV[$j] $ARGV[$j+1] \" : \"  >> /usr/local/etc/wink.log");
    system("date >> /usr/local/etc/wink.log");

                goto next_arg;
            }                               # end "if"
        }                           # end "for $j"
      next_arg:
        $j++;
    }                               # end "for $i"
    exit 0;

0
Tags:
Created by Richard Johnson on 2018/08/01 00:46

- Comments (0)
- Attachments (0)
- History
- Information

# Applications

# Tips

Press the x+x+x+a key combo to toggle the Advanced mode and x+x+x+h to toggle the display of hidden pages.

# Need help?

If you need help with XWiki you can contact:

- Community Support
- Professional Support

XWiki Debian 13.10.2