-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FIrst attempt at implmenting basic HTTP API request to toggle lights
- Loading branch information
Chris Culhane
committed
Jun 18, 2020
1 parent
f0f5023
commit affea6f
Showing
8 changed files
with
324 additions
and
73 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
- Scenes | ||
- Scene 1 | ||
- Scene 2 | ||
|
||
- Toggle All Lights | ||
- Set Light | ||
- Light 1 | ||
- Brightness | ||
- Colour Temp (K) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,32 @@ | ||
|
||
using Toybox.Communications; | ||
using Toybox.WatchUi; | ||
|
||
class ConnectIQLIFXDelegate extends WatchUi.BehaviorDelegate { | ||
|
||
function initialize() { | ||
BehaviorDelegate.initialize(); | ||
var notify; | ||
// Set up the callback to the view | ||
function initialize(handler) { | ||
WatchUi.BehaviorDelegate.initialize(); | ||
notify = handler; | ||
} | ||
|
||
// Handle menu button press | ||
function onMenu() { | ||
WatchUi.pushView(new Rez.Menus.MainMenu(), new ConnectIQLIFXMenuDelegate(), WatchUi.SLIDE_UP); | ||
makeRequest(); | ||
return true; | ||
} | ||
|
||
function onSelect() { | ||
makeRequest(); | ||
return true; | ||
} | ||
|
||
function makeRequest() { | ||
notify.invoke("Executing\nRequest"); | ||
var lifx_api; | ||
lifx_api = new LIFX_API(self.notify); | ||
lifx_api.power_toggle(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,46 @@ | ||
using Toybox.WatchUi; | ||
|
||
class ConnectIQLIFXView extends WatchUi.View { | ||
hidden var mMessage = "Press menu button"; | ||
hidden var mModel; | ||
|
||
function initialize() { | ||
View.initialize(); | ||
WatchUi.View.initialize(); | ||
} | ||
|
||
// Load your resources here | ||
function onLayout(dc) { | ||
setLayout(Rez.Layouts.MainLayout(dc)); | ||
mMessage = "Press menu or\nselect button"; | ||
} | ||
|
||
// Called when this View is brought to the foreground. Restore | ||
// the state of this View and prepare it to be shown. This includes | ||
// loading resources into memory. | ||
// Restore the state of the app and prepare the view to be shown | ||
function onShow() { | ||
} | ||
|
||
// Update the view | ||
function onUpdate(dc) { | ||
// Call the parent onUpdate function to redraw the layout | ||
View.onUpdate(dc); | ||
dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_BLACK); | ||
dc.clear(); | ||
dc.drawText(dc.getWidth()/2, dc.getHeight()/2, Graphics.FONT_MEDIUM, mMessage, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER); | ||
} | ||
|
||
// Called when this View is removed from the screen. Save the | ||
// state of this View here. This includes freeing resources from | ||
// memory. | ||
// state of your app here. | ||
function onHide() { | ||
} | ||
|
||
} | ||
function onReceive(args) { | ||
if (args instanceof Lang.String) { | ||
mMessage = args; | ||
} | ||
else if (args instanceof Dictionary) { | ||
// Print the arguments duplicated and returned by jsonplaceholder.typicode.com | ||
var keys = args.keys(); | ||
mMessage = ""; | ||
for( var i = 0; i < keys.size(); i++ ) { | ||
mMessage += Lang.format("$1$: $2$\n", [keys[i], args[keys[i]]]); | ||
} | ||
} | ||
WatchUi.requestUpdate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// API interface to LIFX servers | ||
|
||
|
||
using Toybox.Communications as comms; | ||
using Toybox.WatchUi; | ||
|
||
const API_TOKEN = "c7b6a7b2936032035025758fd1348d73e34a0e46b8601bee433466933c9b88a2"; | ||
const HEADERS = {"User-Agent"=> "connectIQ-LIFX/0.0.0", | ||
"Authorization"=> "Bearer " + API_TOKEN | ||
}; | ||
|
||
const TEST_SCENE_UUID = "67374067-1a0b-48b6-9120-900312077693"; | ||
|
||
class LIFX_API extends WatchUi.BehaviorDelegate { | ||
var notify; | ||
|
||
function initialize(handler) { | ||
WatchUi.BehaviorDelegate.initialize(); | ||
notify = handler; | ||
} | ||
|
||
function makeRequest() { | ||
notify.invoke("Executing\nRequest"); | ||
|
||
// Communications.makeWebRequest( | ||
// "https://jsonplaceholder.typicode.com/todos/115", | ||
// { | ||
// }, | ||
// { | ||
// "Content-Type" => Communications.REQUEST_CONTENT_TYPE_URL_ENCODED | ||
// }, | ||
// method(:onReceive) | ||
// ); | ||
self.power_toggle(); | ||
} | ||
|
||
// Receive the data from the web request | ||
function onReceive(responseCode, data) { | ||
System.println("Request completed with responseCode: " + responseCode.toString()); | ||
if (responseCode == 200 or responseCode == 207) { | ||
System.println("Request Successful, data below:"); | ||
System.println(data); | ||
notify.invoke(data); | ||
return data; | ||
} else { | ||
notify.invoke("Failed to load\nError: " + responseCode.toString()); | ||
System.println(data); | ||
return null; | ||
} | ||
} | ||
|
||
|
||
function power_toggle(){ | ||
// Just toggles the power state of ALL lights | ||
var url = "https://api.lifx.com/v1/lights/all/toggle"; | ||
System.println(HEADERS); | ||
var params = null; | ||
var options = { | ||
:method => comms.HTTP_REQUEST_METHOD_POST, | ||
:headers => HEADERS, | ||
:responseType => comms.HTTP_RESPONSE_CONTENT_TYPE_JSON | ||
}; | ||
var responseCallback = method(:onReceive); | ||
|
||
// Make the Communications.makeWebRequest() call | ||
comms.makeWebRequest(url, params, options, method(:onReceive)); | ||
return responseCallback; | ||
} | ||
|
||
function set_scene(scene_uuid) { | ||
var url_format = "https://api.lifx.com/v1/scenes/scene_id:$1$/activate"; // set the url | ||
var url = Lang.format(url_format, [scene_uuid]); | ||
System.println(url); | ||
|
||
var params = null; | ||
var options = { // set the options | ||
:method => Communications.HTTP_REQUEST_METHOD_PUT, // set HTTP method | ||
:headers => HEADERS, | ||
:responseType => comms.HTTP_RESPONSE_CONTENT_TYPE_JSON | ||
}; | ||
|
||
var responseCallback = method(:onReceive); // set responseCallback to | ||
// onReceive() method | ||
// Make the Communications.makeWebRequest() call | ||
comms.makeWebRequest(url, params, options, method(:onReceive)); | ||
return responseCallback; | ||
} | ||
} |