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

Porting to Python #1

Closed
keatontaylor opened this issue Nov 9, 2017 · 5 comments
Closed

Porting to Python #1

keatontaylor opened this issue Nov 9, 2017 · 5 comments

Comments

@keatontaylor
Copy link
Contributor

Perhaps you can help me. I've been attempting to port this code to python and regardless what I try it fails with using the devRequest endpoint. Heck. I've even tried using the google chrome postman extension to simply get ensure I am passing the right body and headers, but that doesn't work either.

Do you happen to know what is special about the devRequest endpoint. Maybe a special encoding I am not using for the body request data that I am oblivious to.

Any direction would be extremely helpful. Thanks :)

@dirwin517
Copy link
Contributor

dirwin517 commented Nov 9, 2017

the request options being passed to the https module in node are

{ method: 'POST',
  protocol: 'https:',
  port: '4007',
  host: 'server1.vesync.com',
  path: '/devRequest',
  timeout: 60000,
  agent: false,
  headers: 
   { tk: 'MY_AUTH_TOKEN',
     id: 'MY_USER_ID',
     uniqueId: 'MY_USER_ID',
     cid: 'MY_DEVICE_ID',
     'Content-Type': 'application/json',
     'content-type': 'multipart/form-data; boundary=--------------------------104181880520714403066207',
     'User-Agent': 'request',
     Accept: 'application/json',
     'Accept-Encoding': 'gzip, deflate',
     'Content-Length': 66 } }

were
MY_AUTH_TOKEN is gotten from the login call
MY_USER_ID is also gotten from the login call
MY_DEVICE_ID is gotten from the loadMain call

Hope that helps

@keatontaylor
Copy link
Contributor Author

I think my issue is more to do with the body of the message considering that all other endpoint calls do work such as login and loadMain.

Do you have the same JSON for how the body of the POST request is formed?

@keatontaylor
Copy link
Contributor Author

keatontaylor commented Nov 9, 2017

Actually problem solved, the body needs to be json encoded, something postman and python do not do by default. Thank you for your help!

@dirwin517
Copy link
Contributor

dirwin517 commented Nov 9, 2017

Get Device Status

 { method: 'POST',
  protocol: 'https:',
  port: '4007',
  host: 'server1.vesync.com',
  path: '/devRequest',
  timeout: 60000,
  agent: false,
  headers: 
   { tk: 'TOKEN',
     id: 'USERID',
     uniqueId: 'USERID',
     cid: 'DEVICE_ID',
     'Content-Type': 'application/json',
     'content-type': 'multipart/form-data; boundary=--------------------------658806857654084496706140',
     'User-Agent': 'request',
     Accept: 'application/json',
     'Accept-Encoding': 'gzip, deflate',
     'Content-Length': 66 }, body : {
                cid : deviceId,
                uri : '/getRuntime'
            } }

Turn Device On

 { method: 'POST',
  protocol: 'https:',
  port: '4007',
  host: 'server1.vesync.com',
  path: '/devRequest',
  timeout: 60000,
  agent: false,
  headers: 
   { tk: 'TOKEN',
     id: '1003873',
     uniqueId: '1003873',
     'Content-Type': 'application/json',
     'content-type': 'multipart/form-data; boundary=--------------------------318973605697566956415566',
     'User-Agent': 'request',
     Accept: 'application/json',
     'Accept-Encoding': 'gzip, deflate',
     'Content-Length': 77 }, body : {
                cid : deviceId,
                uri : '/relay',
                action : 'open'
            } }

Turn Device Off


 { method: 'POST',
  protocol: 'https:',
  port: '4007',
  host: 'server1.vesync.com',
  path: '/devRequest',
  timeout: 60000,
  agent: false,
  headers: 
   { tk: 'TOKEN',
     id: 'USERID',
     uniqueId: 'USERID',
     'Content-Type': 'application/json',
     'content-type': 'multipart/form-data; boundary=--------------------------885185493210149583650641',
     'User-Agent': 'request',
     Accept: 'application/json',
     'Accept-Encoding': 'gzip, deflate',
     'Content-Length': 78 }, body : {
                cid : deviceId,
                uri : '/relay',
                action : 'break'
            } }

Note if you want to read the meter data you might want to look at ( math pulled directly from the Android app and ported to JS )
https://github.com/arupex/etekcity-smartplug/blob/master/client.js#L89-L97

@LuisFDuarte
Copy link

LuisFDuarte commented Jan 4, 2018

Hi @dirwin517 thanks for the work on the API. I'm working with Node-Red to query the API and I have the same problem that @keatontaylor had. I'm not sure how to work with form data, and I think my problem is related to the enconding on the body. So far I can query /loadMain and /Login endpoints with no problems but when I try devRequest I don't get any response.

I'm getting these parameters from previous calls to the other endpoints.
tk: 'MY_AUTH_TOKEN',
id: 'MY_USER_ID',
uniqueId: 'MY_USER_ID',

My code looks like this:


msg.url="https://server1.vesync.com:4007/devRequest";
var deviceId= msg.payload.devices[0].id;

msg.headers={ 
    "tk":msg.tk,
    "id": msg.id,
    "uniqueId": msg.id,
    "cid": deviceId,
    "Content-Type": 'application/json',
    'content-type': 'application/x-www-form-urlencoded',
    }
msg.body={
          "cid" : deviceId,
          "uri": '/getRuntime'
    }
return msg;

thanks for all the help

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

3 participants