Renamed class variables

There's a Monkey C convention to have class variable names start with 'm', then be camel case. 'm' for 'member' according to https://developer.garmin.com/connect-iq/reference-guides/monkey-c-reference/.
This commit is contained in:
Philip Abbey
2023-11-11 13:58:35 +00:00
parent e7c4411dd2
commit fde270ff34
9 changed files with 105 additions and 105 deletions

View File

@ -30,55 +30,55 @@ using Toybox.Timer;
const bRadius = 10; const bRadius = 10;
class Alert extends WatchUi.View { class Alert extends WatchUi.View {
hidden var timer; hidden var mTimer;
hidden var timeout; hidden var mTimeout;
hidden var text; hidden var mText;
hidden var font; hidden var mFont;
hidden var fgcolor; hidden var mFgcolor;
hidden var bgcolor; hidden var mBgcolor;
function initialize(params as Lang.Dictionary) { function initialize(params as Lang.Dictionary) {
View.initialize(); View.initialize();
text = params.get(:text); mText = params.get(:text);
if (text == null) { if (mText == null) {
text = "Alert"; mText = "Alert";
} }
font = params.get(:font); mFont = params.get(:font);
if (font == null) { if (mFont == null) {
font = Graphics.FONT_MEDIUM; mFont = Graphics.FONT_MEDIUM;
} }
fgcolor = params.get(:fgcolor); mFgcolor = params.get(:fgcolor);
if (fgcolor == null) { if (mFgcolor == null) {
fgcolor = Graphics.COLOR_BLACK; mFgcolor = Graphics.COLOR_BLACK;
} }
bgcolor = params.get(:bgcolor); mBgcolor = params.get(:bgcolor);
if (bgcolor == null) { if (mBgcolor == null) {
bgcolor = Graphics.COLOR_WHITE; mBgcolor = Graphics.COLOR_WHITE;
} }
timeout = params.get(:timeout); mTimeout = params.get(:timeout);
if (timeout == null) { if (mTimeout == null) {
timeout = 2000; mTimeout = 2000;
} }
timer = new Timer.Timer(); mTimer = new Timer.Timer();
} }
function onShow() { function onShow() {
timer.start(method(:dismiss), timeout, false); mTimer.start(method(:dismiss), mTimeout, false);
} }
function onHide() { function onHide() {
timer.stop(); mTimer.stop();
} }
function onUpdate(dc) { function onUpdate(dc) {
var tWidth = dc.getTextWidthInPixels(text, font); var tWidth = dc.getTextWidthInPixels(mText, mFont);
var tHeight = dc.getFontHeight(font); var tHeight = dc.getFontHeight(mFont);
var bWidth = tWidth + 20; var bWidth = tWidth + 20;
var bHeight = tHeight + 15; var bHeight = tHeight + 15;
var bX = (dc.getWidth() - bWidth) / 2; var bX = (dc.getWidth() - bWidth) / 2;
@ -93,10 +93,10 @@ class Alert extends WatchUi.View {
Graphics.COLOR_TRANSPARENT Graphics.COLOR_TRANSPARENT
); );
dc.clear(); dc.clear();
dc.setColor(bgcolor, bgcolor); dc.setColor(mBgcolor, mBgcolor);
dc.fillRoundedRectangle(bX, bY, bWidth, bHeight, bRadius); dc.fillRoundedRectangle(bX, bY, bWidth, bHeight, bRadius);
dc.setColor(fgcolor, bgcolor); dc.setColor(mFgcolor, mBgcolor);
for (var i = 0; i < 3; ++i) { for (var i = 0; i < 3; ++i) {
bX += i; bX += i;
bY += i; bY += i;
@ -107,8 +107,8 @@ class Alert extends WatchUi.View {
var tX = dc.getWidth() / 2; var tX = dc.getWidth() / 2;
var tY = bY + bHeight / 2; var tY = bY + bHeight / 2;
dc.setColor(fgcolor, bgcolor); dc.setColor(mFgcolor, mBgcolor);
dc.drawText(tX, tY, font, text, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER); dc.drawText(tX, tY, mFont, mText, Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER);
} }
// Remove the alert from view, usually on user input, but that is defined by the calling function. // Remove the alert from view, usually on user input, but that is defined by the calling function.

View File

@ -26,31 +26,31 @@ using Toybox.WatchUi;
using Toybox.Communications; using Toybox.Communications;
class ErrorView extends ScalableView { class ErrorView extends ScalableView {
hidden const settings as Lang.Dictionary = { hidden const cSettings as Lang.Dictionary = {
:errorIconMargin => 7f :errorIconMargin => 7f
}; };
// Vertical spacing between the top of the face and the error icon // Vertical spacing between the top of the face and the error icon
hidden var errorIconMargin; hidden var mErrorIconMargin;
hidden var text as Lang.String; hidden var mText as Lang.String;
hidden var errorIcon; hidden var mErrorIcon;
hidden var textArea; hidden var mTextArea;
function initialize(text as Lang.String) { function initialize(text as Lang.String) {
ScalableView.initialize(); ScalableView.initialize();
self.text = text; mText = text;
// Convert the settings from % of screen size to pixels // Convert the settings from % of screen size to pixels
errorIconMargin = pixelsForScreen(settings.get(:errorIconMargin) as Lang.Float); mErrorIconMargin = pixelsForScreen(cSettings.get(:errorIconMargin) as Lang.Float);
} }
// Load your resources here // Load your resources here
function onLayout(dc as Graphics.Dc) as Void { function onLayout(dc as Graphics.Dc) as Void {
errorIcon = Application.loadResource(Rez.Drawables.ErrorIcon) as Graphics.BitmapResource; mErrorIcon = Application.loadResource(Rez.Drawables.ErrorIcon) as Graphics.BitmapResource;
var w = dc.getWidth(); var w = dc.getWidth();
var h = dc.getHeight(); var h = dc.getHeight();
textArea = new WatchUi.TextArea({ mTextArea = new WatchUi.TextArea({
:text => text, :text => mText,
:color => Graphics.COLOR_WHITE, :color => Graphics.COLOR_WHITE,
:font => Graphics.FONT_XTINY, :font => Graphics.FONT_XTINY,
:justification => Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER, :justification => Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER,
@ -71,8 +71,8 @@ class ErrorView extends ScalableView {
} }
dc.setColor(Graphics.COLOR_WHITE, bg); dc.setColor(Graphics.COLOR_WHITE, bg);
dc.clear(); dc.clear();
dc.drawBitmap(hw - errorIcon.getWidth()/2, errorIconMargin, errorIcon); dc.drawBitmap(hw - mErrorIcon.getWidth()/2, mErrorIconMargin, mErrorIcon);
textArea.draw(dc); mTextArea.draw(dc);
} }
} }

View File

@ -23,9 +23,9 @@ using Toybox.Lang;
class Globals { class Globals {
// Enable printing of messages to the debug console (don't make this a Property // Enable printing of messages to the debug console (don't make this a Property
// as the messages can't be read from a watch!) // as the messages can't be read from a watch!)
static const debug = false; static const scDebug = false;
// There's a danger this time is device sensitive. // There's a danger this time is device sensitive.
static const menuItemUpdateInterval = 100; // ms, 100 ms seems okay for Venu2 static const scMenuItemUpdateInterval = 100; // ms, 100 ms seems okay for Venu2
static const alertTimeout = 2000; // ms static const scAlertTimeout = 2000; // ms
static const tapTimeout = 1000; // ms static const scTapTimeout = 1000; // ms
} }

View File

@ -14,7 +14,7 @@
// //
// Description: // Description:
// //
// Application root for GarminHomeAssistant. // Application root for GarminHomeAssistant
// //
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
@ -25,16 +25,16 @@ using Toybox.Application.Properties;
using Toybox.Timer; using Toybox.Timer;
class HomeAssistantApp extends Application.AppBase { class HomeAssistantApp extends Application.AppBase {
hidden var haMenu; hidden var mHaMenu;
hidden var strNoApiKey as Lang.String; hidden var strNoApiKey as Lang.String;
hidden var strNoApiUrl as Lang.String; hidden var strNoApiUrl as Lang.String;
hidden var strNoConfigUrl as Lang.String; hidden var strNoConfigUrl as Lang.String;
hidden var strNoInternet as Lang.String; hidden var strNoInternet as Lang.String;
hidden var strNoMenu as Lang.String; hidden var strNoMenu as Lang.String;
hidden var strApiFlood as Lang.String; hidden var strApiFlood as Lang.String;
hidden var timer as Timer.Timer; hidden var mTimer as Timer.Timer;
hidden var itemsToUpdate; // Array initialised by onReturnFetchMenuConfig() hidden var mItemsToUpdate; // Array initialised by onReturnFetchMenuConfig()
hidden var nextItemToUpdate = 0; // Index into the above array hidden var mNextItemToUpdate = 0; // Index into the above array
function initialize() { function initialize() {
AppBase.initialize(); AppBase.initialize();
@ -44,7 +44,7 @@ class HomeAssistantApp extends Application.AppBase {
strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
strNoMenu = WatchUi.loadResource($.Rez.Strings.NoMenu); strNoMenu = WatchUi.loadResource($.Rez.Strings.NoMenu);
strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood);
timer = new Timer.Timer(); mTimer = new Timer.Timer();
} }
// onStart() is called on application start up // onStart() is called on application start up
@ -53,25 +53,25 @@ class HomeAssistantApp extends Application.AppBase {
// onStop() is called when your application is exiting // onStop() is called when your application is exiting
function onStop(state as Lang.Dictionary?) as Void { function onStop(state as Lang.Dictionary?) as Void {
if (timer != null) { if (mTimer != null) {
timer.stop(); mTimer.stop();
} }
} }
// Return the initial view of your application here // Return the initial view of your application here
function getInitialView() as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>? { function getInitialView() as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>? {
if ((Properties.getValue("api_key") as Lang.String).length() == 0) { if ((Properties.getValue("api_key") as Lang.String).length() == 0) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem Note - execScript(): No API key in the application settings."); System.println("HomeAssistantMenuItem Note - execScript(): No API key in the application settings.");
} }
return [new ErrorView(strNoApiKey + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new ErrorView(strNoApiKey + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
} else if ((Properties.getValue("api_url") as Lang.String).length() == 0) { } else if ((Properties.getValue("api_url") as Lang.String).length() == 0) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem Note - execScript(): No API URL in the application settings."); System.println("HomeAssistantMenuItem Note - execScript(): No API URL in the application settings.");
} }
return [new ErrorView(strNoApiUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new ErrorView(strNoApiUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
} else if ((Properties.getValue("config_url") as Lang.String).length() == 0) { } else if ((Properties.getValue("config_url") as Lang.String).length() == 0) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem Note - execScript(): No configuration URL in the application settings."); System.println("HomeAssistantMenuItem Note - execScript(): No configuration URL in the application settings.");
} }
return [new ErrorView(strNoConfigUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new ErrorView(strNoConfigUrl + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
@ -79,7 +79,7 @@ class HomeAssistantApp extends Application.AppBase {
fetchMenuConfig(); fetchMenuConfig();
return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new WatchUi.View(), new WatchUi.BehaviorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantApp Note - fetchMenuConfig(): No Internet connection, skipping API call."); System.println("HomeAssistantApp Note - fetchMenuConfig(): No Internet connection, skipping API call.");
} }
return [new ErrorView(strNoInternet + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>; return [new ErrorView(strNoInternet + "."), new ErrorDelegate()] as Lang.Array<WatchUi.Views or WatchUi.InputDelegates>;
@ -89,12 +89,12 @@ class HomeAssistantApp extends Application.AppBase {
// Callback function after completing the GET request to fetch the configuration menu. // Callback function after completing the GET request to fetch the configuration menu.
// //
function onReturnFetchMenuConfig(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { function onReturnFetchMenuConfig(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: " + responseCode); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: " + responseCode);
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Data: " + data); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Data: " + data);
} }
if (responseCode == Communications.BLE_QUEUE_FULL) { if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantApp onReturnFetchMenuConfig() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
var cw = WatchUi.getCurrentView(); var cw = WatchUi.getCurrentView();
@ -103,21 +103,21 @@ class HomeAssistantApp extends Application.AppBase {
WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strApiFlood), new ErrorDelegate(), WatchUi.SLIDE_UP);
} }
} else if (responseCode == 200) { } else if (responseCode == 200) {
haMenu = new HomeAssistantView(data, null); mHaMenu = new HomeAssistantView(data, null);
WatchUi.switchToView(haMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE); WatchUi.switchToView(mHaMenu, new HomeAssistantViewDelegate(), WatchUi.SLIDE_IMMEDIATE);
itemsToUpdate = haMenu.getItemsToUpdate(); mItemsToUpdate = mHaMenu.getItemsToUpdate();
timer.start( mTimer.start(
method(:updateNextMenuItem), method(:updateNextMenuItem),
Globals.menuItemUpdateInterval, Globals.scMenuItemUpdateInterval,
true true
); );
} else if (responseCode == -300) { } else if (responseCode == -300) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Network request timeout."); System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Network request timeout.");
} }
WatchUi.pushView(new ErrorView(strNoMenu + ". " + strNoInternet + "?"), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strNoMenu + ". " + strNoInternet + "?"), new ErrorDelegate(), WatchUi.SLIDE_UP);
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Configuration not found or potential validation issue."); System.println("HomeAssistantApp Note - onReturnFetchMenuConfig(): Configuration not found or potential validation issue.");
} }
WatchUi.pushView(new ErrorView(strNoMenu + " code=" + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strNoMenu + " code=" + responseCode ), new ErrorDelegate(), WatchUi.SLIDE_UP);
@ -140,9 +140,9 @@ class HomeAssistantApp extends Application.AppBase {
// We need to spread out the API calls so as not to overload the results queue and cause Communications.BLE_QUEUE_FULL (-101) error. // We need to spread out the API calls so as not to overload the results queue and cause Communications.BLE_QUEUE_FULL (-101) error.
// This function is called by a timer every Globals.menuItemUpdateInterval ms. // This function is called by a timer every Globals.menuItemUpdateInterval ms.
function updateNextMenuItem() as Void { function updateNextMenuItem() as Void {
var itu = itemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem>; var itu = mItemsToUpdate as Lang.Array<HomeAssistantToggleMenuItem>;
itu[nextItemToUpdate].getState(); itu[mNextItemToUpdate].getState();
nextItemToUpdate = (nextItemToUpdate + 1) % itu.size(); mNextItemToUpdate = (mNextItemToUpdate + 1) % itu.size();
} }
} }

View File

@ -24,10 +24,10 @@ using Toybox.Graphics;
using Toybox.Application.Properties; using Toybox.Application.Properties;
class HomeAssistantMenuItem extends WatchUi.MenuItem { class HomeAssistantMenuItem extends WatchUi.MenuItem {
hidden var api_key = Properties.getValue("api_key"); hidden var mApiKey = Properties.getValue("api_key");
hidden var strNoInternet as Lang.String; hidden var strNoInternet as Lang.String;
hidden var strApiFlood as Lang.String; hidden var strApiFlood as Lang.String;
hidden var mService as Lang.String or Null; hidden var mService as Lang.String;
function initialize( function initialize(
label as Lang.String or Lang.Symbol, label as Lang.String or Lang.Symbol,
@ -53,12 +53,12 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
// Callback function after completing the POST request to call a script. // Callback function after completing the POST request to call a script.
// //
function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { function onReturnExecScript(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: " + responseCode); System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: " + responseCode);
System.println("HomeAssistantMenuItem onReturnExecScript() Response Data: " + data); System.println("HomeAssistantMenuItem onReturnExecScript() Response Data: " + data);
} }
if (responseCode == Communications.BLE_QUEUE_FULL) { if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantMenuItem onReturnExecScript() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
var cw = WatchUi.getCurrentView(); var cw = WatchUi.getCurrentView();
@ -70,7 +70,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
var d = data as Lang.Array; var d = data as Lang.Array;
for(var i = 0; i < d.size(); i++) { for(var i = 0; i < d.size(); i++) {
if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem Note - onReturnExecScript(): Correct script executed."); System.println("HomeAssistantMenuItem Note - onReturnExecScript(): Correct script executed.");
} }
if (WatchUi has :showToast) { if (WatchUi has :showToast) {
@ -88,7 +88,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
} }
if (!(WatchUi has :showToast) && !(Attention has :vibrate)) { if (!(WatchUi has :showToast) && !(Attention has :vibrate)) {
new Alert({ new Alert({
:timeout => Globals.alertTimeout, :timeout => Globals.scAlertTimeout,
:font => Graphics.FONT_MEDIUM, :font => Graphics.FONT_MEDIUM,
:text => (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String, :text => (d[i].get("attributes") as Lang.Dictionary).get("friendly_name") as Lang.String,
:fgcolor => Graphics.COLOR_WHITE, :fgcolor => Graphics.COLOR_WHITE,
@ -105,7 +105,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
"Authorization" => "Bearer " + api_key "Authorization" => "Bearer " + mApiKey
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}; };
@ -115,7 +115,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
var id = mIdentifier as Lang.String; var id = mIdentifier as Lang.String;
if (mService == null) { if (mService == null) {
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, id.length()); var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + id.substring(0, id.find(".")) + "/" + id.substring(id.find(".")+1, id.length());
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem execScript() URL=" + url); System.println("HomeAssistantMenuItem execScript() URL=" + url);
System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier); System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier);
} }
@ -127,7 +127,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
); );
} else { } else {
var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null); var url = (Properties.getValue("api_url") as Lang.String) + "/services/" + mService.substring(0, mService.find(".")) + "/" + mService.substring(mService.find(".")+1, null);
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem execScript() URL=" + url); System.println("HomeAssistantMenuItem execScript() URL=" + url);
System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier); System.println("HomeAssistantMenuItem execScript() mIdentifier=" + mIdentifier);
} }
@ -141,7 +141,7 @@ class HomeAssistantMenuItem extends WatchUi.MenuItem {
); );
} }
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantMenuItem Note - execScript(): No Internet connection, skipping API call."); System.println("HomeAssistantMenuItem Note - execScript(): No Internet connection, skipping API call.");
} }
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);

