Changeset 130 for tools


Ignore:
Timestamp:
12/08/09 14:56:29 (15 years ago)
Author:
ogrimm
Message:
Modified for use with DIM
Location:
tools/SkyQualityMonitor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tools/SkyQualityMonitor/Makefile

    r95 r130  
    22
    33PROG=sqm
    4 INCLUDE=../../drsdaq/
     4CPPFLAGS += -I../DIM/dim
     5LDLIBS += -lpthread
    56
    6 all: $(PROG)
     7$(PROG): $(PROG).o ../Evidence.o ../DIM/linux/libdim.a
    78
    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)
     9clean:
     10        @rm -f $(PROG) $(PROG).o
    1611       
  • tools/SkyQualityMonitor/sqm.cpp

    r96 r130  
    33  Interface to the sky quality monitor (essentially a socket interface)
    44 
    5   Writes slow data at given rate. Terminate with CTRL-c.
     5  Terminate with CTRL-c. Note that socket is closed automatically by exit().
    66 
    7   Oliver Grimm, July 2009
     7  Oliver Grimm, September 2009
    88
    99\********************************************************************/
    1010
    11 #include <stdlib.h>
    12 #include <string.h>
    1311#include <iostream>
    1412#include <sys/socket.h>
    1513#include <netdb.h>
    1614#include <signal.h>
     15#include <errno.h>
     16#include <stdarg.h>
    1717
    18 #include "../../drsdaq/SlowData.h"
     18#define SERVER_NAME "SQM"       // Name to use in DIM
     19#include "../Evidence.h"
    1920
    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
    2323
    24 int SocketDescriptor;   // Global to be accessible by exit handler
    25 SlowData *SlowDataClass;
    26 
    27 // Close socket descriptor when CTRL-c pressed
    28 void ExitHandler(int Signal) {
    29   if (!close(SocketDescriptor)) printf("Connection closed.\n");
    30   delete SlowDataClass;
    31   exit(EXIT_SUCCESS);
    32 }
    3324
    3425// 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;
     26int main() {
     27
     28  int SocketDescriptor;
    4029  struct sockaddr_in SocketAddress;
    4130  fd_set ReadFileDescriptor;
    4231  time_t Time;
    4332 
    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   
    5639  // Open socket descriptor
    5740  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));
    6042  }
    6143   
    62   // Resolve hostname and try to connect to server
    63   struct hostent *hostent = gethostbyname(ServerName);
     44  // Resolve hostname
     45  struct hostent *hostent = gethostbyname(Address);
    6446  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));
    6848  }
    6949
     50  //  Connect to server
    7051  SocketAddress.sin_family = PF_INET;
    71   SocketAddress.sin_port = htons((unsigned short) DAQPort);
     52  SocketAddress.sin_port = htons((unsigned short) Port);
    7253  SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr;
    7354 
    7455  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));
    7857  }
    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);
    8060  signal(SIGPIPE,SIG_IGN);  // Do not kill process if writing to closed socket
    81   signal(SIGINT, &ExitHandler);
    8261
    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);
    9067
    91   while(true) {
     68  while(!EvidenceServer::ExitRequest) {
    9269    // Write read command to socket
    9370    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);
    9572    }
    96     usleep((int) (Period*1e6/2));  // Wait half period here to allow for response
    9773   
    9874    // Wait for data from socket with time-out
     75    sleep(Period/2);  // Wait half period to allow for response
     76
    9977    FD_ZERO(&ReadFileDescriptor);   
    10078    FD_SET(SocketDescriptor, &ReadFileDescriptor);
    10179    struct timeval WaitTime = {Period, 0};
    10280    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));
    10582    }
    10683
    10784    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);
    10986      continue;
    11087    }
    11188
    11289    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");
    11692    }
    11793
    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   
    127100    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)));
    129102    fflush(stdout);
    130     usleep((int) (Period*1e6/2)); // Wait second half period here
     103    sleep(Period/2); // Wait second half period here
    131104 
    132105  } // while()
    133  
    134   ExitHandler(0);
     106
    135107}
Note: See TracChangeset for help on using the changeset viewer.