Commit 4dc3f26d authored by Guilhem Saurel's avatar Guilhem Saurel
Browse files

status & energy

parent b552f890
......@@ -34,6 +34,8 @@ yarn parce src/index.html
```
/odri/voltage
/odri/current
/odri/energy
/odri/status
/odri/joystick
```
......
......@@ -7,7 +7,8 @@
class MqttInterface {
public:
MqttInterface();
void set(unsigned char current, unsigned char voltage, std::string & joystick);
void set(double current, double voltage, double energy, std::string & joystick);
void setStatus(std::string & status);
void start();
void stop();
bool getStop();
......@@ -15,8 +16,10 @@ class MqttInterface {
private:
bool run_;
unsigned char current_;
unsigned char voltage_;
double current_;
double voltage_;
double energy_;
std::string status_;
std::string joystick_;
std::mutex run_m;
std::mutex data_m;
......@@ -24,6 +27,8 @@ class MqttInterface {
bool getRun();
std::string getCurrent();
std::string getVoltage();
std::string getEnergy();
std::string getStatus();
std::string getJoystick();
};
#endif /* !MQTT_INTERFACE_HPP */
......@@ -8,6 +8,8 @@ const app = Vue.createApp({
client: '-',
voltage: '-',
current: '-',
energy: '-',
status: '-',
joystick: '-'
}
},
......@@ -16,13 +18,18 @@ const app = Vue.createApp({
this.client.publish('/odri/commands', message);
},
onConnect() {
this.client.subscribe(['/odri/voltage', '/odri/current', '/odri/joystick']);
this.client.subscribe(['/odri/voltage', '/odri/current',
'/odri/energy', '/odri/joystick', '/odri/status']);
},
onMessage(topic, message) {
if (topic == '/odri/voltage') {
this.voltage = message.toString();
} else if (topic == '/odri/current') {
this.current = message.toString();
} else if (topic == '/odri/energy') {
this.energy = message.toString();
} else if (topic == '/odri/status') {
this.status = message.toString();
} else if (topic == '/odri/joystick') {
this.joystick = message.toString();
} else {
......
......@@ -11,8 +11,10 @@
<main>
<div id="app">
<dl class="row">
<dt class="col-sm-3">Status</dt><dd class="col-sm-9">{{ status }}</dd>
<dt class="col-sm-3">Voltage</dt><dd class="col-sm-9">{{ voltage }}</dd>
<dt class="col-sm-3">Current</dt><dd class="col-sm-9">{{ current }}</dd>
<dt class="col-sm-3">Energy</dt><dd class="col-sm-9">{{ energy }}</dd>
<dt class="col-sm-3">Joystick</dt><dd class="col-sm-9">{{ joystick }}</dd>
</dl>
<button type="button" class="btn btn-warning" v-on:click="onCommand('calibrate')">Calibrate</button>
......
......@@ -4,14 +4,21 @@
#include "qrw/mqtt-interface.hpp"
int main() {
unsigned char current = 12;
double current = 12;
double energy = 33;
std::string joystick = "go";
std::string starting = "starting";
std::string running = "running";
std::string done = "done";
MqttInterface mqtt_interface;
std::thread mqtt_thread(&MqttInterface::start, &mqtt_interface);
mqtt_interface.setStatus(starting);
std::this_thread::sleep_for(std::chrono::seconds(2));
for (unsigned char voltage = 100; voltage > 72; voltage--) {
mqtt_interface.set(current, voltage, joystick);
mqtt_interface.set(current, (double)voltage, energy, joystick);
if (mqtt_interface.getStop()) {
std::cout << "STOP !" << std::endl;
break;
......@@ -19,8 +26,10 @@ int main() {
if (mqtt_interface.getCalibrate()) {
std::cout << "let's calibrate" << std::endl;
}
mqtt_interface.setStatus(running);
std::this_thread::sleep_for(std::chrono::milliseconds(250));
}
mqtt_interface.setStatus(done);
mqtt_interface.stop();
mqtt_thread.join();
......
......@@ -37,14 +37,20 @@ void connlost(void *context, char *cause) {
std::cerr << "cause: " << cause << std::endl;
}
void MqttInterface::set(unsigned char current, unsigned char voltage, std::string & joystick) {
void MqttInterface::set(double current, double voltage, double energy, std::string & joystick) {
std::lock_guard<std::mutex> guard(data_m);
current_ = current;
voltage_ = voltage;
energy_ = energy;
joystick_ = joystick;
}
MqttInterface::MqttInterface() : run_(true), current_(0), voltage_(0), joystick_() {}
void MqttInterface::setStatus(std::string & status) {
std::lock_guard<std::mutex> guard(data_m);
status_ = status;
}
MqttInterface::MqttInterface() : run_(true), current_(0), voltage_(0), energy_(0) {}
void MqttInterface::start() {
int rc;
......@@ -64,24 +70,34 @@ void MqttInterface::start() {
std::string current_s;
std::string voltage_s;
std::string energy_s;
std::string status_s;
std::string joystick_s;
const char* current_c;
const char* voltage_c;
const char* energy_c;
const char* status_c;
const char* joystick_c;
while (getRun()) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
current_s = getCurrent();
voltage_s = getVoltage();
energy_s = getEnergy();
status_s = getStatus();
joystick_s = getJoystick();
current_c = current_s.data();
voltage_c = voltage_s.data();
energy_c = energy_s.data();
status_c = status_s.data();
joystick_c = joystick_s.data();
MQTTClient_publish(client, "/odri/current", std::strlen(current_c), current_c, 0, 0, &token);
MQTTClient_publish(client, "/odri/voltage", std::strlen(voltage_c), voltage_c, 0, 0, &token);
MQTTClient_publish(client, "/odri/energy", std::strlen(energy_c), energy_c, 0, 0, &token);
MQTTClient_publish(client, "/odri/status", std::strlen(status_c), status_c, 0, 0, &token);
MQTTClient_publish(client, "/odri/joystick", std::strlen(joystick_c), joystick_c, 0, 0, &token);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
MQTTClient_disconnect(client, 10000);
......@@ -116,6 +132,11 @@ bool MqttInterface::getRun() {
return run_;
}
std::string MqttInterface::getStatus() {
std::lock_guard<std::mutex> guard(data_m);
return status_;
}
std::string MqttInterface::getJoystick() {
std::lock_guard<std::mutex> guard(data_m);
return joystick_;
......@@ -132,3 +153,9 @@ std::string MqttInterface::getVoltage() {
std::string ret = std::to_string(voltage_);
return ret;
}
std::string MqttInterface::getEnergy() {
std::lock_guard<std::mutex> guard(data_m);
std::string ret = std::to_string(energy_);
return ret;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment