source: tools/SkyQualityMonitor/sqm.cpp@ 138

Last change on this file since 138 was 130, checked in by ogrimm, 15 years ago
Modified for use with DIM
File size: 3.5 KB
Line 
1/********************************************************************\
2
3 Interface to the sky quality monitor (essentially a socket interface)
4
5 Terminate with CTRL-c. Note that socket is closed automatically by exit().
6
7 Oliver Grimm, September 2009
8
9\********************************************************************/
10
11#include <iostream>
12#include <sys/socket.h>
13#include <netdb.h>
14#include <signal.h>
15#include <errno.h>
16#include <stdarg.h>
17
18#define SERVER_NAME "SQM" // Name to use in DIM
19#include "../Evidence.h"
20
21#define READ_CMD "rx" // Command to read from device
22#define BUF_SIZE 1000 // Read and text buffer size
23
24
25// Main program
26int main() {
27
28 int SocketDescriptor;
29 struct sockaddr_in SocketAddress;
30 fd_set ReadFileDescriptor;
31 time_t Time;
32
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
39 // Open socket descriptor
40 if ((SocketDescriptor = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
41 Srv.Msg(Srv.FATAL, "Could not open socket (%s)", strerror(errno));
42 }
43
44 // Resolve hostname
45 struct hostent *hostent = gethostbyname(Address);
46 if (hostent==0) {
47 Srv.Msg(Srv.FATAL, "Could not resolve host name '%s' (%s)", Address, hstrerror(h_errno));
48 }
49
50 // Connect to server
51 SocketAddress.sin_family = PF_INET;
52 SocketAddress.sin_port = htons((unsigned short) Port);
53 SocketAddress.sin_addr = *(struct in_addr*) hostent->h_addr;
54
55 if (connect(SocketDescriptor, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress))==-1) {
56 Srv.Msg(Srv.FATAL, "Could not connect to server '%s' on port %d (%s)", Address, Port, strerror(errno));
57 }
58
59 Srv.Msg(Srv.INFO, "Connected to server '%s' on port %d", Address, Port);
60 signal(SIGPIPE,SIG_IGN); // Do not kill process if writing to closed socket
61
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);
67
68 while(!EvidenceServer::ExitRequest) {
69 // Write read command to socket
70 if ((write(SocketDescriptor, READ_CMD, strlen(READ_CMD)))<1) {
71 Srv.Msg(Srv.ERROR, "Could not write read command '%s' to socket.", READ_CMD);
72 }
73
74 // Wait for data from socket with time-out
75 sleep(Period/2); // Wait half period to allow for response
76
77 FD_ZERO(&ReadFileDescriptor);
78 FD_SET(SocketDescriptor, &ReadFileDescriptor);
79 struct timeval WaitTime = {Period, 0};
80 if (select(((int) SocketDescriptor)+1, &ReadFileDescriptor, NULL, NULL, &WaitTime)==-1) {
81 Srv.Msg(Srv.FATAL, "Error with select() (%s)", strerror(errno));
82 }
83
84 if (!FD_ISSET(SocketDescriptor, &ReadFileDescriptor)) {
85 Srv.Msg(Srv.WARN, "Time-out of %d seconds expired before receiving response from socket", Period);
86 continue;
87 }
88
89 memset(Buffer, 0, BUF_SIZE);
90 if(read(SocketDescriptor, Buffer, BUF_SIZE) == 0) {
91 Srv.Msg(Srv.FATAL, "Server not existing anymore, exiting...\n");
92 }
93
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
100 Time = time(NULL);
101 printf("%s\n %s CTRL-c to exit.\r", Buffer, asctime(localtime(&Time)));
102 fflush(stdout);
103 sleep(Period/2); // Wait second half period here
104
105 } // while()
106
107}
Note: See TracBrowser for help on using the repository browser.