Index: tools/SkyQualityMonitor/Makefile
===================================================================
--- tools/SkyQualityMonitor/Makefile	(revision 96)
+++ tools/SkyQualityMonitor/Makefile	(revision 130)
@@ -2,15 +2,10 @@
 
 PROG=sqm
-INCLUDE=../../drsdaq/
+CPPFLAGS += -I../DIM/dim
+LDLIBS += -lpthread
 
-all: $(PROG)
+$(PROG): $(PROG).o ../Evidence.o ../DIM/linux/libdim.a
 
-$(PROG): $(PROG).o SlowData.o
-	$(CC) $(PROG).o SlowData.o -o $(PROG)
-
-$(PROG).o : $(PROG).cpp
-	$(CC) $(PROG).cpp -c -I$(INCLUDE)
-
-SlowData.o: ../../drsdaq/SlowData.cc
-	$(CC) ../../drsdaq/SlowData.cc -c -I$(INCLUDE)
+clean:
+	@rm -f $(PROG) $(PROG).o
 	
Index: tools/SkyQualityMonitor/sqm.cpp
===================================================================
--- tools/SkyQualityMonitor/sqm.cpp	(revision 96)
+++ tools/SkyQualityMonitor/sqm.cpp	(revision 130)
@@ -3,133 +3,105 @@
   Interface to the sky quality monitor (essentially a socket interface)
   
-  Writes slow data at given rate. Terminate with CTRL-c.
+  Terminate with CTRL-c. Note that socket is closed automatically by exit().
   
-  Oliver Grimm, July 2009
+  Oliver Grimm, September 2009
 
 \********************************************************************/
 
-#include <stdlib.h>
-#include <string.h>
 #include <iostream>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <signal.h>
+#include <errno.h>
+#include <stdarg.h>
 
-#include "../../drsdaq/SlowData.h"
+#define SERVER_NAME "SQM"	// Name to use in DIM
+#include "../Evidence.h"
 
-#define PERIOD 10  	// Measurement period (integer)
-#define READ_CMD "rx"	// Command to read from device
-#define BUF_SIZE 1000	// Read buffer size
+#define READ_CMD "rx"		// Command to read from device
+#define BUF_SIZE 1000		// Read and text buffer size
 
-int SocketDescriptor;	// Global to be accessible by exit handler
-SlowData *SlowDataClass;
-
-// Close socket descriptor when CTRL-c pressed
-void ExitHandler(int Signal) {
-  if (!close(SocketDescriptor)) printf("Connection closed.\n");
-  delete SlowDataClass;
-  exit(EXIT_SUCCESS);
-}
 
 // Main program
