-
Notifications
You must be signed in to change notification settings - Fork 22
/
receivehandling.go
37 lines (34 loc) · 1.27 KB
/
receivehandling.go
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
package can2mqtt
import (
"fmt"
CAN "github.com/brendoncarroll/go-can"
MQTT "github.com/eclipse/paho.mqtt.golang"
)
// handleCAN is the standard receivehandler for CANFrames
// and does the following:
// 1. calling standard convertfunction: convert2MQTT
// 2. sending the message
func handleCAN(cf CAN.CANFrame) {
if dbg {
fmt.Printf("receivehandler: received CANFrame: ID: %d, len: %d, payload %s\n", cf.ID, cf.Len, cf.Data)
}
mqttPayload := convert2MQTT(int(cf.ID), int(cf.Len), cf.Data)
if dbg {
fmt.Printf("receivehandler: converted String: %s\n", mqttPayload)
}
topic := getTopic(int(cf.ID))
mqttPublish(topic, mqttPayload)
fmt.Printf("ID: %d len: %d data: %X -> topic: \"%s\" message: \"%s\"\n", cf.ID, cf.Len, cf.Data, topic, mqttPayload)
}
// handleMQTT is the standard receivehandler for MQTT
// messages and does the following:
// 1. calling the standard convertfunction: convert2CAN
// 2. sending the message
func handleMQTT(cl MQTT.Client, msg MQTT.Message) {
if dbg {
fmt.Printf("receivehandler: received message: topic: %s, msg: %s\n", msg.Topic(), msg.Payload())
}
cf := convert2CAN(msg.Topic(), string(msg.Payload()))
canPublish(cf)
fmt.Printf("ID: %d len: %d data: %X <- topic: \"%s\" message: \"%s\"\n", cf.ID, cf.Len, cf.Data, msg.Topic(), msg.Payload())
}