项目流产,记录一下服务端代码:
1、http解析;
2、json解析;
3、mysql存储
package main
import (
"database/sql"
"encoding/json"
"fmt"
_ "github.com/go-sql-driver/mysql"
"io/ioutil"
// "log"
// "io"
"net/http"
)
///数据解析结构体
type rfid_message struct {
Node_device_number int
Node_device_mac string
// Number int
Rec_time string
Position string
Data string
}
type Mesg_mesg struct {
Rfid_dev rfid_message
}
//数据返回结构体
type Post_Res struct {
PackTpye string `json:"PackTpye"`
State int `json:"state"`
MacAddress string `json:"mac"`
}
type Packslice struct {
Package []Post_Res `json:"pack"`
}
func helloHandler(w http.ResponseWriter, req *http.Request) {
if req.Method == "GET" {
body, _ := ioutil.ReadAll(req.Body)
body_str := string(body)
fmt.Println(body_str)
var mesg Mesg_mesg
err := json.Unmarshal([]byte(body_str), &mesg)
if err != nil {
fmt.Println(err)
}
fmt.Println(mesg.Rfid_dev.Data)
//fmt.Println(mesg.Rfid_dev.Node_device_mac)
var s Packslice
s.Package = append(s.Package, Post_Res{PackTpye: "serv_return_post", State: 1, MacAddress: "1234567890ABCE"})
b, err := json.Marshal(s)
if err != nil {
fmt.Println("json err: ", err)
}
fmt.Println(string(b)) ////打印需要返回的json
w.Write([]byte(b))
db, err := sql.Open("mysql", "toone:123456@(192.168.1.237:3306)/user_data") //链接数据库
if err != nil {
//log.Fatal(err)
}
defer db.Close()
er := db.Ping()
if er != nil {
fmt.Println("数据库连接失败")
return
}
// fmt.Println(mesg.Rfid_dev.Node_device_number, mesg.Rfid_dev.Rec_time, mesg.Rfid_dev.Position, mesg.Rfid_dev.Data, mesg.Rfid_dev.Node_device_mac)
Query(db, mesg.Rfid_dev.Node_device_number, mesg.Rfid_dev.Rec_time, mesg.Rfid_dev.Position, mesg.Rfid_dev.Data, mesg.Rfid_dev.Node_device_mac) /// 首先查询是否有记录
}
}
func Insert(db *sql.DB, a int, b string, c string, d string, e string) {
//a:Node_device_number_dev
//b:Node_device_mac
//c:Rec_time
//d:Position
//e:Data
stmt, err := db.Prepare("INSERT INTO work SET node_device_number=?,rec_time_first=?,postion=?,data=?,dev_mac=?")
if err != nil {
fmt.Println(err)
return
}
res, err := stmt.Exec(a, b, c, d, e)
id, err := res.LastInsertId()
if err != nil {
panic(err)
}
fmt.Println(id)
}
func SubString(str string,begin,length int) (substr string) {
lth := len(str)
if begin < 0 {
begin = 0
}
if begin >= lth {
begin = lth
}
end := begin + length
if end > lth {
end = lth
}
// 返回子串
return string(str[begin:end])
}
func Query(db *sql.DB, a int, b string, c string, d string, e string) {
fmt.Println(a,b,c,d,e)
//fmt.Println(mesg.Rfid_dev.Node_device_number, mesg.Rfid_dev.Rec_time, mesg.Rfid_dev.Position, mesg.Rfid_dev.Data, mesg.Rfid_dev.Node_device_mac)
// var cc int =0;
var s int =0;
//var ss_dat string;
s = len(d)/24;
for i := 0; i < s; i++ {
v:= SubString(d,i*24,24)
fmt.Println("=====>>>>\r\n")
fmt.Println(v)
fmt.Println("=====>>>>\r\n")
var select_string string
select_string = fmt.Sprintf("select * from work where data='%s';", v) //查询data字段
fmt.Println(string(select_string))
rows := db.QueryRow(select_string) //查询单行
//rows, err:= db.Query("select * from work where data = ?;",dta)//查询多行
// defer rows.Close() //查询多行
var ID int // store query count
var node_device_number int
var rec_time_first string
var postion sql.NullString
var data sql.NullString
var rec_time_last sql.NullString
var dev_mac sql.NullString
var time_tmp string
err := rows.Scan(&ID, &node_device_number, &rec_time_first, &postion, &data, &rec_time_last,&dev_mac)
fmt.Println("rows_id ?", ID)
if (err != nil ) { //没有查询到记录
// log.Fatal(err)
fmt.Println("第一次打卡")
fmt.Println(ID, node_device_number, rec_time_first, postion, data, rec_time_last,dev_mac)
Insert(db, a, b, c, v, e)
}else{
time_tmp = (rec_time_first) //存放第一次打卡时间
fmt.Println("第二次打卡")
Updata(db, a, b, c, v, e,time_tmp,ID)
}
}
}
var ID int // store query count
var node_device_number int
var rec_time_first string
var postion sql.NullString
var data sql.NullString
var rec_time_last sql.NullString
var dev_mac sql.NullString
func Updata(db *sql.DB, a int, b string, c string, d string, e string,f string, g int) {
fmt.Println(a,b,c,d,e,f)
sql := fmt.Sprintf(
"update work set node_device_number='%d', rec_time_first='%s', postion='%s', data='%s', rec_time_last='%s', dev_mac='%s' where ID='%d'",
a,
f,
c,
d,
b,
e,
g)
fmt.Println(sql)
result, err := db.Exec(sql)
if err != nil {
fmt.Println("exec failed:", err, ", sql:", sql)
return
}
id, err := result.RowsAffected()
if err != nil {
fmt.Println("RowsAffected failed:", err)
return
}
fmt.Println("受影响行数 %d",id)
}
func main() {
//Query(db)
//Insert(db)
hh := http.HandlerFunc(helloHandler) // 通过 HandlerFunc 把函数转换成 Handler 接口的实现对象
http.Handle("/", hh)
http.ListenAndServe(":8090", nil)
}