-int main(int argc, char *argv[]) {
-        
-  char Buffer[BUF_SIZE],ServerName[BUF_SIZE]="sqm.ethz.ch";
-  char SlowDir[BUF_SIZE]="/ct3data/SlowData";
-  int DAQPort = 10001, Period = PERIOD;
+int main() {
+
+  int SocketDescriptor;
   struct sockaddr_in SocketAddress;
   fd_set ReadFileDescriptor;
   time_t Time;
   
-  if (argc != 5) {
-    printf("+++ Sky Quality Monitor Interface +++\n");
-    printf("Usage: %s <server name> <port> <slow data dir> <period (sec)>\n", argv[0]);
-    printf("Taking defaults: <%s> <%d> <%s> <%d>\n", ServerName, DAQPort, SlowDir, Period);
-  } 
-  else {
-    snprintf(ServerName, sizeof(ServerName), argv[1]);
-    snprintf(SlowDir, sizeof(SlowDir), argv[3]);
-    DAQPort = atoi(argv[2]);
-    Period = atoi(argv[4]);
-  }
-  
+  // Start server and request configuration data
+  EvidenceServer Srv(SERVER_NAME);
+  char *Address = Srv.GetConfig(SERVER_NAME " address");
+  unsigned int Port = atoi(Srv.GetConfig(SERVER_NAME " port"));
+  unsigned int Period = atoi(Srv.GetConfig(SERVER_NAME " period"));
+    
   // Open socket descriptor
   if ((SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
-    printf("Error: Could not open socket.");
-    exit(EXIT_FAILURE);
+    Srv.Msg(Srv.FATAL, "Could not open socket (%s)", strerror(errno));
   }
     
-  // Resolve hostname and try to connect to server
-  struct hostent *hostent = gethostbyname(ServerName);
+  // Resolve hostname
+  struct hostent *hostent = gethostbyname(Address);
   if (hostent==0) {
-    printf("\nCould not resolve host name.\n");
-    close(SocketDescriptor);
-    exit(EXIT_FAILURE);
+    Srv.Msg(Srv.FATAL, "Could not resolve host name '%s' (%s)", Address, hstrerror(h_errno));
   }
 
+  //  Connect to server
   SocketAddress.sin_family = PF_INET;
-  SocketAddress.sin_port = htons((unsigned short) DAQPort);
+  SocketAddress.sin_port = htons((unsigned short) Port);
   SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr;
   
   if (connect(SocketDescriptor, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress))==-1) {
-    printf("Error: Could not connect to server %s (port %d)\n", ServerName, DAQPort);
-    close(SocketDescriptor);
-    exit(EXIT_FAILURE);
+    Srv.Msg(Srv.FATAL, "Could not connect to server '%s' on port %d (%s)", Address, Port, strerror(errno));
   }
-  printf("\nConnected to %s (port %d)\n", ServerName, DAQPort);
+
+  Srv.Msg(Srv.INFO, "Connected to server '%s' on port %d", Address, Port);
   signal(SIGPIPE,SIG_IGN);  // Do not kill process if writing to closed socket
-  signal(SIGINT, &ExitHandler);
 
-  // Create instance of slow data class for monitoring data
-  SlowDataClass = new SlowData("SQM", SlowDir);
-  if(SlowDataClass->ErrorCode != 0) {
-    printf("Error: Could not open slow data file in %s (%s)\n", SlowDir, strerror(SlowDataClass->ErrorCode));
-    exit(EXIT_FAILURE);
-  }
-  SlowDataClass->NewEntry("Data-Info", "Data string as returned by sqm. Commata and the letter 'm' are replaced by whitespaces");
+  // Create DIM services
+  double sqm_reading=0.0;
+  char Buffer[BUF_SIZE];
+  DimService SQM_Brightness(SERVER_NAME"/NSB", sqm_reading);
+  DimService SQM_Response(SERVER_NAME"/Response", Buffer);
 
-  while(true) {
+  while(!EvidenceServer::ExitRequest) {
     // Write read command to socket
     if ((write(SocketDescriptor, READ_CMD, strlen(READ_CMD)))<1) { 
-      printf("Error: Could not write read command to socket.\n");
+      Srv.Msg(Srv.ERROR, "Could not write read command '%s' to socket.", READ_CMD);
     }
-    usleep((int) (Period*1e6/2));  // Wait half period here to allow for response
     
     // Wait for data from socket with time-out
+    sleep(Period/2);  // Wait half period to allow for response
+
     FD_ZERO(&ReadFileDescriptor);   
     FD_SET(SocketDescriptor, &ReadFileDescriptor);
     struct timeval WaitTime = {Period, 0};
     if (select(((int) SocketDescriptor)+1, &ReadFileDescriptor, NULL, NULL, &WaitTime)==-1) {
-      perror("Error with select()");
-      break;
+      Srv.Msg(Srv.FATAL, "Error with select() (%s)", strerror(errno));
     }
 
     if (!FD_ISSET(SocketDescriptor, &ReadFileDescriptor)) { 
-      printf("Time-out of %d seconds expired before receiving response from socket.\n", PERIOD);
+      Srv.Msg(Srv.WARN, "Time-out of %d seconds expired before receiving response from socket", Period);
       continue;
     }
 
     memset(Buffer, 0, BUF_SIZE);
-    if(read(SocketDescriptor, Buffer, BUF_SIZE)==0) {
-      printf("Server not existing anymore, exiting...\n");
-      break;
+    if(read(SocketDescriptor, Buffer, BUF_SIZE) == 0) {
+      Srv.Msg(Srv.FATAL, "Server not existing anymore, exiting...\n");
     }
 
-    // Remove letters and commata from string before writing as slow data
-    for (int i=0; i<strlen(Buffer); i++) {
-      if (Buffer[i]=='m' || Buffer[i]==',' || iscntrl(Buffer[i])) Buffer[i] = ' ';
-    }
-    SlowDataClass->NewEntry("Data", Buffer);
-    if(SlowDataClass->ErrorCode != 0) {
-      printf("Error: Could not write to slow data file (%s)\n", strerror(SlowDataClass->ErrorCode));
-      break;
-    }
+    // Update full textual response and brightness reading
+    for (int i=0; i<strlen(Buffer); i++) if (Buffer[i] == '\n') Buffer[i] = '\0';
+    sqm_reading = strtod(Buffer+2 ,NULL);
+    SQM_Brightness.updateService();
+    SQM_Response.updateService();    
+    
     Time = time(NULL);
-    printf("\r%s %s CTRL-c to exit.",Buffer, asctime(localtime(&Time)));
+    printf("%s\n %s CTRL-c to exit.\r", Buffer, asctime(localtime(&Time)));
     fflush(stdout);
-    usleep((int) (Period*1e6/2)); // Wait second half period here
+    sleep(Period/2); // Wait second half period here
  
   } // while()
-  
-  ExitHandler(0);
+
 }
