dickydoouk / tp-link-tapo-connect

Unofficial Node.js library for connecting to TP-Link Tapo devices. Currently limited to the P100 & P105 smart plugs and L510E smart bulbs.
115 stars 41 forks source link

Temporarily store device token #17

Closed edcarvalholine closed 2 years ago

edcarvalholine commented 2 years ago

I want to keep a device's token until it expires to avoid always requests to tapo API

I thought about this code

let deviceToken
const _getToken = async () => {
  // Expires logic
  // if (deviceToken....timer)
      // return deviceToken

  deviceToken = await tapoHandler.loginDeviceByIp(
    user.email,
    user.password,
    "192.168.1.15"
  );
};

const lightColorChange = async (color) => {
  const token = _getToken();

  await tapoHandler.setColour(token, color);
};

but as soon as I try to get it gives me the error below (node:10324) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received undefined at new NodeError (internal/errors.js:322:7) at prepareSecretKey (internal/crypto/keys.js:322:11) at Cipheriv.createCipherWithIV (internal/crypto/cipher.js:119:9) at new Cipheriv (internal/crypto/cipher.js:227:22) at Object.createCipheriv (crypto.js:124:10) at Object.encrypt (C:\Users\nain\Desktop\tapo-automatted\node_modules\tp-link-tapo-connect\dist\tplinkCipher.js:70:35) at C:\Users\nain\Desktop\tapo-automatted\node_modules\tp-link-tapo-connect\dist\api.js:292:51 at step (C:\Users\nain\Desktop\tapo-automatted\node_modules\tp-link-tapo-connect\dist\api.js:44:23)

But if I put the token to be called every time I invoke a device function, it doesn't give an error

const lightColorChange = async (color) => {
   const token = await tapoHandler.loginDeviceByIp(
     user.email,
     user.password,
     "192.168.1.15"
   );

   await tapoHandler.setColour(token, color);
};
dickydoouk commented 2 years ago

I think you just have a problem in your _getToken function. It should be:

const _getToken = async () => {
  return await tapoHandler.loginDeviceByIp(
    user.email,
    user.password,
    "192.168.1.15"
  );
};
dickydoouk commented 2 years ago

Closed presumably resolved