Skip to content

Commit

Permalink
compressed logs, added force stop when no messages have been received…
Browse files Browse the repository at this point in the history
… in the past 5 minutes
  • Loading branch information
Artaud committed Nov 27, 2018
1 parent be40d96 commit b41852e
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 174 deletions.
13 changes: 7 additions & 6 deletions SleepGarmin-watch/source/SleepAlarmDelegate.mc
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,29 @@ class SleepAlarmDelegate extends Ui.BehaviorDelegate {
}

function onMenu() {
log("OnMenu");
// log("OnMenu");
Ui.pushView(new Rez.Menus.AlarmMenu(), new SleepAlarmMenuDelegate(), Ui.SLIDE_UP);
return true;
}

function onBack() {
return true; //! disables back button
}

function onSelect() {
return true;
}

function onKey(keyEvent){
var k = keyEvent.getKey();
log("onKey: " + k);
// log("onKey: " + k);

if (k == KEY_ENTER) {
log("KEY_ENTER pressed");
// log("KEY_ENTER pressed");
onMenu();
return true;
}
return false;
}

}
64 changes: 33 additions & 31 deletions SleepGarmin-watch/source/SleepApp.mc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ using Toybox.Math as Math;

var now;
var timecurrent = 0;
var startTimestamp = 0;
var logtimestamp;

var messageQueue = [];
Expand All @@ -30,55 +31,57 @@ using Toybox.Math as Math;
var deliveryPauseCount = 0;
const MAX_DELIVERY_ERROR = 3;
const MAX_DELIVERY_PAUSE = 20;
var lastMessageReceived = 0;

var alarm_currently_active = false; // Is alarm currently ringing on phone?
var exitTapped = false;
var alarmViewActive = false;

var trackingBool = false;
var stoppingBool = false;
var targetExitTime = 0;
var shouldExit = false;


function exitTimer(afterCycles) {
targetExitTime = afterCycles;
shouldExit = true;
}

// Logs into the /GARMIN/APPS/LOGS/appname.TXT
// The file has to be created manually first. It is not possible to gather debug logs in production (after distribution in the ConnectIQ store)
function log(a) {
if (debug == true) {
logtimestamp = Time.now().value();
logtimestamp = Time.now().value() - startTimestamp;
Sys.println(logtimestamp + ": " + a);
}
}

function betalog(a) {
if (beta == true) {
logtimestamp = Time.now().value();
logtimestamp = Time.now().value() - startTimestamp;
Sys.println(logtimestamp + ": " + a);
}
}

// Puts message in the messageQueue, also attempts to do some memory checks so as not to overload the underlying watch's queue
function enqueue(message) {
var freeMemRatio = Sys.getSystemStats().freeMemory*100/Sys.getSystemStats().totalMemory;
log("free: " + Sys.getSystemStats().freeMemory);
log("free: " + Sys.getSystemStats().freeMemory + " freeRatio:" + freeMemRatio);

if (messageQueue.size() > 50) {
log("MsgQ > 50!!!");
}
// if (messageQueue.size() > 50) {
// log("MsgQ > 50!!!");
// }

if (((freeMemRatio <= 7) && (messageQueue.size() > 0)) || (messageQueue.size() > 50)) {
log("Removing" + messageQueue[0] + "from q, freeMemRatio:" + freeMemRatio + ",q size:" + messageQueue.size());
log("Rem from q, freeRatio:" + freeMemRatio + ",q:" + messageQueue.size());
messageQueue.remove(messageQueue[0]);
}


if (messageQueue.indexOf(message) == -1) {
messageQueue.add(message);
log("Adding to q:" + message + " outQ: " + messageQueue);
// log("Adding to q:" + message + " outQ: " + messageQueue);
}
}

Expand Down Expand Up @@ -114,18 +117,19 @@ using Toybox.Math as Math;
Ui.switchToView(new SleepMainView(), new SleepMainDelegate(), Ui.SLIDE_IMMEDIATE);
alarmViewActive = false;
}

function normalExit(){
log("Normal Exit");
stoppingBool = true;
if (Sys.getDeviceSettings().phoneConnected && !fakeTransmit) {
Comm.transmit("STOPPING", null, new SleepNowListener("STOPPING"));

} else if (!Sys.getDeviceSettings().phoneConnected){
} else if (!Sys.getDeviceSettings().phoneConnected) {
exitTimer(20);
} else if ( lastMessageReceived == 0 || ((lastMessageReceived - Time.now().value()) > 30000)) {
log("Possible failure_during_transfer");
exitTimer(20);
}
}

