Пример #1
0
func publishMqtt() {
	opts := mqtt.NewClientOptions()
	opts.SetBroker(*broker)
	opts.SetClientId("pinpad-main")
	opts.SetCleanSession(true)
	opts.SetTraceLevel(mqtt.Off)

	opts.SetOnConnectionLost(func(client *mqtt.MqttClient, err error) {
		fmt.Printf("lost mqtt connection, trying to reconnect: %s\n", err)
		client.Start()
	})

	client := mqtt.NewClient(opts)
	_, err := client.Start()

	if err != nil {
		fmt.Printf("could not connect to mqtt broker: %s\n", err)
		return
	}

	var msg string
	if newStatus.Open {
		msg = "\"open\""
	} else {
		msg = "\"closed\""
	}

	mqttMsg := mqtt.NewMessage([]byte(msg))
	mqttMsg.SetQoS(mqtt.QOS_ONE)
	mqttMsg.SetRetainedFlag(true)
	r := client.PublishMessage(*topic, mqttMsg)
	<-r
	lastPublishedStatus = newStatus
	client.ForceDisconnect()
}
Пример #2
0
func (s *LoadBusSuite) TestDecode(c *C) {
	req := &connectRequest{}
	msg := mqtt.NewMessage([]byte(s.sampleJson))
	err := s.bus.decodeRequest(msg, req)
	c.Assert(err, IsNil)
	c.Assert(req, DeepEquals, s.connectReq)
}
Пример #3
0
func (m *MQTTClient) Publish(topic string, payload []byte, qos int, retain bool) error {
	mqttmsg := MQTT.NewMessage(payload)
	// FIXME: validate qos number
	mqttmsg.SetQoS(MQTT.QoS(qos))
	mqttmsg.SetRetainedFlag(retain)

	receipt := m.Client.PublishMessage(topic, mqttmsg)
	<-receipt

	return nil
}
Пример #4
0
func Publish(client *MQTT.MqttClient, topic string, payload []byte) {
	message := MQTT.NewMessage(payload)
	message.SetQoS(0)
	receipt := client.PublishMessage(topic, message)

	fmt.Println("publish:")
	fmt.Printf("TOPIC: %s\n", topic)
	fmt.Printf("MSG: %s\n", message.Payload())

	<-receipt
}
Пример #5
0
// post WeatherData structure on JSON to MQTT topic
func PostCurrentData(dataChannel <-chan *weatherStation.WeatherData, mqttClient *MQTT.MqttClient, opts *MQTT.ClientOptions) {
	//log.Println("start posting data")
	for {
		currentWeather := <-dataChannel
		//fmt.Printf("#########################getting data =%+v\n", currentWeather)
		jsonWeather, err := json.Marshal(currentWeather)
		//fmt.Printf("#########################sended data =%s\n", jsonWeather)
		check(err)
		msg := MQTT.NewMessage(jsonWeather)
		reconnectMqttBroker(mqttClient, opts)
		if mqttConnected {
			mqttClient.PublishMessage("/mackristof/weather-mtp/davis1", msg)
		}
	}
}
Пример #6
0
func (b *Bus) encodeRequest(data interface{}) *mqtt.Message {
	buf := bytes.NewBuffer(nil)
	json.NewEncoder(buf).Encode(data)
	return mqtt.NewMessage(buf.Bytes())
}