2 Copyright (C) 2012 Intel 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
20 #include "bluemonkey.h"
21 #include "abstractroutingengine.h"
25 #include <QJsonDocument>
26 #include <QScriptEngine>
32 Q_SCRIPT_DECLARE_QMETAOBJECT(QTimer, QObject*)
34 #define foreach Q_FOREACH
36 extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
38 return new BluemonkeySinkManager(routingengine, config);
41 QVariant gvariantToQVariant(GVariant *value)
43 GVariantClass c = g_variant_classify(value);
44 if(c == G_VARIANT_CLASS_BOOLEAN)
45 return QVariant((bool) g_variant_get_boolean(value));
47 else if(c == G_VARIANT_CLASS_BYTE)
48 return QVariant((char) g_variant_get_byte(value));
50 else if(c == G_VARIANT_CLASS_INT16)
51 return QVariant((int) g_variant_get_int16(value));
53 else if(c == G_VARIANT_CLASS_UINT16)
54 return QVariant((unsigned int) g_variant_get_uint16(value));
56 else if(c == G_VARIANT_CLASS_INT32)
57 return QVariant((int) g_variant_get_int32(value));
59 else if(c == G_VARIANT_CLASS_UINT32)
60 return QVariant((unsigned int) g_variant_get_uint32(value));
62 else if(c == G_VARIANT_CLASS_INT64)
63 return QVariant((long long) g_variant_get_int64(value));
65 else if(c == G_VARIANT_CLASS_UINT64)
66 return QVariant((unsigned long long) g_variant_get_uint64(value));
68 else if(c == G_VARIANT_CLASS_DOUBLE)
69 return QVariant(g_variant_get_double(value));
71 else if(c == G_VARIANT_CLASS_STRING)
72 return QVariant(g_variant_get_string(value, NULL));
74 else if(c == G_VARIANT_CLASS_ARRAY)
76 gsize dictsize = g_variant_n_children(value);
78 for (int i=0;i<dictsize;i++)
80 GVariant *childvariant = g_variant_get_child_value(value,i);
81 GVariant *innervariant = g_variant_get_variant(childvariant);
82 list.append(gvariantToQVariant(innervariant));
88 return QVariant::Invalid;
92 BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config): QObject(0), AbstractSink(e, config), agent(nullptr), engine(nullptr), mSilentMode(false)
94 irc = new IrcCommunication(config, this);
98 auth = new Authenticate(config, this);
100 connect(irc, &IrcCommunication::message, [&](QString sender, QString prefix, QString codes ) {
102 if(codes.contains("authenticate"))
105 int i = codes.indexOf("authenticate");
106 QString pin = codes.mid(i+13);
110 if(!auth->authorize(prefix, pin))
111 irc->respond(sender,"failed");
115 else if(codes.startsWith("bluemonkey"))
117 if(!auth->isAuthorized(prefix))
120 irc->respond(sender, "denied");
124 QString bm("bluemonkey");
126 codes = codes.mid(bm.length()+1);
128 QString response = engine->evaluate(codes).toString();
130 if(!mSilentMode || response != "undefined" )
131 irc->respond(sender, response);
138 PropertyList BluemonkeySink::subscriptions()
143 void BluemonkeySink::supportedChanged(PropertyList supportedProperties)
145 DebugOut()<<"supported changed"<<endl;
148 void BluemonkeySink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
153 const string BluemonkeySink::uuid()
158 QObject *BluemonkeySink::subscribeTo(QString str)
160 return new Property(str.toStdString(), "", routingEngine, this);
163 QObject *BluemonkeySink::subscribeTo(QString str, QString srcFilter)
165 return new Property(str.toStdString(), srcFilter, routingEngine, this);
168 QStringList BluemonkeySink::sourcesForProperty(QString property)
170 std::list<std::string> list = routingEngine->sourcesForProperty(property.toStdString());
172 for(auto itr = list.begin(); itr != list.end(); itr++)
174 strList<<(*itr).c_str();
181 bool BluemonkeySink::authenticate(QString pass)
186 void BluemonkeySink::loadConfig(QString str)
188 configsToLoad.append(str);
189 QTimer::singleShot(1,this,SLOT(loadConfigPriv()));
192 void BluemonkeySink::loadConfigPriv()
194 if(!configsToLoad.count()) return;
196 QString str = configsToLoad.first();
197 configsToLoad.pop_front();
200 if(!file.open(QIODevice::ReadOnly))
202 DebugOut()<<"failed to open config file: "<<str.toStdString()<<endl;
206 QString script = file.readAll();
210 DebugOut()<<"evaluating script: "<<script.toStdString();
212 QScriptValue val = engine->evaluate(script);
214 DebugOut()<<val.toString().toStdString()<<endl;
217 void BluemonkeySink::reloadEngine()
220 engine->deleteLater();
222 engine = new QScriptEngine(this);
224 if(agent) delete agent;
226 agent = new BluemonkeyAgent(engine);
228 engine->setAgent(agent);
230 QScriptValue value = engine->newQObject(this);
231 engine->globalObject().setProperty("bluemonkey", value);
233 QScriptValue qtimerClass = engine->scriptValueFromQMetaObject<QTimer>();
234 engine->globalObject().setProperty("QTimer", qtimerClass);
236 QScriptValue ircValue = engine->newQObject(irc);
237 engine->globalObject().setProperty("irc", ircValue);
239 loadConfig(configuration["config"].c_str());
242 void BluemonkeySink::writeProgram(QString program)
244 QFile file(configuration["customPrograms"].c_str());
246 if(!file.open(QIODevice::ReadWrite | QIODevice::Append))
248 DebugOut(DebugOut::Error)<<"failed to open file: "<<file.fileName().toStdString()<<endl;
252 file.write(program.toUtf8());
258 void BluemonkeySink::log(QString str)
260 DebugOut()<<str.toStdString()<<endl;
263 void BluemonkeySink::getHistory(QStringList properties, QDateTime begin, QDateTime end, QScriptValue cbFunction)
265 double b = (double)begin.toMSecsSinceEpoch() / 1000.0;
266 double e = (double)end.toMSecsSinceEpoch() / 1000.0;
267 AsyncRangePropertyRequest request;
268 request.timeBegin = b;
271 PropertyList reqlist;
273 foreach(QString prop, properties)
275 reqlist.push_back(prop.toStdString());
278 request.properties = reqlist;
279 request.completed = [&cbFunction](AsyncRangePropertyReply* reply)
283 DebugOut(DebugOut::Error)<<"bluemoney get history call failed"<<endl;
287 if(cbFunction.isFunction())
291 for(auto itr = reply->values.begin(); itr != reply->values.end(); itr++)
293 AbstractPropertyType *val = *itr;
295 list.append(gvariantToQVariant(val->toVariant()));
298 cbFunction.call(QScriptValue(),cbFunction.engine()->newVariant(list));
305 routingEngine->getRangePropertyAsync(request);
309 QVariant Property::value()
311 return mValue ? gvariantToQVariant(mValue->toVariant()) : QVariant::Invalid;
314 void Property::setValue(QVariant v)
316 if(v.type() == QVariant::List || v.type() == QVariant::Map)
319 QJsonDocument doc = QJsonDocument::fromVariant(v);
321 QString json = doc.toJson();
323 mValue->fromString(json.toStdString());
327 QString tempVal = v.toString();
328 mValue->fromString(tempVal.toStdString());
331 AsyncSetPropertyRequest request;
332 request.property = mValue->name;
333 request.value = mValue->copy();
334 request.completed = [&](AsyncPropertyReply* reply)
338 propertyChanged(reply->property,reply->value,reply->value->sourceUuid);
342 routingEngine->setProperty(request);
345 void Property::getHistory(QDateTime begin, QDateTime end, QScriptValue cbFunction)
347 double b = (double)begin.toMSecsSinceEpoch() / 1000.0;
348 double e = (double)end.toMSecsSinceEpoch() / 1000.0;
349 AsyncRangePropertyRequest request;
350 request.timeBegin = b;
353 PropertyList reqlist;
354 reqlist.push_back(mValue->name);
356 request.properties = reqlist;
357 request.completed = [&cbFunction](AsyncRangePropertyReply* reply)
361 DebugOut(DebugOut::Error)<<"bluemoney get history call failed"<<endl;
365 if(cbFunction.isFunction())
369 for(auto itr = reply->values.begin(); itr != reply->values.end(); itr++)
371 AbstractPropertyType *val = *itr;
373 list.append(gvariantToQVariant(val->toVariant()));
376 cbFunction.call(QScriptValue(),cbFunction.engine()->newVariant(list));
383 routingEngine->getRangePropertyAsync(request);
386 Property::Property(VehicleProperty::Property prop, QString srcFilter, AbstractRoutingEngine* re, QObject *parent)
387 :QObject(parent), AbstractSink(re, std::map<std::string,std::string>()),mValue(nullptr), mUuid(amb::createUuid())
389 setType(prop.c_str());
392 QString Property::type()
394 return mValue->name.c_str();
397 void Property::setType(QString t)
399 if(mValue && type() != "")
400 routingEngine->unsubscribeToProperty(type().toStdString(),this);
402 routingEngine->subscribeToProperty(t.toStdString(),this);
404 mValue = VehicleProperty::getPropertyTypeForPropertyNameValue(t.toStdString());
409 AsyncPropertyRequest request;
410 request.property = mValue->name;
411 request.completed = [this](AsyncPropertyReply* reply)
414 propertyChanged(reply->property, reply->value,uuid());
419 routingEngine->getPropertyAsync(request);
422 void Property::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, string uuid)
428 mValue = value->copy();
430 changed(gvariantToQVariant(mValue->toVariant()));