View File

@ -24,7 +24,7 @@ using Toybox.Graphics;
using Toybox.Application.Properties; using Toybox.Application.Properties;
class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem { class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
hidden var api_key = Properties.getValue("api_key"); hidden var mApiKey = Properties.getValue("api_key");
hidden var strNoInternet as Lang.String; hidden var strNoInternet as Lang.String;
hidden var strApiFlood as Lang.String; hidden var strApiFlood as Lang.String;
@ -44,7 +44,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet); strNoInternet = WatchUi.loadResource($.Rez.Strings.NoInternet);
strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood); strApiFlood = WatchUi.loadResource($.Rez.Strings.ApiFlood);
WatchUi.ToggleMenuItem.initialize(label, subLabel, identifier, enabled, options); WatchUi.ToggleMenuItem.initialize(label, subLabel, identifier, enabled, options);
api_key = Properties.getValue("api_key"); mApiKey = Properties.getValue("api_key");
} }
private function setUiToggle(state as Null or Lang.String) as Void { private function setUiToggle(state as Null or Lang.String) as Void {
@ -62,12 +62,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
// Callback function after completing the GET request to fetch the status. // Callback function after completing the GET request to fetch the status.
// //
function onReturnGetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { function onReturnGetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: " + responseCode); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: " + responseCode);
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Data: " + data); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Data: " + data);
} }
if (responseCode == Communications.BLE_QUEUE_FULL) { if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantToggleMenuItem onReturnGetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
var cw = WatchUi.getCurrentView(); var cw = WatchUi.getCurrentView();
@ -77,7 +77,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
} }
} else if (responseCode == 200) { } else if (responseCode == 200) {
var state = data.get("state") as Lang.String; var state = data.get("state") as Lang.String;
if (Globals.debug) { if (Globals.scDebug) {
System.println((data.get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); System.println((data.get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
} }
if (getLabel().equals("...")) { if (getLabel().equals("...")) {
@ -91,13 +91,13 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
var options = { var options = {
:method => Communications.HTTP_REQUEST_METHOD_GET, :method => Communications.HTTP_REQUEST_METHOD_GET,
:headers => { :headers => {
"Authorization" => "Bearer " + api_key "Authorization" => "Bearer " + mApiKey
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}; };
if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) { if (System.getDeviceSettings().phoneConnected && System.getDeviceSettings().connectionAvailable) {
var url = Properties.getValue("api_url") + "/states/" + mIdentifier; var url = Properties.getValue("api_url") + "/states/" + mIdentifier;
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem getState() URL=" + url); System.println("HomeAssistantToggleMenuItem getState() URL=" + url);
} }
Communications.makeWebRequest( Communications.makeWebRequest(
@ -107,7 +107,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
method(:onReturnGetState) method(:onReturnGetState)
); );
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem Note - getState(): No Internet connection, skipping API call."); System.println("HomeAssistantToggleMenuItem Note - getState(): No Internet connection, skipping API call.");
} }
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);
@ -117,12 +117,12 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
// Callback function after completing the POST request to set the status. // Callback function after completing the POST request to set the status.
// //
function onReturnSetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void { function onReturnSetState(responseCode as Lang.Number, data as Null or Lang.Dictionary or Lang.String) as Void {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: " + responseCode); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: " + responseCode);
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Data: " + data); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Data: " + data);
} }
if (responseCode == Communications.BLE_QUEUE_FULL) { if (responseCode == Communications.BLE_QUEUE_FULL) {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_QUEUE_FULL, API calls too rapid."); System.println("HomeAssistantToggleMenuItem onReturnSetState() Response Code: BLE_QUEUE_FULL, API calls too rapid.");
} }
var cw = WatchUi.getCurrentView(); var cw = WatchUi.getCurrentView();
@ -136,7 +136,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
for(var i = 0; i < d.size(); i++) { for(var i = 0; i < d.size(); i++) {
if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) { if ((d[i].get("entity_id") as Lang.String).equals(mIdentifier)) {
state = d[i].get("state") as Lang.String; state = d[i].get("state") as Lang.String;
if (Globals.debug) { if (Globals.scDebug) {
System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state); System.println((d[i].get("attributes") as Lang.Dictionary).get("friendly_name") + " State=" + state);
} }
setUiToggle(state); setUiToggle(state);
@ -150,7 +150,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
:method => Communications.HTTP_REQUEST_METHOD_POST, :method => Communications.HTTP_REQUEST_METHOD_POST,
:headers => { :headers => {
"Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON, "Content-Type" => Communications.REQUEST_CONTENT_TYPE_JSON,
"Authorization" => "Bearer " + api_key "Authorization" => "Bearer " + mApiKey
}, },
:responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON :responseType => Communications.HTTP_RESPONSE_CONTENT_TYPE_JSON
}; };
@ -164,7 +164,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
} else { } else {
url = Properties.getValue("api_url") + "/services/" + id.substring(0, id.find(".")) + "/turn_off"; url = Properties.getValue("api_url") + "/services/" + id.substring(0, id.find(".")) + "/turn_off";
} }
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem setState() URL=" + url); System.println("HomeAssistantToggleMenuItem setState() URL=" + url);
System.println("HomeAssistantToggleMenuItem setState() mIdentifier=" + mIdentifier); System.println("HomeAssistantToggleMenuItem setState() mIdentifier=" + mIdentifier);
} }
@ -177,7 +177,7 @@ class HomeAssistantToggleMenuItem extends WatchUi.ToggleMenuItem {
method(:onReturnSetState) method(:onReturnSetState)
); );
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println("HomeAssistantToggleMenuItem Note - setState(): No Internet connection, skipping API call."); System.println("HomeAssistantToggleMenuItem Note - setState(): No Internet connection, skipping API call.");
} }
WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP); WatchUi.pushView(new ErrorView(strNoInternet + "."), new ErrorDelegate(), WatchUi.SLIDE_UP);

