Thursday, March 29, 2012

MT Connecting to Twitter

Google Apps Script is awesome.  I've been using Google Apps for years, but until recently (when adding a snooze function to gmail) I had never used or really noticed Google Apps Script.  I'm going to abbreviate it to GAS, which isn't great, but I didn't pick the name.

So, GAS is basically the VBA of Google Docs, in that it's designed to help you integrate many of their services together.  I think GAS is even better than VBA because, for one, you get to write JavaScript, which, outside the browser context, is a joy to use.  It's great that you can just add new methods/properties all willy nilly, which, for someone who's been stuck with VBA for going on 9 years, is pretty great.

Another reason to love GAS (sorry) is that your scripts live "in the cloud", and can be run at periodic intervals.  Combine that with Google Docs' spreadsheet module, and you can do some interesting data collection without having to run software on your own computer.

And now I'll start getting to the point.  Because MT Connect is built on the same technologies as the web, we can use many of GAS's built-in XML tools to pretty easily parse out an Agent's data stream.  Also, remember how I said JavaScript lets you add new properties to objects willy nilly?  Well, the XML library adds XML attributes and nodes to objects, rather than making you have to call routines to check for them and get their values (I'm looking at you, VBA).

So, to show how cool this is, I put together a GAS App in a Google Docs spreadsheet that runs a function every minute, to grab the current power status of our Yasda H40i milling machine, and store the latest value in a spreadsheet cell.  Any time that value changes, the script calls a function to send a tweet on the Yasda machine's Twitter account (@CCATYasda5) denoting the new power status.  Ridiculous?  Absolutely.

At some point I may dig deeper into this tutorial and see if I can set up a UI where you can simply enter a few items (agent URL, device UUID, and data item(s) to monitor) and have it authenticate into your own Twitter account.  But the fact of the matter is, Google App Script made it very easy for me to pull in XML data over the web, pull up the previous status information, and handle authentication into Twitter with very little effort on my part, all for free, running on the web 24x7, which is why Google Apps Script is awesome.