Author Topic: Lifx Bulb Scrape.Put  (Read 5887 times)

0 Members and 1 Guest are viewing this topic.

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Lifx Bulb Scrape.Put
« on: October 11, 2015, 08:10:44 AM »
Hi All,

I could do with some help getting my Lifx bulb to work.
You can control using an HTTP API. http://api.developer.lifx.com/docs/set-state

This is my entry with my api key changed.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.8-->
<command id="872" name="Lifx HTTP" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Scrape.Put</cmdType>
    <params>
      <param>https://api.lifx.com/v1/lights/all/state</param>
      <param>{"power": "on"}</param>
      <param>xxxxxxxxxbd1fba4f168349edc221477e00153769c417xxxxxxxxxx</param>
      <param />
      <param>application/json</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

I have double checked the key.
This is my error with key changed
Code: [Select]
11/10/2015 12:05:49 381 doCommand:Lifx HTTP
11/10/2015 12:05:49 381 action repeat set to: 1
11/10/2015 12:05:49 381 Action:  Scrape.Put - https://api.lifx.com/v1/lights/all/state&&{"power": "on"}&&xxxxxxxxa4f168349edc221477e00153769c4178e79470fc6d4fa02b0cb&&&&application/json
11/10/2015 12:05:49 381 [action] Scrape.Put:https://api.lifx.com/v1/lights/all/state&&{"power": "on"}&&xxxxxxxa4f168349edc221477e00153769c4178e79470fc6d4fa02b0cb&&&&application/json

11/10/2015 12:05:49 390 Doing scrape put/post with headers:
Content-Type: application/json


11/10/2015 12:05:50 121 Action ERROR: Web exception: The remote server returned an error: (401) Unauthorized.
{"error":"Token required"}

Any assistance would be greatly appreciated.

Best wishes.

James.

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #1 on: October 11, 2015, 08:30:02 AM »
Hi All,

I could do with some help getting my Lifx bulb to work.
You can control using an HTTP API. http://api.developer.lifx.com/docs/set-state

This is my entry with my api key changed.


The basic syntax for your command looks correct based on the documentation LIFX provides. The error you're getting suggests there's a problem with the access token you're using.

You mention that you're using your "API key". Is it possible you're using an API key instead of the access token, or are those the same thing?

Per the site:
Quote
All requests require an OAuth 2 access token. You can generate an access token in your account settings.
http://api.developer.lifx.com/docs/authentication
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #2 on: October 11, 2015, 08:55:40 AM »
Thank you for your quick reply.

Sorry. I used the wrong terminology API Key = Token.
Just tried creating a new token and used it in the "Try it out" section of http://api.developer.lifx.com/docs/set-state and it works.
Then just copied into my LCB.

Best wishes.

James.


jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #3 on: October 11, 2015, 09:16:01 AM »
Try putting something (anything) in the password field. 

The reason I suggest this is that VC looks at the username and password and if either of them is blank it won't add the credentials to the request.  I guess this was based on an erroneous assumption on my part.  Either that or this api is using credentials incorrectly.  In any case I will update the next release so that credentials are sent as long as there is a username specified.
« Last Edit: October 11, 2015, 09:38:14 AM by jitterjames »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #4 on: October 11, 2015, 09:51:05 AM »
If that doesn't work you can try adding the authorization as a header as follows:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.0.9-->
<command id="-1" name="Actions copied from LCB">
  <action>
    <cmdType>Scrape.Put</cmdType>
    <params>
      <param>https://api.lifx.com/v1/lights/all/state</param>
      <param>{"power": "on"}</param>
      <param />
      <param />
      <param>application/json</param>
      <param>Authorization: Bearer YourAccessToken</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #5 on: October 11, 2015, 09:56:22 AM »
Thank you for your suggestion.

That did not appear to work.

However I did try on my Linux machine the suggested cURL command and I am getting what looks like a valid response. So I am happy that I have the correct token in my LCB

Code: [Select]
pi@raspberrypi ~ $ curl -u "xxxxxxxxx1fba4f168349edc221477e00153769c4178e79470fc6d4fa02b0cb:" "https://api.lifx.com/v1/lights/all"
[
  {
    "id": "d073d5025942",
    "uuid": "026cb890-5f61-49c2-afba-a88285bbdae1",
    "label": "LIFX bulb 025942",
    "connected": true,
    "power": "off",
    "color": {
      "hue": 286.3520256351568,
      "saturation": 1.0,
      "kelvin": 3500
    },
    "brightness": 0.5112687876707103,
    "group": {
      "id": "c67ba9229b6029e8c12ae0c60bc0c02a",
      "name": "My Room"
    },
    "location": {
      "id": "61be619a9d4f4524a94ea00df35c2691",
      "name": "My Home"
    },
    "product": {
      "name": "Original 1000",
      "identifier": "lifx_original_a21",
      "company": "LIFX",
      "capabilities": {
        "has_color": true,
        "has_variable_color_temp": true
      }
    },
    "last_seen": "2015-10-11T12:38:47.754+00:00",
    "seconds_since_seen": 0.001937142
  }
]pi@raspberrypi ~ $

