#include "tms_device.h"

extern Telemetry_String telstring; 
extern int tms_string_status;
int i_cab_out = 0;
int i_cab_speed = 0;
int i_tension = 0;
int i_pressure = 0;

TmsDevice::TmsDevice(char* devName, int freq, int type)
: SerialDevice(devName, freq, type) 
{}


TmsDevice::~TmsDevice()
{
	// release data???
	
}


bool
TmsDevice::parse()
{
        static int tms_string_timer;

        memset(this->rxbuffer, 0, 255);
	int n = read(this->handle(), this->rxbuffer, 255);
        if (n < 0)
		{
	        if(++tms_string_timer > 500000)  //this is about a second or so
	        	{
	        	tms_string_status = 0;
	        	tms_string_timer = 0;
	        	}
                return false;	
                }		
	this->rxbuffer[n] = 0;
	
	if (this->rxbuffer[34] == '*') {
 		for (int i = 0; i < 4; i++)
 			cab_speed[i] = this->rxbuffer[i+45];
		for (int i = 0; i < 4; i++)
			cab_out[i] = this->rxbuffer[i+68];
		for (int i = 0; i < 4; i++)
			pressure[i] = this->rxbuffer[i+106];
		for (int i = 0; i < 4; i++)
			tension[i] = this->rxbuffer[i+123];

		cab_out[4] = 0;
		cab_speed[4] = 0;
		tension[4] = 0;
		pressure[4] = 0;
		i_cab_out = atoi(cab_out);
		i_cab_speed = atoi(cab_speed);
		i_tension =  atoi(tension);
		i_pressure = atoi(pressure);

		tms_string_status = 1;
		tms_string_timer = 0;
		}
	else {
		//tms_string_status = 0;	
		}
	return true;
}


void
TmsDevice::setSystemData()
{

	telstring.TmsControlStringIn.cable_out = i_cab_out;
	telstring.TmsControlStringIn.cable_speed = i_cab_speed;
	telstring.TmsControlStringIn.tension = i_tension;
	telstring.TmsControlStringIn.pressure  = i_pressure;

}


int
TmsDevice::send()
{
	return 0;
}


