/*
PROGRAM NAME	UMDauto.DLL

USED BY			BBC Academy Bredon Room 4 installation

VERSION			1.0
DATE			15th March 2015
PROGRAMMER		Andy Woodhouse

Overview
========
This module runs as an automatic panel whose function is to monitor destinations on the audio and video router and 
send the names of the current sources to the TSL UMD under the audio and video monitoring units.

It UMD serial data stream is created by the BNCS TSL UMD driver. This driver hooks into an infodriver with the same
driver id as the UMD driver. 

Infodriver slots 1 to 128 (non-virtualised mode) or 1 to 256 (virtualised mode) are used for the names to be sent to
the disply units.

*/

#include <windows.h>
#include <stdio.h>
#include <bncs_string.h>
#include <bncs_config.h>
#include "UMDauto.h"

#define MAIN_PANEL	1
#define MAIN_PANEL_NAME "UMDauto.bncs_ui"

#define VIDMON_DEST 64
#define AUDMON_DEST 64

// Make our class visible to the outside world
EXPORT_BNCS_SCRIPT( UMDauto )


// ++++++++++++++++++++++++++++++++++++++++++++ //
// Constructor - equivalent to ApplCore STARTUP //
// ++++++++++++++++++++++++++++++++++++++++++++ //
UMDauto::UMDauto( bncs_client_callback * parent, const char * path ) : bncs_script_helper( parent, path )
{
	// Initialize variables
	video_id = 0;		// B4 BNCS video driver id 
	audio_id = 0;		// B4 BNCS audio driver id
	umd_id = 0;			// B4 BNCS umd driver id
	for (int i = 0; i <= MATRIXSIZE; i++) UMD_V_addr[i] = 0;
	for (int i = 0; i <= MATRIXSIZE; i++) UMD_A_addr[i] = 0;

	// Load the UMD arrays with the data for our monitoring task
	UMD_V_addr[VIDMON_DEST] = 1;	// Video tally goes to address 1
	UMD_A_addr[AUDMON_DEST] = 2;	// Audio tally goes to address 2

	// Show our mimic panel
	panelShow(MAIN_PANEL, MAIN_PANEL_NAME);

	// Get the ids of the drivers from instance.xml
	getDev("B4 SDI Router", &video_id);
	getDev("B4 Audio Router", &audio_id);
	getDev("B4 UMD Driver", &umd_id);

	// Update the mimic displays with our router data
	textPut("text", bncs_string(video_id), MAIN_PANEL, "VideoID");
	textPut("text", bncs_string(audio_id), MAIN_PANEL, "AudioID");
	textPut("text", bncs_string(umd_id), MAIN_PANEL, "UMD_ID");

	// Register for revertives
	routerRegister(video_id, 1, MATRIXSIZE);
	routerRegister(audio_id, 1, MATRIXSIZE);

	// Poll our known destinations
	routerPoll(video_id, VIDMON_DEST, VIDMON_DEST);
	routerPoll(audio_id, AUDMON_DEST, AUDMON_DEST);
}


// +++++++++++++++++++++++++++++++++++++++++++++ //
// Destructor - equivalent to ApplCore CLOSEDOWN //
// +++++++++++++++++++++++++++++++++++++++++++++ //
UMDauto::~UMDauto()
{
}


// ++++++++++++++++++++++++++++++++++++++++++++++ //
// All button pushes and notifications come here. //
// ++++++++++++++++++++++++++++++++++++++++++++++ //
void UMDauto::buttonCallback( buttonNotify *b )
{
	// No buttons on our mimic panel.
}

// all revertives come here
int UMDauto::revertiveCallback( revertiveNotify * r )
{
	// We expect revertives from the video and audio routers
	if (r->device() == video_id)
	{
		int dest = 0;

		dest = r->index();
		if (UMD_V_addr[dest] != 0)
		{
			// Found a non-zero UMD address to tally
			routerName(video_id, 2, r->info(), vid_name);
			textPut("text", vid_name, MAIN_PANEL, bncs_string("UMD_%1").arg(UMD_V_addr[dest]));
			infoWrite(umd_id, vid_name, UMD_V_addr[dest]);
		}
		return 0;
	}

	if (r->device() == audio_id)
	{
		int adest = 0;

		adest = r->index();
		if (UMD_A_addr[adest] != 0)
		{
			// Found a non-zero UMD address to tally
			routerName(audio_id, 2, r->info(), aud_name);
			textPut("text", aud_name, MAIN_PANEL, bncs_string("UMD_%1").arg(UMD_A_addr[adest]));
			infoWrite(umd_id, aud_name, UMD_A_addr[adest]);
		}
		return 0;
	}


	return 0;
}


// +++++++++++++++++++++++++++++++++++++++++ //
// All database name changes come back here. //
// +++++++++++++++++++++++++++++++++++++++++ //
void UMDauto::databaseCallback( revertiveNotify * r )
{
}


// ++++++++++++++++++++++++++++++++++++++++++++ //
// All parent notifications come here.          //
// For this auto panel, no parent comms needed. //
// ++++++++++++++++++++++++++++++++++++++++++++ //
bncs_string UMDauto::parentCallback( parentNotify *p )
{
	return "";
}

// timer events come here
void UMDauto::timerCallback( int id )
{
	switch( id )
	{

	default:	// Unhandled timer event
		timerStop(id);
		break;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// Callbacks above - Methods below ///////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////


