Skip to content

Commit

Permalink
FIrst attempt at implmenting basic HTTP API request to toggle lights
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Culhane committed Jun 18, 2020
1 parent f0f5023 commit affea6f
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 73 deletions.
Binary file modified bin/ConnectIQ-LIFX.prg
Binary file not shown.
228 changes: 173 additions & 55 deletions bin/ConnectIQ-LIFX.prg.debug.xml

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions docs/menu requirements.md
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)
6 changes: 4 additions & 2 deletions manifest.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<!-- This is a generated file. It is highly recommended that you DO NOT edit this file. --><iq:manifest xmlns:iq="http://www.garmin.com/xml/connectiq" version="3">
<iq:application entry="ConnectIQLIFXApp" id="ac12596f-0a08-4c97-b4dc-53da24b6ea77" launcherIcon="@Drawables.LauncherIcon" name="@Strings.AppName" type="watch-app" version="0.0.0">
<iq:application entry="ConnectIQLIFXApp" id="ac12596f0a084c97b4dc53da24b6ea77" launcherIcon="@Drawables.LauncherIcon" minSdkVersion="3.0.0" name="@Strings.AppName" type="watch-app" version="0.0.0">
<iq:products>
<iq:product id="fr935"/>
</iq:products>
<iq:permissions/>
<iq:permissions>
<iq:uses-permission id="Communications"/>
</iq:permissions>
<iq:languages>
<iq:language>eng</iq:language>
</iq:languages>
Expand Down
5 changes: 3 additions & 2 deletions source/ConnectIQLIFXApp.mc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Toybox.Application;
using Toybox.WatchUi;

class ConnectIQLIFXApp extends Application.AppBase {

hidden var mView;
function initialize() {
AppBase.initialize();
}
Expand All @@ -17,7 +17,8 @@ class ConnectIQLIFXApp extends Application.AppBase {

// Return the initial view of your application here
function getInitialView() {
return [ new ConnectIQLIFXView(), new ConnectIQLIFXDelegate() ];
mView = new ConnectIQLIFXView();
return [mView, new ConnectIQLIFXDelegate(mView.method(:onReceive))];
}

}
26 changes: 22 additions & 4 deletions source/ConnectIQLIFXDelegate.mc
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();
}


}
34 changes: 24 additions & 10 deletions source/ConnectIQLIFXView.mc
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();
}
}
88 changes: 88 additions & 0 deletions source/LIFX_API.mc
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;
}
}

0 comments on commit affea6f

Please sign in to comment.