2 * Copyright (C) 2012 TOYOTA MOTOR CORPORATION.
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.
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.
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
27 #include <json-glib/json-glib.h>
31 #include "configamb.h"
32 #include "scenarioengine.h"
34 std::string AMBCONFPATH = "/etc/ambd/config";
35 const int sleeptime = 100 * 1000;
37 WebsocketAppScenarioEngine::WebsocketAppScenarioEngine()
41 WebsocketAppScenarioEngine::~WebsocketAppScenarioEngine()
47 WebsocketAppScenarioEngine::start()
50 std::stringstream sstr;
53 double timestamp = 0.0;
55 int setnotifycall = namelist.size() - 1;
56 DebugOut(10) << "=========" << "WebsocketApp Subscribe" << "========="
59 for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
60 for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
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()
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;
79 DebugOut(10) << "vehiclename_scenario = "
80 << ControlWebsocketClientApp::vehiclename_scenario << std::endl;
81 DebugOut(10) << "=========" << "WebsocketApp Set" << "========="
84 for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
85 for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
88 gettimeofday(&time, NULL);
89 timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
92 DebugOut(10) << "Send Data : " << sstr.str() << "," << timestamp
94 client.send("method", "set", (*itr2), sstr.str(), timestamp);
95 DebugOut(10) << "[S]: " << (*itr2) << " , set , " << timestamp
96 << " , " << sstr.str() << std::endl;
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" << "========="
111 for (auto itr = namelist.begin(); itr != namelist.end(); itr++) {
112 for (auto itr2 = (*itr).second.begin(); itr2 != (*itr).second.end();
115 gettimeofday(&time, NULL);
116 timestamp = time.tv_sec + (time.tv_usec / (1000 * 1000));
119 DebugOut(10) << "Send Data : " << sstr.str() << "," << timestamp
121 client.send("method", "set", (*itr2), sstr.str(), timestamp);
122 DebugOut(10) << "[S]: " << (*itr2) << " , set , " << timestamp
123 << " , " << sstr.str() << std::endl;
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;
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;
141 WebsocketAppScenarioEngine::initialize()
144 if (!confamb.readConfig(AMBCONFPATH)) {
145 std::cerr << "Error configamb.initialize." << std::endl;
148 if (!conf.readConfig(confamb.getAMBformatPath())) {
149 std::cerr << "Error config.initialize." << std::endl;
152 if (!client.initialize(confamb.getPort())) {
153 std::cerr << "Error controlwebsocketclientapp.initialize("
154 << confamb.getPort() << "." << std::endl;
158 std::vector<VehicleInfoDefine> vehicleinfoList;
159 vehicleinfoList = conf.getVehicleInfoConfig();
160 for (auto itr = vehicleinfoList.begin(); itr != vehicleinfoList.end();
162 for (auto itr2 = (*itr).status.begin(); itr2 != (*itr).status.end();
164 namelist[std::string((*itr).KeyEventType)].push_back(
165 (*itr2).ambPropertyName);
175 DebugOut::setDebugThreshhold(5);
176 WebsocketAppScenarioEngine wsengine;
177 if (!wsengine.initialize()) {
178 std::cerr << "Error websocketengine.initialize." << std::endl;