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

bad json payload #22

Open
costascontis opened this issue Feb 11, 2022 · 4 comments
Open

bad json payload #22

costascontis opened this issue Feb 11, 2022 · 4 comments

Comments

@costascontis
Copy link

i have found a weird bug(?) …using the original blakadder’s nspanel.be .When i change the thermostat’s setting for example from 19 to 20 i get at mqtt

{"NSPanel":{"ATCMode":0,"ATCExpect0":20}}

and everything is working ok parsing and transforming reading the number setpoint in openhab
but when i set to 21 and only then i get

{"NSPanel":{"ATCMode":0,"ATCExpect0":21}B}}

This is only happening with “21” value…

@mbbrenner
Copy link

mbbrenner commented Feb 14, 2022

I found the same... I found a little solution. Please comment....

def every_100ms()
   if self.ser.available() > 0
   var msg = self.ser.read()   # read bytes from serial as bytes
   import string
     if size(msg) > 0
       print("NSP: Received Raw =", msg)
       if msg[0..1] == self.header
         mode = "NSPanel"
         var lst = self.split_55(msg)
         for i:0..size(lst)-1
           msg = lst[i]
             if self.atc['mirror'] == true
               if msg[2] == 0x84 self.ser.write(msg)   # resend messages with type 0x84 for thermostat page
               end
             end
           var j = size(msg) - 1
           while msg[j] != 0x7D
             msg = msg[0..-1]
             j -= 1
           end        
           msg = msg[5..j]
             if size(msg) > 2
               if msg == bytes('7B226572726F72223A307D') # don't publish {"error":0}
               else 
               var jm = string.format("{\"NSPanel\":%s}",msg.asstring())
--->                if ((string.find(jm,"}B}")) != -1)
--->                    var substring =  string.split(jm, "}B}", 1) 
--->                    jm = substring[0]+substring[1]+"}"
--->                end
               
               tasmota.publish_result(jm, "RESULT")
               end
             end
         end
       elif msg == bytes('000000FFFFFF88FFFFFF')
         log("NSP: Screen Initialized")   # print the message as string
         self.screeninit()
       else
         var jm = string.format("{\"NSPanel\":{\"Nextion\":\"%s\"}}",str(msg[0..-4]))
         tasmota.publish_result(jm, "RESULT")        end       
     end
   end
 end


@yvesdm3000
Copy link

I have a similar issue with a bad json payload, but on scene buttons. I have 3 scene buttons, but when I press on the last button, the last '}' is doubled.
Press on the second button gives me:
{"id":"2"}
Press on the third button gives me:
{"id":"3"}}

@yvesdm3000
Copy link

See #27

@mulder82
Copy link

mulder82 commented Dec 16, 2024

I had a similar issue and wrote my own version of the split_55 function, which checks the payload length in the data sent by the panel:

55 AA [type] [!!payload length!!] [00] [JSON payload] [crc] [crc].

In this version, no additional payload validation tests are needed and any value received from the display is processed correctly.

def split_55(b)
    var ret = []
    var maxpos = size(b)-1
    var pos = 0

    while pos <= maxpos - 7 # minimum message size 7 bytes
      if b[pos] == 0x55 && b[pos+1] == 0xAA #if valid message header
        pos+=3 #go to payload length
        var plength=b.get(pos,1) #get payload length
        pos+=2 #go to payload begining
        var payload = b[(pos..pos+plength-1)] #copy payload content
        ret.push(payload) #add to result
        pos+=plength+3 #go to next message => plength + 2 for crc +1 for x00 messages separator 
      else 
        return ret #if no valid message return
      end
    end

    return ret
  end

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

4 participants