#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <string.h>
#include "serial.h"

int DEBUG_LEVEL = 0;

main()
{
  int gasGauge, cellVoltage[8],current;
  int temp[9], rH, balance;
  int tempAlert,voltAlert,currentAlert,batteryState;
  int batteryPort;
  char response[80], *resp;
  int status, i;
  short sval;
  FILE *log;
  time_t sampleTime;
  char logFileName[80];
  int statusRecvd;

  sprintf(logFileName,"%depi.log", time(NULL));
  log = fopen(logFileName,"a");

  status = openPort("tcp:134.89.32.126:10001", &batteryPort, 9600,8, 1, "none");
  if (!status) {
    perror("unable to open connection to battery:");
    exit(-1);
  }

  do {
    sendStr(batteryPort,"@RQ\n");
    sleep(1);
    if (checkPort(batteryPort) <= 0) continue;
    status = recvStr(batteryPort, response, 80);
    printf("status is %d\n", status);
    if (status==77) {
      resp = response;
      printf("response is %s\n", response);
      statusRecvd = 1;
      if (sscanf(resp,"%4x", &gasGauge)) resp+=4;
      for (i=0;i<8;i++) {
	if (sscanf(resp,"%4x", &cellVoltage[i])) resp+=4;
      }
      if (sscanf(resp,"%4x", &current)) resp+=4;
      for (i=0;i<9;i++) {
	if (sscanf(resp,"%2x", &temp[i])) resp+=2;
      }
      if (sscanf(resp,"%2x", &rH)) resp+=2;
      if (sscanf(resp,"%2x", &balance)) resp+=2;
      if (sscanf(resp,"%4x", &tempAlert)) resp+=4;
      if (sscanf(resp,"%4x", &voltAlert)) resp+=4;
      if (sscanf(resp,"%4x", &batteryState)) resp+=4;

      sampleTime = time(NULL);
      printf("time: %d\n", sampleTime);
      printf("gasGauge : %f\n", (float)gasGauge/100.0);
      printf("V1:%1.3f V2:%1.3f V3:%1.3f V4:%1.3f\n",
	     (float)cellVoltage[0]/1000.0,
	     (float)cellVoltage[1]/1000.0,
	     (float)cellVoltage[2]/1000.0,
	     (float)cellVoltage[3]/1000.0);

      printf("V5:%1.3f V6:%1.3f V7:%1.3f V8:%1.3f\n",
	     (float)cellVoltage[4]/1000.0,
	     (float)cellVoltage[5]/1000.0,
	     (float)cellVoltage[6]/1000.0,
	     (float)cellVoltage[7]/1000.0);

      sval = current;
      printf("Current: %.2f\n", (float)sval/100.0);
      printf("Stack Voltage: %3f\n",
	     (float)(cellVoltage[0]+
		     cellVoltage[1]+
		     cellVoltage[2]+
		     cellVoltage[3]+
		     cellVoltage[4]+
		     cellVoltage[5]+
		     cellVoltage[6]+
		     cellVoltage[7])/1000.0);
      printf("Temps(layer 8 to layer 1): %2d %2d %2d %2d %2d %2d %2d %2d\n",
	     temp[0],temp[1],temp[2],temp[3],temp[4],temp[5],temp[6],temp[7]);
      printf("Heatsink temp: %2d\n", temp[8]);
      printf("relative Humidity: %d\n", rH);
      printf("balance register: %2x\n", balance);
      if (tempAlert) printf("WARNING: ");
      printf("temp alert register: %2x\n", tempAlert);
      if (voltAlert) printf("WARNING: ");
      printf("volt alert register: %2x\n", voltAlert);
      printf("Battery Status Register: %4x\n", batteryState);
      printf("Fan state: %s\n", (batteryState&(1<<3))?"On":"Off");
      printf("Main Fuse: %s\n", (batteryState&(1<<4))?"Fault":"Good");
      printf("Safe Operating Area: %s\n", (batteryState&(1<<5))?"Fault":"Good");
      printf("Overcurrent: %s\n",  (batteryState&(1<<6))?"Fault":"Good");
      printf("Undervoltage: %s\n",  (batteryState&(1<<7))?"Fault":"Good");
      printf("Overvoltage: %s\n",  (batteryState&(1<<8))?"Fault":"Good");
      printf("Discharge Mode: %s\n",  (batteryState&(1<<9))?"On":"Off");
      printf("Charge Mode: %s\n",  (batteryState&(1<<10))?"On":"Off");
      printf("Discharge Transient: %s\n",  (batteryState&(1<<11))?"Fault":"Good");
      printf("Battery Cold Limit: %s\n",  (batteryState&(1<<12))?"Fault":"Good");
      printf("Heatsink Hot Limit: %s\n",  (batteryState&(1<<13))?"Fault":"Good");
      printf("Battery Hot Limit: %s\n",  (batteryState&(1<<14))?"Fault":"Good");
      printf("Battery Enable: %s\n",  (batteryState&(1<<15))?"On":"Off");

      printf("\n");
 
      fprintf(log, "%d,", sampleTime);
      fprintf(log, "%4.2f,", (float)gasGauge/100.0);
      sval = current;
      fprintf(log, "%.2f,", (float)sval/100.0);
      //	    fprintf(log, "%3.3f,", (float)(v1+v2+v3+v4+v5+v6+v7+v8)/1000.0);
      fprintf(log,"%d,", rH);
      fprintf(log,"%2x,", balance);
      fprintf(log,"%2x,", tempAlert);
      fprintf(log,"%2x,", voltAlert);
      fprintf(log,"%2x\n", currentAlert);
      fflush(log);
      //     	    syslog((int)LOG_ERR, "EPI Battery Volts: %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f %1.2f",
      //			(float)v1/1000.0, (float)v2/1000.0, (float)v3/1000.0,
      //			(float)v4/1000.0, (float)v5/1000.0, (float)v6/1000.0,
      //			(float)v7/1000.0, (float)v8/1000.0);
      syslog(LOG_ERR, "EPI Battery Current & Gas Gauge: %.2f %4.2f", 
	     (float)sval/100.0, (float)gasGauge/100.0); 
      syslog(LOG_ERR, "EPI Battery RH: %2d", rH);
      sleep(5);
    }
  } while (1);
}
