Changeset 130 for tools/SkyQualityMonitor
- Timestamp:
- 12/08/09 14:56:29 (15 years ago)
- Location:
- tools/SkyQualityMonitor
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/SkyQualityMonitor/Makefile
r95 r130 2 2 3 3 PROG=sqm 4 INCLUDE=../../drsdaq/ 4 CPPFLAGS += -I../DIM/dim 5 LDLIBS += -lpthread 5 6 6 all: $(PROG) 7 $(PROG): $(PROG).o ../Evidence.o ../DIM/linux/libdim.a 7 8 8 $(PROG): $(PROG).o SlowData.o 9 $(CC) $(PROG).o SlowData.o -o $(PROG) 10 11 $(PROG).o : $(PROG).cpp 12 $(CC) $(PROG).cpp -c -I$(INCLUDE) 13 14 SlowData.o: ../../drsdaq/SlowData.cc 15 $(CC) ../../drsdaq/SlowData.cc -c -I$(INCLUDE) 9 clean: 10 @rm -f $(PROG) $(PROG).o 16 11 -
tools/SkyQualityMonitor/sqm.cpp
r96 r130 3 3 Interface to the sky quality monitor (essentially a socket interface) 4 4 5 Writes slow data at given rate. Terminate with CTRL-c.5 Terminate with CTRL-c. Note that socket is closed automatically by exit(). 6 6 7 Oliver Grimm, July20097 Oliver Grimm, September 2009 8 8 9 9 \********************************************************************/ 10 10 11 #include <stdlib.h>12 #include <string.h>13 11 #include <iostream> 14 12 #include <sys/socket.h> 15 13 #include <netdb.h> 16 14 #include <signal.h> 15 #include <errno.h> 16 #include <stdarg.h> 17 17 18 #include "../../drsdaq/SlowData.h" 18 #define SERVER_NAME "SQM" // Name to use in DIM 19 #include "../Evidence.h" 19 20 20 #define PERIOD 10 // Measurement period (integer) 21 #define READ_CMD "rx" // Command to read from device 22 #define BUF_SIZE 1000 // Read buffer size 21 #define READ_CMD "rx" // Command to read from device 22 #define BUF_SIZE 1000 // Read and text buffer size 23 23 24 int SocketDescriptor; // Global to be accessible by exit handler25 SlowData *SlowDataClass;26 27 // Close socket descriptor when CTRL-c pressed28 void ExitHandler(int Signal) {29 if (!close(SocketDescriptor)) printf("Connection closed.\n");30 delete SlowDataClass;31 exit(EXIT_SUCCESS);32 }33 24 34 25 // Main program 35 int main(int argc, char *argv[]) { 36 37 char Buffer[BUF_SIZE],ServerName[BUF_SIZE]="sqm.ethz.ch"; 38 char SlowDir[BUF_SIZE]="/ct3data/SlowData"; 39 int DAQPort = 10001, Period = PERIOD; 26 int main() { 27 28 int SocketDescriptor; 40 29 struct sockaddr_in SocketAddress; 41 30 fd_set ReadFileDescriptor; 42 31 time_t Time; 43 32 44 if (argc != 5) { 45 printf("+++ Sky Quality Monitor Interface +++\n"); 46 printf("Usage: %s <server name> <port> <slow data dir> <period (sec)>\n", argv[0]); 47 printf("Taking defaults: <%s> <%d> <%s> <%d>\n", ServerName, DAQPort, SlowDir, Period); 48 } 49 else { 50 snprintf(ServerName, sizeof(ServerName), argv[1]); 51 snprintf(SlowDir, sizeof(SlowDir), argv[3]); 52 DAQPort = atoi(argv[2]); 53 Period = atoi(argv[4]); 54 } 55 33 // Start server and request configuration data 34 EvidenceServer Srv(SERVER_NAME); 35 char *Address = Srv.GetConfig(SERVER_NAME " address"); 36 unsigned int Port = atoi(Srv.GetConfig(SERVER_NAME " port")); 37 unsigned int Period = atoi(Srv.GetConfig(SERVER_NAME " period")); 38 56 39 // Open socket descriptor 57 40 if ((SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 58 printf("Error: Could not open socket."); 59 exit(EXIT_FAILURE); 41 Srv.Msg(Srv.FATAL, "Could not open socket (%s)", strerror(errno)); 60 42 } 61 43 62 // Resolve hostname and try to connect to server63 struct hostent *hostent = gethostbyname( ServerName);44 // Resolve hostname 45 struct hostent *hostent = gethostbyname(Address); 64 46 if (hostent==0) { 65 printf("\nCould not resolve host name.\n"); 66 close(SocketDescriptor); 67 exit(EXIT_FAILURE); 47 Srv.Msg(Srv.FATAL, "Could not resolve host name '%s' (%s)", Address, hstrerror(h_errno)); 68 48 } 69 49 50 // Connect to server 70 51 SocketAddress.sin_family = PF_INET; 71 SocketAddress.sin_port = htons((unsigned short) DAQPort);52 SocketAddress.sin_port = htons((unsigned short) Port); 72 53 SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr; 73 54 74 55 if (connect(SocketDescriptor, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress))==-1) { 75 printf("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort); 76 close(SocketDescriptor); 77 exit(EXIT_FAILURE); 56 Srv.Msg(Srv.FATAL, "Could not connect to server '%s' on port %d (%s)", Address, Port, strerror(errno)); 78 57 } 79 printf("\nConnected to %s (port %d)\n", ServerName, DAQPort); 58 59 Srv.Msg(Srv.INFO, "Connected to server '%s' on port %d", Address, Port); 80 60 signal(SIGPIPE,SIG_IGN); // Do not kill process if writing to closed socket 81 signal(SIGINT, &ExitHandler);82 61 83 // Create instance of slow data class for monitoring data 84 SlowDataClass = new SlowData("SQM", SlowDir); 85 if(SlowDataClass->ErrorCode != 0) { 86 printf("Error: Could not open slow data file in %s (%s)\n", SlowDir, strerror(SlowDataClass->ErrorCode)); 87 exit(EXIT_FAILURE); 88 } 89 SlowDataClass->NewEntry("Data-Info", "Data string as returned by sqm. Commata and the letter 'm' are replaced by whitespaces"); 62 // Create DIM services 63 double sqm_reading=0.0; 64 char Buffer[BUF_SIZE]; 65 DimService SQM_Brightness(SERVER_NAME"/NSB", sqm_reading); 66 DimService SQM_Response(SERVER_NAME"/Response", Buffer); 90 67 91 while( true) {68 while(!EvidenceServer::ExitRequest) { 92 69 // Write read command to socket 93 70 if ((write(SocketDescriptor, READ_CMD, strlen(READ_CMD)))<1) { 94 printf("Error: Could not write read command to socket.\n");71 Srv.Msg(Srv.ERROR, "Could not write read command '%s' to socket.", READ_CMD); 95 72 } 96 usleep((int) (Period*1e6/2)); // Wait half period here to allow for response97 73 98 74 // Wait for data from socket with time-out 75 sleep(Period/2); // Wait half period to allow for response 76 99 77 FD_ZERO(&ReadFileDescriptor); 100 78 FD_SET(SocketDescriptor, &ReadFileDescriptor); 101 79 struct timeval WaitTime = {Period, 0}; 102 80 if (select(((int) SocketDescriptor)+1, &ReadFileDescriptor, NULL, NULL, &WaitTime)==-1) { 103 perror("Error with select()"); 104 break; 81 Srv.Msg(Srv.FATAL, "Error with select() (%s)", strerror(errno)); 105 82 } 106 83 107 84 if (!FD_ISSET(SocketDescriptor, &ReadFileDescriptor)) { 108 printf("Time-out of %d seconds expired before receiving response from socket.\n", PERIOD);85 Srv.Msg(Srv.WARN, "Time-out of %d seconds expired before receiving response from socket", Period); 109 86 continue; 110 87 } 111 88 112 89 memset(Buffer, 0, BUF_SIZE); 113 if(read(SocketDescriptor, Buffer, BUF_SIZE)==0) { 114 printf("Server not existing anymore, exiting...\n"); 115 break; 90 if(read(SocketDescriptor, Buffer, BUF_SIZE) == 0) { 91 Srv.Msg(Srv.FATAL, "Server not existing anymore, exiting...\n"); 116 92 } 117 93 118 // Remove letters and commata from string before writing as slow data 119 for (int i=0; i<strlen(Buffer); i++) { 120 if (Buffer[i]=='m' || Buffer[i]==',' || iscntrl(Buffer[i])) Buffer[i] = ' '; 121 } 122 SlowDataClass->NewEntry("Data", Buffer); 123 if(SlowDataClass->ErrorCode != 0) { 124 printf("Error: Could not write to slow data file (%s)\n", strerror(SlowDataClass->ErrorCode)); 125 break; 126 } 94 // Update full textual response and brightness reading 95 for (int i=0; i<strlen(Buffer); i++) if (Buffer[i] == '\n') Buffer[i] = '\0'; 96 sqm_reading = strtod(Buffer+2 ,NULL); 97 SQM_Brightness.updateService(); 98 SQM_Response.updateService(); 99 127 100 Time = time(NULL); 128 printf(" \r%s %s CTRL-c to exit.",Buffer, asctime(localtime(&Time)));101 printf("%s\n %s CTRL-c to exit.\r", Buffer, asctime(localtime(&Time))); 129 102 fflush(stdout); 130 usleep((int) (Period*1e6/2)); // Wait second half period here103 sleep(Period/2); // Wait second half period here 131 104 132 105 } // while() 133 134 ExitHandler(0); 106 135 107 }
Note:
See TracChangeset
for help on using the changeset viewer.