nodejs - tvguide
================

[8-4-2015] Time fix... again
----------------------------
Since I've been quite active with my Pi home automation lately, I thought I'd finally tackle the still sketchy wait time calculation. The main issue wasn't the code so much as my Pi not being set to the correct timezone - although there may have been something wrong with the code (at the very least it's cleaner now).

If you're using Raspbian (or any kind of Linux, I suppose), check if your timezone is correct by typing
<pre>
date
</pre>
in your terminal. If this doesn't show the correct time AND timezone offset, do the following:
<pre>
sudo cp /usr/share/zoneinfo/Europe/Amsterdam /etc/localtime
</pre>
Make sure that after /zoneinfo/ you choose your correct location!

Description
-----------
tvguide is a Node.JS module that takes data from tvgids.nl and calls back with a list of new show information every time a new show starts. The module can be used to make a web app that provides real-time tv show information on the channels of your choice.
Currently, only the running and next shows are listed. Maybe I will implement a full guide later.

Install
-------
As always, use the npm package manager:
<pre>
npm install tvguide
</pre>

Next, include the module with a require at the top of your node server script
<pre>
var tvguide = require('tvguide');
</pre>
Below is the full reference, but for the trial-and-error users, the following snippet is all that is implemented in my own server.js:
<pre>
var channels = [1, 2, 3, 4, 31, 36, 46, 92, 34, 37, 89, 25, 91, 18, 29];

/* Setup and start tvguide
 */
tvguide.SetDebug(debug);
tvguide.SetGuideTimezoneOffset(0);
tvguide.SetChannels(channels, function () {
    /* Start auto-updating guide 
     */
    tvguide.Start(function (err, channels, guide) {
        if (err) throw new Error(err);
        /* Will be called each time there is/are new show(s)
         * @channels channels that have new shows
         * @guide the entire guide
         */
        io.sockets.emit(updateTrigger, channels, guide);
    });
    /* Start auto-updating show progress getter
     * Called every 60 s, since seconds are not calculated
     */
    tvguide.StartProgress(60000, function (progresses) {
        /* Will be called every minute, @callback(updated progresses) 
         */
        io.sockets.emit(progressTrigger, progresses);
    });
});
</pre>

The fun stuff
-------------
This NPM library is part of a bigger project - which is a universal remote/tv guide web app hosted on a Raspberry pi. The ingredients:

* A Raspberry pi running Raspbian (will also test with ArchLinux)
* Node.JS for Raspbian (see http://weworkweplay.com/play/raspberry-pi-nodejs/)
* LIRC (definitely check out http://alexba.in/blog/2013/01/06/setting-up-lirc-on-the-raspberrypi/; very clear information on the remote control part of the project - this is where i got 90% of my info from)
* A simple IR-led cicuit (from the previous source: http://upverter.com/alexbain/f24516375cfae8b9/Open-Source-Universal-Remote/). I did, however, make some changes:
  * No resistor between the gpio pin and the base of the transistor
  * A current-limiting resistor of 5.6 Ohm between the source voltage and the emitter
* To get you going, there is a small node.js script and basic HTML web-app provided in the 'example' folder. I am developing this seperately on https://github.com/jouweneel/tvguideRemote

Function reference
==================

Channel list functions
----------------------
The channel list is an array of numbers representing a TV channel.
<pre>
/* Lists all available channels in console 
 */
ListChannels: function ()

/* Returns the channel list 
 * @return channel_list
 */
GetChannelList: function()

/* Set the channels to be retrieved to the guide
 * Use ListChannels to see all available channels 
 * Order them in the way you want them to appear in the guide
 * @new_channels array of new channels
 * @callback(error) when the new channels are retrieved
 */
SetChannels: function (new_channels, callback)
</pre>

Guide fields functions
----------------------
The guide fields is an array of strings representing channel information (show name, start time, etc.)
Channels are indexed 0,1,2,... in the order defined by the channel list (previous section).
<pre>
/* Lists all available guide fields in console
 */
ListGuideFields: function ()

/* Gets all available guide fields
 */
GetGuideFields: function ()

/* Set the fields to be retrieved to the guide
 * Use ListGuideFields to see all available fields. 
 * Order doesn't matter, as long as 'eindtijd' is the final one!
 * @new_guide_fields array of new guide fields
 * @callback(error) when the new fields are retrieved
 */
SetGuideFields: function(new_guide_fields, callback)
</pre>

Guide functions
---------------
The guide contains an updated version of all channels in the channel list. It contains the currently playing and the next show. For some reason, the Date() function in node doesn't always adjust for local timezone, so a function to adjust for the timezone is present.
<pre>
/* Gets the current local guide 
 * @return guide
 */
GetGuide: function()

/* Set the timezone offset
 * @new_timezone_offset Time offset in hours
 * @callback(error) when the new times are adjusted
 */
SetGuideTimezoneOffset: function (new_timezone_offset)
</pre>

Updater functions
-----------------
The updater auto-updates the guide when new show information is available. In order to acces the guide as soon as it's updated.

<pre>
/* Starts the auto-updating TV guide
 * @callback(error, channels, guide) everytime there is updated data
 *     @channels array of updated channels
 *     @guide the entire updated guide
 * The including server should use this to update connected clients
 */
Start: function (trigger)

/* Starts interval calculation of current show progresses
 * @interval Interval to call the callback
 * @trigger(progresses) current progresses at the requested interval
 *      @progresses array of current progress per channel in percent
 */
StartProgress: function (interval, trigger)

/* Stops the auto-updater
 */
Stop: function(callback)
</pre>
