Last active
August 29, 2015 14:06
-
-
Save ismaelc/44d084346018a367a3bd to your computer and use it in GitHub Desktop.
Pebble Concur Mojio demo (Javascript)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Copyright (c) 2014 Gabi Zuniga | |
| // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. | |
| // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
| // | |
| // Configuration: Hardcoded for Hackathon | |
| // TODO: The updated Mojio endpoint should be https://api.moj.io:443/v1/Trips?sortBy=&desc={desc}&criteria={criteria} | |
| var mojioTripUrl = "https://sandbox.api.moj.io/v1/Trips?sortBy=EndTime&limit=1&desc=true"; | |
| var mojioAPIToken = "<insert Mojio token here>"; | |
| var concurUrl = "https://www.concursolutions.com/api/v3.0/expense/entries"; | |
| var concurToken = "<insert Concur token here>"; | |
| // Globals | |
| var config; | |
| // MessageQueue Singleton | |
| var appMessageQueue = { | |
| queue: [], | |
| numTries: 0, | |
| maxTries: 5, | |
| maxBuffer: 480, | |
| lastId: "", | |
| add: function(obj) { | |
| this.queue.push(obj); | |
| }, | |
| clear: function() { | |
| this.queue = []; | |
| }, | |
| isEmpty: function() { | |
| return this.queue.length === 0; | |
| }, | |
| nextMessage: function() { | |
| return this.isEmpty() ? {} : this.queue[0]; | |
| }, | |
| send: function() { | |
| if (this.queue.length > 0) { | |
| var ack = function() { | |
| appMessageQueue.numTries = 0; | |
| appMessageQueue.queue.shift(); | |
| appMessageQueue.send(); | |
| }; | |
| var nack = function() { | |
| appMessageQueue.numTries++; | |
| appMessageQueue.send(); | |
| }; | |
| if (this.numTries >= this.maxTries) { | |
| console.log('Failed sending AppMessage: ' + JSON.stringify(this.nextMessage())); | |
| ack(); | |
| } | |
| // console.log('Sending AppMessage: ' + JSON.stringify(this.nextMessage())); | |
| Pebble.sendAppMessage(this.nextMessage(), ack, nack); | |
| } | |
| } | |
| }; | |
| // Fetch from Mojo | |
| function fetchLastMojoTrip() { | |
| var response; | |
| var req = new XMLHttpRequest(); | |
| // build the GET request | |
| req.setRequestHeader("MojioAPIToken", mojioAPIToken); | |
| req.open('GET', mojioTripUrl, true); | |
| req.onload = function(e) { | |
| if (req.readyState == 4) { | |
| // 200 - HTTP OK | |
| if(req.status == 200) { | |
| // console.log(req.responseText); | |
| response = JSON.parse(req.responseText); | |
| var address = response.Data[0].StartAddress.Address1; | |
| // console.log(response.Data[0].EndTime); | |
| var currentId = response.Data[0]._id; | |
| if (currentId !== localStorage.getItem('lastId')) { | |
| // Send Trip address to Pebble | |
| sendEntry(0, address); | |
| // Save last trip | |
| localStorage.setItem('lastId', currentId); | |
| localStorage.setItem('lastTrip', JSON.stringify(response.Data[0])); | |
| } | |
| } else { | |
| console.log("Request returned error code " + req.status.toString()); | |
| } | |
| } | |
| } | |
| req.send(null); | |
| } | |
| // Post to Concur | |
| // concur body template | |
| var concurBody = { | |
| "Description": "Bussiness Trip", | |
| "ExpenseTypeCode": "MILEG", | |
| "Journey": { | |
| "BusinessDistance": 0, | |
| "EndLocation": "End", | |
| "NumberOfPassengers": 0, | |
| "OdometerEnd": 0, | |
| "OdometerStart": 0, | |
| "PersonalDistance": 0, | |
| "StartLocation": "Start", | |
| "UnitOfMeasure": "m" | |
| }, | |
| "ReportID": "88FA6C20AED7ABC123456", | |
| "TransactionDate": "2104-05-05", | |
| "VendorDescription": "Posted from Pebble", | |
| }; | |
| function postToConcur() { | |
| console.log("Posting to Concur"); | |
| var lastTrip = localStorage.getItem('lastTrip'); | |
| if (lastTrip != null) { | |
| console.log("lastTrip "+lastTrip); | |
| lastTrip = JSON.parse(lastTrip); | |
| concurBody.Journey.StartLocation = lastTrip.StartAddress.Address1; | |
| concurBody.Journey.EndLocation = lastTrip.EndAddress.Address1; | |
| concurBody.TransactionDate = lastTrip.EndTime; | |
| concurBody.Journey.OdometerStart = lastTrip.StartMilage; | |
| concurBody.Journey.OdometerEnd = lastTrip.EndMilage; | |
| concurBody.Journey.PersonalDistance = lastTrip.Distance; | |
| } else { | |
| console.log("lastTrip was null!"); | |
| } | |
| var response; | |
| var req = new XMLHttpRequest(); | |
| var body = JSON.stringify(concurBody); | |
| // build the POST request | |
| req.open('POST', concurUrl, true); | |
| req.setRequestHeader("Authorization", "OAuth "+concurToken); | |
| req.setRequestHeader("Content-Type" , "application/json"); | |
| req.onreadystatechange = function(e) { | |
| console.log("Got Response"); | |
| if (req.readyState == 4) { | |
| // 200 - HTTP OK | |
| if(req.status == 200) { | |
| console.log("Concur post: success"); | |
| } else { | |
| console.log("Request returned error code " + req.status.toString()); | |
| } | |
| } | |
| } | |
| req.send(body); | |
| } | |
| // Send values to Pebble | |
| function sendEntry(index, value) { | |
| appMessageQueue.clear(); | |
| var safeIndex = index % config.length; | |
| var enabled = config[safeIndex]['cb-enable'] == 'true' ? true : false; | |
| appMessageQueue.add({ | |
| title : config[safeIndex].title, | |
| measure : value != null ? value : "?", | |
| enabled : enabled ? 1 : 0, | |
| period : parseInt(config[safeIndex]['sampling-rate']) | |
| }); | |
| appMessageQueue.send(); | |
| } | |
| // Initialization | |
| Pebble.addEventListener('ready', function(e) { | |
| config = [ | |
| {"cb-enable":"true","title":"Expense Pro","sampling-rate":"1"} | |
| ]; | |
| sendEntry(0, "-"); | |
| fetchLastMojoTrip(); | |
| }); | |
| // Messages from the Pebble App | |
| Pebble.addEventListener('appmessage', function(e) { | |
| if (e.payload.command != null) { | |
| switch (e.payload.command) { | |
| case 0: // FETCH | |
| fetchLastMojoTrip(); | |
| break; | |
| case 1: // Up/Next button pressed | |
| break; | |
| case 2: // Down/Prev button pressed | |
| postToConcur(); | |
| break; | |
| case 3: // Middle button pressed | |
| break; | |
| } | |
| } else { | |
| appMessageQueue.clear(); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment