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