Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decoding of values with prefix '\u001b' does not work #13

Open
SDC-Automation opened this issue Apr 23, 2020 · 5 comments
Open

decoding of values with prefix '\u001b' does not work #13

SDC-Automation opened this issue Apr 23, 2020 · 5 comments

Comments

@SDC-Automation
Copy link

SDC-Automation commented Apr 23, 2020

Decoding Infinity or NaN values from Niagara via dgApi does not work over websocket communication. When decoding fails the websocket connection is closed.

When subscribing to values from dgApi the communication fails due to the above error, which
closes the subscribing clients websocket communication.

The serverlog in DGLux shows the following error:

[2020-04-21 05:27:45.021741] [SEVERE] [Link at /downstream/publisher] Failed to decode string data from WebSocket Connection
[2020-04-21 05:27:45.021741] FormatException: Unexpected character (at character 9262)
[2020-04-21 05:27:45.021741] .../25%2E1/1%2E1/3%2E20/value","Value":Infinity}},{"rid":3515340,"method":...

In the above log the value is '\u001bInfinity' which cannot be decoded. I made a change in node.dart that stripped away u001b but that doesn't seem to be the correct solution. Since the prefixed value is handled in for example JsonWriter.class in dslink-v2-api. So it seems like this should be handled correctly by dgApi.

The error seems to occur in websocket_conn.dart:

try {
        m = codec.decodeStringFrame(data);
        if (logger.isLoggable(Level.FINEST)) {
          logger.finest(formatLogMessage("receive: ${m}"));
        }
      } catch (err, stack) {
        logger.severe(
          formatLogMessage("Failed to decode string data from WebSocket Connection"),
          err,
          stack
        );
        close();
        return;
      }

Log from dgApi when receiving values from Niagara:

[2020-04-23 00:25:41.755477] [FINE][DSA] Got Response: {responses: [{method: PollSubscription, name: DG34831413167, values: [{path: slot:/System/JohannesDangarous/out, value: Infinity, type: number, precision: 1, unit: °C, min: -Infinity, max: Infinity, formatted: +inf °C, lastUpdate: 2020-04-23T00:26:45.217+02:00}]}]}
[2020-04-23 00:25:41.755477] [FINEST][DSA] send: {responses: [{rid: 0, updates: [[252, Infinity, 2020-04-23T00:26:45.217+02:00]]}, {rid: 464, updates: [[@formatted, +inf °C]]}], msg: 621}
[2020-04-23 00:25:41.755477] [FINEST][DSA] receive: {ack: 621, msg: 789}

Is there something wrong with how the decoding is done?

I have tested changing in dgApi, removing \u001b from the beginning of the value and then it worked. But of course it should be possible to pass on the escape character too.

JSON Decoding seems to be the culprit. JSon with 'u001b' cannot be decoded. But i don't know if it can be stripped away safely. Since it is used in dslink-java-v2 to indicate Nan and Infinite values.

Where i think the actual error occurs, codec.dart

Map decodeStringFrame(String str) {
   ---------
    var result = _unsafeDecoder.convert(str);
    return result;
  }

Simple test with esapce character that fails:

String testJson = "{\"value\":\"\u001Bvalue\" }";
var res = new JsonDecoder().convert(testJson);
@rinick
Copy link
Member

rinick commented May 4, 2020

hi
NaN and Infinity are not supported by JSON
we did the trick of using "\u001Bxxxx" to escape such special values.
but this conversion is only done in DGLux client and Niagara module
dsa broker and other dslink still don't know what to do about it.

@rinick
Copy link
Member

rinick commented May 4, 2020

however it does send a valid json and can be normally decoded.
It shouldn't crash the json decoder.

@jdsdc
Copy link

jdsdc commented May 5, 2020

Hi, @rinick , thanks for the correction!

@rinick
Copy link
Member

rinick commented May 5, 2020

@jdsdc @SDC-Automation
please check the latest version, I added a conversion for the msgpack encoding.
it's now able to encode the value properly, because msgpack supports NaN and Infinity

@jdsdc
Copy link

jdsdc commented May 5, 2020

@rinick , i have tested 1.0.6 and it seems to work fine now. What i could see from the code you convert
infinity to double min, max and nan to double.nan.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants