GithubHelp home page GithubHelp logo

Transferring data over socket failed: The client is not connected to a broker. The requested operation is impossible at this point. about laravel-client HOT 3 CLOSED

php-mqtt avatar php-mqtt commented on July 17, 2024
Transferring data over socket failed: The client is not connected to a broker. The requested operation is impossible at this point.

from laravel-client.

Comments (3)

Namoshek avatar Namoshek commented on July 17, 2024

I think it is because you are calling $mqtt->disconnect() in both methods. The ConnectionManager (which is what is used behind the MQTT facade) does not know that you are disconnecting and will not call connect() for you the second time.

My advice would be to remove both calls to $mqtt->disconnect() and add one single call to MQTT::disconnect() before returning the response.

from laravel-client.

pheelaphong avatar pheelaphong commented on July 17, 2024

Thank you very much. I removed all $mqtt->disconnect();, but somtimes the returned message is empty:

[
  {
    device_id: 10000,
    status: "Online",
    power: "OFF"
  },
  {
    device_id: 10001,
    status: "Online",
    power: "OFF"
  },
  {
    device_id: 10002,
    status: "Online",
    power: ""
  },
  {
    device_id: 10003,
    status: "",
    power: ""
  },
  {
    device_id: 10004,
    status: "",
    power: ""
  }
]

My code now:

public function subDeviceAll(Request $request)
{
    $device = Device::all();
    $dataDevice = [];
    foreach ($device as $key => $value) {
        //$data['data_device'] = $value;
        $data['device_id'] = $value['device_id'];
        $data['status'] = $this->checkDeviceOnline($value['device_id']);
        $data['power'] = $this->checkDevicePower($value['device_id']);
        $dataDevice[] = $data;
    }
    return response()->json($dataDevice, 200);
}

public function checkDeviceOnline($device_id)
{
    $device = Device::where('device_id', $device_id)->first();
    $mqtt = MQTT::connection();
    $res = '';
    $topic = $device['topic'];

    $mqtt->publish('cmnd/' . $topic . '/POWER', '', 2);
    $mqtt->subscribe('tele/' . $topic . '/LWT', function (string $topic, string $message) use ($mqtt, &$res) {
        $res = $message;
        $mqtt->interrupt();
    }, 1);

    $mqtt->loop(true);
    return $res;
}

public function checkDevicePower($device_id)
{
    $device = Device::where('device_id', $device_id)->first();
    $mqtt = MQTT::connection();
    $res = '';
    $topic = $device['topic'];

    $mqtt->publish('cmnd/' . $topic . '/POWER', '', 2);
    $mqtt->subscribe('stat/' . $topic . '/POWER', function (string $topic, string $message) use ($mqtt, &$res) {
        $res = $message;
        $mqtt->interrupt();
    }, 1);
    $mqtt->loop(true);
    return $res;
}

from laravel-client.

Namoshek avatar Namoshek commented on July 17, 2024

Well, that has nothing to do with the library but your devices. I cannot help you with that part.

from laravel-client.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.