APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
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
#include <cstdio>
#include <cstring>
 
#include "temperature_conversion.h"
#include <mosquittopp.h>
 
mqtt_tempconv::mqtt_tempconv(const char *id, const char *host, int port) : mosquittopp(id)
{
    int keepalive = 60;
 
    /* Connect immediately. This could also be done by calling
     * mqtt_tempconv->connect(). */
    connect(host, port, keepalive);
};
 
mqtt_tempconv::~mqtt_tempconv()
{
}
 
void mqtt_tempconv::on_connect(int rc)
{
    printf("Connected with code %d.\n", rc);
    if(rc == 0){
        /* Only attempt to subscribe on a successful connect. */
        subscribe(NULL, "temperature/celsius");
    }
}
 
void mqtt_tempconv::on_message(const struct mosquitto_message *message)
{
    double temp_celsius, temp_farenheit;
    char buf[51];
 
    if(!strcmp(message->topic, "temperature/celsius")){
        memset(buf, 0, 51*sizeof(char));
        /* Copy N-1 bytes to ensure always 0 terminated. */
        memcpy(buf, message->payload, 50*sizeof(char));
        temp_celsius = atof(buf);
        temp_farenheit = temp_celsius*9.0/5.0 + 32.0;
        snprintf(buf, 50, "%f", temp_farenheit);
        publish(NULL, "temperature/farenheit", strlen(buf), buf);
    }
}
 
void mqtt_tempconv::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
    printf("Subscription succeeded.\n");
}