function forceExit(){
log("Force Exit");
stoppingBool = true;
Expand All @@ -134,8 +138,8 @@ using Toybox.Math as Math;
}
exitTimer(20);
}




class SleepApp extends App.AppBase {
Expand Down Expand Up @@ -186,9 +190,10 @@ class SleepApp extends App.AppBase {

function initialize() {
AppBase.initialize();
startTimestamp = Time.now().value();

if(Comm has :registerForPhoneAppMessages) {
log("Registering for Phone Messages");
log("registerForPhoneAppMessages");
Comm.registerForPhoneAppMessages( method(:onMsg) );
} else {
notice = notice + "Err: Old CIQ version\n";
Expand Down Expand Up @@ -227,7 +232,7 @@ class SleepApp extends App.AppBase {
if (now.sec == 0) {
Ui.requestUpdate();
}

if (shouldExit) {
if (targetExitTime == 0) {
Sys.exit();
Expand Down Expand Up @@ -300,7 +305,8 @@ class SleepApp extends App.AppBase {
// Handling messages coming from the phone
function handleIncomingMessage(mail) {
var data;
log("Incoming mail: " + mail);
log("In: " + mail);
lastMessageReceived = Time.now().value();

if ( mail.equals("StopApp") && stopAppDelay == 5) {
stopAlarm();
Expand Down Expand Up @@ -338,17 +344,17 @@ class SleepApp extends App.AppBase {
if (!trackingBool) {
trackingBool = true;
Ui.requestUpdate();
log("Switched trackingBool to true");
log("tracking true");
}
} else if ( mail.equals("StartTracking")) {
if (!trackingBool) {
trackingBool = true;
Ui.requestUpdate();
log("Switched trackingBool to true");
log("tracking true");
}
} else {
// mail = "Message not handled: " + mail;
log("Message not handled" + mail.toString());
log("Msg fail" + mail.toString());
}
}

Expand Down Expand Up @@ -384,9 +390,6 @@ class SleepApp extends App.AppBase {

hrCount = hrCount + 1;
// log(hrCount);
if (beta == true) {
current_heartrate = hrInfo.heartRate;
}

if (hrCurrentlyReading == true) {
// log("hrinfo, heartrate" + hrInfo + " " +hrInfo.heartRate);
Expand All @@ -413,7 +416,7 @@ class SleepApp extends App.AppBase {

// Batch can be any number but usually we set it either to 1 when the phone user is currently viewing the phone so he has data from watch sent to phone immediately for viewing, or to 12 when the phone is idle, to conserve battery (we don't have to send via bluetooth as often)
function setBatchSize(newBatchSize) {
log("Batch set to " + newBatchSize.toString());
log("Batch " + newBatchSize.toString());
batchSize = newBatchSize;
sendCurrentDataAndResetBatch();
}
Expand All @@ -425,7 +428,7 @@ class SleepApp extends App.AppBase {
}

function setAlarm(timestamp) {
log("Alarm set to " + timestamp.toString());
log("Alarm " + timestamp.toString());
scheduled_alarm_ts = timestamp;
}

Expand All @@ -442,15 +445,14 @@ class SleepApp extends App.AppBase {
Attention.vibrate(shortPulse);
} else {
if (Attention has :playTone) {
Attention.playTone(8);
Attention.playTone(8);
}
}
}
}

// Hint is lucid dreaming or anti-snoring vibration
function doHint(repeat) {
log("Hint requested " + repeat.toString() + " times.");
// Garmin only supports vibrating up to 8 VibeProfiles, so we have to cap repeating on 4
if (repeat > 4) {
repeat = 4;
Expand Down Expand Up @@ -487,7 +489,7 @@ class SleepApp extends App.AppBase {
log("FakeTransmit: " + message);
new SleepListener(message).onComplete();
} else {
log("Send next message " + message);
log("Send " + message.toString().substring(0,5) + "...");
Comm.transmit(message, null, new SleepListener(message));
}
}
Expand Down
48 changes: 0 additions & 48 deletions SleepGarmin-watch/source/SleepDelegate.mc

This file was deleted.

3 changes: 1 addition & 2 deletions SleepGarmin-watch/source/SleepListener.mc
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class SleepListener extends Comm.ConnectionListener
{
messageQueue.remove(message);
deliveryInProgress = false;
log("msgQ: " + messageQueue);
log(message + "sent OK");
log("msg sent ok, q size " + messageQueue.size());
betalog(message + "sent OK");
}

Expand Down
Loading

0 comments on commit b41852e

Please sign in to comment.