Start a new topic
Answered

A guide for starting an app?

Is there any quick guide to start building an app?  I'm a novice programmer at best, but I'm messed around enough with some other coding and I'd like to at least get my feet wet and try something.


My idea is to get the current baseball score when my team is playing and display it.  I know that MLB provides json pages and I can parse the data out of there that I need.  I already do this in Tasker to push score data to my Android Wear watch, so I'm familiar enough (I think) with the json parsing part but there's a lot I'm just not sure of how to do.


Specifically:


I need to call the json page for the current date.  How do I get today's date in the LaMetric code area?    How do I call a website?


Next I'm assuming I'm going to dumb the json data from the site into a variable... I might be able to figure that out from looking at examples, then I'll need to parse out specific parts of the json data (again, I might be able to figure this out from examples) and put the data I need into another variable.


This leads to a second json call, and dumping that data into yet another variable.  I'll need to parse that data as well and pull out the pieces that I want.  I'll end up with four variables (Away team name, away team score, home team name, and home team score).  How do I display the contents of those variables as text?


I mean I know I gotta do this:

        {
            "index": 0,
            "text": ??What do I put here??,
            "icon": "icon code here"
        },

  But what I do put in the text area for it to display variables?


Finally, is there currently anyway to display this data ONLY if a specific variable contains a specific value?  What I mean is that I only want to display the data when the game is going.  I can pull that info from the json pages, but I want LaMetric to only display the data if the the variable "Status" = "In progress".  Can that be done?


Sorry for the newbie questions.  I learn best by looking at other examples and sorta pulling pieces together to make it work the way I want to, but there's precious few good examples right now.


Best Answer

Hey guys, here is tutorial we just made that can potentially explain how things are done on LaMetric and we are going to release few more that explains push apps and ability to use user authentication and options. Probably it will be useful especially for newcomers.


There is an app simulator and it shows sample values for "icon". "index" is an integer for the screens, i.e. 0 for the 1st screen, 1 for the 2nd one, while "text" is just what you want to display, so you need to specify that.

Yeah, I've played in the simulator a bit but there's a lot of coding I need to put in there.  And for text I know how to enter text, but if I want to display a variable string (well, four actually), what do I put there?

Hi Chris,


On your server you can put whatever you want in the "Text" section and the app will display what is written there.


The pattern should be


When the clock requests your url

Do some logic to workout what you should send as a response

Return the response.


The logic in the middle is up to you.


Which server backend do you use? PHP? Ruby? Let us know and we can provide examples.







Hey Richard...


I want to grab json data from a website, specifically the mlb website.  In tasker I use an HTTP Get task to grab the site and drop it into a variable.


Then I parse the json data to pull out a key bit of data.  Again in tasker the javascriptlet looks like this:

 

for (var i = 0; i < arr.data.games.game.length; i++) {
    if ( (arr.data.games.game[i].home_team_name == "[My Team]") ||
         (arr.data.games.game[i].away_team_name == "[My Team]") ) {
         var gamedir = arr.data.games.game[i].game_data_directory;
         }
    }

arr = the json data from the site


Then I used the variable gamedir to do a new HTTP Get for a new site.  And then I parse out the new data again using a javascriptlet.  Basically just assign variables based on parts of the json data from the new site.


The text part is going to be made up of variables... the variables are just going to be text (Team name abbreviations and scores), but they will change so I can't just type in static text.  How would I indicate that it's a variable rather than straight text?

There's no HTTP Get request in the lametric api, there's also no concept of variables. You can't do any form of programming within it atm. 


All LaMetric apps written by other developers(at the moment) do is pull a json url from a website. Or receive a JSON payload from another site.


You'll need to write a server side file which handles everything you've just said and then outputs the result as a JSON formatted file which is reachable by the server. 


Does that make sense?


I've made a new file for demonstration at: http://uploads.ahref.co.uk/cgi-bin/clock Go there and hit refresh a couple of times. See how it returns the current time? My server is rendering that file using a server side language called perl. All variables etc occur on the server and all the lamtric gets is some plaintext json. 


The source code for the "app" i made which prints out the server time in a lametric format can be found at: https://gist.github.com/rfox90/d345b88b1c8d261bea89 You'd need some understanding of perl and cgi within perl to see how it works but just looking at it should be enough demonstrate where the logic on an lametric app belongs right now. 


I am extremely hopeful that in the future the lametric team will allow us a lower level access to the api. From some error messages ive seen this looks like it might be written in java. 



