1 | // Reading data from FSC over Ethernet
|
---|
2 |
|
---|
3 | #include <stdio.h>
|
---|
4 | #include <string.h>
|
---|
5 | #include <sys/socket.h>
|
---|
6 | #include <netdb.h>
|
---|
7 |
|
---|
8 | #include "Evidence.h"
|
---|
9 |
|
---|
10 | #define SERVER_NAME "FSCctrl" // Name to use in DIM
|
---|
11 |
|
---|
12 | const unsigned int MAX_SIZE = 10000;
|
---|
13 |
|
---|
14 | const unsigned int TIME_OFF = 3;
|
---|
15 | const unsigned int TEMP_NUM = 64;
|
---|
16 | const unsigned int TEMP_OFF = 134;
|
---|
17 | const unsigned int VOLT_NUM = 40;
|
---|
18 | const unsigned int VOLT_OFF = 30;
|
---|
19 | const unsigned int CURR_NUM = 40;
|
---|
20 | const unsigned int CURR_OFF = 70;
|
---|
21 | const unsigned int HUMI_NUM = 4;
|
---|
22 | const unsigned int HUMI_OFF = 110;
|
---|
23 |
|
---|
24 | const unsigned int MAX_VAL = TEMP_OFF + TEMP_NUM +100;
|
---|
25 |
|
---|
26 | using namespace std;
|
---|
27 |
|
---|
28 | //
|
---|
29 | // Main program
|
---|
30 | //
|
---|
31 | int main(int argc, char *argv[]) {
|
---|
32 |
|
---|
33 | int Result;
|
---|
34 | char Buffer[1000];
|
---|
35 | float Val[MAX_VAL]={0};
|
---|
36 | string Data;
|
---|
37 | int Socket;
|
---|
38 | struct sockaddr_in SocketAddress;
|
---|
39 | vector<string> Items;
|
---|
40 | time_t Time;
|
---|
41 |
|
---|
42 | // Start Evidence server
|
---|
43 | EvidenceServer S(SERVER_NAME);
|
---|
44 |
|
---|
45 | // Get configuration data
|
---|
46 | string Address = S.GetConfig("Address", "10.0.128.127");
|
---|
47 | int Port = atoi(S.GetConfig("Port", "5000").c_str());
|
---|
48 |
|
---|
49 | if (argc > 1) Address = argv[1];
|
---|
50 | if (argc > 2) Port = atoi(argv[2]);
|
---|
51 |
|
---|
52 | // Resolve hostname
|
---|
53 | struct hostent *Host = gethostbyname(Address.c_str());
|
---|
54 | if (Host == 0) S.Message(S.FATAL, "Could not resolve host name '%s'", Address.c_str());
|
---|
55 |
|
---|
56 | SocketAddress.sin_family = PF_INET;
|
---|
57 | SocketAddress.sin_port = htons(Port);
|
---|
58 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
---|
59 |
|
---|
60 | // Open socket descriptor
|
---|
61 | if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
---|
62 | S.Message(S.FATAL, "Could not open socket (%s)\n", strerror(errno));
|
---|
63 | }
|
---|
64 |
|
---|
65 | // Connect to server
|
---|
66 | if (connect(Socket, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
|
---|
67 | S.Message(S.FATAL, "Could not connect to %s:%hu (%s)", Address.c_str(), Port, strerror(errno));
|
---|
68 | }
|
---|
69 |
|
---|
70 | // Create DIM services
|
---|
71 | DimService Update(SERVER_NAME"/Time", "F", Val+TIME_OFF, sizeof(float));
|
---|
72 | DimService Temperature(SERVER_NAME"/Temperature", "F", Val+TEMP_OFF, sizeof(float)*TEMP_NUM);
|
---|
73 | DimService Humidity(SERVER_NAME"/Humindity", "F", Val+HUMI_OFF, sizeof(float)*HUMI_NUM);
|
---|
74 | DimService Voltage(SERVER_NAME"/Voltage", "F",Val+VOLT_OFF, sizeof(float)*VOLT_NUM);
|
---|
75 | DimService Current(SERVER_NAME"/Current", "F", Val+CURR_OFF, sizeof(float)*CURR_NUM);
|
---|
76 |
|
---|
77 | // Send start command to FSC board
|
---|
78 | snprintf(Buffer, sizeof(Buffer), "m");
|
---|
79 | Result = write(Socket, Buffer, 1);
|
---|
80 | if (Result != 1) S.Message(S.ERROR, "Could not write to socket (%s)", strerror(errno));
|
---|
81 |
|
---|
82 | // Loop receiving data from FSC board
|
---|
83 | while (!S.ExitRequest) {
|
---|
84 |
|
---|
85 | Result = read(Socket, Buffer, sizeof(Buffer));
|
---|
86 |
|
---|
87 | if (Result == -1) {
|
---|
88 | S.Message(S.ERROR, "Could not read from socket, exiting (%s)", strerror(errno));
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | else if (Result == 0) {
|
---|
92 | S.Message(S.INFO, "Server not existing anymore, exiting");
|
---|
93 | break;
|
---|
94 | }
|
---|
95 |
|
---|
96 | // Append received data and check for maxumum length and end marker
|
---|
97 | Data.append(Buffer, Result);
|
---|
98 |
|
---|
99 | if (Data.size() > MAX_SIZE) {
|
---|
100 | S.Message(S.ERROR, "Received more than %u characters without end marker, deleting", MAX_SIZE);
|
---|
101 | Data.clear();
|
---|
102 | }
|
---|
103 |
|
---|
104 | if (Data.find("end.\n")==string::npos || Data.find("status:")==string::npos) continue;
|
---|
105 |
|
---|
106 | Data = Data.substr(Data.rfind("status:"), Data.rfind("end.\n")-Data.rfind("status:"));
|
---|
107 |
|
---|
108 | //
|
---|
109 | // Full event received
|
---|
110 | //
|
---|
111 | Time = time(NULL);
|
---|
112 | printf("%s%s\n", ctime(&Time), Data.c_str());
|
---|
113 |
|
---|
114 | // Extract numbers
|
---|
115 | Items = S.Tokenize(Data, " \n\t:");
|
---|
116 |
|
---|
117 | for (unsigned int i=0; i<MAX_VAL && i<Items.size(); i++) {
|
---|
118 | Val[i] = atof(Items[i].c_str());
|
---|
119 | //printf("Value %i: %s / %.2f \n", i, Items[i].c_str(), Val[i]);
|
---|
120 | }
|
---|
121 |
|
---|
122 | // Update DIM service
|
---|
123 | Temperature.updateService();
|
---|
124 | Humidity.updateService();
|
---|
125 | Voltage.updateService();
|
---|
126 | Current.updateService();
|
---|
127 | Update.updateService();
|
---|
128 |
|
---|
129 | Data.clear();
|
---|
130 | } // while()
|
---|
131 |
|
---|
132 | if (close(Socket) == -1) {
|
---|
133 | S.Message(S.ERROR, "Could not close socket descriptor (%s)", strerror(errno));
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|