/* Copyright 2025 by COG9 Enterprises LLC
 * All rights reserved.
 * Title: netmon.ino
 * Author: Jed Marti KI7NNP
 * Description: A serial port command wants a web page served. This will have the current
 *   draws, voltage and what not.
 * Created: Mon Dec 29 16:36:50 2025
 * Revision History:
 */


#include "powerard.h"
#include "eepromconst.h"

extern eePROMValues E;

// The three amperage sensors.
extern INA219 MA;
extern INA219 MB;
extern INA219 MC;

// We handle 4 devices.
extern uint8_t aTemp_i2c;
extern uint8_t bTemp_i2c;
extern uint8_t cTemp_i2c;

// Lifetime consumption (since power on).
extern uint32_t lifeTimeA;
extern uint32_t lifeTimeB;
extern uint32_t lifeTimeC;
extern uint32_t lifeTimeCount;

void netpage(void)
{
  char  msg[60];
  float v;
  float asecs, whours;
  
  Serial.write(0x01);                // Indicates start.
  Serial.print(F("<html>\n"));
  Serial.print(F("<head>\n"));
  Serial.print(F("<style>\n"));
  Serial.print(F("table, th, td { border-spacing: 2px; border-color: gray; border: 1px solid black; }\n"));
  Serial.print(F("</style>\n"));
  Serial.print(F("<title> KI7NNP Power Supply </title>\n"));
  Serial.print(F("</head>\n"));
  Serial.print(F("<body>\n"));
  if (E.eeOwner[0] != 0)
  {
    snprintf(msg, 60, "<h1 align=\"center\"> %s Power Status</h1>\n", E.eeOwner);
    Serial.print(msg);
  }

  snprintf(msg, 60, "<p><b>POWERARD</b> V%d.%02d.%03d", VERSION, RELEASE, PATCH);
  Serial.print(msg);
  
  // Battery voltage.
  v = raw2Voltf(batRaw());
  snprintf(msg, 60, "<p> Battery %2d.%02d volts\n", int(v), frac(v));
  Serial.print(msg);

  if (isOK(MA))
  {
    v = ina219GetVoltage(&MA);
    whours = MA.Wms / 36000.0f;
    snprintf(msg, 60, "<p> <b>A: </b>%2d.%02dV %4dma %3dF %3d.%02d Wh\n", int(v), frac(v),
             int(ina219GetCurrent(&MA)), getFahrenheit(aTemp_i2c), int(whours), frac(whours));
    Serial.print(msg);
  }

  if (isOK(MB))
  {
    v = ina219GetVoltage(&MB);
    whours = MB.Wms / 36000.0f;
    snprintf(msg, 60, "<p> <b>B: </b>%2d.%02dV %4dma %3dF %3d.%02d Wh\n", int(v), frac(v),
             int(ina219GetCurrent(&MB)), getFahrenheit(aTemp_i2c), int(whours), frac(whours));
    Serial.print(msg);
  }

  if (isOK(MC))
  {
    v = ina219GetVoltage(&MC);
    whours = MC.Wms / 36000.0f;
    snprintf(msg, 60, "<p> <b>C: </b>%2d.%02dV %4dma %3dF %3d.%02d Wh\n", int(v), frac(v),
             int(ina219GetCurrent(&MC)), getFahrenheit(cTemp_i2c), int(whours), frac(whours));
    Serial.print(msg);
  }

  Serial.print("</body>\n");
  Serial.print("</html>\n");
  Serial.write(4);                 // End of file.
}


// EOF
