ggeorgovassilis / linuxscripts

Script collection for linux
Other
391 stars 113 forks source link

hs100 Script doesn't check for 'discover' or 'list' #10

Closed Johnny-Larue closed 7 years ago

Johnny-Larue commented 7 years ago

Hi! This is a wonderful script, but I've run into two things. Maybe I'm doing something wrong, but I don't think so. When I run:

sudo hs100.sh discover

the script asks for the IP address. In perusing your code, I found that it doesn't seem to look for anything other than the IP and port. Here's my dumb little hack that made it work for me:

check_arguments() {
   if [ $BASH_ARGV[1] != "discover" ] && [ $BASH_ARGV[1] != "list" ]; then
       check_arg() {
          name="$1"
          value="$2"
          if [ -z "$value" ]; then
             echo "missing argument $name"
             usage
          fi
       }
       check_arg "ip" $plugs
       check_arg "port" $port
       check_arg "cmd_dis" $cmd
    fi
}

Also, when I do discovery, it picks up an IP camera on the network and throws it in the list, which I think is because the IP camera has port 9999 open. Just FYI.

One last thing: Would you consider implementing the {"system":{"reboot":{"delay":1}}} function? I'm using the code to reset a funky POE switch that the computer happens to be connected to, thus it's a bit tough to turn the thing back on through code!

Thank you so much!

ggeorgovassilis commented 7 years ago

@Johnny-Larue I'm pretty sure there is no "discover" command in the script. Do you have any documentation on how to communicate the delayed reboot command to the hs100?

Johnny-Larue commented 7 years ago

Hrmm. Unless I'm mistaking something, line 199 in hs100.sh looks to be the subroutine to discover smart plugs:

# plug commands
cmd_discover(){

...etc.

Here is a reverse-engineer of the device. All the relevant commands are under "system commands". I'd do it myself, but I'm new to this, and having difficulty trying to figure out how they've encoded the JSON to be sent as a payload. This is the JSON for 1 second wait until rebooting:

{"system":{"reboot":{"delay":1}}}

Thanks!

ggeorgovassilis commented 7 years ago

You're probably referring to a fork somebody made (can you point me to it?). The original script is here [1] and doesn't perform any discovery.

[1] https://github.com/ggeorgovassilis/linuxscripts/blob/master/tp-link-hs100-smartplug/hs100.sh

Johnny-Larue commented 7 years ago

Oh, gosh, you're right. I'm a bonehead, ha! Here it is: Control the tp-link HS100 smart plug from the command line Just out of curiosity, how are those JSON commands being encoded? I don't see any functions within your scripts, so I assume it must be pre-encoded and just dropped right in.