Ejemplo n.º 1
0
func MsgRecvfun(client *MQTT.Client, msg MQTT.Message) {
	appUpChan <- msg.Topic()
	fmt.Printf("appUp")
	fmt.Printf("topic:[%s]  ", msg.Topic())
	fmt.Printf("Mesg:%s\n", msg.Payload())

}
Ejemplo n.º 2
0
func mqttMsgToWolfMsg(msg MQTT.Message) models.IncomingPacket {
	inMsg := models.IncomingPacket{}
	inMsg.Message = string(msg.Payload())
	inMsg.Topic = msg.Topic()
	inMsg.QoS = int(msg.Qos())
	return inMsg
}
Ejemplo n.º 3
0
// messageHandler is called when a new message arrives
func handleMessage(client *MQTT.Client, msg MQTT.Message) {

	// Unmarshal JSON to RxPacket
	var packet shared.RxPacket
	err := json.Unmarshal(msg.Payload(), &packet)
	if err != nil {
		log.WithField("topic", msg.Topic()).WithError(err).Warn("Failed to unmarshal JSON.")
		return
	}

	// Filter messages by gateway
	if gateway != "" && packet.GatewayEui != gateway {
		return
	}

	// Decode payload
	data, err := base64.StdEncoding.DecodeString(packet.Data)
	if err != nil {
		log.WithField("topic", msg.Topic()).WithError(err).Warn("Failed to decode Payload.")
		return
	}

	ctx := log.WithFields(log.Fields{
		"devAddr": packet.NodeEui,
	})

	if showMeta {
		ctx = ctx.WithFields(log.Fields{
			"gatewayEui": packet.GatewayEui,
			"time":       packet.Time,
			"frequency":  *packet.Frequency,
			"dataRate":   packet.DataRate,
			"rssi":       *packet.Rssi,
			"snr":        *packet.Snr,
		})
	}

	if showRaw {
		ctx = ctx.WithField("data", fmt.Sprintf("%x", data))
	}

	if showTiming {
		rawData, err := base64.StdEncoding.DecodeString(packet.Data)
		if err == nil {
			airtime, err := util.CalculatePacketTime(len(rawData), packet.DataRate)
			if err == nil {
				ctx = ctx.WithField("airtime", fmt.Sprintf("%.1f ms", airtime))
			}
		}
	}

	// Check for unprintable characters
	unprintable, _ := regexp.Compile(`[^[:print:]]`)
	if unprintable.Match(data) {
		ctx.Debug("Received Message")
	} else {
		ctx.WithField("message", fmt.Sprintf("%s", data)).Info("Received Message")
	}

}
Ejemplo n.º 4
0
func onMessageReceived(client *MQTT.MqttClient, message MQTT.Message) {
	fmt.Printf("Received message on topic: %s\n", message.Topic())
	fmt.Printf("Message: %s\n", message.Payload())

	if string(message.Payload()) == "ぬるぽ" {
		Publish(client, "say", "ガッ")
	}
}
Ejemplo n.º 5
0
func (b *Broker) onMessageReceived(client *MQTT.Client, m MQTT.Message) {
	log.Debugf("topic:%s / msg:%s", m.Topic(), m.Payload())

	msg := message.Message{
		Sender: b.Name,
		Type:   message.TypeSubscribed,
		Body:   m.Payload(),
		Topic:  m.Topic(),
	}
	b.GwChan <- msg
}
Ejemplo n.º 6
0
Archivo: main.go Proyecto: alsm/goIoT
func actionHandler(client *MQTT.Client, message MQTT.Message) {
	fmt.Println("Received action message on", message.Topic(), "-", string(message.Payload()))
	action := strings.ToLower(string(message.Payload()))
	switch action {
	case "off":
		host.LedsOff()
	case "on":
		host.LedsOn()
	case "toggle":
		host.LedsToggle()
	case "slide":
		host.LedsCycle(3)
	}
}
Ejemplo n.º 7
0
func (m *MqttClient) onMessageReceived(client *MQTT.Client, message MQTT.Message) {
	log.Infof("topic:%s", message.Topic())

	// Remove topic root
	ct := strings.TrimRight(m.Config.Topic, "#")
	topic := strings.Replace(message.Topic(), ct, "", 1)

	chun := Message{
		Topic:   topic,
		Payload: message.Payload(),
	}

	m.mqttChan <- chun
}
Ejemplo n.º 8
0
func (d *Device) messageHandler(client *MQTT.Client, msg MQTT.Message) {
	fmt.Printf("TOPIC: %s\n", msg.Topic())
	fmt.Printf("MSG: %x\n", msg.Payload())
	msgtype := msg.Topic()
	fmt.Println(msgtype)

	switch msgtype {
	case "c":
		d.commandHandler(client, msg)
	case "s":
		d.statusHandler(client, msg)
	default:
		fmt.Println("unsuported message type :", msgtype)
	}
}
Ejemplo n.º 9
0
// BrokerHandler -
func (c *Connection) BrokerHandler(client *MQTT.Client, msg MQTT.Message) {
	c.Info(
		"Received new mqtt (worker: %s) - (message: %s) for (topic: %s). Building event now ...",
		c.Name(), msg.Payload(), msg.Topic(),
	)

	event, err := events.NewEvent(msg)

	if err != nil {
		c.Error("Could not handle received event due to (err: %s)", err)
		return
	}

	c.Info("Event successfully created (data: %v)", event)
	c.events <- event
}
Ejemplo n.º 10
0
func messageReceived(client *MQTT.Client, msg MQTT.Message) {
	topics := strings.Split(msg.Topic(), "/")
	msgFrom := topics[len(topics)-1]
	fmt.Print(msgFrom + ": " + string(msg.Payload()))
}
Ejemplo n.º 11
0
Archivo: mqtt.go Proyecto: kgbu/mqttcli
func onMessageReceived(client *MQTT.MqttClient, message MQTT.Message) {
	log.Infof("topic:%s  / msg:%s", message.Topic(), message.Payload())
	fmt.Println(string(message.Payload()))
}
func onMessageReceived(client *MQTT.MqttClient, message MQTT.Message) {
	fmt.Printf("Received message on topic: %s\n", message.Topic())
	fmt.Printf("Message: %s\n", message.Payload())
}
Ejemplo n.º 13
0
func brokerClientsHandler(client *MQTT.Client, msg MQTT.Message) {
	brokerClients <- true
	fmt.Printf("BrokerClientsHandler      ")
	fmt.Printf("[%s]  ", msg.Topic())
	fmt.Printf("%s\n", msg.Payload())
}