1.3.0 • Published 3 years ago

mqttsngw-core v1.3.0

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

MQTT-SN Gateway: Core

This module is part of mqttsngw. It is responsible for handling parsed MQTT-SN packets from the event bus and maintaining state machines for connected sensors. If necessary it requests a connection to the broker on the event bus.

Factory

const Core = require('mqttsngw-core');
mqttsngw.attach(Core(opts));

Creates a new Core factory and attaches it to an existing instance of mqttsngw. opts has the following fields:

  • log: Optional. An object containing logging callbacks for all log levels (error, warn, info, debug). Every callback is called with a human-readable message as the first argument followed by an object containing more information regarding the event: { error: (msg, info) => { ... }, ...}.
  • enforceUniqueClientIds: If set to true, existing connections will be disconnected if a new connection with the same MQTT Client ID is established.

State Machines

Core Main

  • init: Preparing the state machine
  • listening: Listens for incoming connection requests from the sensor network. If a request is received, a new instance of Core Client is created and started.

Core Client

Reflects the current sensor state.

  • init: Preparing the state machine.
  • willTopic: Waits for a will topic sent by the sensor.
  • willMessage: Waits for a will message sent by the sensor.
  • connectBroker: Tries to connect to the broker.
  • active: The sensor is connected. Depending on the received sensor messages, it will start the state machines Core Subscribe, Core PublishToBroker or Core PublishToClient.
  • sleep: The sensor may enter sleep state by stating a non-zero duration in the DISCONNECT packet. The gateway will collect ingress publishes from the broker and sends them to the sensores once it's active again.

Core Subscribe

Handles topic subscription requested sent by sensors.

  • init: Checks subscription request.
  • brokerSubscribe: Subscribes the topic at the broker.

Core PublishToBroker

Sends a message to the broker

  • init: Checks the publish request and translates the topicId to the topic name.
  • publishToBroker: Sends the message to the broker.

Core PublishToClient

Sends a message to the sensor

  • init: Lookup the topicId to the given topic name or register one.
  • registerTopic: Registers a topic at the sensor.
  • publishToClient: Sends the message to the sensor.

Events

Several events are consumed and emitted by the Core module on the event bus.

Consumed

EventState MachineDescription
snUnicastIngress,*,connectCore MainIncoming connection requests packet
brokerConnect,*,resCore ClientResponse to a broker connection request
snUnicastIngress,*,disconnectCore ClientSensor sent disconnect
snUnicastIngress,*,registerCore ClientSensor registers a new topic name
snUnicastIngress,*,subscribeCore ClientSensor subscribes to a new topic
snUnicastIngress,*,unsubscribeCore ClientSensor unsubscribes from a topic
snUnicastIngress,*,publishCore ClientSensor publishes a message
snUnicastIngress,*,pingreqCore ClientSensor sent ping request
brokerDisconnect,*,notifyCore ClientConnection to the broker has been disconnected
brokerPublishToClient,*,reqCore ClientA message shall be sent from the broker to the sensor
brokerSubscribe,*,resCore SubscribeResponse to a subscription request
brokerUnsubscribe,*,resCore UnsubscribeResponse to a desubscription request
brokerPublishFromClient,*,resCore PublishToBrokerResponse to a publish to broker request
snUnicastIngress,*,regackCore PublishToClientReaction to a register request from the sensor
snUnicastIngress,*,pubackCore PublishToClientReaction to a publish request from the sensor

Emitted

EventState MachineDescription
brokerConnect,*,reqCore ClientRequest a connection to the broker
brokerDisconnect,*,callCore ClientDisconnect from the broker
snUnicastOutgress,*,connackCore ClientSend a CONNACK to the sensor
snUnicastOutgress,*,disconnectCore ClientDisconnect from sensor
snUnicastOutgress,*,regackCore ClientSend a REGACK to the sensor
snUnicastOutgress,*,pingrespCore ClientRespond to a ping request
brokerSubscribe,*,reqCore SubscribeSubscribes a topic at the broker
snUnicastOutgress,*,subackCore SubscribeRespond to a subscription request
brokerUnsubscribe,*,reqCore UnsubscribeUnsubscribes a topic at the broker
snUnicastOutgress,*,unsubackCore UnsubscribeRespond to a desubscription request
brokerPublishFromClient,*,reqCore PublishToBrokerPublish a message to the broker
snUnicastOutgress,*,pubackCore PublishToBrokerSend PUBACK to the sensor
snUnicastOutgress,*,registerCore PublishToClientRegister a topic at the sensor
snUnicastOutgress,*,publishCore PublishToClientPublish a message to the sensor
brokerPublishToClient,*,resCore PublishToClientRespond to publish request from the broker
1.3.0

3 years ago

1.2.0

4 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

6 years ago