//------------------------------------------------------------------// // freezer.cc // // Latest revision: 06-10-2003 // //------------------------------------------------------------------// #include "freezer.h" speed_t BAUDRATE = B300; Alarm *a; Freezer *f; int ALARM_COUNT=0; enum{ OFF, READY, RINGING }; const int SLEEPTIME=10; // seconds between successive readings int main(void) { int fd[MAX_PORTS]; initialize_app_globals(); //// Open any additional ports here fd[0] = open_port(SERIAL_PORT_0); fd[1] = open_port(SERIAL_PORT_1); sleep(5); initialize_curses(); clear_screen(); monitor(fd); endwin(); // reset terminal //// Close any additional ports here close(fd[1]); close(fd[0]); exit(EXIT_SUCCESS); } //------------------------------------------------------------------// // initialize_app_globals // // Change alarm threshold and initial states here. // //------------------------------------------------------------------// void initialize_app_globals(void) { int frzr,k,count=0,cmd_start,location=0,label_start,skip=0; a = new Alarm[ALARMS]; f = new Freezer[FREEZERS]; char tempstring[FILENAMELENGTH]; char label[FILENAMELENGTH]; FILE *fp; for(k=0;k= FREEZERS) { printf("Error reading alarms\n"); exit(1); } f[frzr].alarm_state = READY; if(feof(fp)) break; count++; if(count >= ALARMS) { printf("Too many alarms\n"); exit(1); } } ALARM_COUNT = count; if(!count){ printf("No alarms\n"); exit(1); } fclose(fp); } } //------------------------------------------------------------------// // find_command_start // //------------------------------------------------------------------// int find_command_start(char *string) { int start=0,k,hit=0,inspace=0; for(k=0;k<(int)strlen(string);k++) { if(string[k]==' ' && !inspace){ inspace=1; hit++; } if(string[k]!=' ') { inspace=0; if(hit==2){ start=k; break; } } } return start; } //------------------------------------------------------------------// // initialize_curses // //------------------------------------------------------------------// void initialize_curses(void) { initscr(); // initialize curses cbreak(); // put terminal in raw mode noecho(); // no echo nonl(); // no nl intrflush(stdscr, FALSE); // no flush stdscr on ^C keypad(stdscr, TRUE); // enable numeric keypad move(0,0); // moveto upper left nodelay(stdscr,TRUE); // make getch() nonblocking } //------------------------------------------------------------------// // open_port // //------------------------------------------------------------------// int open_port(const char *port_name) { int fd; int port = 0x2e8; static struct serial_struct ss; static struct termios newtio; //// Open port if((fd = open(port_name, O_RDWR | O_NOCTTY | O_NDELAY )) < 0) { printf("Cant open serial port %s\n", port_name); exit(1); } //// Find out what port and get permission to write to it if(ioctl(fd, TIOCGSERIAL, &ss)) { printf("ioctl(TIOCSERGSTRUCT) failed: -- exiting\n"); exit(1); } //// Should be 3f8 for cua0, 2f8 for cua1, 3e8 for cua2, 0x2e8 for cua3 port=ss.port; printf("%s port %x\n" ,port_name, port); //// Enable asynchronous I/O if(fcntl(fd, F_SETFL, FNDELAY)) printf("fcntl didnt work\n");; printf("Opened port %s, fd = %d\n", port_name, fd); //// Obtain current port settings tcgetattr(fd, &newtio); //// Set new port settings newtio.c_cflag |= (CLOCAL | CREAD); newtio.c_cflag &= ~PARENB; newtio.c_cflag &= ~CSTOPB; newtio.c_cflag &= ~CSIZE; newtio.c_cflag |= CS8; newtio.c_cflag &= ~CRTSCTS; newtio.c_lflag &= ~(ICANON | ECHO | ISIG); newtio.c_cc[VMIN]=1; newtio.c_cc[VTIME]=0; // Tenths of seconds before timeout tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); //// Set baud rate cfsetispeed(&newtio, BAUDRATE); cfsetospeed(&newtio, BAUDRATE); tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &newtio); return fd; } //------------------------------------------------------------------// // monitor // // It is necessary to have getch() all over the place, otherwise // // ncurses won't update the screen. // //------------------------------------------------------------------// void monitor(int file_descriptor[]) { int freezer,c=0,alarm,ringcount=0,port,fd,row,channel; clear_screen(); print(1,0,"In monitor"); char blank_string[81]; memset(blank_string, ' ', 80); blank_string[79]=0; //// NOTE: channels 0,1,2,3 are read using commands $1,$2,$3,and $4 //// respectively. char *command[FREEZERS_PER_PORT] = { { "$1RD\r\n" }, { "$2RD\r\n" }, { "$3RD\r\n" }, { "$4RD\r\n" } }; int k=0,n; char tempstring[128]; char answer[128]; char *buffer; buffer = new char[128]; print(1,0,"Entering loop"); nodelay(stdscr,TRUE); // make getch() nonblocking while((c=getch())!='q') { if(c=='q') break; if((c=getch())=='q') break; if((c=getch())==27) break; for(freezer=0; freezer= a[alarm].threshold && f[freezer].alarm_state != OFF) { alarm_ring(alarm); f[freezer].alarm_state = RINGING; a[alarm].state = RINGING; } } } //// Reset alarms if temp is gone back down for(alarm=0; alarm\n"); fprintf(fp,"\n\nTemperatures\n\n\ \n\

