Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse data error #23

Closed
xoniq opened this issue Aug 25, 2019 · 6 comments
Closed

parse data error #23

xoniq opened this issue Aug 25, 2019 · 6 comments

Comments

@xoniq
Copy link

xoniq commented Aug 25, 2019

When running:

DEBUG=* tuya-cli get --ip 192.168.1.XXX --id ******************* --key ******************* -a

I get this response:

  TuyAPI IP and ID are already both resolved. +0ms
  TuyAPI Connecting to 192.168.1.XXX... +29ms
  TuyAPI Socket connected. +32ms
  TuyAPI GET Payload: +8ms
  TuyAPI { gwId: ''*******************',',
  TuyAPI   devId: ''*******************',' } +1ms
  TuyAPI Received data: ******************************************************** +174ms
  TuyAPI Parsed: +18ms
  TuyAPI { payload: 'parse data error',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 1 } +1ms
  TuyAPI GET Payload: +8ms
  TuyAPI { gwId: '*******************',
  TuyAPI   devId: '*******************', } +1ms
  TuyAPI Received data: ******************************************************** +134ms
  TuyAPI Parsed: +4ms
  TuyAPI { payload: 'parse data error',
  TuyAPI   leftover: false,
  TuyAPI   commandByte: 10,
  TuyAPI   sequenceN: 2 } +1ms
  TuyAPI Disconnect +4ms
parse data error
  TuyAPI Socket closed: 192.168.1.XXX +8ms

It keeps throwing parse data error.

@codetheweb
Copy link
Member

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

@xoniq
Copy link
Author

xoniq commented Aug 25, 2019

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.

@xoniq
Copy link
Author

xoniq commented Aug 27, 2019

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.

@codetheweb
Copy link
Member

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

@xoniq
Copy link
Author

xoniq commented Aug 28, 2019

@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
    };

@codetheweb
Copy link
Member

Closing this as issue was opened on TuyAPI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants