plasticrake / tplink-smarthome-api

TP-Link Smarthome WiFi API
MIT License
1.02k stars 141 forks source link

Fixed a small bug in multi-plug example, and separated out device ip … #120

Closed scubanarc closed 3 years ago

scubanarc commented 3 years ago

The multi-plug example worked, but the line that got the childPlug had a bug in that it didn't specify the childId properly. The example functioned, but if the code was used to write a real-world program then the bug surfaced. For example, I used this code to turn a multi-switch on/off. As written all 3 ports of the switch would toggle. This pull request fixes that in the example.

I also refactored out the IP address of the device just to make the example more clear.

codecov[bot] commented 3 years ago

Codecov Report

Merging #120 (6e5f04d) into master (7e05eb2) will decrease coverage by 0.15%. The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #120      +/-   ##
==========================================
- Coverage   75.62%   75.47%   -0.16%     
==========================================
  Files          25       25              
  Lines        1329     1329              
  Branches      301      301              
==========================================
- Hits         1005     1003       -2     
- Misses        250      251       +1     
- Partials       74       75       +1     
Impacted Files Coverage Δ
src/network/tcp-socket.ts 76.47% <0.00%> (-2.36%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 7e05eb2...6e5f04d. Read the comment docs.

plasticrake commented 3 years ago

The line with the childId that was changed in this PR is functionally identical to the original. There shouldn't be any difference (and the lint rule that fails for that line is pointing that out)

scubanarc commented 3 years ago

The line with the childId that was changed in this PR is functionally identical to the original. There shouldn't be any difference (and the lint rule that fails for that line is pointing that out)

Perhaps. In my node environment, if I run the sample code as written it does not work. It instead finds the parent, not the child. It appears to work, but if you inspect the object returned I think you'll see that it's actually the parent. However, if I specify the parameter explicitly it works correctly. This was not obvious in the sample given. I didn't figure it out until it was in my code and I kept getting the parent object back.

Is it possible that the linter is using TS instead of JS?

plasticrake commented 3 years ago

{ host: '10.0.1.136', childId } and { host: '10.0.1.136', childId: childId } are identical since node v4. Read about the shorthand notation here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer

This is the output I get when running which looks correct to me:

node examples/multi-plug.js                                                                                                                                                                                                                                                 1:42PM
r
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0200',
  state: 1,
  alias: 'Plug 1',
  on_time: 74,
  next_action: { type: -1 }
}
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0201',
  state: 1,
  alias: 'Plug A',
  on_time: 74,
  next_action: { type: -1 }
}
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0202',
  state: 1,
  alias: 'Plug 3',
  on_time: 74,
  next_action: { type: -1 }
}
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0203',
  state: 0,
  alias: 'Testing 25',
  on_time: 0,
  next_action: { type: -1 }
}
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0204',
  state: 1,
  alias: 'Testing 8',
  on_time: 74,
  next_action: { type: -1 }
}
{
  id: '8006579C4910F870CEB5F25F972EBD561AA33F0205',
  state: 1,
  alias: 'Testing 87',
  on_time: 74,
  next_action: { type: -1 }
}
2021-02-28T21:42:51.454Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0205 true
2021-02-28T21:42:51.454Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0205 true
2021-02-28T21:42:51.454Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0205 {
  voltage_mv: 120079,
  current_ma: 4,
  power_mw: 51,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0.051,
  total: 0,
  voltage: 120.079
}
2021-02-28T21:42:51.598Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0204 true
2021-02-28T21:42:51.598Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0204 true
2021-02-28T21:42:51.599Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0204 {
  voltage_mv: 120079,
  current_ma: 4,
  power_mw: 0,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0,
  total: 0,
  voltage: 120.079
}
2021-02-28T21:42:52.034Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0203 false
2021-02-28T21:42:52.034Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0203 false
2021-02-28T21:42:52.034Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0203 {
  voltage_mv: 120112,
  current_ma: 3,
  power_mw: 20,
  total_wh: 0,
  err_code: 0,
  current: 0.003,
  power: 0.02,
  total: 0,
  voltage: 120.112
}
2021-02-28T21:42:52.179Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0202 true
2021-02-28T21:42:52.179Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0202 true
2021-02-28T21:42:52.179Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0202 {
  voltage_mv: 120062,
  current_ma: 4,
  power_mw: 5,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0.005,
  total: 0,
  voltage: 120.062
}
2021-02-28T21:42:52.615Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0201 true
2021-02-28T21:42:52.615Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0201 true
2021-02-28T21:42:52.615Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0201 {
  voltage_mv: 120112,
  current_ma: 4,
  power_mw: 36,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0.036,
  total: 0,
  voltage: 120.112
}
2021-02-28T21:42:52.767Z in-use-update HS300(US) 10.0.1.136:9999 undefined true
2021-02-28T21:42:52.767Z power-update HS300(US) 10.0.1.136:9999 undefined true
2021-02-28T21:42:52.767Z emeter-realtime-update HS300(US) 10.0.1.136:9999 undefined {
  voltage_mv: 120195,
  current_ma: 4,
  power_mw: 33,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0.033,
  total: 0,
  voltage: 120.195
}
2021-02-28T21:42:52.906Z in-use-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0200 true
2021-02-28T21:42:52.906Z power-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0200 true
2021-02-28T21:42:52.906Z emeter-realtime-update HS300(US) 10.0.1.136:9999 8006579C4910F870CEB5F25F972EBD561AA33F0200 {
  voltage_mv: 120145,
  current_ma: 4,
  power_mw: 34,
  total_wh: 0,
  err_code: 0,
  current: 0.004,
  power: 0.034,
  total: 0,
  voltage: 120.145
}
scubanarc commented 3 years ago

I'm not really sure what's going on here. I've made a couple of screenshots to show you the linter in VSCode shows an error on line 29, and the node CLI agrees. If I then change the line to "childId: child.id" the error goes away. I'll paste the code here, as well as the screenshots.

VsCode with error on line 29: https://imgur.com/J4jkz9O VsCode with no error on line 29: https://imgur.com/Ot4cGwN

device.children.forEach((child) => {
        console.log("alias:" + child.alias);
        if (child.alias == alias) {
            console.log("childId found: " + child.id);
            var childId = child.id;
            client.getDevice({ host: ip, childId: child.id }).then((childDevice) => {
                console.log("Found child");
                console.log(childDevice);
            });
        }
});

And the error that Node throws:

client.getDevice({ host: ip, child.id }).then((childDevice) => {
                                  ^ 
SyntaxError: Unexpected token .

It seems like (to me) specifying the parameter by name makes the syntax error go away.

plasticrake commented 3 years ago

Yes in your case you do need to specify it that way. But in this example in the pull request it works as-is

childId: child.id vs childId: childId