Freezer temperatures at %s

\n\ Time stamp: %ld
\n\ \n\ \n", tempstring,current_time); for(k=0;k\n", f[k].temperature, f[k].alarm_state_string, f[k].location); fprintf(fp,"
Temp. Alarm state Location %+f \t %s\t %s
\n\n\n"); fclose(fp); print(20,1," "); system("/home/freezer/logcommands"); } //--------------------------------------------------------------------// // print // //--------------------------------------------------------------------// void print(int row, int col, int number) { char tempstring[80]; sprintf(tempstring,"%d ",number); move(row,col); addstr(tempstring); getch(); } void print(int row, int col, double number) { char tempstring[80]; move(row,col); sprintf(tempstring,"%.2f ",number); addstr(tempstring); getch(); } void print(int row, int col, char *string) { int k; char s[strlen(string)+1]; strcpy(s,string); for(k=0;k<(int)strlen(s);k++) if(s[k]<' ') s[k]=' '; move(row,col); addstr(s); getch(); } //--------------------------------------------------------------------// // alarm_ring // //--------------------------------------------------------------------// void alarm_ring(int alarm) { char tempstring[FILENAMELENGTH]; int freezer = a[alarm].freezer; f[freezer].alarm_state=RINGING; int status=0; print_status(freezer); log_temperatures(); sprintf(tempstring, "Executing %s", a[alarm].command); tempstring[79]=0; print(20, 0, tempstring); getch(); //// Needed to make sure it prints sleep(1); sprintf(tempstring,"Freezer %d temperature %g alarm %d\n", freezer, f[freezer].temperature, alarm); syslog(LOG_WARNING, tempstring); print(20,1," "); status = system(a[alarm].command); if(status!=0) { sprintf(tempstring,"Error executing alarm command: \n%d %s\n", freezer, a[alarm].command); syslog(LOG_WARNING, tempstring); } } //--------------------------------------------------------------------// // print_status // //--------------------------------------------------------------------// void print_status(int freezer) { int state; state = f[freezer].alarm_state; switch(state) { case OFF: strcpy(f[freezer].alarm_state_string,"Inactive "); break; case READY: strcpy(f[freezer].alarm_state_string,"Alarm ready "); break; case RINGING: strcpy(f[freezer].alarm_state_string,"ALARM TRIGGERED "); break; } print(5+freezer, 14, f[freezer].alarm_state_string); print(20,1," "); } //--------------------------------------------------------------------// // clear_screen // //--------------------------------------------------------------------// void clear_screen(void) { clear(); print(0,0,"Freezer temperature monitor"); print(3,2,"Temp. Alarm state Location"); print(4,2,"--------- ------------- --------"); } //--------------------------------------------------------------------------// // remove_terminal_cr // //--------------------------------------------------------------------------// void remove_terminal_cr(char *s) { int pos = max(0, (int)(strlen(s))-1); if(s[pos]=='\n') s[pos]=0; } //--------------------------------------------------------------------------// // between - returns 1 if a is between b and c // //--------------------------------------------------------------------------// int between(int a,int b,int c) { if((a>=b)&&(a<=c)) return(1); else return(0); }