Yes that does make sense, thanks.  I guess I was assuming incorrectly that this was more like the SmartThings model where I could put all the code on the LaMetric side of things or cloud or whatever.  But it looks like it's more just a simple push in or at the most a scheduled pull side thing.


That might limit some of what I hoped to do with LaMetric, but I know it's still early in the process so we'll see what develops down the line. 


Being a dabbler in coding I'm not super experienced in all this so I don't really have my own server to work with.

Chris, you could use Tasker to accomplish this I think. You'd set up your LaMetric app as a push app, then use Tasker to grab and parse the MLB JSON. Take the relevant bits from the MLB JSON (team name, score, inning/outs/strikes, etc), and put them into the text field.


This might give you an idea of how it's done...


 

var framedata = new Object();
var foundgame = false;
var ballicon = "i1342"; // baseball icon
var inningtop = "i1344"; // top of the inning icon
var inningbottom = "i1345"; // bottom of the inning icon

for (var i = 0; i < arr.data.games.game.length; i++) {
    if ( (arr.data.games.game[i].home_team_name == "[My Team]") ||
         (arr.data.games.game[i].away_team_name == "[My Team]") ) {
        // stop iterating, we found what we're looking for
        foundgame = true;
        break;
    }
}

// if we never found it, there must not be a game going on
if (!foundgame) {
    framedata = {
        "frames": [
            {
                "index": 0,
                "text": "No game :(",
                "icon": ballicon;
            },
            {
                "index": 1,
                "text": "No game :(",
                "icon": ballicon;
            }
        ]
    };
// We found a game. Since we broke the loop, "i" will be set to the relevant game already
} else {
    var game = arr.data.games.game[i]; // let's shorten our variable here...
    var teams = game.home_team_name + " VS " + game.away_team_name;
    var inningicon;
    if (game.top_inning == "Y") {
        inningicon = inningtop;
    } else {
        inningicon = inningbottom;
    }
    var inning = game.inning.toString();
    if (inning == "1") { inning += "th"; }
    else if (inning == "2") { inning += "nd"; }
    else if (inning == "3") { inning += "rd"; }
    else { inning += "th"; }
    var score = inning + ", " + game.home_score + "-" + game.away_score;
    // load the first frame with the team info, and the second with the game info
    framedata = {
        "frames": [
            {
                "index": 0,
                "text": teams,
                "icon": ballicon;
            },
            {
                "index": 1,
                "text": score,
                "icon": inningicon;
            }
        ]
    };
}
// Stringify the frame object to pass to LaMetric.
// Specify spacing of 4 so it doesn't get too cranky (LaMetric doesn't seem to like unspaced JSON)
var output = JSON.stringify(framedata, null, 4);

// Do your Tasker thing here to submit the postdata.
// Don't forget to set the X-Access-Token header before POSTing!

 

Okay, thanks Jesse Jones.  I think I can handle the Tasker part, though the last part that formats the output is very helpful.


But how do I post the data?  That I have no clue how to do.  I mean there is a HTTP post function in tasker, but what's the server (and port)?

When building your LaMetric app, if you've selected "push" for your application type it should give you a "push URL". Simply make the request to that URL with the relevant header set and the body of the data.


If I've Googled things right, this should be similar to what you need to do:


 

var http = new XMLHttpRequest(); 

////////////////////////////////////////////////
// be sure not to publish this bit if you share your code!
var app_url = "https://developer.lametric.com/api/V1/dev/widget/update/com.lametric.YourAppId/1";
var my_access_token = "youraccesstokenhere";
////////////////////////////////////////////////

// put the other bit here from above that processes the data

http.open("POST", app_url, false);
http.setRequestHeader("Accept", "application/json");
http.setRequestHeader("X-Access-Token", my_access_token);
http.setRequestHeader("Cache-Control", "no-cache");
http.send(output);

 


1 person likes this

Cool... Thanks again Jessie.  I might play with this a little bit.  The bummer is that the Tigers are long since out of things so there isn't a use, at least for me, until Spring.  Some one might whip up something better than what I can do or it might even be officially supported by LM.  We'll see...


Maybe if I'm feeling really ambition I'll see if I can figure out if the NHL has a public api that I can pull this data for hockey games.  That could be fun.

I don't know how up to date this is, or how useful the data within will be for your purposes, but http://hfboards.hockeysfuture.com/showthread.php?t=1596119 might get you headed in the right direction for NHL :)


1 person likes this

Definitely a place to start looking!  Thanks.

Answer

Hey guys, here is tutorial we just made that can potentially explain how things are done on LaMetric and we are going to release few more that explains push apps and ability to use user authentication and options. Probably it will be useful especially for newcomers.

Login or Signup to post a comment