//----------------------------------------------------------------------------- // Copyright 2004 //----------------------------------------------------------------------------- // Project : AO-27 // Title : TOPR Schedule // Designer : Michael Wyrick // Platform : Linux 2.4.x // Language : GNU C++ // Date : 1/4/2004 //----------------------------------------------------------------------------- // Purpose : To List the next TOPR Events // Docs : //----------------------------------------------------------------------------- // RCS: // $Locker: wyrick $ // $Id: $ // $Log: $ // //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- // Includes //----------------------------------------------------------------------------- #include #include #include #include #include #include #include //----------------------------------------------------------------------------- // Globals //----------------------------------------------------------------------------- //TSatellite *satellite = NULL; char SatName[] = "22825"; unsigned long op; time_t epoch; typedef struct { unsigned short dur[7]; unsigned char mode[7]; } TTOPR; TTOPR newTOPR; char* ModeStr(int mode) { switch (mode) { case 0 : return "OFF"; case 1 : return "Digital Exciter"; case 2 : return "Digital Med"; case 3 : return "Digital High"; case 4 : return "Analogue Exciter"; case 5 : return "Analogue Med"; case 6 : return "Analogue High"; default: return "Unknown"; } } //----------------------------------------------------------------------------- // Function : PrintTOPR // Inputs : // Outputs : // Description: // Remarks : // History : //-------------------------------------------------------------------------mjw- void PrintTOPR(int currentstate) { printf("TOPR Epoch (UTC) = %s", asctime(gmtime(&epoch))); printf("TOPR Epoch (ctime) = %i\n", (int)epoch); printf("Schedule Period (sec) = %4.3f\n", (op / 1000.0)); printf("\n"); printf("State Start Time End Time Duration(sec) Mode\n"); printf("--------------------------------------------------------\n"); int totaldur = 0; tm *st; time_t t; for(int i=0; i < 7; i++) { if (i == currentstate) printf(""); printf(" %i", i); t = (time_t)totaldur; st = gmtime(&t); printf(" %02i:%02i:%02i",st->tm_hour, st->tm_min, st->tm_sec); totaldur += newTOPR.dur[i]; t = (time_t)totaldur; st = gmtime(&t); printf(" %02i:%02i:%02i",st->tm_hour, st->tm_min, st->tm_sec); printf(" %4i %s\n", newTOPR.dur[i],ModeStr(newTOPR.mode[i])); if (i == currentstate) printf(""); } int dur7 = (int)(op / 1000.0) - totaldur; if (currentstate == 7) printf(""); printf(" 7"); printf(" %02i:%02i:%02i",st->tm_hour, st->tm_min, st->tm_sec); totaldur += dur7; t = (time_t)totaldur; st = gmtime(&t); printf(" %02i:%02i:%02i",st->tm_hour, st->tm_min, st->tm_sec); printf(" %4i %s\n", dur7, ModeStr(0)); if (currentstate == 7) printf(""); } //----------------------------------------------------------------------------- // Function : Find the State for time t // Inputs : // Outputs : // Description: // Remarks : // History : //-------------------------------------------------------------------------mjw- int FindState(time_t t, int &ac) { unsigned long sec = (t - epoch) * 1000; time_t stime = (time_t)(((sec) % op) / 1000.0); int state = 7; ac = newTOPR.dur[0]; for(int s = 0; s < 7; s++) { if (stime < ac) { state = s; break; } ac += newTOPR.dur[s + 1]; } return state; } //----------------------------------------------------------------------------- // Function : TOPRstuff // Inputs : // Outputs : // Description: // Remarks : // History : //-------------------------------------------------------------------------mjw- int TOPRstuff() { // // Update TOPR Times // time_t currenttime; time(¤ttime); printf(""); printf("Current Time UTC => %s", asctime(gmtime(¤ttime))); printf(""); unsigned long sec = (currenttime - epoch) * 1000; time_t stime = (time_t)(((sec) % op) / 1000.0); tm *st = gmtime(&stime); printf("Schedule Time => %02i:%02i:%02i\n",st->tm_hour, st->tm_min, st->tm_sec); int ac; //Accumlative Time int state = FindState(currenttime, ac); printf("Current State => %i\n", state); printf(""); printf(" Mode => %s\n", ModeStr(newTOPR.mode[state])); printf(""); time_t NextSch = 0; if (state < 7) { NextSch = ac - stime; } else { NextSch = (op / 1000) - stime; } time_t ne = currenttime + NextSch; char nes[25]; strncpy(nes, asctime(gmtime(&ne)), 24); nes[24] = 0; st = gmtime(&NextSch); int nextstate = state + 1; if (nextstate >= 8) nextstate = 0; printf(" Remaining => %02i:%02i:%02i\n",st->tm_hour, st->tm_min, st->tm_sec); printf("\n"); printf("Next Event UTC => %s",asctime(gmtime(&ne))); printf(" State => %i\n", nextstate); printf(" Mode => %s\n", ModeStr(newTOPR.mode[nextstate])); return(state); } //----------------------------------------------------------------------------- // Function : // Inputs : // Outputs : // Description: // Remarks : // History : //----------------------------------------------------------------------------- void Table() { time_t currenttime; time(¤ttime); int a; int state = FindState(currenttime, a); int Mode = newTOPR.mode[state]; printf("Current Mode = %s\n", ModeStr(Mode)); printf(" Time (UTC) Mode\n"); printf("------------------------------------------\n"); for(int i=0; i < 86400; i++) { time_t newtime = (time_t)(currenttime + i); state = FindState(newtime, a); if (Mode != newTOPR.mode[state]) { Mode = newTOPR.mode[state]; char tstr[30]; strncpy(tstr, asctime(gmtime(&newtime)), 24); tstr[24] = 0; printf("%s %s\n", tstr, ModeStr(Mode)); } } } //----------------------------------------------------------------------------- // Function : // Inputs : // Outputs : // Description: // Remarks : // History : //----------------------------------------------------------------------------- int ReadEpoch(void) { op = 0; epoch = 0; // Try to Read the TOPR Schedule from the File FILE *fin = fopen("Epoch.txt", "r"); if (fin) { fscanf(fin, "%li", &op); fscanf(fin, "%li", &epoch); fclose(fin); return 1; } return 0; // Error } //----------------------------------------------------------------------------- // Function : // Inputs : // Outputs : // Description: // Remarks : // History : //----------------------------------------------------------------------------- void ReadTOPR(void) { // Try to Read the TOPR Schedule from the File FILE *fin = fopen("TOPR.txt", "r"); if (fin) { for(int x=0; x<7; x++) { int v; fscanf(fin, "%i", &v); newTOPR.dur[x] = (short)v; } for(int x=0; x<7; x++) { int v; fscanf(fin, "%i", &v); newTOPR.mode[x] = (unsigned char)v; } fclose(fin); } } //----------------------------------------------------------------------------- // Function : main // Inputs : // Outputs : // Description: // Remarks : // History : 9/20/2000 mjw Started //-------------------------------------------------------------------------mjw- int main (int argc, char *argv[]) { ReadTOPR(); if(!ReadEpoch()) exit(1); printf("\n"); int s = TOPRstuff(); printf("\n"); PrintTOPR(s); return 0; }