Skip to content

Commit

Permalink
Merge pull request #132 from Northeastern-Electric-Racing/protobuf-up…
Browse files Browse the repository at this point in the history
…date

finished protobuf update
  • Loading branch information
mattrwang authored Jan 12, 2025
2 parents cd840cd + 0e2fbde commit 59c5640
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 34 deletions.
9 changes: 6 additions & 3 deletions NERODevelopment/src/controllers/configurationcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,12 @@ void ConfigurationController::upButtonPressed() {
void ConfigurationController::enterButtonPressed() {
if (!this->m_isKeyboardSelected) {
if (this->m_selectedConfigurationIndex == 3) {
this->m_model->sendMessage("driver", this->m_driverName);
this->m_model->sendMessage("system", this->m_systemName);
this->m_model->sendMessage("location", this->m_locationName);
// this->m_model->sendMessage("driver", this->m_driverName);
// this->m_model->sendMessage("system", this->m_systemName);
// this->m_model->sendMessage("location", this->m_locationName);
this->m_model->sendMessage("driver", 0);
this->m_model->sendMessage("system", 0);
this->m_model->sendMessage("location", 0);
} else {
this->setIsKeyboardSelected(true);
}
Expand Down
3 changes: 1 addition & 2 deletions NERODevelopment/src/controllers/flappybirdcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ void FlappyBirdController::enterButtonPressed() {

void FlappyBirdController::saveScore(int score) {
QString topic = "NERO/FLAPPYBIRD/SCORE";
QString message = QString::number(score);
m_model->sendMessage(topic, message);
m_model->sendMessage(topic, score);
}
3 changes: 1 addition & 2 deletions NERODevelopment/src/controllers/snakecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ bool SnakeController::isSameOrOppositeDirection(int newDirection) {

void SnakeController::saveScore(int score) {
QString topic = "NERO/SNAKE/SCORE";
QString message = QString::number(score);
m_model->sendMessage(topic, message);
m_model->sendMessage(topic, score);
}
8 changes: 4 additions & 4 deletions NERODevelopment/src/models/mock_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ std::optional<float> MockModel::getLowVoltageStateOfCharge() {
return lvBattery;
}

void MockModel::sendMessage(QString topic, QString message) {
qDebug() << "Sending Message: " << topic << " " << message;
void MockModel::sendMessage(QString topic, float value) {
qDebug() << "Sending Message: " << topic << " " << value;
}

QVector<QString> MockModel::convertNumberToDataInfoValue(float value) {
QVector<float> MockModel::convertNumberToDataInfoValue(float value) {
// qDebug() << value;
QVector<QString> val = {QString::number(value)};
QVector<float> val = {value};
// qDebug() << val;
return val;
}
Expand Down
4 changes: 2 additions & 2 deletions NERODevelopment/src/models/mock_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MockModel : public Model {
std::optional<int> getNumberOfNonCriticalFaults() override;
std::optional<float> getLowVoltageStateOfCharge() override;

void sendMessage(const QString topic, const QString message) override;
void sendMessage(const QString topic, const float value) override;

private slots:
void updateCurrentData() override;
Expand Down Expand Up @@ -135,7 +135,7 @@ private slots:
float down;
float right;

QVector<QString> convertNumberToDataInfoValue(float value);
QVector<float> convertNumberToDataInfoValue(float value);
};

#endif // MOCK_MODEL_H
6 changes: 3 additions & 3 deletions NERODevelopment/src/models/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Model::updatePackTempData() {
void Model::addPinnedData(QString id) {
bool success;
DataInfo dataInfo = this->currentData.value(id, DataInfo());
float value = dataInfo.values[0].toFloat(&success);
float value = dataInfo.values[0];

if (success && value != -9999) {
pinnedData.insert(
Expand Down Expand Up @@ -78,7 +78,7 @@ QList<DebugTableRowValue> Model::getDebugTableValues() {
const DataInfo &dataInfo = it.value();
QString name = dataInfo.topic;
QString units = dataInfo.unit;
float value = dataInfo.values[0].toFloat();
float value = dataInfo.values[0];

DebugTableRowValue row = {name, (std::round(value)), units};

Expand All @@ -90,7 +90,7 @@ QList<DebugTableRowValue> Model::getDebugTableValues() {

std::optional<float> Model::getById(QString id) {
bool ok;
float value = this->currentData.value(id, DataInfo()).values[0].toFloat(&ok);
float value = this->currentData.value(id, DataInfo()).values[0];
if (ok && value != -9999) {
return value;
} else {
Expand Down
2 changes: 1 addition & 1 deletion NERODevelopment/src/models/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Model : public QObject {
virtual std::optional<bool> getIsTalking() = 0;
virtual std::optional<int> getNumberOfCriticalFaults() = 0;
virtual std::optional<int> getNumberOfNonCriticalFaults() = 0;
virtual void sendMessage(QString topic, QString message) = 0;
virtual void sendMessage(QString topic, float value) = 0;
virtual std::optional<float> getLowVoltageStateOfCharge() = 0;

std::optional<int> getTime();
Expand Down
17 changes: 13 additions & 4 deletions NERODevelopment/src/models/mqtt_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QtCore/QDateTime>
#include <QtMqtt/QMqttClient>
#include <QtWidgets/QMessageBox>
#include <chrono>
#include <serverdata.qpb.h>

MqttClient::MqttClient(QObject *parent) : QObject(parent) {
Expand Down Expand Up @@ -66,7 +67,7 @@ void MqttClient::brokerConnected() {

void MqttClient::receiveMessage(const QByteArray &message,
const QMqttTopicName &topic) {
serverdata::ServerData serverData;
serverdata::v2::ServerData serverData;

bool success = m_serializer.deserialize(&serverData, message);

Expand All @@ -85,10 +86,18 @@ void MqttClient::updateMessage(const QMqttMessage &msg) {
// qDebug() << debug;
}

void MqttClient::sendMessage(const QString topic, const QString msg) {
serverdata::ServerData serverData;
void MqttClient::sendMessage(const QString topic, const float value) {
serverdata::v2::ServerData serverData;
serverData.setUnit("");
serverData.setValues({msg});
serverData.setValues({value});

auto now = std::chrono::system_clock::now();
auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(
now.time_since_epoch())
.count();
QtProtobuf::uint64 timeUs = static_cast<QtProtobuf::uint64>(microseconds);

serverData.setTimeUs(timeUs);
QByteArray data = serverData.serialize(&this->m_serializer);
m_client->publish("NERO/" + topic, data);
}
4 changes: 2 additions & 2 deletions NERODevelopment/src/models/mqtt_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public slots:
* topic and then the passed following topic path.
* @param msg, the message to send
*/
void sendMessage(const QString topic, const QString msg);
void sendMessage(const QString topic, const float value);

signals:
/**
* @brief emitServerData, Emits the parsed server data object from protobuf
*/
void emitServerData(const serverdata::ServerData, const QString);
void emitServerData(const serverdata::v2::ServerData, const QString);

private slots:

Expand Down
6 changes: 3 additions & 3 deletions NERODevelopment/src/models/raspberry_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ void RaspberryModel::connectToMQTT() {
this->m_client = client;
}

void RaspberryModel::sendMessage(const QString topic, const QString message) {
this->m_client->sendMessage(topic, message);
void RaspberryModel::sendMessage(const QString topic, const float value) {
this->m_client->sendMessage(topic, value);
}

void RaspberryModel::receiveServerData(const serverdata::ServerData data,
void RaspberryModel::receiveServerData(const serverdata::v2::ServerData data,
const QString topic) {
// qDebug() << "Topic: " << topic << "Data" << data.values();
this->currentData[topic] = DataInfo(topic, data.unit(), data.values());
Expand Down
4 changes: 2 additions & 2 deletions NERODevelopment/src/models/raspberry_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ class RaspberryModel : public Model {
std::optional<int> getNumberOfNonCriticalFaults() override;
std::optional<float> getLowVoltageStateOfCharge() override;

void sendMessage(const QString topic, const QString message) override;
void sendMessage(const QString topic, const float value) override;

private slots:
void updateCurrentData() override;
void receiveServerData(const serverdata::ServerData, const QString topic);
void receiveServerData(const serverdata::v2::ServerData, const QString topic);

private:
void processData(const std::string &data);
Expand Down
9 changes: 7 additions & 2 deletions NERODevelopment/src/proto/serverdata/serverdata.proto
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
syntax = "proto3";

package serverdata;
package serverdata.v2;

message ServerData {
repeated string values = 1;
// ensure old type is reserved
reserved 1;
reserved "value";
string unit = 2;
// time since unix epoch in MICROSECONDS
uint64 time_us = 3;
repeated float values = 4;
}
4 changes: 2 additions & 2 deletions NERODevelopment/src/utils/server_data.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../utils/server_data.h"

DataInfo::DataInfo(const QString topic, const QString unit,
const QVector<QString> values) {
const QVector<float> values) {
this->topic = topic;
this->unit = unit;
this->values = values;
Expand All @@ -10,5 +10,5 @@ DataInfo::DataInfo(const QString topic, const QString unit,
DataInfo::DataInfo() {
this->topic = nullptr;
this->unit = nullptr;
this->values = {"-9999"};
this->values = {-9999};
}
4 changes: 2 additions & 2 deletions NERODevelopment/src/utils/server_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
struct DataInfo {
QString topic;
QString unit;
QVector<QString> values;
QVector<float> values;

DataInfo();
DataInfo(const QString topic, const QString unit,
const QVector<QString> values);
const QVector<float> values);
};

#endif // SERVER_DATA_H

0 comments on commit 59c5640

Please sign in to comment.