ksheumaker / homeassistant-apsystems_ecur

Home Assistant custom component for local querying of APSystems ECU-R Solar System
Apache License 2.0
186 stars 43 forks source link

[ECU-B] Script crash #192

Closed max3163 closed 1 year ago

max3163 commented 1 year ago

Hi,

Since this morning, the query script crash with this error :

Traceback (most recent call last):
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/ECU_B.py", line 48, in <module>
    data = loop.run_until_complete(ecu.async_query_ecu())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 124, in async_query_ecu
    self.ecu_raw_data = await self.async_send_read_from_socket(cmd)
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 101, in async_send_read_from_socket
    return await asyncio.wait_for(self.async_read_from_socket(), timeout=self.timeout)
  File "/usr/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
    return fut.result()
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 93, in async_read_from_socket
    raise APSystemsInvalidData(error)
APSystemsECU.APSystemsInvalidData: End suffix (b'END\n') missing from ECU response end_data=b'1\x00\x00\n' data=b'APS110095000121630005826801\x00\x00\n'

When I change the end pattern to match with the new format, the script just crash a few lines after ...

Traceback (most recent call last):
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/ECU_B.py", line 48, in <module>
    data = loop.run_until_complete(ecu.async_query_ecu())
  File "/usr/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 126, in async_query_ecu
    self.process_ecu_data()
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 234, in process_ecu_data
    self.check_ecu_checksum(data, "ECU Query")
  File "/k8s_storage/domoticz-config-pvc-43977a86-d8db-4206-94e9-fce6d4179d97/scripts/python/ECU/APSystemsECU.py", line 211, in check_ecu_checksum
    raise APSystemsInvalidData(error)
APSystemsECU.APSystemsInvalidData: Checksum on 'ECU Query' failed checksum=95 datalen=29 data=b'41505331313030393530303031323136333030303538323638303100000a'

I guess an update change the format of the protocol and the query is not correct now ...

How can I help you to fix this ??

HAEdwin commented 1 year ago

Do other Domoticz users also suffer from this? If not, powercycle the ECU. I have had this issue in the past, data is being received on the 8899 port but does not belong there. Data does not contain a checksum and is therefore misinterpreted. sockets are intertwined, check your code.

max3163 commented 1 year ago

I've already powercycle my ECU-B without sucess.... One person on the domoticz forum have had this issue few days ago and it come back OK without doing anything. For me, so far it's still KO...

HAEdwin commented 1 year ago

Is all the data received malformed? If not, you could temporary ignore these datastrings and wait until some days are passed. If you are not overstressing the ECU with queries I wouldn't know how to solve this.

max3163 commented 1 year ago

Sadly all queries failed... and it's still not working ... There are some users with the same problems on the domoticz forum but no solution so far...

max3163 commented 1 year ago

Hi,

I've found the bug ! In fact the problem is when youo read the response from the ECU you use the readline method which read data until the ' /n ' caractere. But as response contain ascii caractere and hex code , some time if un number contain the code '0A'

00:54:11.267453 IP (tos 0x0, ttl 128, id 2513, offset 0, flags [none], proto TCP (6), length 136) 
   ecub.lan.8899 > server.59286: Flags [P.], cksum 0xb79a (correct), seq 1:97, ack 18, win 2903, length 96 
       0x0000:  001b 2121 1287 4855 196a 1298 0800 4500  ..!!..HU.j....E. 
       0x0010:  0088 09d1 0000 8006 acdc c0a8 0171 c0a8  .............q.. 
       0x0020:  0101 22c3 e796 0015 16da 1990 586e 5018  ..".........XnP. 
       0x0030:  0b57 b79a 0000 4150 5331 3130 3039 3530  .W....APS1100950 
       0x0040:  3030 3132 3136 3330 3030 3538 3236 3830  0012163000583480 
       0x0050:  3100 000a f900 0000 0000 0003 71d0 d0d0  1...........q... 
       0x0060:  d0d0 d0d0 0002 0000 3130 3031 3345 4355  ........10013ECU 
       0x0070:  5f42 5f31 2e32 2e32 3642 3030 3945 7463  _B_1.2.26B009Etc 
       0x0080:  2f47 4d54 2d38 8097 1b06 30b2 0000 0000  /GMT-8....0..... 
       0x0090:  0000 454e 440a                           ..END.

Problem is here => 0x0050: 3100 000a f900 0000 0000 0003 71d0 d0d0 1...........q...

The quick fix for me is to read twice the buffer but it's a dirty fix :) so I will change it to read until the end prefix 'END\n'

But all is OK now :)

If you want I can send a PR with the correct way?

HAEdwin commented 1 year ago

All contributions are welcome

HAEdwin commented 1 year ago

All contributions are welcome but I think the spinoff focusses on Domoticz and this git is all about HA. To avoid further confusion I'll close this issue since it is not part of an existing issue for this integration or its code.