summaryrefslogtreecommitdiff
path: root/plugins/bluemonkey/bluemonkey.cpp
blob: 05ea3eeb8b7c9e729db23627cdc24183a280951c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
    Copyright (C) 2012  Intel Corporation

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include "bluemonkey.h"
#include "abstractroutingengine.h"
#include "debugout.h"
#include "irccoms.h"

#include <QJsonDocument>
#include <QScriptEngine>
#include <QString>
#include <QFile>

extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, map<string, string> config)
{
	return new BluemonkeySinkManager(routingengine, config);
}

QVariant gvariantToQVariant(GVariant *value)
{
	switch (g_variant_classify(value)) {
		case G_VARIANT_CLASS_BOOLEAN:
			return QVariant((bool) g_variant_get_boolean(value));

		case G_VARIANT_CLASS_BYTE:
			return QVariant((char) g_variant_get_byte(value));

		case G_VARIANT_CLASS_INT16:
			return QVariant((int) g_variant_get_int16(value));

		case G_VARIANT_CLASS_UINT16:
			return QVariant((unsigned int) g_variant_get_uint16(value));

		case G_VARIANT_CLASS_INT32:
			return QVariant((int) g_variant_get_int32(value));

		case G_VARIANT_CLASS_UINT32:
			return QVariant((unsigned int) g_variant_get_uint32(value));

		case G_VARIANT_CLASS_INT64:
			return QVariant((long long) g_variant_get_int64(value));

		case G_VARIANT_CLASS_UINT64:
			return QVariant((unsigned long long) g_variant_get_uint64(value));

		case G_VARIANT_CLASS_DOUBLE:
			return QVariant(g_variant_get_double(value));

		case G_VARIANT_CLASS_STRING:
			return QVariant(g_variant_get_string(value, NULL));

		default:
			return QVariant::Invalid;
	}
}

BluemonkeySink::BluemonkeySink(AbstractRoutingEngine* e, map<string, string> config): QObject(0), AbstractSink(e, config)
{
	irc = new IrcCommunication(this);
	irc->connect("chat.freenode.com",8001,"","tripzero","bluemonkey","");
	connect(irc,&IrcCommunication::connected, [&]() {
		irc->join("#linuxice");
	});

	engine = new QScriptEngine(this);

	auth = new Authenticate(this);

	QScriptValue value = engine->newQObject(this);
	engine->globalObject().setProperty("bluemonkey", value);

	connect(irc, &IrcCommunication::message, [&](QString sender, QString prefix, QString codes ) {

		if(codes.contains("authenticate"))
		{

			int i = codes.indexOf("authenticate");
			QString pin = codes.mid(i+13);
			pin = pin.trimmed();


			if(!auth->authorize(prefix, pin))
				irc->respond(sender,"failed");
			qDebug()<<sender;

		}
		else if(codes.startsWith("bluemonkey"))
		{
			if(!auth->isAuthorized(prefix))
			{
				irc->respond(sender, "denied");
				return;
			}

			QString bm("bluemonkey");

			codes = codes.mid(bm.length()+1);

			irc->respond(sender, engine->evaluate(codes).toString());
		}
	});

	loadConfig("config.js");

}


PropertyList BluemonkeySink::subscriptions()
{

}

void BluemonkeySink::supportedChanged(PropertyList supportedProperties)
{

}

void BluemonkeySink::propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, std::string uuid)
{

}

std::string BluemonkeySink::uuid()
{
	return "bluemonkey";
}

QObject *BluemonkeySink::subscribeTo(QString str)
{
	return new Property(str.toStdString(), routingEngine, this);
}

bool BluemonkeySink::authenticate(QString pass)
{

}

void BluemonkeySink::loadConfig(QString str)
{
	QFile file(str);
	if(!file.open(QIODevice::ReadOnly))
	{
		qDebug()<<"failed to open config file: "<<str;
		return;
	}

	QString script = file.readAll();

	file.close();

	engine->evaluate(script);
}


QVariant Property::value()
{
	return gvariantToQVariant(mValue->toVariant());
}

void Property::setValue(QVariant v)
{
	QJsonDocument doc;
	doc.fromVariant(v);

	mValue->fromString(doc.toJson().data());

	AsyncSetPropertyRequest request;
	request.property = mValue->name;
	request.value = mValue;
	request.completed = [](AsyncPropertyReply* reply) { delete reply; };
	routingEngine->setProperty(request);
}

Property::Property(VehicleProperty::Property prop, AbstractRoutingEngine* re, QObject *parent)
	:QObject(parent), AbstractSink(re, std::map<std::string,std::string>()),mValue(nullptr)
{
	setType(prop.c_str());
}

QString Property::type()
{
	return mValue->name.c_str();
}

void Property::setType(QString t)
{
	if(mValue && type() != "")
		routingEngine->unsubscribeToProperty(type().toStdString(),this);

	routingEngine->subscribeToProperty(t.toStdString(),this);

	mValue = VehicleProperty::getPropertyTypeForPropertyNameValue(t.toStdString());

	AsyncPropertyRequest request;
	request.property = mValue->name;
	request.completed = [this](AsyncPropertyReply* reply)
	{
		propertyChanged(reply->property, reply->value,uuid());
		delete reply;
	};

	routingEngine->getPropertyAsync(request);
}

void Property::propertyChanged(VehicleProperty::Property property, AbstractPropertyType *value, string uuid)
{
	mValue = value->copy();

	changed(gvariantToQVariant(mValue->toVariant()));
}