Giter Club home page Giter Club logo

Comments (6)

codetheweb avatar codetheweb commented on June 16, 2024

Have you tried using TuyAPI from a script directly? Your device may use the v3.3 protocol.

from cli.

xoniq avatar xoniq commented on June 16, 2024

I will try it soon. It’s from a brand recently sold in The Netherlands named “LSC”, which just uses the Tuya protocol and also works with the TuyaSmart app (apps also almost looks identical).

Tomorrow I will try this out and see how the API works.

from cli.

xoniq avatar xoniq commented on June 16, 2024

Whatever I try, also to update the source of Tuya-Cli to debug stuff, I cannot get wrap around it. This is my latest response, after I reconnected to light to TuyaSmart app, and extracted the new LocalKey:

  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.1.XXX... +4ms
  TuyAPI Socket connected. +47ms
  TuyAPI GET Payload: +1ms
  TuyAPI { gwId: 'XXXXXXXXXXXXXXXXXXXXXX',
  TuyAPI   devId: 'XXXXXXXXXXXXXXXXXXXXXX' } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000002c00000001d3b9b5e2ddf04c73b46561a2c693302215c1a06219267c189a96e0dd4e12a2b75972a4090000aa55 +148ms
  TuyAPI Parsed: +4ms
  TuyAPI { payload: 'parse data error',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1 } +0ms
  TuyAPI GET Payload: +2ms
  TuyAPI { gwId: 'XXXXXXXXXXXXXXXXXXXXXX',
  TuyAPI   devId: 'XXXXXXXXXXXXXXXXXXXXXX' } +0ms
  TuyAPI Received data: 000055aa000000020000000a0000002c00000001d3b9b5e2ddf04c73b46561a2c693302215c1a06219267c189a96e0dd4e12a2b766b9da9c0000aa55 +171ms
  TuyAPI Parsed: +1ms
  TuyAPI { payload: 'parse data error',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 2 } +0ms
  TuyAPI Disconnect +0ms
parse data error
  TuyAPI Socket closed: 192.168.1.XXX +1ms

I fails with decrypting, but I don't know if it's because the data is bad, or the encryption or so. So i'm quite stuck on this.

from cli.

codetheweb avatar codetheweb commented on June 16, 2024

Ok, did you try using TuyAPI directly from a script and specifying the v3.3 version as I suggested?

from cli.

xoniq avatar xoniq commented on June 16, 2024

@codetheweb
(2 edits and more testing at the end of this feedback)

This is the response of a custom script against TuyAPI:

  TuyAPI Finding missing IP undefined or ID XXXXXXXXXXXXXXXXX +0ms
  TuyAPI Received UDP message. +77ms
  TuyAPI UDP data: +3ms
  TuyAPI { payload:
  TuyAPI    { ip: '192.168.1.XXX',
  TuyAPI      gwId: 'XXXXXXXXXXXXXXXXX',
  TuyAPI      active: 2,
  TuyAPI      ablilty: 0,
  TuyAPI      encrypt: true,
  TuyAPI      productKey: 'XXXXXXXXXXXXXXXXX',
  TuyAPI      version: '3.3' },
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 19,
  TuyAPI   sequenceN: 0 } +0ms
  TuyAPI Connecting to 192.168.1.XXX... +4ms
  TuyAPI Socket connected. +73ms
Connected to device!
  TuyAPI GET Payload: +1ms
  TuyAPI { gwId: 'XXXXXXXXXXXXXXXXX',
  TuyAPI   devId: 'XXXXXXXXXXXXXXXXX' } +0ms
  TuyAPI Received data: 000055aa000000010000000a0000002c00000001e43a9f9fb09424634104f66db124feee8e3bf2aeee7be51f9590b6b57c1fe62e4f61356e0000aa55 +86ms
  TuyAPI Parsed: +0ms
  TuyAPI { payload: 'json obj data unvalid',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1 } +0ms
Data from device: json obj data unvalid

Using this almost unchanged sample code:

const TuyAPI = require('tuyapi');

const device = new TuyAPI({
  id: 'XXXXXXXXXXXXXXXXX',
  key: 'XXXXXXXXXXXXXXXXX'});

let stateHasChanged = false;

// Find device on network
device.find().then(() => {
  // Connect to device
  device.connect();
});

// Add event listeners
device.on('connected', () => {
  console.log('Connected to device!');
});

device.on('disconnected', () => {
  console.log('Disconnected from device.');
});

device.on('error', error => {
  console.log('Error!', error);
});

device.on('data', data => {
  console.log('Data from device:', data);

  console.log(`Boolean status of default property: ${data.dps['1']}.`);

  // Set default property to opposite
  if (!stateHasChanged) {
    device.set({set: !(data.dps['1'])});

    // Otherwise we'll be stuck in an endless
    // loop of toggling the state.
    stateHasChanged = true;
  }
});

// Disconnect after 10 seconds
setTimeout(() => { device.disconnect(); }, 10000);

Command:

DEBUG=* node index.js

// EDIT 1/2 //

In file node_modules/tuyapi/lib/message-parser.js I followed it to getPayload (#173) and did some console.log's, in the first try statement I can log the buffer, then after that it does:

data = this.cipher.decrypt(data);

Followed by another console.log which gives me the following (partially cutted unrelated feedback):

  TuyAPI Received data: 000055aa000000010000000a0000002c00000001e43a9f9fb09424634104f66db124feee8e3bf2aeee7be51f9590b6b57c1fe62e4f61356e0000aa55 +13ms
========== BEFORE this.cipher.decrypt ==========
<Buffer e4 3a 9f 9f b0 94 24 63 41 04 f6 6d b1 24 fe ee 8e 3b f2 ae ee 7b e5 1f 95 90 b6 b5 7c 1f e6 2e>
========== AFTER this.cipher.decrypt ==========
json obj data unvalid

No idea how to move further, also no idea if the json obj data unvalid text is really the decrypted text, or an error from cipher (given the spelling error in unvalid).

// EDIT 2/2 //

Tested more in cipher, the weird error is already generated here:

node_modules/tuyapi/lib/cipher.js (#75)

      const decipher = crypto.createDecipheriv('aes-128-ecb', this.key, '');
      result = decipher.update(data, format, 'utf8');
      result += decipher.final('utf8');

It's the literal content of result after this part.
Tested a bit more on the cipher stuff:

      const decipher = crypto.createDecipheriv('aes-128-ecb', this.key, '');
      result = decipher.update(data, format, 'utf8');
      console.log( 'RESULT 1' );
      console.log( result );
      result += decipher.final('utf8');
      console.log( 'RESULT 2' );
      console.log( result );

Results in:

RESULT 1
json obj data un
RESULT 2
json obj data unvalid

At this point it goes to crypto, and now i'm clueless what I could test more. Looks like the decryption works perfectly, since it fails when I change alghoritm, so the error is the decoded message. Do I guess the JSON payload is not correct or something, for my device:

const payload = {
      gwId: this.device.gwID,
      devId: this.device.id
    };

from cli.

codetheweb avatar codetheweb commented on June 16, 2024

Closing this as issue was opened on TuyAPI.

from cli.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.