mysa-js-sdk
    Preparing search index...

    Class MysaApiClient

    Main client for interacting with the Mysa API and real-time device communication.

    The MysaApiClient provides a comprehensive interface for authenticating with Mysa services, managing device data, and receiving real-time updates from Mysa thermostats and heating devices. It handles both REST API calls for device management and MQTT connections for live status updates and control commands.

    const client = new MysaApiClient();

    await client.login('user@example.com', 'password');
    const devices = await client.getDevices();

    client.emitter.on('statusChanged', (status) => {
    console.log(`Device ${status.deviceId} temperature: ${status.temperature}°C`);
    });

    for (const device of Object.entries(devices.DevicesObj)) {
    await client.startRealtimeUpdates(device[0]);
    }
    Index

    Constructors

    Properties

    Event emitter for client events.

    MysaApiClientEventTypes for the possible events and their payloads.

    Accessors

    • get isAuthenticated(): boolean

      Returns whether the client currently has an active session.

      Returns boolean

      True if the client has an active session, false otherwise.

    Methods

    • Retrieves the list of devices associated with the user.

      This method fetches all Mysa devices linked to the authenticated user's account, including device information such as models, locations, and configuration details.

      Returns Promise<Devices>

      A promise that resolves to the list of devices.

      const devices = await client.getDevices();
      for (const [deviceId, device] of Object.entries(devices.DevicesObj)) {
      console.log(`Device: ${device.DisplayName} (${device.Model})`);
      }

      MysaApiError When the API request fails.

      UnauthenticatedError When the user is not authenticated.

    • Retrieves the serial number for a specific device.

      This method uses AWS IoT's DescribeThing API to fetch the serial number attribute for the specified device. This requires additional AWS IoT permissions and may not be available for all devices.

      Parameters

      • deviceId: string

        The ID of the device to get the serial number for.

      Returns Promise<undefined | string>

      A promise that resolves to the serial number, or undefined if not found.

      const serialNumber = await client.getDeviceSerialNumber('device123');
      if (serialNumber) {
      console.log(`Device serial: ${serialNumber}`);
      } else {
      console.log('Serial number not available');
      }

      UnauthenticatedError When the user is not authenticated.

    • Logs in the user with the given email address and password.

      This method authenticates the user with Mysa's Cognito user pool and establishes a session that can be used for subsequent API calls. Upon successful login, a 'sessionChanged' event is emitted.

      Parameters

      • emailAddress: string

        The email address of the user.

      • password: string

        The password of the user.

      Returns Promise<void>

      try {
      await client.login('user@example.com', 'password123');
      console.log('Login successful!');
      } catch (error) {
      console.error('Login failed:', error.message);
      }

      Error When authentication fails due to invalid credentials or network issues.

    • Sets the state of a specific device by sending commands via MQTT.

      This method allows you to change the temperature set point and/or operating mode of a Mysa device. The command is sent through the MQTT connection for real-time device control.

      Parameters

      • deviceId: string

        The ID of the device to control.

      • OptionalsetPoint: number

        The target temperature set point (optional).

      • Optionalmode: MysaDeviceMode

        The operating mode to set ('off', 'heat', or undefined to leave unchanged).

      Returns Promise<void>

      // Set temperature to 22°C
      await client.setDeviceState('device123', 22);

      // Turn device off
      await client.setDeviceState('device123', undefined, 'off');

      // Set temperature and mode
      await client.setDeviceState('device123', 20, 'heat');

      UnauthenticatedError When the user is not authenticated.

      Error When MQTT connection or command sending fails.

    • Starts receiving real-time updates for the specified device.

      This method establishes an MQTT subscription to receive live status updates from the device, including temperature, humidity, set point changes, and other state information. The client will automatically send keep-alive messages to maintain the connection.

      Parameters

      • deviceId: string

        The ID of the device to start receiving updates for.

      Returns Promise<void>

      // Start receiving updates and listen for events
      await client.startRealtimeUpdates('device123');

      client.emitter.on('statusChanged', (status) => {
      console.log(`Temperature: ${status.temperature}°C`);
      });

      Error When MQTT connection or subscription fails.

    • Stops receiving real-time updates for the specified device.

      This method unsubscribes from the MQTT topic for the specified device and clears any associated timers to stop the keep-alive messages.

      Parameters

      • deviceId: string

        The ID of the device to stop receiving real-time updates for.

      Returns Promise<void>

      Error When MQTT unsubscription fails.