thingsboard / thingsboard-gateway

Open-source IoT Gateway - integrates devices connected to legacy and third-party systems with ThingsBoard IoT Platform using Modbus, CAN bus, BACnet, BLE, OPC-UA, MQTT, ODBC and REST protocols
https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
Apache License 2.0
1.72k stars 829 forks source link

[BUG] ‘BrokenPipeError: [Errno 32] Broken pipe’ #639

Closed jayneev closed 2 years ago

jayneev commented 2 years ago

Describe the bug ‘BrokenPipeError: [Errno 32] Broken pipe’ Steps:

  1. 3 BLE end devices connected to TB gateway.
  2. notification data received for 3 min intervals.
  3. devices remains disconnected for 2 min.
  4. Steps#2 and Step#3 repeated again. After 2-3 above iterations one of the devices error’d out during BLE notifications data read with ‘broken pipe’ error, just after stdin.flush

Connector name (If bug in the some connector): BLE Connector

Versions (please complete the following information):

samson0v commented 2 years ago

Hi @jayneev, we are remaking the BLE connector now. We will let you know when it is done.

Thanks for your interest in ThignsBoard IoT Gateway!

ndrs-cwt commented 2 years ago

@samson0v I have this Broken pipe issue too and after this exception happens, gateway can't reconnect to device that has this exception anymore. The only way to connect to that device again is restart gateway process.

Do you have any recommended workaround for this issue while waiting for the new BLE connector?

Best regard

samson0v commented 2 years ago

@ndrs-cwt, we fixed the problem, you can update your Gateway from the master branch and try to test it. But we are afraid that it does not help because of a very big waiting time for notification.

ndrs-cwt commented 2 years ago

@samson0v, Thank you for the new BLE connector, but re-read approach doesn't fix my Broken pipe problem but maybe it works for OP case.

I just found some workaround for my problem by record the name of the Broken pipe device in some list

except BrokenPipeError:
    log.exception('Broken Pipe. Device %s, wait for remake', device)
    self.__remake_list.append(device)
    continue

and delete all of its Peripheral relate objects and let's connector create all related objects again

if time.time() - self.__previous_read_time >= self.__check_interval_seconds:
    self.__get_services_and_chars()
    if len(self.__remake_list) > 0:
        for device in self.__remake_list:
            log.warning("Clear %s entry from __devices_around", device)
            del self.__devices_around[device]["peripheral"]
            del self.__devices_around[device]["services"]
            self.__devices_around[device]["is_new_device"] = True # Trig the connector to remake all object
        self.__remake_list.clear()
    self.__previous_read_time = time.time()

this is from my custom BLE connector that almost exact copy from TB's BLE connector but remove notify support and always disconnect from device. This band-aid just makes my gw can reconnect to the Broken pipe device again but have to wait for next read interval to recreate device object and start reading data again.

samson0v commented 2 years ago

Hi @ndrs-cwt, we did a remake of the BLE Connector yesterday, so you can install the new version of Gateway from the master branch and test the connector. Let us know if the new version of BLE Connector fixes your problem.

jayneev commented 2 years ago

Thank you @samson0v and @zbeacon for sharing the ble connector remake, We are trying to find right way to get 'data notification updates from BLE peripheral devices, with latest checked out code. Below are few observations and queries , if provided feedback, will definitely help us to get notify data on gateway.

OBSERVATION: Below parameters in ble.json now removed from latest one. Are they replaced with new parameter ? Old parameters: <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< "rescanIntervalSeconds": 18000, "checkIntervalSeconds": 45, "scanTimeSeconds": 35, <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< New parameters: (Is this suffice above requirements?) <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< "scanner": { "timeout": 10000, "deviceName": "Device name" <==== Is this 'Device name' needs to be replaced with gateway name at TB side? }, "devices": [ { ...... "pollPeriod": 500000, <==== per devices ..... } <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

QUERY: Is the newly added above json parameter tag 'deviceName' value is the gateway name specified on Thingsboard?

QUERY: Once device connected to gateway, will they get added in TB dashboard as in previous versions?

QUERY: As in QUERY#3 above, are the below JSON parameters still serve purpose of identifying the device connected? "attributes": [ { "key": "name", "method": "read", "characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB", "byteFrom": 0, "byteTo": -1 } ], "attributeUpdates": [ { "attributeOnThingsBoard": "sharedName", "characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB" } ],

QUERY: Does server side RPC if not used, should the below feel be empty? "serverSideRpc": [ { "methodRPC": "rpcMethod1", "withResponse": true, "characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB", "methodProcessing": "read" }, { "methodRPC": "rpcMethod2", "withResponse": true, "characteristicUUID": "00002A00-0000-1000-8000-00805F9B34FB", "methodProcessing": "write" }, { "methodRPC": "rpcMethod3", "withResponse": true, "methodProcessing": "scan" } ]

QUERY: Does current configuration supports ACTIVE scan mode? If yes, then does setting "passiveScanMode": false, will suffice this requirement?

I appreciate you all for your valuable contribution and help.

Warm Regards, @jayneev

samson0v commented 2 years ago

Was discussed and a new issue was opened.