@machhub-dev/sdk-ts 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/classes/tag.d.ts +1 -0
- package/dist/cjs/classes/tag.js +6 -0
- package/dist/cjs/services/mqtt.service.d.ts +1 -0
- package/dist/cjs/services/mqtt.service.js +24 -0
- package/dist/classes/tag.d.ts +1 -0
- package/dist/classes/tag.js +6 -0
- package/dist/services/mqtt.service.d.ts +1 -0
- package/dist/services/mqtt.service.js +24 -0
- package/package.json +1 -1
- package/src/classes/tag.ts +7 -0
- package/src/services/mqtt.service.ts +27 -0
package/dist/cjs/classes/tag.js
CHANGED
|
@@ -21,5 +21,11 @@ class Tag {
|
|
|
21
21
|
}
|
|
22
22
|
this.mqttService.addTopicHandler(topic, callback);
|
|
23
23
|
}
|
|
24
|
+
async unsubscribe(topic) {
|
|
25
|
+
if (!this.mqttService) {
|
|
26
|
+
throw new Error("MQTT service not connected");
|
|
27
|
+
}
|
|
28
|
+
this.mqttService.removeTopicHandler(topic);
|
|
29
|
+
}
|
|
24
30
|
}
|
|
25
31
|
exports.Tag = Tag;
|
|
@@ -8,6 +8,7 @@ export declare class MQTTService {
|
|
|
8
8
|
static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
|
|
9
9
|
static resetInstance(): void;
|
|
10
10
|
addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void;
|
|
11
|
+
removeTopicHandler(topic: string): void;
|
|
11
12
|
clearTopics(): void;
|
|
12
13
|
publish(topic: string, message: unknown): boolean;
|
|
13
14
|
private attachMessageListener;
|
|
@@ -39,6 +39,12 @@ class MQTTService {
|
|
|
39
39
|
// addTopicHandler Adds a topic and handler to the subscribed list
|
|
40
40
|
addTopicHandler(topic, handler) {
|
|
41
41
|
try {
|
|
42
|
+
// Check if already subscribed to this topic
|
|
43
|
+
const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
|
|
44
|
+
if (existingSubscription) {
|
|
45
|
+
// If already subscribed, unsubscribe first to get retained message again
|
|
46
|
+
this.removeTopicHandler(topic);
|
|
47
|
+
}
|
|
42
48
|
this.subscribedTopics.push({ topic, handler });
|
|
43
49
|
if (topic == "")
|
|
44
50
|
return;
|
|
@@ -53,6 +59,24 @@ class MQTTService {
|
|
|
53
59
|
console.error(`Failed to subscribe to topic ${topic}:`, e);
|
|
54
60
|
}
|
|
55
61
|
}
|
|
62
|
+
// removeTopicHandler removes a specific topic handler and unsubscribes from the topic
|
|
63
|
+
removeTopicHandler(topic) {
|
|
64
|
+
try {
|
|
65
|
+
// Remove all handlers for this topic
|
|
66
|
+
this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
|
|
67
|
+
// Unsubscribe from the MQTT topic
|
|
68
|
+
if (topic && topic !== "") {
|
|
69
|
+
this.client.unsubscribe(topic, (err) => {
|
|
70
|
+
if (err) {
|
|
71
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, err);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
catch (e) {
|
|
77
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, e);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
56
80
|
// clearTopics clears all the topics subscribed to
|
|
57
81
|
clearTopics() {
|
|
58
82
|
this.subscribedTopics = [];
|
package/dist/classes/tag.d.ts
CHANGED
package/dist/classes/tag.js
CHANGED
|
@@ -8,6 +8,7 @@ export declare class MQTTService {
|
|
|
8
8
|
static getInstance(url?: string, developerKey?: string): Promise<MQTTService>;
|
|
9
9
|
static resetInstance(): void;
|
|
10
10
|
addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void;
|
|
11
|
+
removeTopicHandler(topic: string): void;
|
|
11
12
|
clearTopics(): void;
|
|
12
13
|
publish(topic: string, message: unknown): boolean;
|
|
13
14
|
private attachMessageListener;
|
|
@@ -33,6 +33,12 @@ export class MQTTService {
|
|
|
33
33
|
// addTopicHandler Adds a topic and handler to the subscribed list
|
|
34
34
|
addTopicHandler(topic, handler) {
|
|
35
35
|
try {
|
|
36
|
+
// Check if already subscribed to this topic
|
|
37
|
+
const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
|
|
38
|
+
if (existingSubscription) {
|
|
39
|
+
// If already subscribed, unsubscribe first to get retained message again
|
|
40
|
+
this.removeTopicHandler(topic);
|
|
41
|
+
}
|
|
36
42
|
this.subscribedTopics.push({ topic, handler });
|
|
37
43
|
if (topic == "")
|
|
38
44
|
return;
|
|
@@ -47,6 +53,24 @@ export class MQTTService {
|
|
|
47
53
|
console.error(`Failed to subscribe to topic ${topic}:`, e);
|
|
48
54
|
}
|
|
49
55
|
}
|
|
56
|
+
// removeTopicHandler removes a specific topic handler and unsubscribes from the topic
|
|
57
|
+
removeTopicHandler(topic) {
|
|
58
|
+
try {
|
|
59
|
+
// Remove all handlers for this topic
|
|
60
|
+
this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
|
|
61
|
+
// Unsubscribe from the MQTT topic
|
|
62
|
+
if (topic && topic !== "") {
|
|
63
|
+
this.client.unsubscribe(topic, (err) => {
|
|
64
|
+
if (err) {
|
|
65
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, err);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, e);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
50
74
|
// clearTopics clears all the topics subscribed to
|
|
51
75
|
clearTopics() {
|
|
52
76
|
this.subscribedTopics = [];
|
package/package.json
CHANGED
package/src/classes/tag.ts
CHANGED
|
@@ -27,4 +27,11 @@ export class Tag {
|
|
|
27
27
|
}
|
|
28
28
|
this.mqttService.addTopicHandler(topic, callback);
|
|
29
29
|
}
|
|
30
|
+
|
|
31
|
+
async unsubscribe(topic: string): Promise<void> {
|
|
32
|
+
if (!this.mqttService) {
|
|
33
|
+
throw new Error("MQTT service not connected");
|
|
34
|
+
}
|
|
35
|
+
this.mqttService.removeTopicHandler(topic);
|
|
36
|
+
}
|
|
30
37
|
}
|
|
@@ -48,6 +48,14 @@ export class MQTTService {
|
|
|
48
48
|
// addTopicHandler Adds a topic and handler to the subscribed list
|
|
49
49
|
public addTopicHandler(topic: string, handler: (message: unknown, topic?: string) => void): void {
|
|
50
50
|
try {
|
|
51
|
+
// Check if already subscribed to this topic
|
|
52
|
+
const existingSubscription = this.subscribedTopics.find(sub => sub.topic === topic);
|
|
53
|
+
|
|
54
|
+
if (existingSubscription) {
|
|
55
|
+
// If already subscribed, unsubscribe first to get retained message again
|
|
56
|
+
this.removeTopicHandler(topic);
|
|
57
|
+
}
|
|
58
|
+
|
|
51
59
|
this.subscribedTopics.push({ topic, handler });
|
|
52
60
|
if (topic == "") return;
|
|
53
61
|
// console.log("New Subscription Handler:", topic);
|
|
@@ -61,6 +69,25 @@ export class MQTTService {
|
|
|
61
69
|
}
|
|
62
70
|
}
|
|
63
71
|
|
|
72
|
+
// removeTopicHandler removes a specific topic handler and unsubscribes from the topic
|
|
73
|
+
public removeTopicHandler(topic: string): void {
|
|
74
|
+
try {
|
|
75
|
+
// Remove all handlers for this topic
|
|
76
|
+
this.subscribedTopics = this.subscribedTopics.filter(sub => sub.topic !== topic);
|
|
77
|
+
|
|
78
|
+
// Unsubscribe from the MQTT topic
|
|
79
|
+
if (topic && topic !== "") {
|
|
80
|
+
this.client.unsubscribe(topic, (err?: unknown) => {
|
|
81
|
+
if (err) {
|
|
82
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, err);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
} catch (e) {
|
|
87
|
+
console.error(`Failed to unsubscribe from topic ${topic}:`, e);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
64
91
|
// clearTopics clears all the topics subscribed to
|
|
65
92
|
public clearTopics(): void {
|
|
66
93
|
this.subscribedTopics = [];
|