kuzzleio / kuzzle-freeboard-plugin

Freeboard plugin to help integrating data from a Kuzzle Server to your Freeboard IoT Dashboard
Apache License 2.0
0 stars 0 forks source link

Outdated plugin #1

Open 2guti2 opened 4 years ago

2guti2 commented 4 years ago

Tried to use this in a production environment and after some debugging i found out that the code is outdated regarding how to use Kuzzle-SDK in vanilla js.

Here is the new code so you can update it:

(function() {
  freeboard.loadDatasourcePlugin({
    type_name: "kuzzle_subscribe_data",
    display_name: "Kuzzle Datasource",
    description: "Monitor Kuzzle Documents",
    external_scripts: ["node_modules/kuzzle-sdk/dist/kuzzle.js"],
    settings: [
      {
        name: "kuzzle_host",
        display_name: "Kuzzle host",
        type: "text",
        default_value: "localhost",
        description: "URL to you Kuzzle instance"
      },
      {
        name: "kuzzle_port",
        display_name: "Kuzzle port",
        type: "text",
        default_value: 7512,
        description: "Kuzzle port"
      },
      {
        name: "kuzzle_token",
        display_name: "Token",
        type: "text",
        description:
          "API token retreived using: https://docs.kuzzle.io/api-documentation/controller-auth/login/"
      },
      {
        name: "kuzzle_index",
        display_name: "Index",
        type: "text",
        description: "The index that holds the collection"
      },
      {
        name: "kuzzle_collection",
        display_name: "Collection",
        type: "text",
        description: "The collection"
      },
      {
        name: "kuzzle_subscribe_query",
        display_name: "Subscribe query",
        type: "text",
        description:
          "The subscribe query: https://docs.kuzzle.io/sdk-reference/collection/subscribe/"
      }
    ],
    newInstance: function(settings, newInstanceCallback, updateCallback) {
      newInstanceCallback(new myDatasourcePlugin(settings, updateCallback));
    }
  });

  var myDatasourcePlugin = function(settings, updateCallback) {
    var self = this;
    var currentSettings = settings;

    var callback = (notification) => {
      console.log(notification);
      updateCallback(notification.result);
    };

    var kuzzle = new KuzzleSDK.Kuzzle(
      new KuzzleSDK.WebSocket(currentSettings.kuzzle_host), {
        port: currentSettings.kuzzle_port,
        autoReconnect: true
      }
    );
    kuzzle.on('networkError', error => {
      console.error('Network Error: ', error);
    });
    kuzzle.on('connected', () => {
      console.log('Successfully connected to Kuzzle');
    });

    kuzzle.connect().then(() => {
      kuzzle.realtime.subscribe(currentSettings.kuzzle_index, currentSettings.kuzzle_collection, JSON.parse(currentSettings.kuzzle_subscribe_query), callback);
    })
      .then(() => {
        console.log('Successfully subscribed to document notifications!');
      })
      .catch(error => {
        console.error('Ooops! An error occurred: ', error);
        kuzzle.disconnect();
      });

    self.onSettingsChanged = function (newSettings) {
      currentSettings = newSettings;
    };

    self.updateNow = function () {
      console.log("Pub/Sub model does not allow to update on demand.");
    };

    self.onDispose = function () {
      kuzzle.disconnect();
    };
  };
})();
Aschen commented 4 years ago

Hi @2guti2, Sorry for the long response time.

Thanks you for contributing! It has been a long time since we released the plugin and we never take the time to upgrade it :sweat_smile:

Please can you submit a proper PR with the code so when can merge it into the plugin?

2guti2 commented 4 years ago

@Aschen Yeah, of course! Give me a couple of days and I'll enhance it a little bit to make it as useful for any general scenario as possible and then submit that PR. Thanks!

Aschen commented 4 years ago

Don't hesitate to come and get in touch on our Discord if you have any question :smiley: http://join.discord.kuzzle.io/