Skip to content

Commit

Permalink
Update charger json parser
Browse files Browse the repository at this point in the history
Adapt the charger parser to the new format.

Signed-off-by: Francis Giraldeau <[email protected]>
  • Loading branch information
giraldeau committed Oct 19, 2016
1 parent 2d1e019 commit 503e61a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
14 changes: 8 additions & 6 deletions libevnav/charger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ Charger::Charger()
void Charger::read(const QJsonObject &json)
{
m_name = json["ParkName"].toString();
m_level = json["Level"].toInt();
m_network_id = json["NetworkId"].toInt();

QJsonObject loc = json["LatLng"].toObject();
double lat = loc["Lat"].toDouble();
double lng = loc["Lng"].toDouble();
QJsonObject tmp = json["Ports"].toArray().at(0).toObject();
m_level = tmp["Level"].toInt();
double lat = json["Latitude"].toDouble();
double lng = json["Longitude"].toDouble();
m_loc = util::Coordinate{util::FloatLongitude{lng}, util::FloatLatitude{lat}};
}

bool Charger::isValid()
{
return (!m_name.isEmpty() && m_level != 0 && m_loc.IsValid());
}
3 changes: 1 addition & 2 deletions libevnav/charger.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ class Charger
QString name() { return m_name; }
VertexId id() { return m_id; }
osrm::util::Coordinate loc() { return m_loc; }
int network_id() { return m_network_id; }
int level() { return m_level; }
void read(const QJsonObject &json);
bool isValid();

private:
VertexId m_id;
QString m_name;
osrm::util::Coordinate m_loc;
int m_level;
int m_network_id;
static QAtomicInt m_count;
};

Expand Down
7 changes: 5 additions & 2 deletions libevnav/chargerprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ void ChargerProvider::read(const QJsonArray &json)
QJsonObject obj = json[i].toObject();
Charger c;
c.read(obj);
m_chargers[c.id()] = c;
if (c.isValid()) {
m_chargers[c.id()] = c;
}
}
}

Expand All @@ -29,7 +31,8 @@ void ChargerProvider::loadJson(QString path)
if (file.open(QIODevice::ReadOnly)) {
QByteArray data = file.readAll();
QJsonDocument doc(QJsonDocument::fromJson(data));
read(doc.array());
QJsonObject root = doc.object();
read(root["list"].toArray());
}
}

Expand Down
12 changes: 12 additions & 0 deletions tests/graph/tst_graphtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "graph.h"
#include "shortestpath.h"
#include "evnavrequest.h"
#include "chargerprovider.h"

class GraphTest : public QObject
{
Expand All @@ -18,6 +19,7 @@ private Q_SLOTS:
void testCase2();
void testCase3();
void testCase4();
void testLoadCharger();
};

GraphTest::GraphTest()
Expand Down Expand Up @@ -94,6 +96,16 @@ void GraphTest::testCase4()
QVERIFY(QString{"0:16"}.compare(time.toString("h:m")) == 0);
}

void GraphTest::testLoadCharger()
{
ChargerProvider provider;
QString path(TOPSRCDIR "/res/result_circuit_electrique_20161016170731.json");
provider.loadJson(path);
QVERIFY2(provider.size() > 0, "failed to load the charger list");
ChargerProvider dcfc = provider.filter(provider.fastChargerFilter());
QVERIFY2(dcfc.size() > 0, "failed to filter chargers");
}

QTEST_APPLESS_MAIN(GraphTest)

#include "tst_graphtest.moc"

0 comments on commit 503e61a

Please sign in to comment.