View File

@ -115,25 +115,25 @@ class HomeAssistantViewDelegate extends WatchUi.Menu2InputDelegate {
function onSelect(item as WatchUi.MenuItem) as Void { function onSelect(item as WatchUi.MenuItem) as Void {
if (item instanceof HomeAssistantToggleMenuItem) { if (item instanceof HomeAssistantToggleMenuItem) {
var haToggleItem = item as HomeAssistantToggleMenuItem; var haToggleItem = item as HomeAssistantToggleMenuItem;
if (Globals.debug) { if (Globals.scDebug) {
System.println(haToggleItem.getLabel() + " " + haToggleItem.getId() + " " + haToggleItem.isEnabled()); System.println(haToggleItem.getLabel() + " " + haToggleItem.getId() + " " + haToggleItem.isEnabled());
} }
haToggleItem.setState(haToggleItem.isEnabled()); haToggleItem.setState(haToggleItem.isEnabled());
} else if (item instanceof HomeAssistantMenuItem) { } else if (item instanceof HomeAssistantMenuItem) {
var haItem = item as HomeAssistantMenuItem; var haItem = item as HomeAssistantMenuItem;
if (Globals.debug) { if (Globals.scDebug) {
System.println(haItem.getLabel() + " " + haItem.getId()); System.println(haItem.getLabel() + " " + haItem.getId());
} }
haItem.execScript(); haItem.execScript();
} else if (item instanceof HomeAssistantViewMenuItem) { } else if (item instanceof HomeAssistantViewMenuItem) {
var haMenuItem = item as HomeAssistantViewMenuItem; var haMenuItem = item as HomeAssistantViewMenuItem;
if (Globals.debug) { if (Globals.scDebug) {
System.println("Menu: " + haMenuItem.getLabel() + " " + haMenuItem.getId()); System.println("Menu: " + haMenuItem.getLabel() + " " + haMenuItem.getId());
} }
// No delegate state to be amended, so re-use 'self'. // No delegate state to be amended, so re-use 'self'.
WatchUi.pushView(haMenuItem.getMenuView(), self, WatchUi.SLIDE_LEFT); WatchUi.pushView(haMenuItem.getMenuView(), self, WatchUi.SLIDE_LEFT);
} else { } else {
if (Globals.debug) { if (Globals.scDebug) {
System.println(item.getLabel() + " " + item.getId()); System.println(item.getLabel() + " " + item.getId());
} }
} }

View File

@ -22,7 +22,7 @@ using Toybox.Lang;
using Toybox.WatchUi; using Toybox.WatchUi;
class HomeAssistantViewMenuItem extends WatchUi.MenuItem { class HomeAssistantViewMenuItem extends WatchUi.MenuItem {
hidden var menu as HomeAssistantView; hidden var mMenu as HomeAssistantView;
function initialize(definition as Lang.Dictionary) { function initialize(definition as Lang.Dictionary) {
// definitions.get(...) are Strings here as they have been checked by HomeAssistantView first // definitions.get(...) are Strings here as they have been checked by HomeAssistantView first
@ -33,11 +33,11 @@ class HomeAssistantViewMenuItem extends WatchUi.MenuItem {
null null
); );
menu = new HomeAssistantView(definition, null); mMenu = new HomeAssistantView(definition, null);
} }
function getMenuView() as HomeAssistantView { function getMenuView() as HomeAssistantView {
return menu; return mMenu;
} }
} }

View File

@ -23,7 +23,7 @@ using Toybox.WatchUi;
using Toybox.Math; using Toybox.Math;
class ScalableView extends WatchUi.View { class ScalableView extends WatchUi.View {
hidden var screenWidth; hidden var mScreenWidth;
function initialize() { function initialize() {
View.initialize(); View.initialize();
@ -40,9 +40,9 @@ class ScalableView extends WatchUi.View {
// height > width. // height > width.
// //
function pixelsForScreen(pc as Lang.Float) as Lang.Number { function pixelsForScreen(pc as Lang.Float) as Lang.Number {
if (screenWidth == null) { if (mScreenWidth == null) {
screenWidth = System.getDeviceSettings().screenWidth; mScreenWidth = System.getDeviceSettings().screenWidth;
} }
return Math.round(pc * screenWidth) / 100; return Math.round(pc * mScreenWidth) / 100;
} }
} }