-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmqtt-publish.html
151 lines (128 loc) · 3.95 KB
/
mqtt-publish.html
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="mqtt-publish-regestration-behaviour.html">
<!--
The `mqtt-publish` element publishes MQTT messages to an MQTT topic. Place the element within a `<mqtt-connection>`
to create the binding between the connection and the element. The following example
<mqtt-connection >
<mqtt-publish
auto
retained
qos="1"
topic="foo/bar"
payload="Hello world!"></mqtt-publish>
</mqtt-connection>
publishes a retained message to the topic "foo/bar".
@demo demo/index.html
-->
<dom-module id="mqtt-publish">
<template>
</template>
</dom-module>
<script>
'use strict';
(function (scope) {
var MqttElements = scope.MqttElements = scope.MqttElements || {};
MqttElements.MqttPublish = Polymer({
is: 'mqtt-publish',
/**
* Fired when a publication is published.
*
* @event mqtt-publication-published
* @param {{
* topic: string,
* qos: number,
* payload: string | Object
* retained: boolean
* }}
*/
properties: {
/**
* If auto is set to true `<mqtt-publish auto>` then an MQTT message is send each time
* the payload is changed.
*/
auto: {
type: Boolean,
value: false,
},
/**
* The quality of service the MQTT publication is send to the MQTT broker
*/
qos: {
type: Number,
value: 0,
},
/**
* The topic the publication is made to
*/
topic: {
type: String,
},
/**
* The payload that is send to the topic
*/
payload: {
type: Object,
value: {},
},
/**
* Set retained to true if the message should be saved at the MQTT broker an should "stick" on it.
*/
retained: {
type: Boolean,
value: false,
},
/**
* True if the publication was successful
*/
published: {
type: Boolean,
value: false,
readOnly: true,
},
},
behaviors: [MqttElements.MqttPublishRegestrationBehaviour],
observers: [
'_propertiesChanged(topic, payload, qos, retained, _parentConnection)'
],
/**
* A property observer to send messages if `auto` is set to true and values change
* @param topic
* @param payload
* @param qos
* @param retained
* @private
*/
_propertiesChanged: function (topic, payload, qos, retained, _parentConnection) {
if(this.auto && topic && payload && this._parentConnection) {
this.publish();
}
},
/**
* Manually publishes the <mqtt-publish> element to the <mqtt-connection> it is bound to. Use the attributes on the
* Element to set the topic, payload, QoS an retained flag for the MQTT message.
*/
publish: function () {
if(this.topic && this.topic.length > 0 && this.payload && this.payload.length > 0 && this._parentConnection) {
this._setPublished(false);
this._parentConnection.publish(this.topic, this.payload, this.qos, this.retained, this._handelPublicationPublished.bind(this));
}
},
/**
* Callback handel to notify the element that the payload has been published
* @private
*/
_handelPublicationPublished: function () {
this._setPublished(true);
this.fire(
"mqtt-publication-published",
{
qos: this.qos,
topic: this.topic,
payload: this.payload,
retained: this.retained
}
);
},
})
})(window);
</script>