Please explain how I can use case statements inside conditions!

Hello Gophers! I am trying to write a device driver for a parking sensor, which detects the payload received and tells what the payload contains. But I’m missing some concepts here, can anyone help me with what is missing. Thank you.

> package main
> 
> import (
> 				"fmt"
> 
> 
> )
>  func main(){
> 	 var payload = []byte(
> 		 								"0x01 0x06 0x01 0x3B 0x0A 0x2D 0xFF 0x00",
> 
> 
> 									)
> 	 if payload[0] == 1{
> 		 fmt.Println(" Parking status message ")
> 	 }else if payload[0] == 2 {
> 		 fmt.Println(" Heart beat message ")
> 	 }else if payload[0] == 3{
> 		 fmt.Println(" Start uo message ")
> 	 }else {
> 		 fmt.Println(" Wrong payload")
> 	 }
> 	 if payload[1] == 6{
> 		 fmt.Println(" Payload length is:  ")
> 		 fmt.Printf(" %v", payload[1])
> 		 } else if payload[1] == 0 {
> 			 fmt.Println(" Payload length is:  ")
> 			 fmt.Printf("%v", payload[1])
> 			 } else if payload[1] == 1 {
> 				 fmt.Println(" Payload length is: ")
> 				 fmt.Printf(" %v", payload[1])
> 		  } else {
> 				fmt.Println(" wrong payload")
> 			}
> 		if payload[2] == 0||1{
> 			switch  payload{
> 			case 0:
> 					fmt.Println("Free parking space")
> 			case 1:
> 					fmt.Println("Parking space ocuupied")

41 > }else if payload[2] == 1{
> fmt.Println(" Parking status message “)
> }else {
> fmt.Println(” Wrong payload “)
> }
> }
> if payload[3] == 1{
> fmt.Println(“Payload length of Heartbeat Length”)
> fmt.Printf(”%v", payload[3])
> }else {
> fmt.Println(“Wrong Payload”)
> }
> if payload[14:16] == []byte{
54 > fmt.Printf(" The firmware version is: %x")
> }
56 > else {
> fmt.Println(“wrong payload”)
> }
> }

These are the errors i’m getting
41 > // syntax error: unexpected else at end of statement (build)
>
> golint
>
> expected ‘;’, found ‘else’ (and 3 more errors) (golint)
>
> vet
>
> syntax error: unexpected else at end of statement (vet)//
>
54 > // syntax error: unexpected newline, expecting comma or } (build)
>
> vet
>
> syntax error: unexpected newline, expecting comma or } (vet)//
>
56 > // syntax error: unexpected else, expecting expression (build)
>
> vet
>
> syntax error: unexpected else, expecting expression (vet)//

1 Like

@hemanth_naidu do the errors you shared have line numbers associated with them? It would make it much easier to know what statements to look at.

1 Like

@jrswab I have updated with the line numbers.

1 Like

It’s just a missing } before else.

But could be anything else… Your code is barely readable due to the missing code annotations and the leading >

1 Like

I took some a few minutes to run it through the Go Playground. It runs without errors. Keep in mind that I did comment out the following because I’m not sure what you are looking for there.

if payload[14:16] == []byte {
    fmt.Println("The firmware version is:")
} else {
    fmt.Println("wrong payload")
}

Here is the link to the Go Playground with the formatted code: https://play.golang.org/p/cawRiZLL89f

As @NobbZ said, the code posted here is really hard to read and really lowers the chances of someone being able to help. I just happened to have a small break at work and wanted to play around with your code. The more clear you make your posts the better.

1 Like

@jrswab. I will remove the indentation next time I post. I have few questions:

  1. Everytime I access a byte from hex payload will it consider two characters like ‘0A’ since it comprises of 1 byte.
  2. For example when I say ‘payload[2] == 6’. Here 6 is decimal and payload[2] is hex. Should I convert the payload into decimal before this or not required.
  3. Array indexing starts from MSB or LSB

