Skip to content

Instantly share code, notes, and snippets.

@johnypony3
Last active January 11, 2017 22:48
Show Gist options
  • Save johnypony3/9b953747aac970bac9ad0a109de740dd to your computer and use it in GitHub Desktop.
Save johnypony3/9b953747aac970bac9ad0a109de740dd to your computer and use it in GitHub Desktop.
this is the content source directory for a fenix4 app posts to a restful api, which in turn does home automation
//
// Copyright 2015-2016 by Garmin Ltd. or its subsidiaries.
// Subject to Garmin SDK License Agreement and Wearables
// Application Developer Agreement.
//
using Toybox.Application as App;
class WebRequestApp extends App.AppBase {
hidden var mView;
function initialize() {
App.AppBase.initialize();
}
// onStart() is called on application start up
function onStart(state) {
}
// onStop() is called when your application is exiting
function onStop(state) {
}
// Return the initial view of your application here
function getInitialView() {
mView = new WebRequestView();
return [mView, new WebRequestDelegate(mView.method(:onReceive))];
}
}
//
// Copyright 2016 by Garmin Ltd. or its subsidiaries.
// Subject to Garmin SDK License Agreement and Wearables
// Application Developer Agreement.
//
using Toybox.Communications as Comm;
using Toybox.WatchUi as Ui;
using Toybox.System as Sys;
class MyMenuDelegate extends Ui.MenuInputDelegate {
function onMenuItem(item) {
if ( item == :item_1 ) {
Sys.println("LR Light On");
makeRequest("0", "true");
} else if ( item == :item_2 ) {
Sys.println("LR Light Off");
makeRequest("0", "false");
} else if ( item == :item_3 ) {
Sys.println("LR Lamp On");
makeRequest("2", "true");
} else if ( item == :item_4 ) {
Sys.println("LR Lamp Off");
makeRequest("2", "false");
} else if ( item == :item_5 ) {
Sys.println("OFC Lamp On");
makeRequest("4", "true");
} else if ( item == :item_6 ) {
Sys.println("OFC Lamp Off");
makeRequest("4", "false");
}
}
// Receive the data from the web request
function onReceive(responseCode, data) {
if (responseCode == 200) {
} else {
Sys.println("data = " + data);
}
}
function makeRequest(device, action) {
Sys.println(device);
Sys.println(action);
Comm.makeWebRequest(
"****API GATEWAY URL******" + device + "/" +action,
null,
{
:method => Comm.HTTP_REQUEST_METHOD_POST,
:headers => {
"Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
}
},
method(:onReceive)
);
}
}
class WebRequestDelegate extends Ui.BehaviorDelegate {
var notify;
// Handle menu button press
function onMenu() {
makeRequest();
return true;
}
function onSelect() {
makeRequest();
return true;
}
function makeRequest() {
notify.invoke("Executing\nRequest");
Comm.makeWebRequest(
"****API GATEWAY URL******",
null,
{
:method => Comm.HTTP_REQUEST_METHOD_POST,
:headers => {
"Content-Type" => Comm.REQUEST_CONTENT_TYPE_JSON
}
},
method(:onReceive)
);
}
// Set up the callback to the view
function initialize(handler) {
Ui.BehaviorDelegate.initialize();
notify = handler;
}
// Receive the data from the web request
function onReceive(responseCode, data) {
if (responseCode == 200) {
notify.invoke(data["args"]);
} else {
Sys.println("data = " + data);
notify.invoke("Failed to load\nError: " + responseCode.toString());
}
}
}
//
// Copyright 2015-2016 by Garmin Ltd. or its subsidiaries.
// Subject to Garmin SDK License Agreement and Wearables
// Application Developer Agreement.
//
using Toybox.WatchUi as Ui;
using Toybox.Graphics;
using Toybox.System as Sys;
class WebRequestView extends Ui.View {
hidden var mMessage = "Press menu button";
hidden var mModel;
function initialize() {
Ui.View.initialize();
}
// Load your resources here
function onLayout(dc) {
mMessage = "Press menu or\nselect button";
}
// Restore the state of the app and prepare the view to be shown
function onShow() {
}
// Update the view
function onUpdate(dc) {
openTheMenu();
//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 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 httpbin.org
var keys = args.keys();
mMessage = "";
for( var i = 0; i < keys.size(); i++ ) {
mMessage += Lang.format("$1$: $2$\n", [keys[i], args[keys[i]]]);
}
}
Ui.requestUpdate();
}
function openTheMenu() {
Ui.pushView( new Rez.Menus.MainMenu(), new MyMenuDelegate(), Ui.SLIDE_UP );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment