Guest User

Untitled

a guest
Feb 22nd, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Observable } from 'rxjs/Observable';
  3. import { Observer } from 'rxjs/Observer';
  4. import { map, catchError } from 'rxjs/operators';
  5. import { Paho } from 'ng2-mqtt/mqttws31';
  6. import { Subject } from 'rxjs/Subject';
  7.  
  8. // declare var Paho: any;
  9. @Injectable()
  10. export class MqttService {
  11. private _client: Paho.MQTT.Client;
  12.  
  13. private mqtt_connect = new Subject<any>();
  14.  
  15. private mqtt_pubsub_list = {}
  16. private d = new Date();
  17. private date = this.d.getTime();
  18. constructor() {
  19.  
  20.  
  21. // this.doConnect()
  22. }
  23.  
  24. loginUser(username : any, password:any,callback) : Observable<any> {
  25. this._client = new Paho.MQTT.Client("digital-mrkt.tpfsoftware.com", Number("8884"), username+this.date.toString());
  26. console.log(this._client)
  27.  
  28. this._client.onConnectionLost = (responseObject: Object) => {
  29. console.log(responseObject)
  30. console.log('Connection lost.');
  31. // this.doConnect()
  32. };
  33.  
  34. this._client.onMessageArrived = (message: Paho.MQTT.Message) => {
  35. this.messageToTopic(message.destinationName, message.payloadString)
  36. };
  37.  
  38. this._client.connect({
  39. onSuccess: this.onConnected.bind(this,callback),
  40. onFailure: this.onFailure.bind(this),
  41. userName: username,
  42. password: password,
  43. useSSL: true,
  44. cleanSession :true
  45. });
  46. return this.mqtt_connect.asObservable();
  47.  
  48. }
  49.  
  50. // doConnect() {
  51. // this._client.connect({
  52. // onSuccess: this.onConnected.bind(this),
  53. // userName: "backend",
  54. // password: "backend",
  55. // useSSL: true
  56. // });
  57. // }
  58.  
  59. addSubscription(topic: string) {
  60. console.log(topic)
  61. this.mqtt_pubsub_list[topic] = new Subject<any>()
  62. this._client.subscribe(topic, {})
  63. }
  64. listenTopic(topic:string) : Observable<any> {
  65. this.mqtt_pubsub_list[topic] = new Subject<any>()
  66. this._client.subscribe(topic, {})
  67. return this._client[topic].asObservable();
  68. }
  69. private onConnected(callback){
  70. callback()
  71. this.mqtt_connect.next(true);
  72. console.log('Connected to broker.');
  73. }
  74.  
  75. private onFailure(error): void {
  76. this.mqtt_connect.next(true);
  77. console.log('Error.',error);
  78. }
  79.  
  80. messageToTopic(topic: any, message: string) {
  81. this.mqtt_pubsub_list[topic].next(message);
  82. }
  83.  
  84. publishToTopic(topic: any, message: string) {
  85. this._client.send(new Paho.MQTT.Message(message))
  86. }
  87.  
  88. getTopic(topic: string): Observable<any> {
  89. return this.mqtt_pubsub_list[topic].asObservable();
  90. }
  91.  
  92. getConnection(): Observable<any> {
  93. if (this._client.isConnected()) {
  94. this.mqtt_connect.next(true);
  95. }
  96. return this.mqtt_connect.asObservable();
  97. }
  98. unSubscribeTopic(topic:  string) {
  99. this._client.unsubscribe(topic, {})
  100. this.mqtt_pubsub_list[topic]  = null;
  101. console.log("deleted", this.mqtt_pubsub_list[topic])
  102. }
  103. }
Add Comment
Please, Sign In to add comment