ctrysbita / miio.dart

Dart implementation for MIIO LAN protocol.
GNU General Public License v3.0
9 stars 3 forks source link

Add support for Linux #3

Open guyluz11 opened 3 years ago

guyluz11 commented 3 years ago

Discover does not list my light bulb and I don't know if this is because it does not support or I am doing something wrong while using this package.

This is my light bulb GPX4021GL, a quick search will say that this is XIAOMI - Mi Smart LED Bulb Essential so it is mi product.

To run the program I have cloned the project and run it with the program arguments discover --ip=192.168.31.154 while 192.168.31.154 is my computer IP. The program exit fast without any errors.

I have tested it with 192.168.31.255 but I am getting


⛔ Command failed with exception:
│ ⛔ SocketException: Send failed (OS Error: Permission denied, errno = 13), address = 0.0.0.0, port = 0
guyluz11 commented 3 years ago

Why discover method does not find the devices IP at home?. I have managed to find the device IP and was surprised to find out that the discover command only verifies that the IP that you gave it is indeed a Xiaomi device (and return the Token).

I have expected that it will not take any arguments and will return a list of devices at home. I can try to create pull request for that if you like.

guyluz11 commented 3 years ago

Found 2 resources for discovering MiIO devices in the local network that I would like to share here

python-miio discovery documentation

Device discovery

Devices already connected to the same network where the command-line tool is run are automatically detected when miiocli discover is invoked. This command will execute two types of discovery: discovery by handshake and discovery by mDNS. mDNS discovery returns information that can be used to detect the device type which does not work with all devices. The handshake method works on all MiIO devices and may expose the token needed to communicate with the device, but does not provide device type information.

And the search function in MiIO javascript project

ctrysbita commented 2 years ago

@guyluz11 The discover function sends a handshake packet to the broadcast IP of your local network. If the device supports MIIO protocol, it will send back. This can be used to discover all MIIO device in your local network.

ctrysbita commented 2 years ago

You have to use the broadcast IP address of your local network for --ip argument and the command line interface also has a flag --table to list all supported devices.

guyluz11 commented 2 years ago

Ho ok didn't know about the --table, so in that case I might have a different error. When using my broadcast IP .255 I get an Exception (even as root)

# dart bin/main.dart discover --ip 192.168.31.255 --table
   Woah! You appear to be trying to run flutter as root.
   We strongly recommend running the flutter tool without superuser privileges.
  /
📎
subnet IP 192.168.31
Address         ID              Stamp           Token
┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ 02:43:47.591 (+0:00:00.002702)
├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
│ ⛔ Command failed with exception:
│ ⛔ SocketException: Send failed (OS Error: Permission denied, errno = 13), address = 0.0.0.0, port = 0
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
ctrysbita commented 2 years ago

I think this is caused by your runtime. The address and port cannot be 0.

guyluz11 commented 2 years ago

Here is my code, can you test it to check if it is working?. I am wary that this is happening because I don't use it from the CLI.

import 'dart:io';

import 'package:miio/miio.dart';

void main(List<String> arguments) async {
  final InternetAddress internetAddress = InternetAddress('Enter.your.ip.here');

  await for (var miDevice in MiIo.instance.discover(internetAddress)) {
    print('miDevice devices $miDevice');
    // MiIo.instance.send(address, packet);
  }
}

Running from the CLI does not work, I get miio: command not found even after using pub global activate miio.

Also what branch of dart do you use?

$ dart --version
Dart SDK version: 2.14.0 (stable) (Mon Sep 6 13:47:59 2021 +0200) on "linux_x64"
guyluz11 commented 2 years ago

Wait

I think this is caused by your runtime. The address and port cannot be 0.

https://github.com/ctrysbita/miio-dart/blob/76ff7699015ea0055c1e235561c7762c9941ef22/lib/src/protocol.dart#L53

But they are zero in the code.

guyluz11 commented 2 years ago

Tested it both on Linux Kubuntu , Linux Ubuntu (with different computer), even after formating reinstalling Kubuntu again it did not work.

Tested on Windows and it is working!.

So it seems that it does not work only on Linux. Any idea what can cause this?.

ctrysbita commented 2 years ago

Actually this package is developed under windows. I will try it on different os later XD

guyluz11 commented 2 years ago

Hi @ctrysbita Did you find time to test it on Linux?