// Read analog inputs from shared memory and output formatted HTML

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "../io/io2.h"
#include "../usr/include/mysql/mysql.h"

#define NUM_CHANNELS 8

char *split(char *source, char **p, char **v)
{

  char *amp, *eq;

  /* if there's an = sign, make it an end-of-line */
  if(eq=strchr(source,'=')){
    eq[0] = '\0';
    *p = source;
    *v = eq+1;
    eq++;
    if(amp=strchr(eq,'&')){
      amp[0] = '\0';
      return(amp+1);
    }else{
      return(0);
    }
  }
  return(0);
}

main(){
  float reading;
  int c,i,j;
  char bgcolor[10]; 
  char entry[100];
  char chname[20];
  char *next, *p[10], *v[10];
  int intval;
  int shmid, ddata, ddir, dio1, dio2, mybit;
  key_t key;
  char *shmat();
  //short *shm, *s;
  shmstruct *shm;
   
  MYSQL server;
  MYSQL_RES *result;
  MYSQL_ROW row;
  char *query[128];
  
  // Start HTML
  printf ("Content-type: text/html\n\n<html><head><link rel=stylesheet type=text/css href=http://www.nofossil.org/arm.css></head><body bgcolor=lightblue><h2>System status</h2>\n");
  
  mysql_init(&server);
  if (!(mysql_real_connect(&server,"copper.cld.home","wkuhns",NULL,"sensor",0,NULL,0))){
    printf("<h1>Error Connecting</h1></body></html>\n");
    abort();
  }
  
  // Key for out shared memory is 9700
  
  key = 9700;
  
  // Create our shared memory
  if ((shmid = shmget(key, sizeof(shmstruct), 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  //printf("Shm1 OK\n");
    
  // Link to our shared memory
  if ((shm=shmat(shmid, NULL, 0)) == (char *) -1) {
    perror("shmat");
    exit(1);
  }
  //printf("Shm2 OK\n");
  
  // Get form data
  entry[0] = 0;
  fscanf(stdin,"%s",&entry);
  //printf("fd0 OK: %s\n",entry);
  j = 0;
  next = &entry[0];
  //printf("fd1 OK\n");
  
  while(next=(split(next,&p[j],&v[j]))){
    j++;
  }
  //printf("fd2 OK: params=%d\n",j);

  // Set default channel number
  c = 0;
  
  if (j>0){
    for(i=0;i<j;i++){
      //printf("p1... \n");
      if(!strcmp(p[i],"channel")){
        sscanf(v[i],"%d",&c);
        //printf("Channel=%d<br>\n",c);
      }
      //printf("OK.\n p2... ");
      if(!strcmp(p[i],"dir")){
        sscanf(v[i],"%x",&intval);
        shm->diodir[c] = (byte) intval;
        //printf("Dir=%x<br>\n",shm->dio[c].dir);
      }
      //printf("OK.\n p3... ");
      if(!strcmp(p[i],"data")){
        sscanf(v[i],"%x",&intval);
        shm->diovalue[c] = (byte) intval;
        //printf("Data=%x<br>\n",shm->dio[c].value);
      }
      //printf("xx\n");
    }
  }
  //printf("<img src=/armsw/7260/images/50.gif height=131 width=10 align=right>\n");
  
  printf ("<table class=framed bgcolor=black cellpadding=0>\n");
  printf ("<tr><th bgcolor=white>Channel</th><th bgcolor=white>raw a/d</th><th bgcolor=white>Name</th><th bgcolor=white>deg. C</th><th bgcolor=white>deg. F</th><th background=/armsw/7260/images/scale.gif>&nbsp;</th></tr>\n");
  
  sprintf(query,"SELECT hwch, name from channel where apid = 6 and ctype='analog' order by board,hwch");
  // Query mysql server
  if (mysql_query(&server, query)){
    printf("<h1>Error querying database</h1>%s</body></html>\n",query);
    abort();
  }
  result = mysql_store_result(&server);

    
  // Cycle through channels
  for (j=0; j<mysql_num_rows(result); j++){
    row=mysql_fetch_row(result);
    if (shm->adstatus[j]){
      reading = shm->advalue[j];
      printf ("<tr><td align=right class=padded bgcolor=lightgreen>%d</td>\n",j);
      printf ("<td align=right class=padded bgcolor=lightgreen>%d</td>\n",shm->adraw[j]);
      printf ("<td align=left class=padded bgcolor=lightgreen>%s</td>\n",row[1]);
      printf ("<td align=right class=padded bgcolor=lightgreen>%1.1f</td>\n",reading);
      printf ("<td align=right class=padded bgcolor=lightgreen>%1.1f</td>\n",reading*1.8+32);
      if (j==8 || j==9){
        printf ("<td background=/armsw/7260/images/bars.gif align=left width=262><img src=/armsw/7260/images/pxred.gif height=10 width=%d></td></tr>\n",(int)(reading*.36-36));
      }else{
        printf ("<td background=/armsw/7260/images/bars.gif align=left width=262><img src=/armsw/7260/images/pxred.gif height=10 width=%d></td></tr>\n",(int)(reading*3.6-36));
      }
    }else{
      printf ("<tr><td align=right class=padded bgcolor=lightgreen>%d</td>\n",j);
      printf ("<td align=right class=padded bgcolor=lightgreen>%d</td>\n",shm->adraw[j]);
      printf ("<td align=left class=padded bgcolor=lightgreen>%s</td>\n",row[1]);
      printf ("<td colspan=2 align=right class=padded bgcolor=lightgreen>Out Of Range</td>\n");
      printf ("<td bgcolor=lightgrey align=left width=131>&nbsp;</td></tr>\n");
    }
  }
  printf("</table>\n");

  // Digital IO
  printf ("<h2>Digital I/O</h2>\n");
  
  // Container table
  printf("<table border=0 cellpadding=10><tr>\n");
  
  sprintf(query,"SELECT hwch, name, board from channel where apid = 6 and ctype='digital' order by board,hwch");
  // Query mysql server
  if (mysql_query(&server, query)){
    printf("<h1>Error querying database</h1>%s</body></html>\n",query);
    abort();
  }
  result = mysql_store_result(&server);

    
  // Cycle through channels
  j=-1;
  i = 0;

  while(row=mysql_fetch_row(result)){
    if (j != atoi(row[2])){
      if (j > -1){
        printf ("</table></td>\n");
      }
      j = atoi(row[2]);
      i = 0;
      ddata = shm->diovalue[j];
      ddir = shm->diodir[j];
      printf ("<td><table class=framed bgcolor=black>\n");
      printf ("<tr><th bgcolor=white>Block %d</th><th bgcolor=white>dir</th><th bgcolor=white>&nbsp</th></tr>\n",j);
    }

    printf ("<tr><td class=padded bgcolor=lightgreen>%d: %s</td>",i,row[1]);
    if(ddir & 1 << i){
      printf ("<td class=padded bgcolor=lightgreen>Out</td>");
    }else{
      printf ("<td class=padded bgcolor=lightgreen>In</td>");
    }    
    
    if(ddata & 1 << i){
      strcpy(&bgcolor,"red");
    }else{
      strcpy(&bgcolor,"grey");
    }    
    printf ("<td bgcolor=%s class=padded>&nbsp</td></tr>\n",bgcolor);
    i++;
  }
  printf ("</table></td>\n");

  printf ("</tr></table>\n");
    
  // Refresh button
  //printf("j = %d, data = %d",j,ddata);
  printf("<form action=7260client method=get><input type=submit name=Submit value=Refresh></form><br>\n");
  printf("<form action=/index.html><input type=submit name=Submit value=Home></form>\n");

}

