-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMQTTClient.h
More file actions
61 lines (47 loc) · 1.44 KB
/
MQTTClient.h
File metadata and controls
61 lines (47 loc) · 1.44 KB
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
/*
* File: MQTTClient.h
* Author: debian
*
* Created on 15. Juni 2017, 11:45
*/
#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H
#include <cstdio>
#include <vector>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <mosquitto.h>
#include "MosqCallback.h"
#define ORDER_CHANNEL "order"
#define CONFIRM_CHANNEL "confirm"
#define DEMAND_PREFIX "demand/"
#define OFFER_PREFIX "offer/"
#define MAX_ITEM_LEN 16
class MQTTClient {
public:
MQTTClient(const char* id, const char* const addr, MosqCallback* callback);
virtual ~MQTTClient();
bool subscribe(const char* channel);
bool publish(const char* channel, void* msg, int len);
void disconnect();
static void on_message(struct mosquitto* mosq, void* data, const struct mosquitto_message* msg);
static void on_subscribe(struct mosquitto* mosq, void* data, int mid, int qos_count, const int * granted_qos);
static void on_log(mosquitto* mosq, void* data, int level, const char* msg);
void addObserver(void (*observer)(void*, int));
void notifyObservers(void* msg, int len) const;
void loop() const;
const char* getID() const;
struct Offer {
char item[MAX_ITEM_LEN];
unsigned int price;
unsigned int amount;
};
private:
static bool lib_initialized;
char* id;
bool connected;
mosquitto* client;
std::vector<void(*)(void*, int)> observers;
};
#endif /* MQTTCLIENT_H */