|
@@ -1,14 +1,14 @@
|
|
package comm
|
|
package comm
|
|
|
|
|
|
import (
|
|
import (
|
|
- "encoding/hex"
|
|
|
|
- "fmt"
|
|
|
|
- "github.com/gin-gonic/gin"
|
|
|
|
- "github.com/jacobsa/go-serial/serial"
|
|
|
|
"../pkg/app"
|
|
"../pkg/app"
|
|
"../pkg/e"
|
|
"../pkg/e"
|
|
"../pkg/setting"
|
|
"../pkg/setting"
|
|
"../routers/restful"
|
|
"../routers/restful"
|
|
|
|
+ "encoding/hex"
|
|
|
|
+ "fmt"
|
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
|
+ "github.com/jacobsa/go-serial/serial"
|
|
"log"
|
|
"log"
|
|
"net/http"
|
|
"net/http"
|
|
"reflect"
|
|
"reflect"
|
|
@@ -22,6 +22,7 @@ var done = make(chan struct{})
|
|
var Id = make(chan int)
|
|
var Id = make(chan int)
|
|
var timeout = make(chan int)
|
|
var timeout = make(chan int)
|
|
var ReturnString = make(chan string)
|
|
var ReturnString = make(chan string)
|
|
|
|
+
|
|
//判断done是否关闭,即是否执行goroutine退出
|
|
//判断done是否关闭,即是否执行goroutine退出
|
|
func cancelled() bool {
|
|
func cancelled() bool {
|
|
select {
|
|
select {
|
|
@@ -32,41 +33,41 @@ func cancelled() bool {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func Int2Byte(data int)(ret []byte){
|
|
|
|
|
|
+func Int2Byte(data int) (ret []byte) {
|
|
var len uintptr = unsafe.Sizeof(data)
|
|
var len uintptr = unsafe.Sizeof(data)
|
|
ret = make([]byte, len)
|
|
ret = make([]byte, len)
|
|
var tmp int = 0xff
|
|
var tmp int = 0xff
|
|
var index uint = 0
|
|
var index uint = 0
|
|
- for index=0; index<uint(len); index++{
|
|
|
|
- ret[index] = byte((tmp<<(index*8) & data)>>(index*8))
|
|
|
|
|
|
+ for index = 0; index < uint(len); index++ {
|
|
|
|
+ ret[index] = byte((tmp << (index * 8) & data) >> (index * 8))
|
|
}
|
|
}
|
|
return ret
|
|
return ret
|
|
}
|
|
}
|
|
|
|
|
|
func OpenComm() {
|
|
func OpenComm() {
|
|
var sum byte
|
|
var sum byte
|
|
- var recLength,dataLength, state int
|
|
|
|
- comtimeout := 0
|
|
|
|
|
|
+ var recLength, dataLength, state int
|
|
|
|
+ comtimeout := 0
|
|
buf := make([]byte, 32)
|
|
buf := make([]byte, 32)
|
|
recbuf := make([]byte, 1)
|
|
recbuf := make([]byte, 1)
|
|
//s_params := make([]interface{}, 1)
|
|
//s_params := make([]interface{}, 1)
|
|
- openSerial:
|
|
|
|
|
|
+openSerial:
|
|
f, err := serial.Open(*setting.CommSetting)
|
|
f, err := serial.Open(*setting.CommSetting)
|
|
|
|
|
|
if err != nil {
|
|
if err != nil {
|
|
fmt.Println("Error opening serial port: ", err)
|
|
fmt.Println("Error opening serial port: ", err)
|
|
time.Sleep(1 * time.Second)
|
|
time.Sleep(1 * time.Second)
|
|
goto openSerial
|
|
goto openSerial
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
log.Printf("[info] Serial %s is Started", setting.CommSetting.PortName)
|
|
log.Printf("[info] Serial %s is Started", setting.CommSetting.PortName)
|
|
- if comtimeout==0 {
|
|
|
|
|
|
+ if comtimeout == 0 {
|
|
defer f.Close()
|
|
defer f.Close()
|
|
}
|
|
}
|
|
for {
|
|
for {
|
|
sum = (0x55 + 0xAA + 0x06 + 0x01 + 0x03 + 0x01) & 0xFF
|
|
sum = (0x55 + 0xAA + 0x06 + 0x01 + 0x03 + 0x01) & 0xFF
|
|
- v := 0
|
|
|
|
- if comtimeout==0 {
|
|
|
|
- v, _ = <- Id
|
|
|
|
|
|
+ v := 0
|
|
|
|
+ if comtimeout == 0 {
|
|
|
|
+ v, _ = <-Id
|
|
}
|
|
}
|
|
|
|
|
|
comtimeout = 0
|
|
comtimeout = 0
|
|
@@ -87,68 +88,68 @@ func OpenComm() {
|
|
comtimeout = 1
|
|
comtimeout = 1
|
|
goto openSerial
|
|
goto openSerial
|
|
}
|
|
}
|
|
- loop:
|
|
|
|
- for {
|
|
|
|
- select {
|
|
|
|
- case <-timeout:
|
|
|
|
- break loop
|
|
|
|
- default:
|
|
|
|
- n, err := f.Read(buf)
|
|
|
|
- if err != nil {
|
|
|
|
- //if err != io.EOF {
|
|
|
|
- fmt.Println("Error reading from serial port: ", err)
|
|
|
|
- time.Sleep(1 * time.Second)
|
|
|
|
- comtimeout = 1
|
|
|
|
- goto openSerial
|
|
|
|
- //}
|
|
|
|
- //break loop
|
|
|
|
- } else if n > 0 {
|
|
|
|
- buf = buf[:n]
|
|
|
|
- for _, value := range buf {
|
|
|
|
- switch state {
|
|
|
|
- case 0: //头判断
|
|
|
|
- if value == 0x55 {
|
|
|
|
- state = 1
|
|
|
|
- recLength = 1
|
|
|
|
- recbuf = recbuf[:0]
|
|
|
|
- recbuf = append(recbuf, (byte)(value&0xFF))
|
|
|
|
- }
|
|
|
|
- case 1: //头判断中
|
|
|
|
- if value == 0xAA {
|
|
|
|
- state = 2
|
|
|
|
- recLength = 2
|
|
|
|
- recbuf = append(recbuf, (byte)(value&0xFF))
|
|
|
|
- } else {
|
|
|
|
- state = 0
|
|
|
|
- recLength = 0
|
|
|
|
- }
|
|
|
|
- case 2: //接收数据长度
|
|
|
|
- state = 3
|
|
|
|
- recLength = 3
|
|
|
|
- dataLength = (int)(value + 4)
|
|
|
|
|
|
+ loop:
|
|
|
|
+ for {
|
|
|
|
+ select {
|
|
|
|
+ case <-timeout:
|
|
|
|
+ break loop
|
|
|
|
+ default:
|
|
|
|
+ n, err := f.Read(buf)
|
|
|
|
+ if err != nil {
|
|
|
|
+ //if err != io.EOF {
|
|
|
|
+ fmt.Println("Error reading from serial port: ", err)
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
+ comtimeout = 1
|
|
|
|
+ goto openSerial
|
|
|
|
+ //}
|
|
|
|
+ //break loop
|
|
|
|
+ } else if n > 0 {
|
|
|
|
+ buf = buf[:n]
|
|
|
|
+ for _, value := range buf {
|
|
|
|
+ switch state {
|
|
|
|
+ case 0: //头判断
|
|
|
|
+ if value == 0x55 {
|
|
|
|
+ state = 1
|
|
|
|
+ recLength = 1
|
|
|
|
+ recbuf = recbuf[:0]
|
|
recbuf = append(recbuf, (byte)(value&0xFF))
|
|
recbuf = append(recbuf, (byte)(value&0xFF))
|
|
- case 3: //接收数据中
|
|
|
|
|
|
+ }
|
|
|
|
+ case 1: //头判断中
|
|
|
|
+ if value == 0xAA {
|
|
|
|
+ state = 2
|
|
|
|
+ recLength = 2
|
|
recbuf = append(recbuf, (byte)(value&0xFF))
|
|
recbuf = append(recbuf, (byte)(value&0xFF))
|
|
- recLength++
|
|
|
|
- if recLength == dataLength {
|
|
|
|
- fmt.Println(hex.EncodeToString(recbuf))
|
|
|
|
- ReturnString <- hex.EncodeToString(recbuf)
|
|
|
|
- state = 0
|
|
|
|
- recLength = 0
|
|
|
|
- break loop
|
|
|
|
- }
|
|
|
|
|
|
+ } else {
|
|
|
|
+ state = 0
|
|
|
|
+ recLength = 0
|
|
|
|
+ }
|
|
|
|
+ case 2: //接收数据长度
|
|
|
|
+ state = 3
|
|
|
|
+ recLength = 3
|
|
|
|
+ dataLength = (int)(value + 4)
|
|
|
|
+ recbuf = append(recbuf, (byte)(value&0xFF))
|
|
|
|
+ case 3: //接收数据中
|
|
|
|
+ recbuf = append(recbuf, (byte)(value&0xFF))
|
|
|
|
+ recLength++
|
|
|
|
+ if recLength == dataLength {
|
|
|
|
+ fmt.Println(hex.EncodeToString(recbuf))
|
|
|
|
+ ReturnString <- hex.EncodeToString(recbuf)
|
|
|
|
+ state = 0
|
|
|
|
+ recLength = 0
|
|
|
|
+ break loop
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- time.Sleep(10 * time.Millisecond)
|
|
|
|
}
|
|
}
|
|
|
|
+ } else {
|
|
|
|
+ time.Sleep(10 * time.Millisecond)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func saveData(ReceStr string,x int,y int) (string, error){
|
|
|
|
|
|
+func saveData(ReceStr string, x int, y int) (string, error) {
|
|
s_params := make([]interface{}, 0)
|
|
s_params := make([]interface{}, 0)
|
|
//bytelist, err := hex.DecodeString(ReturnS)
|
|
//bytelist, err := hex.DecodeString(ReturnS)
|
|
s_params = append(s_params, 1)
|
|
s_params = append(s_params, 1)
|
|
@@ -160,10 +161,10 @@ func saveData(ReceStr string,x int,y int) (string, error){
|
|
return "", err
|
|
return "", err
|
|
} else {
|
|
} else {
|
|
bytelist, err := hex.DecodeString(ReceStr)
|
|
bytelist, err := hex.DecodeString(ReceStr)
|
|
- rodecount := (int)((bytelist[2]-5)>>1)
|
|
|
|
|
|
+ rodecount := (int)((bytelist[2] - 5) >> 1)
|
|
s_params = s_params[1:3]
|
|
s_params = s_params[1:3]
|
|
- if rodecount>0{
|
|
|
|
- for i := 0; i < rodecount; i++{
|
|
|
|
|
|
+ if rodecount > 0 {
|
|
|
|
+ for i := 0; i < rodecount; i++ {
|
|
beaconname := (int)(bytelist[8+2*i])
|
|
beaconname := (int)(bytelist[8+2*i])
|
|
rssi := (int)(bytelist[9+2*i])
|
|
rssi := (int)(bytelist[9+2*i])
|
|
s_params = s_params[:2]
|
|
s_params = s_params[:2]
|
|
@@ -177,30 +178,30 @@ func saveData(ReceStr string,x int,y int) (string, error){
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if err == nil{
|
|
|
|
|
|
+ if err == nil {
|
|
return "成功保存", nil
|
|
return "成功保存", nil
|
|
}
|
|
}
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
return "没有收到数据", nil
|
|
return "没有收到数据", nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return "没有收到数据", nil
|
|
return "没有收到数据", nil
|
|
}
|
|
}
|
|
|
|
|
|
-func savePointData(ReceStr string,limit int) (interface{}, error){
|
|
|
|
|
|
+func savePointData(ReceStr string, limit int) (interface{}, error) {
|
|
s_params := make([]interface{}, 1)
|
|
s_params := make([]interface{}, 1)
|
|
//bytelist, err := hex.DecodeString(ReturnS)
|
|
//bytelist, err := hex.DecodeString(ReturnS)
|
|
bytelist, err := hex.DecodeString(ReceStr)
|
|
bytelist, err := hex.DecodeString(ReceStr)
|
|
- rodecount := (int)((bytelist[2]-5)>>1)
|
|
|
|
|
|
+ rodecount := (int)((bytelist[2] - 5) >> 1)
|
|
low := (int)(bytelist[5])
|
|
low := (int)(bytelist[5])
|
|
mid := (int)(bytelist[6])
|
|
mid := (int)(bytelist[6])
|
|
- mid = mid<<8
|
|
|
|
|
|
+ mid = mid << 8
|
|
high := (int)(bytelist[7])
|
|
high := (int)(bytelist[7])
|
|
- high = high<<16
|
|
|
|
- tagid := (int)(low|mid|high)
|
|
|
|
|
|
+ high = high << 16
|
|
|
|
+ tagid := (int)(low | mid | high)
|
|
s_params[0] = tagid
|
|
s_params[0] = tagid
|
|
- if rodecount>0{
|
|
|
|
- for i := 0; i < rodecount; i++{
|
|
|
|
|
|
+ if rodecount > 0 {
|
|
|
|
+ for i := 0; i < rodecount; i++ {
|
|
beaconname := (int)(bytelist[8+2*i])
|
|
beaconname := (int)(bytelist[8+2*i])
|
|
rssi := (int)(bytelist[9+2*i])
|
|
rssi := (int)(bytelist[9+2*i])
|
|
s_params = s_params[:1]
|
|
s_params = s_params[:1]
|
|
@@ -208,58 +209,57 @@ func savePointData(ReceStr string,limit int) (interface{}, error){
|
|
s_params = append(s_params, rssi)
|
|
s_params = append(s_params, rssi)
|
|
s_params = append(s_params, rssi)
|
|
s_params = append(s_params, rssi)
|
|
_, err = restful.ExecQuery(" INSERT INTO `cps_point`(`tagid`,`beaconid`,`rssi`) "+
|
|
_, err = restful.ExecQuery(" INSERT INTO `cps_point`(`tagid`,`beaconid`,`rssi`) "+
|
|
- " Values( ?, ?, ?) ON DUPLICATE KEY UPDATE rssi = ?", s_params)
|
|
|
|
|
|
+ " Values( ?, ?, ?) ON DUPLICATE KEY UPDATE rssi = ?", s_params)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", err
|
|
return "", err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- if err == nil{
|
|
|
|
|
|
+ if err == nil {
|
|
s_params = s_params[:2]
|
|
s_params = s_params[:2]
|
|
s_params[0] = tagid
|
|
s_params[0] = tagid
|
|
s_params[1] = limit
|
|
s_params[1] = limit
|
|
resultdata, err := restful.QueryByMap(" SELECT AVG(X) X,AVG(Y) Y FROM ( "+
|
|
resultdata, err := restful.QueryByMap(" SELECT AVG(X) X,AVG(Y) Y FROM ( "+
|
|
- "SELECT `cps_m`.x,`cps_m`.y, SUM(ABS(`cps_d`.`rssi`-`cps_point`.`rssi`)) absrssi "+
|
|
|
|
- "FROM `cps_d` JOIN `cps_point` ON `cps_d`.`beaconid`=`cps_point`.`beaconid` "+
|
|
|
|
- "JOIN `cps_m` ON `cps_m`.`id`=`cps_d`.`pid` WHERE `cps_point`.`tagid`= ? "+
|
|
|
|
- "GROUP BY `cps_m`.x,`cps_m`.y "+
|
|
|
|
- "ORDER BY SUM(ABS(`cps_d`.`rssi`-`cps_point`.`rssi`)) "+
|
|
|
|
- "LIMIT ?) AS d " , 0,0,s_params)
|
|
|
|
|
|
+ "SELECT `cps_m`.x,`cps_m`.y, SUM(ABS(`cps_d`.`rssi`-`cps_point`.`rssi`)) absrssi "+
|
|
|
|
+ "FROM `cps_d` JOIN `cps_point` ON `cps_d`.`beaconid`=`cps_point`.`beaconid` "+
|
|
|
|
+ "JOIN `cps_m` ON `cps_m`.`id`=`cps_d`.`pid` WHERE `cps_point`.`tagid`= ? "+
|
|
|
|
+ "GROUP BY `cps_m`.x,`cps_m`.y "+
|
|
|
|
+ "ORDER BY SUM(ABS(`cps_d`.`rssi`-`cps_point`.`rssi`)) "+
|
|
|
|
+ "LIMIT ?) AS d ", 0, 0, s_params)
|
|
if err != nil {
|
|
if err != nil {
|
|
return "", err
|
|
return "", err
|
|
} else {
|
|
} else {
|
|
return resultdata, nil
|
|
return resultdata, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
return "没有收到数据", nil
|
|
return "没有收到数据", nil
|
|
}
|
|
}
|
|
return "没有收到数据", nil
|
|
return "没有收到数据", nil
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
func ProcessHttp(c *gin.Context) {
|
|
func ProcessHttp(c *gin.Context) {
|
|
var (
|
|
var (
|
|
appG = app.Gin{C: c}
|
|
appG = app.Gin{C: c}
|
|
)
|
|
)
|
|
|
|
|
|
- id := 0//com.StrTo(c.Param("id")).MustInt()
|
|
|
|
- x := 0//com.StrTo(c.Param("x")).MustInt()
|
|
|
|
- y := 0//com.StrTo(c.Param("y")).MustInt()
|
|
|
|
|
|
+ id := 0 //com.StrTo(c.Param("id")).MustInt()
|
|
|
|
+ x := 0 //com.StrTo(c.Param("x")).MustInt()
|
|
|
|
+ y := 0 //com.StrTo(c.Param("y")).MustInt()
|
|
Id <- id
|
|
Id <- id
|
|
select {
|
|
select {
|
|
- case ReturnS := <- ReturnString: //拿到锁
|
|
|
|
- if x>=0 && y>=0{
|
|
|
|
- returnData, err := saveData(ReturnS,x,y)
|
|
|
|
- if err != nil{
|
|
|
|
|
|
+ case ReturnS := <-ReturnString: //拿到锁
|
|
|
|
+ if x >= 0 && y >= 0 {
|
|
|
|
+ returnData, err := saveData(ReturnS, x, y)
|
|
|
|
+ if err != nil {
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
}
|
|
}
|
|
- }else {
|
|
|
|
- returnData, err := savePointData(ReturnS,1)
|
|
|
|
- if err != nil{
|
|
|
|
|
|
+ } else {
|
|
|
|
+ returnData, err := savePointData(ReturnS, 1)
|
|
|
|
+ if err != nil {
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -274,17 +274,17 @@ func ProcessHttpCPS(c *gin.Context) {
|
|
appG = app.Gin{C: c}
|
|
appG = app.Gin{C: c}
|
|
)
|
|
)
|
|
|
|
|
|
- id := 0//com.StrTo(c.Param("id")).MustInt()
|
|
|
|
- limit := 0//com.StrTo(c.Param("limit")).MustInt()
|
|
|
|
|
|
+ id := 0 //com.StrTo(c.Param("id")).MustInt()
|
|
|
|
+ limit := 0 //com.StrTo(c.Param("limit")).MustInt()
|
|
Id <- id
|
|
Id <- id
|
|
select {
|
|
select {
|
|
- case ReturnS := <- ReturnString: //拿到锁
|
|
|
|
|
|
+ case ReturnS := <-ReturnString: //拿到锁
|
|
{
|
|
{
|
|
|
|
|
|
returnData, err := savePointData(ReturnS, limit)
|
|
returnData, err := savePointData(ReturnS, limit)
|
|
- if err != nil{
|
|
|
|
|
|
+ if err != nil {
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
appG.Response(http.StatusOK, e.ERROR, err.Error())
|
|
- }else {
|
|
|
|
|
|
+ } else {
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
appG.Response(http.StatusOK, e.SUCCESS, returnData)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -294,16 +294,15 @@ func ProcessHttpCPS(c *gin.Context) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// interface转map[string]interface{}
|
|
// interface转map[string]interface{}
|
|
|
|
|
|
-func ToMap(in interface{}, tagName string) (map[string]interface{}, error){
|
|
|
|
|
|
+func ToMap(in interface{}, tagName string) (map[string]interface{}, error) {
|
|
out := make(map[string]interface{})
|
|
out := make(map[string]interface{})
|
|
v := reflect.ValueOf(in)
|
|
v := reflect.ValueOf(in)
|
|
if v.Kind() == reflect.Ptr {
|
|
if v.Kind() == reflect.Ptr {
|
|
v = v.Elem()
|
|
v = v.Elem()
|
|
}
|
|
}
|
|
- if v.Kind() != reflect.Struct { // 非结构体返回错误提示
|
|
|
|
|
|
+ if v.Kind() != reflect.Struct { // 非结构体返回错误提示
|
|
return nil, fmt.Errorf("ToMap only accepts struct or struct pointer; got %T", v)
|
|
return nil, fmt.Errorf("ToMap only accepts struct or struct pointer; got %T", v)
|
|
}
|
|
}
|
|
t := v.Type()
|
|
t := v.Type()
|
|
@@ -316,4 +315,16 @@ func ToMap(in interface{}, tagName string) (map[string]interface{}, error){
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return out, nil
|
|
return out, nil
|
|
-}
|
|
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// 错误返回提示语
|
|
|
|
+func ErrorWoring(err string) (end string) {
|
|
|
|
+ var endReturn string
|
|
|
|
+ switch err {
|
|
|
|
+ case "":
|
|
|
|
+ endReturn = ""
|
|
|
|
+ default:
|
|
|
|
+ endReturn = err
|
|
|
|
+ }
|
|
|
|
+ return endReturn
|
|
|
|
+}
|