1b8d9f1b6a367051b11f786be1226b8119613e11
[profile/ivi/ico-vic-amb-plugin.git] / tests / websocketscenario.cc
1 /**
2  * Copyright (C) 2012  TOYOTA MOTOR CORPORATION.
3  * 
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  * 
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  * 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17  * 
18  */
19 #include <sys/time.h>
20
21 #include <algorithm>
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26
27 #include <json-glib/json-glib.h>
28
29 #include "debugout.h"
30
31 #include "configamb.h"
32 #include "scenarioengine.h"
33
34 std::string AMBCONFPATH = "/etc/ambd/config";
35 const int sleeptime = 100 * 1000;
36
37 WebsocketAppScenarioEngine::WebsocketAppScenarioEngine()
38 {
39 }
40
41 WebsocketAppScenarioEngine::~WebsocketAppScenarioEngine()
42 {
43     namelist.clear();
44 }
45
46 void
47 WebsocketAppScenarioEngine::start()
48 {
49     int value = 0;
50     std::stringstream sstr;
51     sstr.str("");
52     timeval time;
53     double timestamp = 0.0;
54
55     int setnotifycall = namelist.size() - 1;
56     DebugOut(10) << "=========" << "WebsocketApp Subscribe" << "========="
57                  << std::endl;
58     usleep(12 * 1000);
59     for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
60         for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
61              itr2++) {
62             usleep(sleeptime);
63             gettimeofday(&time, NULL);
64             timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
65             ControlWebsocketClientApp::vehiclename_scenario = (*itr2);
66             client.send("method", "subscribe", (*itr2), sstr.str(), timestamp);
67             DebugOut(10) << "[S]: " << (*itr).first << "->" << (*itr2)
68                          << " , subscribe , " << timestamp << " , " << sstr.str()
69                          << std::endl;
70         }
71     }
72     for (int i = 0; i < 2; i++) {
73         pthread_mutex_lock(&ControlWebsocketClientApp::mutex_scenario);
74         pthread_cond_wait(&ControlWebsocketClientApp::cond_scenario,
75                           &ControlWebsocketClientApp::mutex_scenario);
76         pthread_mutex_unlock(&ControlWebsocketClientApp::mutex_scenario);
77         DebugOut(10) << "WebsocketApp Next." << std::endl;
78     }
79     DebugOut(10) << "vehiclename_scenario = "
80                  << ControlWebsocketClientApp::vehiclename_scenario << std::endl;
81     DebugOut(10) << "=========" << "WebsocketApp Set" << "========="
82                  << std::endl;
83
84     for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
85         for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
86              itr2++) {
87             usleep(sleeptime);
88             gettimeofday(&time, NULL);
89             timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
90             sstr.str("");
91             sstr << value;
92             DebugOut(10) << "Send Data : " << sstr.str() << "," << timestamp
93                          << std::endl;
94             client.send("method", "set", (*itr2), sstr.str(), timestamp);
95             DebugOut(10) << "[S]: " << (*itr2) << " , set , " << timestamp
96                          << " , " << sstr.str() << std::endl;
97         }
98     }
99     pthread_mutex_lock(&ControlWebsocketClientApp::mutex_scenario);
100     pthread_cond_wait(&ControlWebsocketClientApp::cond_scenario,
101                       &ControlWebsocketClientApp::mutex_scenario);
102     pthread_mutex_unlock(&ControlWebsocketClientApp::mutex_scenario);
103     DebugOut(10) << "WebsocketApp Next." << std::endl;
104     pthread_mutex_lock(&ControlWebsocketClientApp::mutex_scenario);
105     pthread_cond_wait(&ControlWebsocketClientApp::cond_scenario,
106                       &ControlWebsocketClientApp::mutex_scenario);
107     pthread_mutex_unlock(&ControlWebsocketClientApp::mutex_scenario);
108     DebugOut(10) << "WebsocketApp Next." << std::endl;
109     DebugOut(10) << "=========" << "WebsocketApp Set & Get" << "========="
110             << std::endl;
111     for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
112         for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
113              itr2++) {
114             usleep(sleeptime);
115             gettimeofday(&time, NULL);
116             timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
117             sstr.str("");
118             sstr << value;
119             DebugOut(10) << "Send Data : " << sstr.str() << "," << timestamp
120                          << std::endl;
121             client.send("method", "set", (*itr2), sstr.str(), timestamp);
122             DebugOut(10) << "[S]: " << (*itr2) << " , set , " << timestamp
123                          << " , " << sstr.str() << std::endl;
124             usleep(sleeptime);
125             gettimeofday(&time, NULL);
126             timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
127             client.send("method", "get", (*itr2), sstr.str(), timestamp);
128             DebugOut(10) << "[S]: " << (*itr2) << " , get , " << timestamp
129                          << " , " << sstr.str() << std::endl;
130         }
131     }
132     // Wait Get
133     pthread_mutex_lock(&ControlWebsocketClientApp::mutex_scenario);
134     pthread_cond_wait(&ControlWebsocketClientApp::cond_scenario,
135                       &ControlWebsocketClientApp::mutex_scenario);
136     pthread_mutex_unlock(&ControlWebsocketClientApp::mutex_scenario);
137     std::cerr << "=========" << "WebsocketApp End" << "=========" << std::endl;
138 }
139
140 bool
141 WebsocketAppScenarioEngine::initialize()
142 {
143     ConfigAMB confamb;
144     if (!confamb.readConfig(AMBCONFPATH)) {
145         std::cerr << "Error configamb.initialize." << std::endl;
146         return false;
147     }
148     if (!conf.readConfig(confamb.getAMBformatPath())) {
149         std::cerr << "Error config.initialize." << std::endl;
150         return false;
151     }
152     if (!client.initialize(confamb.getPort())) {
153         std::cerr << "Error controlwebsocketclientapp.initialize("
154                   << confamb.getPort() << "." << std::endl;
155         return false;
156     }
157
158     std::vector<VehicleInfoDefine> vehicleinfoList;
159     vehicleinfoList = conf.getVehicleInfoConfig();
160     for (auto itr = vehicleinfoList.begin(); itr != vehicleinfoList.end();
161          itr++) {
162         for (auto itr2 = (*itr).status.begin(); itr2 != (*itr).status.end();
163              itr2++) {
164             namelist[std::string((*itr).KeyEventType)].push_back(
165                     (*itr2).ambPropertyName);
166         }
167     }
168     return true;
169 }
170
171 int
172 main()
173 {
174     g_type_init();
175     DebugOut::setDebugThreshhold(5);
176     WebsocketAppScenarioEngine wsengine;
177     if (!wsengine.initialize()) {
178         std::cerr << "Error websocketengine.initialize." << std::endl;
179         return 0;
180     }
181     wsengine.start();
182
183     return 0;
184 }