Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
2311 linlun 1
/*
2
 *
3
 *  Copyright (C) 2016  Linus Lundin
4
 *
5
 *  This program is free software; you can redistribute it and/or modify
6
 *  it under the terms of the GNU General Public License as published by
7
 *  the Free Software Foundation; either version 2 of the License, or
8
 *  (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License along
16
 *  with this program; if not, write to the Free Software Foundation, Inc.,
17
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 */
20
 
21
#include "Mqtt.h"
22
 
23
#include <mosquittopp.h>
24
#include <boost/lexical_cast.hpp>
25
 
26
#include "vm/Manager.h"
27
#include "common/log.h"
28
#include "common/exception.h"
29
 
30
namespace atom {
31
namespace vm {
32
namespace plugin {
33
 
34
 
35
Mqtt::Resources Mqtt::resources_;
36
 
37
static const std::string log_module_ = "vm::plugin::mqtt";
38
 
39
Mqtt::Mqtt(boost::asio::io_service& io_service) : Plugin(io_service)
40
{
41
  this->name_ = "mqtt";
42
 
43
  this->ExportFunction("MqttExport_Connect",       Mqtt::Export_Connect);
44
  this->ExportFunction("MqttExport_Close",         Mqtt::Export_Close);
45
  this->ExportFunction("MqttExport_SendMessage",   Mqtt::Export_SendMessage);
46
}
47
 
48
Mqtt::~Mqtt()
49
{
50
}
51
 
52
void Mqtt::InitializeDone()
53
{
54
  Plugin::InitializeDone();
55
}
56
 
57
void Mqtt::CallOutput(unsigned int request_id, std::string output)
58
{
59
  atom::log::Info(log_module_, output);
60
}
61
 
62
Mqtt::ResourceId Mqtt::GetFreeResourceId()
63
{
64
  ResourceId resource_id = 1;
65
 
66
  while (NULL != Mqtt::resources_[resource_id])
67
  {
68
    resource_id++;
69
  }
70
 
71
  return resource_id;
72
}
73
 
74
Value Mqtt::Export_Connect(const v8::Arguments& args)
75
{
76
  ATOM_VM_PLUGIN_SCOPE;
77
 
78
  Mqtt*      resource = NULL;
79
  ResourceId  resource_id;
80
 
81
  atom::log::Debug(log_module_, "%s called!", __FUNCTION__);
82
 
83
  try
84
  {
85
    ATOM_VM_PLUGIN_NUM_PARAMS(3);
86
 
87
    v8::String::AsciiValue server(args[0]);
88
    v8::String::AsciiValue username(args[1]);
89
    v8::String::AsciiValue password(args[2]);
90
 
91
 
92
    /* Initialize MySQL resource */
93
    //resource = mysql_init(NULL);
94
 
95
    if (NULL == resource)
96
    {
97
      throw atom::exception::initialization_failed();
98
    }
99
 
100
 
101
    /* Connect to database */
102
    /*
103
    if (mysql_real_connect(resource, *server, *username, *password, NULL, 0, NULL, 0) == NULL)
104
    {
105
      atom::log::Error(log_module_, "Failed to connect to database %s, error code %d, error message \"%s\".", *server, mysql_errno(resource), mysql_error(resource));
106
      throw atom::exception::connect_failed();
107
    }
108
*/
109
 
110
    /* Add resource to list */
111
    resource_id = Mqtt::GetFreeResourceId();
112
    Mqtt::resources_[resource_id] = resource;
113
 
114
 
115
    return handle_scope.Close(v8::Uint32::New(resource_id));
116
  }
117
  catch (std::exception& exception)
118
  {
119
    if (NULL != resource)
120
    {
121
      //mysql_close(resource);
122
      resource = NULL;
123
    }
124
 
125
    atom::log::Exception(log_module_, exception);
126
 
127
    return handle_scope.Close(v8::Boolean::New(false));
128
  }
129
}
130
 
131
Value Mqtt::Export_Close(const v8::Arguments& args)
132
{
133
  ATOM_VM_PLUGIN_SCOPE;
134
 
135
  atom::log::Debug(log_module_, "%s called!", __FUNCTION__);
136
 
137
  try
138
  {
139
    ATOM_VM_PLUGIN_NUM_PARAMS(1);
140
 
141
 
142
    /* Check that the resource exist */
143
    if (NULL == Mqtt::resources_[(ResourceId)args[0]->Uint32Value()])
144
    {
145
      throw atom::exception::missing_resource();
146
    }
147
 
148
 
149
    /* Close connection */
150
    //mysql_close(MySql::resources_[(ResourceId)args[0]->Uint32Value()]);
151
    Mqtt::resources_.erase((ResourceId)args[0]->Uint32Value());
152
 
153
 
154
    return handle_scope.Close(v8::Boolean::New(true));
155
  }
156
  catch (std::exception& exception)
157
  {
158
    atom::log::Exception(log_module_, exception);
159
 
160
    return handle_scope.Close(v8::Boolean::New(false));
161
  }
162
}
163
 
164
Value Mqtt::Export_SendMessage(const v8::Arguments& args)
165
{
166
  ATOM_VM_PLUGIN_SCOPE;
167
 
168
  Mqtt* result = NULL;
169
 
170
  atom::log::Debug(log_module_, "%s called!", __FUNCTION__);
171
 
172
  try
173
  {
174
    v8::Local<v8::Array> result_array = v8::Array::New();
175
 
176
    ATOM_VM_PLUGIN_NUM_PARAMS(2);
177
 
178
    v8::String::AsciiValue query(args[1]);
179
 
180
 
181
    /* Check that the resource exist */
182
    if (NULL == Mqtt::resources_[(ResourceId)args[0]->Uint32Value()])
183
    {
184
      throw atom::exception::missing_resource();
185
    }
186
 
187
 
188
    /* Execute the query */
189
    /*
190
    if (0 != mysql_query(MySql::resources_[(ResourceId)args[0]->Uint32Value()], *query))
191
    {
192
      atom::log::Error(log_module_, "Failed to execute query \"%s\", error code %d, error message \"%s\".", *query, mysql_errno(MySql::resources_[(ResourceId)args[0]->Uint32Value()]), mysql_error(MySql::resources_[(ResourceId)args[0]->Uint32Value()]));
193
      throw atom::exception::action_failed();
194
    }
195
    */
196
 
197
    /* Get the result */
198
    //result = mysql_store_result(MySql::resources_[(ResourceId)args[0]->Uint32Value()]);
199
 
200
    if (NULL == result)
201
    {
202
      /*if (mysql_field_count(MySql::resources_[(ResourceId)args[0]->Uint32Value()]) > 0)
203
      {
204
        atom::log::Error(log_module_, "Supposed to get data but got none!");
205
        throw atom::exception::action_failed();
206
      }
207
      */
208
    }
209
    else
210
    {
211
      /*
212
      MYSQL_ROW     row;
213
      uint32_t      number_of_fields = mysql_num_fields(result);
214
      MYSQL_FIELD*  fields = mysql_fetch_fields(result);
215
      uint32_t      row_index = 0;
216
 
217
      while (NULL != (row = mysql_fetch_row(result)))
218
      {
219
        v8::Local<v8::Object> vars = v8::Object::New();
220
 
221
 
222
        for (uint32_t index = 0; index < number_of_fields; index++)
223
        {
224
          vars->Set(v8::String::New(fields[index].name), v8::String::New(row[index]));
225
 
226
//          atom::log::Info(log_module_, "%s = %s\n", fields[index].name, row[index]);
227
        }
228
 
229
        result_array->Set(row_index, vars);
230
        row_index++;
231
      }
232
 
233
      mysql_free_result(result);
234
      */
235
      result = NULL;
236
    }
237
 
238
    return handle_scope.Close(v8::Handle<v8::Value>(result_array));
239
  }
240
  catch (std::exception& exception)
241
  {
242
    if (NULL != result)
243
    {
244
      //mysql_free_result(result);
245
      result = NULL;
246
    }
247
 
248
    atom::log::Exception(log_module_, exception);
249
 
250
    return handle_scope.Close(v8::Boolean::New(false));
251
  }
252
}
253
 
254
}; // namespace plugin
255
}; // namespace vm
256
}; // namespace atom