I have xxxx'd out my token.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #6 on: October 11, 2015, 09:57:44 AM »
I suggested two different things.

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #7 on: October 11, 2015, 10:00:01 AM »
Yes sorry. Mails got crossed. I'll try your header.

Best wishes.

James.

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #8 on: October 11, 2015, 10:05:52 AM »
Yep. Adding the authorisation as a header worked.

Thank you very much.


Best wishes.


James.

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #9 on: October 11, 2015, 11:19:54 AM »
Great. Thanks for reporting back.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #10 on: January 03, 2016, 09:57:51 AM »
Hello again.

Whilst this now works very well within Voxcommando. There are some times when I am working at my computer and I don't have my Amulet near by. Is it possible to run a Voxcommando command from within a Windows *.bat file ie from the Windows command line.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.8-->
<command id="-1" name="Actions copied from LCB">
  <action>
    <cmdType>Scrape.Put</cmdType>
    <params>
      <param>https://api.lifx.com/v1/scenes/scene_id:xxx....................REMOVED/activate</param>
      <param />
      <param />
      <param />
      <param>application/json</param>
      <param>Authorization: Bearer xxxxxxx....................REMOVED</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Best wishes for 2016

James.

VC SP 2.1.4.8  Windows 10
« Last Edit: January 03, 2016, 01:29:07 PM by nime5ter »

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #11 on: January 03, 2016, 11:50:17 AM »
Hi Foggy,

Your best bet is probably to generate an event which can be used to trigger the command.  There are a few ways to do this.  You could use the simple web server which is part of the TCP plugin, or you can use TellVoxCommando.exe if you want to be able to test different things without using a microphone.

If you want to send actions or events to VoxCommando from the command line the best way to do that is probably to use the UDPSender.exe command line utility which you can download as part of the "Extra utilities" available on the download page.

For more info and examples see:

http://voxcommando.com/mediawiki/index.php?title=API_Application_Programming_Interface

http://voxcommando.com/forum/index.php?topic=414.msg2815#msg2815

By the way, I think it is usually better to copy and paste commands from the command tree editor instead of the LCB (macro editor), that way we can see the complete commands with the input phrases, payloads, events etc.  In this case it probably doesn't really matter but I thought I should mention it for future reference.

Foggy

  • $upporter
  • Contributor
  • *****
  • Posts: 86
  • Karma: 4
    • View Profile
Re: Lifx Bulb Scrape.Put
« Reply #12 on: January 03, 2016, 12:54:56 PM »
Many thanks James.

Thank you for the links. I think UDPSender.exe is worth a look at.
Looking through the forum link you gave I am confused if I should use (Send actions or events) or if I can call the action /command directly?

If it can. Can you assist with the syntax? I would move the UDPSender.exe into c:/
Please see corrected command.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.1.4.8-->
<command id="34" name="http Scene" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="" loopMax="" description="">
  <action>
    <cmdType>Scrape.Put</cmdType>
    <params>
      <param>https://api.lifx.com/v1/scenes/scene_id:12d754....................REMOVED/activate</param>
      <param />
      <param />
      <param />
      <param>application/json</param>
      <param>Authorization: Bearer c6f4a4e1bd1fba4....................REMOVED</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
</command>

Your assistance is appreciated.

Best wishes.
« Last Edit: January 03, 2016, 01:20:14 PM by jitterjames »

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 1999
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #13 on: January 03, 2016, 01:09:27 PM »
How do you currently trigger this command?

Although you could duplicate that exact action in your UDP command line message, the usual way is to send the voice command or event name for the command.

Below, I've created a voice command and an event trigger for your command. Either of these could be used in your command line message:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.2.1.4-->
<command id="34" name="http Scene" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="" loopMax="" description="">
  <action>
    <cmdType>Scrape.Put</cmdType>
    <params>
      <param>https://api.lifx.com/v1/scenes/scene_id:12d75....................REMOVED/activate</param>
      <param />
      <param />
      <param />
      <param>application/json</param>
      <param>Authorization: Bearer c6f4a4....................REMOVED</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>trigger Lif X scene</phrase>
  <event>lifx-scene</event>
</command>

command line option 1:
Code: [Select]
udpsender.exe 33000 "vc.triggerevent&&lifx-scene"
option 2:
Code: [Select]
udpsender.exe 33000 "vc.tellvox&&trigger Lif X scene"
You could also re-create the syntax of that specific Scrape.Put action, but with all the parameters and long strings it seems prone to typos.

« Last Edit: January 03, 2016, 01:19:50 PM by jitterjames »
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Lifx Bulb Scrape.Put
« Reply #14 on: January 03, 2016, 01:14:27 PM »
Although you can execute actions directly using the VC API you cannot execute commands directly.  In order to execute a command via the API you will either need to trigger it using an EVENT or using the VC.TellVox action which allows you to emulate a voice command.

I think the easiest way is to trigger an event.  Once you get the UDPSender working you should see the event appearing in your history window.  Then just click edit and drag the event from the history window to the command in your Command Tree Editor.  Save and close the Command Tree Editor and then the event will trigger the command.
« Last Edit: January 03, 2016, 01:16:32 PM by jitterjames »