Skip to content

Commit

Permalink
Watch: Add second layer of communication with HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
Artaud committed Jan 7, 2022
1 parent cbfb8e7 commit 9859c16
Showing 1 changed file with 76 additions and 23 deletions.
99 changes: 76 additions & 23 deletions SleepGarmin-watch2/source/CommManager.mc
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class CommManager {
static const MSG_HINT = "Hint;";
static const MSG_STOP_ALARM = "StopAlarm;";

// To phone
// To phone via TRANSMIT
static const MSG_START_TRACKING = "STARTING";
static const MSG_CONFIRMCHECK = "CONFIRMCHECK";

// To phone via HTTP
static const MSG_SNOOZE_ALARM = "SNOOZE";
static const MSG_DISMISS_ALARM = "DISMISS";
static const MSG_STOP_TRACKING = "STOPPING";
Expand All @@ -35,30 +37,33 @@ class CommManager {
const MAX_DELIVERY_PAUSE = 3;
const MAX_WAITING_TIME_IN_TRANSMIT_MS = 60000;

const WEB_URL = "http://127.0.0.1:1765";

function initialize(ctx) {
DebugManager.log("CommManager initialized");

self.ctx = ctx;
}

public function start() {
Communications.registerForPhoneAppMessages(method(:handleMessageReceived));
self.queue = new MessageQueue();
Communications.registerForPhoneAppMessages(method(:onPhoneMsgReceive));

self.queue = new MessageQueue();
self.commListener = new CommListener(self.queue, self.ctx);

enqueue(CommManager.MSG_START_TRACKING);

if (DebugManager.commDebug) {
var msg = new Communications.PhoneAppMessage();
msg.data = CommManager.MSG_START;
handleMessageReceived(msg);
onPhoneMsgReceive(msg);

msg.data = CommManager.MSG_BATCH_SIZE + "12";
handleMessageReceived(msg);
onPhoneMsgReceive(msg);

// var msg2 = new Communications.PhoneAppMessage();
// msg2.data = CommManager.MSG_START_ALARM + "0";
// handleMessageReceived(msg2);
// var msg2 = new Communications.PhoneAppMessage();
// msg2.data = CommManager.MSG_START_ALARM + "0";
// onPhoneMsgReceive(msg2);
}
}

Expand Down Expand Up @@ -92,36 +97,84 @@ class CommManager {
self.ctx.state.deliveryPauseCount = 0;
self.ctx.state.deliveryErrorCount = 0;
}

return;
}


var msg = self.queue.getFirst();
DebugManager.log("First msg: " + msg);
// DebugManager.log("First msg: " + msg);
if (msg != null) {
self.ctx.state.deliveryInProgress = true;
DebugManager.log("CommManager transmit: " + msg);
DebugManager.log("CommManager sending: " + msg);
self.ctx.state.lastTransmitTs = System.getTimer();

if (DebugManager.commDebug) {
DebugManager.log("NOT Transmitted");
// self.commListener.onComplete();
// self.commListener.onComplete();
self.commListener.onError();
} else if (self.ctx.state.isHttpCommunicationMode()) {
// TODO: Transmit via makeWebRequest()
} else {
Communications.transmit(msg, {}, self.commListener);
}
return;
}

if (msg.equals(CommManager.MSG_START_TRACKING) || msg.equals(CommManager.MSG_CONFIRMCHECK)) {
Communications.transmit(msg, {}, self.commListener);
} else {
var messageToPhone = new MessageToPhone(msg);
var req = {messageToPhone.command => messageToPhone.data};
DebugManager.log("Making web request" + req);
Communications.makeWebRequest(
WEB_URL,
req,
{:method => Communications.HTTP_REQUEST_METHOD_GET},
method(:onWebMsgReceive)
);
}

}
}

// message is of type PhoneAppMessage
function handleMessageReceived(message) {
var msg = message.data;

DebugManager.log("Msg received: " + msg);
function onPhoneMsgReceive(phoneAppMessage) {
handleMessageReceived(phoneAppMessage.data);
}

function onWebMsgReceive(responseCode, data) {
if (responseCode == 200) {
DebugManager.log("onWebMsgReceive Request Successful: " + data);
self.commListener.onComplete();
// Expecting to receive a JSON string
var msgArray = parseJsonDataToArray(data);

for( var i = 0; i < msgArray.size(); i += 1 ) {
handleMessageReceived(msgArray[i]);
}
}
else {
DebugManager.log("onWebMsgReceive Response: " + responseCode);
self.commListener.onError();
}
}

function parseJsonDataToArray(json) {
DebugManager.log("parseJsonDataToArray" + json);
// contract is be c:command, d:data (d is optional)
var ar = [];

for (var i = 0; i < json.size(); ++i) {
var entry = json[i];
var command = entry["c"];
var param = entry["d"];

if (param == null) {
ar.add(command);
} else {
ar.add(command + ";" + param);
}
}

return ar;
}

function handleMessageReceived(msg) {
DebugManager.log("handleMessageReceived: " + msg);

if (msg.equals(CommManager.MSG_START)) {
self.ctx.businessManager.startTracking();
Expand Down

0 comments on commit 9859c16

Please sign in to comment.