Skip to content

Commit

Permalink
New unit YYMMDD and code simplyfication
Browse files Browse the repository at this point in the history
  • Loading branch information
zbchristian committed Jan 16, 2023
1 parent a98834f commit 7748ad7
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
15 changes: 7 additions & 8 deletions tcp_udp_http_bridge/devices.config
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,21 @@
}
},
// ------------ END TK103_H02 ------------------------------------------

// ------------ TK105 ---------------------------------------------
{
"Device": "TK105 - binary data",
"Login": {
// "msg":"", "resp":""
// response requests every 30sec the location for 10 times
"msg": "^\\*(\\w{2}),([0-9]{10}),V1,%REGEXP_GPRMC%,.*$", "resp":"*\\S1\\,\\DEVID\\,D1,\\TIME\\,30,20#",
// "msg": "^\\*(\\w{2}),([0-9]{10}),V1,%REGEXP_GPRMC%,.*$", "resp":"*\\S1\\,\\DEVID\\,S17,\\TIME\\,30#",
// response requests every 30sec the location for 20 times
"Order": [ %S1%, %DEVID%, %GPRMC% ]
},
"Heartbeat": {
"msg": "^\\*\\w{2},([0-9]{10}),V19,%REGEXP_GPRMC%,.*$", "resp":"",
"msg1": "^\\*\\w{2},([0-9]{10}),V4,D1,.*$", "resp1":""
// "msg1": "^\\*\\w{2},([0-9]{10}),V4,S17,.*$", "resp1":""
},
"Gps_data": {
"Mode": "bcd",
// "msg": "^\\*(\\w{2}),([0-9]{10}),V1,%REGEXP_GPRMC%,.*$", "resp":"",
"msg": "^24([0-9]{10})([0-9]{6})([0-9]{6})([0-9]{9})[[:xdigit:]]([0-9]{9})((([[:xdigit:]])))([0-9]{3})([0-9]{3}).*$", "resp":"",
"Order": [ %DEVID%, %TIME%, %DATE%, %LAT%, %LON%, %ACTIVE%,%NS%,%EW%, %SPEED%, %ANGLE%],
"Units": [ %NONE%, %NONE%, %NONE%, %DEGMIN%, %DEGMIN%, %NONE%, %NONE%,%NONE%,%KNOTS%, %DEGREE%],
Expand Down Expand Up @@ -111,7 +108,7 @@
"msg1": "^78781F12([[:xdigit:]]{6})([[:xdigit:]]{6})[[:xdigit:]]{2}([[:xdigit:]]{8})([[:xdigit:]]{8})([[:xdigit:]]{2})(((([[:xdigit:]]{2})))[[:xdigit:]]{2}).*0D0A$", "resp1":"787805120001B32D0D0A",
"Order": [ %DATE%, %TIME%, %LAT%, %LON%, %SPEED%, %ANGLE%, %NS%, %EW%, %ACTIVE% ],
// Angle: matches 2 bytes; NS,EW, ACTIVE: the same single first byte
"Units": [ %INV%, %NONE%, %DEGREE%, %DEGREE%, %KMPERH%, %DEGREE%, %NONE%, %NONE%, %NONE% ],
"Units": [ %YYMMDD%, %NONE%, %DEGREE%, %DEGREE%, %KMPERH%, %DEGREE%, %NONE%, %NONE%, %NONE% ],
"Scale": [ 1, 1, 5.5555555E-7, 5.5555555E-7, 1, 1, 1, 1, 1 ],
// 1/30000/60
"Bits": { "pat": [ "", "", "", "", "", "3ff", "4", "8" , "10"],
Expand Down Expand Up @@ -193,7 +190,7 @@
// SPEED : current speed
// ANGLE : angle of movement
// TIME : time (HHMMSS)
// DATE : date (DDMMYY) - for inverse date (YYMMDD) set units to "INV"
// DATE : date (DDMMYY) - for inverse date set units to "YYMMDD"
// ALT : altitude
// ACC : accuracy/precision of location
// BITS : bits need to be analysed. Requires additional Bits block (see above)
Expand All @@ -204,4 +201,6 @@
// KMPERH : speed in km/h
// MPERS : speed in m/s
// KNOTS : speed in knots
// INV : invert order (e.g. DATE YYMMDD)
// YYMMDD : inverted order of the date
//
// S1, S2, S3 : items to store a string to be placed into the response
35 changes: 17 additions & 18 deletions tcp_udp_http_bridge/gps_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const (
HEAD int = iota
CHECK int = iota
MAGN int = iota
INV int = iota
YYMMDD int = iota
)

var keywords = map[string]int{
Expand Down Expand Up @@ -82,7 +82,7 @@ var keywords = map[string]int{
"HEAD": HEAD,
"CHECK": CHECK,
"MAGN": MAGN,
"INV": INV,
"YYMMDD": YYMMDD,
}

type bitsMatch struct { // match bit pattern and define result
Expand Down Expand Up @@ -180,7 +180,7 @@ func readDeviceConfig(fileconf string) (err error) {
jsonBlob = strings.Replace(jsonBlob, "%"+key+"%", strconv.Itoa(idx), -1)
}
// find remaining keywords
re := regexp.MustCompile("\\%\\w+\\%")
re := regexp.MustCompile(`\%\w+\%`)
byMatch := re.Find([]byte(jsonBlob))
if byMatch != nil {
fmt.Printf("Unknown key %s found \n", string(byMatch))
Expand Down Expand Up @@ -372,11 +372,11 @@ func filter_gps_device(msg string, status *statInfo) (response string, query str
}
} else {
query = ""
err = errors.New("No device ID or IMEI found")
err = errors.New("no device ID or IMEI found")
}
}
} else {
err = errors.New("Unknown Device")
err = errors.New("unknown device")
if isVerbose {
logger.Print("Unknown Device")
}
Expand Down Expand Up @@ -527,7 +527,7 @@ func getGPSValue(dev MsgPattern, matches []string, key int) (val string, idx int
case TIME:
fallthrough
case DATE:
isInv := len(dev.Units) > i && dev.Units[i] == INV
isInv := len(dev.Units) > i && dev.Units[i] == YYMMDD
if !isInv { // DDMMYY and hhmmss format
if isBinary {
valByte, err := hex.DecodeString(val)
Expand Down Expand Up @@ -565,7 +565,7 @@ func getGPSValue(dev MsgPattern, matches []string, key int) (val string, idx int
case LAT:
fallthrough
case LON:
degval := math.Abs(valFloat) // What about signed LAT/LON?
degval := math.Abs(valFloat) // Sign of LAT/LON used to obtain N/S, E/W (see below)
if len(dev.Units) > i && dev.Units[i] == DEGREE { // calculate degree*100 + minutes
deg := float64(int(degval))
min := (degval - deg) * 60.0
Expand All @@ -574,14 +574,13 @@ func getGPSValue(dev MsgPattern, matches []string, key int) (val string, idx int
val = fmt.Sprintf("%.5f", degval)
case SPEED: // get value in m/s (GPRMC stores KNOTS, openGTS expects m/s)
v := valFloat
if len(dev.Units) > i && dev.Units[i] == KMPERH {
v /= 1.852
} // calc knots
if len(dev.Units) > i && dev.Units[i] == MPERS {
v *= 3.6 / 1.852
} // calc knots
if len(dev.Units) > i && dev.Units[i] == KNOTS {
} // nothing to do
if len(dev.Units) > i { // need speed in knots
if dev.Units[i] == KMPERH {
v /= 1.852
} else if dev.Units[i] == MPERS {
v *= 3.6 / 1.852
}
}
val = fmt.Sprintf("%.1f", v)
case DEVIMEI: // imei too short -> extend to 15 digits
if len(val) < 15 {
Expand Down Expand Up @@ -627,7 +626,7 @@ func getGPSValue(dev MsgPattern, matches []string, key int) (val string, idx int
}

// expected response from web server: device-ID/IMEI OK|REJECTED
var regexpHTTPResponse = regexp.MustCompile("^\\s*[0-9A-Za-z]+\\s+(OK|REJECTED)\\s*")
var regexpHTTPResponse = regexp.MustCompile(`^\s*[0-9A-Za-z]+\s+(OK|REJECTED)\s*`)

func analyseHTTPResponse(response string) (ans string, err error) {
ans = "no valid response - check connection to HTTP server"
Expand Down Expand Up @@ -668,7 +667,7 @@ var preshared_key = ""

func decryptMessage(msg string) (plaintxt string, err error) {
plaintxt = msg
err = errors.New("Message is not encrypted")
err = errors.New("message is not encrypted")
if len(msg) >= MIN_MSG_LEN {
txtcomp := strings.Split(msg, "-")
if len(txtcomp) == 4 {
Expand Down Expand Up @@ -711,7 +710,7 @@ func decryptMessage(msg string) (plaintxt string, err error) {
}
ptxt := strings.Trim(string(plain), " ")
if len(ptxt) < 10 {
err = errors.New("Message too short")
err = errors.New("message too short")
} else {
plaintxt = ptxt
}
Expand Down
Binary file modified tcp_udp_http_bridge/tcp-bridge.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion tcp_udp_http_bridge/tcp_udp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func handleMessage(bufmsg []byte, connType string, status *statInfo) (response s
if isVerbose {
logger.Print("close/exit message received")
}
err = errors.New("Close connection")
err = errors.New("close connection")
return
} else if cmd == "status" {
response = "OK"
Expand Down

0 comments on commit 7748ad7

Please sign in to comment.