For the part which you have commented. There are three different payloads sent by the sensor. In the third kind of payload there is no condition there and I just want to print the value of payload or the message it contains (firmware version like 1.0.1).
And the code is printing only wrong payload statements even when the payload is correct. The payload comes as one single string for example “2837f889a888c98e89”. I have to decode it.
Here is the link I have put the other two messages sent by the sensor.
https://play.golang.org/p/To6b-_ZA3hJ

1 Like

I think you will need to convert it somehow but since I have yet to work with hexadecimals like this I am not sure the best way to do so.

Yes, because you are checking if payload[2] is an int not the bytes that you are expecting. (As far as I can tell…)

Sorry that I could not be a better help.

1 Like

What is the type of your payload? This will help to answer the follow up questions…

And can you perhaps a sample output of fmt.Printf("%#v", payload)?

1 Like

The payload is hexadecimal. Everything that a sensor communicates is in hexadecimal I have to decode it.

1 Like

@jrswab no problem thanks for the help. How do I mention the payload is hex?

1 Like

I can see the payload as you have it in code, but that doesn’t make much sense…

var payload = []byte("0x01 0x06 0x01 0x3B 0x0A 0x2D 0xFF 0x00")

I assume, that your real payload as you’ll receive it from the device will be much more like:

var payload = []byte{0x01, 0x06, 0x01, 0x3B, 0x0A, 0x2D, 0xFF, 0x00}

You’ll need to check which of those is true.

1 Like

The real payload sent by the device will look like this in the lora network server.
“110a000f00551001”

This is how it is given in the sensor datasheet.

1 Like

I want to know how it is received in your application. Nothing else counts. Unless you know how your lora inspected packet will look like for go…

I also have no clue what lora is, so I can not make any assumptions…

1 Like

by lora I meant LoRaWAN its [What is LoRaWAN® Specification - LoRa Alliance®].

this is how it will look like in my application and it is received in hexadecimal.

1 Like

Does it look like that when you do a fmt.Printf("%#v", payload) as I asked earlier?

1 Like

@NobbZ. This is how it looks like when I do fmt.Printf(“%v”, payload) .

[48 120 48 49 32 48 120 48 54 32 48 120 48 49 32 48 120 51 66 32 48 120 48 65 32 48 120 50 68 32 48 120 70 70 32 48 120 48 48]Wrong payload
wrong payload
Wrong Payload

1 Like

The first 2 bytes are ASCII for 0x, are you sure this is received from the wire and not just your example that you hardcoded?

Also every fifth byte is 32, which is a space… I doubt this is a payload as any embedded device or sensor would send it over the wire, as it would increase power consumption on the device for no reason…

1 Like

@NobbZ. Thanks for the explanation. ( 2 bytes are ASCII for 0x, fifth byte is 32, which is a space)
You’re right as of now i’m giving the hardcoded example payload. Once I have a working code for hardcoded payload I then want to implement the next part.
However the payload sent by the sensor when connected to the LoRaWAN network is still this “110a000f00551001”. without spaces and ‘0x01’.

1 Like

And the device is a Bosch parking sensor with a built in battery and LoRa chip once its triggered it starts sending the payloads. It’s not a plug and play device.

1 Like

@hemanth_naidu @NobbZ is right: the payload that you are using for test purposes is wrong.
If you are getting payload in string but in hex encoded, so you need to first decode it. I did something based on your script but not sure it will help you.

    var payloadStr = "110a000f00551001"
 	payload, err := hex.DecodeString(payloadStr)
 	if err != nil {
 		panic(err)
 	}
 	fmt.Printf("%#v", payload)
 	// use hex format when you compare them
 	if payload[0] == 0x11 {
 		fmt.Println(" Parking status message ")
 	} else if payload[0] == 0x02 {
 		fmt.Println(" Heart beat message ")
 	} else if payload[0] == 0x03 {
 		fmt.Println(" Start uo message ")
 	} else {
 		fmt.Println("Wrong payload")
 	}

tip: if you are working on bytes, it is good to know the bytes package:

    // bytes package has many helpful functions that can ease your job.
	if bytes.Equal(payload[0:2], []byte{0x11, 0xa}) {
		fmt.Printf(" The firmware version is: %x")
	} else {
		fmt.Println("wrong payload")
	}

https://play.golang.org/p/puOQe30nUJ-

1 Like