1.4.0 • Published 3 years ago

indi-client v1.4.0

Weekly downloads
21
License
ISC
Repository
github
Last release
3 years ago

indi-client

A node client for INDI.

This client will translate the XML INDI protocol into a more JS friendly JSON.

Here's an example that will send all INDI protocol messages received to MQTT topics, and will subscribe to MQTT topics to send commands to INDI devices.

Installation

npm install indi-client

Example

const mqtt = require("mqtt");

const {
  getProperties,
  enableBLOB,
  mapping,
  newSwitchVector,
  newTextVector,
  newNumberVector,
  INDIClient,
} = require("indi-client");

const brokerURL = process.env.MQTT_BROKER_URL || "mqtt://127.0.0.1";
const indiHost = process.env.INDI_HOST || "127.0.0.1";
const indiPort = process.env.INDI_PORT ? parseInt(process.env.INDI_PORT) : 7624;

const indiClient = new INDIClient(indiHost, indiPort);

indiClient.on("connect", () => {
  console.log("indi connection connected");

  indiClient.getProperties();
  indiClient.enableBLOB(null, null, "Also"); // Get blobs as well as everything else.
})

indiClient.on("close", () => {
  console.log("indi connection closed");
})

indiClient.connect();

const mqttClient = mqtt.connect(brokerURL);

Object.keys(mapping).forEach(key => {
  indiClient.on(key, (obj) => {
    let topic = `indi/data/${obj.device}`;

    if (obj.name) topic += `/${obj.name}`;

    topic += `/${key}`;

    mqttClient.publish(topic, JSON.stringify(obj));
  })
});

mqttClient.on("connect", () => {
  mqttClient.subscribe("indi/commands/+", (err) => {
    if (err)
      console.error(err);
  });
});

mqttClient.on("message", (topic, message) => {
  if (topic.endsWith("newTextVector")) {
    const obj = newTextVector.fromJSON(JSON.parse(message));
    indiClient.send(obj);
  } else if (topic.endsWith("newSwitchVector")) {
    const obj = newSwitchVector.fromJSON(JSON.parse(message));
    indiClient.send(obj);
  } else if (topic.endsWith("newNumberVector")) {
    const obj = newNumberVector.fromJSON(JSON.parse(message));
    indiClient.send(obj);
  } else if (topic.endsWith("getProperties")) {
    const obj = getProperties.fromJSON(JSON.parse(message));
    indiClient.send(obj);
  } else if (topic.endsWith("enableBLOB")) {
    const obj = enableBLOB.fromJSON(JSON.parse(message));
    indiClient.send(obj);
  }
});
1.4.0

3 years ago

1.2.0

3 years ago

1.3.0

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.4

3 years ago