From 38f3778b7a2937648c58d272964fcbfdafe306b9 Mon Sep 17 00:00:00 2001 From: zimujun20 <1192147078@qq.com> Date: Sun, 2 Oct 2022 12:51:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=8E=B7=E5=8F=96=E5=BD=93?= =?UTF-8?q?=E5=89=8D=E8=AE=BE=E5=A4=87=E5=9C=B0=E5=9D=80=E5=92=8C=E8=B4=9F?= =?UTF-8?q?=E8=BD=BD=E5=9D=87=E8=A1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/webserver/api/android/media_pipe.go | 47 --------- src/webserver/api/pc/load_alancing.go | 54 ---------- src/webserver/go.mod | 5 + src/webserver/go.sum | 2 + src/webserver/info.db | Bin 0 -> 20480 bytes src/webserver/main.go | 6 +- .../{ => pkg}/api/android/hands_3d.go | 0 .../api/android/hands_recegnation.go | 0 .../api/android/hands_translation.go | 0 .../{ => pkg}/api/android/media_camera.go | 0 src/webserver/pkg/api/android/media_pipe.go | 92 ++++++++++++++++++ .../{ => pkg}/api/android/word_input.go | 0 .../{ => pkg}/api/android/word_recv.go | 0 .../{ => pkg}/api/android/word_send.go | 10 +- .../{ => pkg}/api/android/word_show.go | 0 src/webserver/pkg/api/pc/load_alancing.go | 62 ++++++++++++ src/webserver/{ => pkg}/api/pc/nginx-rtmp.go | 0 src/webserver/{ => pkg}/api/pc/webserver.go | 0 src/webserver/pkg/sql.go | 4 +- src/webserver/routers/router.go | 4 +- src/webserver/static/js/mediaToWords.js | 8 ++ .../static/js/{homepage.js => setting.js} | 0 src/webserver/static/js/wordsToMedia.js | 24 +++++ 23 files changed, 204 insertions(+), 114 deletions(-) delete mode 100644 src/webserver/api/android/media_pipe.go delete mode 100644 src/webserver/api/pc/load_alancing.go create mode 100644 src/webserver/go.mod create mode 100644 src/webserver/go.sum create mode 100644 src/webserver/info.db rename src/webserver/{ => pkg}/api/android/hands_3d.go (100%) rename src/webserver/{ => pkg}/api/android/hands_recegnation.go (100%) rename src/webserver/{ => pkg}/api/android/hands_translation.go (100%) rename src/webserver/{ => pkg}/api/android/media_camera.go (100%) create mode 100644 src/webserver/pkg/api/android/media_pipe.go rename src/webserver/{ => pkg}/api/android/word_input.go (100%) rename src/webserver/{ => pkg}/api/android/word_recv.go (100%) rename src/webserver/{ => pkg}/api/android/word_send.go (73%) rename src/webserver/{ => pkg}/api/android/word_show.go (100%) create mode 100644 src/webserver/pkg/api/pc/load_alancing.go rename src/webserver/{ => pkg}/api/pc/nginx-rtmp.go (100%) rename src/webserver/{ => pkg}/api/pc/webserver.go (100%) create mode 100644 src/webserver/static/js/mediaToWords.js rename src/webserver/static/js/{homepage.js => setting.js} (100%) create mode 100644 src/webserver/static/js/wordsToMedia.js diff --git a/src/webserver/api/android/media_pipe.go b/src/webserver/api/android/media_pipe.go deleted file mode 100644 index 3117155..0000000 --- a/src/webserver/api/android/media_pipe.go +++ /dev/null @@ -1,47 +0,0 @@ -package android - -import ( - "fmt" - "net/http" - "database/sql" - _ "go-sqlite3-master" - "application/pkg/app" - - "github.com/gin-gonic/gin" -) - -func getMacAddress() (macAddress []string) { - netInterfaces, err := net.Interfaces() - if err != nil { - fmt.Printf("fail to get net interfaces: %v", err) - return macAddress - } - - for _, netInterface := range netInterfaces { - MacAddress := netInterface.HardwareAddr.String() - if len(MacAddress) == 0 { - continue - } - macAddress = append(macAddress, MacAddress) - } - return macAddress -} - -func sendMacAddress(c *gin.Context) { - appG := app.Gin{C: c} - macAddress = getMacAddress() - db, err := sql.Open("sqlite3", "info.db") - if err != nil { - fmt.Println(err) - } - defer db.Close() - db.Exec(`insert into Endpoint values (?,?)`, macAddress, 0) - appG.Response(http.StatusOK, "成功", null) -} - -func mediaPipe(c *gin.Context) { - appG := app.Gin{C: c} - //这里将视频流转化为自然语言 - data := - appG.Response(http.StatusOK, "成功", data) -} \ No newline at end of file diff --git a/src/webserver/api/pc/load_alancing.go b/src/webserver/api/pc/load_alancing.go deleted file mode 100644 index 74df7f2..0000000 --- a/src/webserver/api/pc/load_alancing.go +++ /dev/null @@ -1,54 +0,0 @@ -package pc - -import ( - "database/sql" - _ "go-sqlite3-master" - "net/http" -) - -//拉取在线的算力设备 -func endpoint(w http.ResponseWriter, r *http.Request) { - db, err := sql.Open("sqlite3", "info.db") - if err != nil { - fmt.Println(err) - } - defer db.Close() - rows, _ := db.Query(`select endpoint.macAddress from Endpoint where endpoint.times < Average.average;`) - defer rows.Close() - var endpoint Endpoint - var endpoints = []string {} - for rows.Next() { - rows.Scan(&endpoint.macAddress) - endpoints.append(endpoint.macAddress) - } - rows.Close() -} - -func shuffle(n int) []int { - b := rand.Perm(n) - return b -} - -func request(params map[string]interface{}) error { - var indexes = []int {0,1,2,3,4,5,6} - var err error - - shuffle(indexes) - maxRetryTimes := 3 - - idx := 0 - for i :=0; i < maxRetryTimes; i++ { - err = apiRequest(params, indexes[idx]) - if err == nil { - break - } - idx++ - } - - if err != nil { - //logging - return err - } - - return nil -} \ No newline at end of file diff --git a/src/webserver/go.mod b/src/webserver/go.mod new file mode 100644 index 0000000..3e8f52e --- /dev/null +++ b/src/webserver/go.mod @@ -0,0 +1,5 @@ +module main.go + +go 1.19 + +require github.com/mattn/go-sqlite3 v1.14.15 // indirect diff --git a/src/webserver/go.sum b/src/webserver/go.sum new file mode 100644 index 0000000..252cb10 --- /dev/null +++ b/src/webserver/go.sum @@ -0,0 +1,2 @@ +github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= diff --git a/src/webserver/info.db b/src/webserver/info.db new file mode 100644 index 0000000000000000000000000000000000000000..65142addb562d96814e65fb69bebf48bbeac0d1a GIT binary patch literal 20480 zcmeI%OG^Sl0KoBCvkW1%lLwLJ5-3U$MdvKM2|X`CAd^ltDGMjh+R^_4m{t8Qr2 z7qI`p>^ydM9>1B(u$Si@6Ki=lbfJpnz9@+x5Jyr9Au>L$`k02KoJ|Wp4d(KHWiw)@ zTiGanifmylirdAH!iUcg5I_I{1Q0*~0R#|0009L4hrn5Ot=wn?*Ri@C=;&$SZ=l^s zKaI8R{VcAYT(nzPZF$u??zH8Pwl%9l-Sl79Z&q^UN+ozJPP%M8Yp3q@EXgi(F{_ld zyqdO>#>VQ|}8@zB_w)1kI;{cS7Am8;d@eRI-%+xAEPZJef=g#pr9 zKSHQ_t$yF>D3Z_0^&XU~9Uj!@2WsZc=(i?zJQy_du?cnbJ-=z4iB4_DSiMkP-_5L* zPpW}1wy$4iO4Bv?ayOk)cQ4JY_=C2d9JY<*QA!>J1Q0*~0R#|0009ILKmY**5Li}$ ze6ld^|I50)tP24I5I_I{1Q0*~0R#|00D*}B|Nl7%2q1s}0tg_000IagfB*srEWZHv a|K;CfR)_!s2q1s}0tg_000IagfWQ~ls*aHW literal 0 HcmV?d00001 diff --git a/src/webserver/main.go b/src/webserver/main.go index 2aa77d1..266b4c3 100644 --- a/src/webserver/main.go +++ b/src/webserver/main.go @@ -1,11 +1,9 @@ -package main - import ( "fmt" "log" "net/http" - "application/routers" + "webserver/routers" ) func main() { @@ -18,4 +16,4 @@ func main() { if err := server.ListenAndServe(); err != nil { log.Printf("start http server failed %s", err) } -} +} \ No newline at end of file diff --git a/src/webserver/api/android/hands_3d.go b/src/webserver/pkg/api/android/hands_3d.go similarity index 100% rename from src/webserver/api/android/hands_3d.go rename to src/webserver/pkg/api/android/hands_3d.go diff --git a/src/webserver/api/android/hands_recegnation.go b/src/webserver/pkg/api/android/hands_recegnation.go similarity index 100% rename from src/webserver/api/android/hands_recegnation.go rename to src/webserver/pkg/api/android/hands_recegnation.go diff --git a/src/webserver/api/android/hands_translation.go b/src/webserver/pkg/api/android/hands_translation.go similarity index 100% rename from src/webserver/api/android/hands_translation.go rename to src/webserver/pkg/api/android/hands_translation.go diff --git a/src/webserver/api/android/media_camera.go b/src/webserver/pkg/api/android/media_camera.go similarity index 100% rename from src/webserver/api/android/media_camera.go rename to src/webserver/pkg/api/android/media_camera.go diff --git a/src/webserver/pkg/api/android/media_pipe.go b/src/webserver/pkg/api/android/media_pipe.go new file mode 100644 index 0000000..22b8f3b --- /dev/null +++ b/src/webserver/pkg/api/android/media_pipe.go @@ -0,0 +1,92 @@ +package android + +import ( + "fmt" + "net" + "database/sql" + "time" + "pkg/app" + "errors" + _ "github.com/mattn/go-sqlite3" + + "github.com/gin-gonic/gin" +) + +//获取本设备的mac地址 +func getMacAddress() (macAddress string) { + netInterfaces, err := net.Interfaces() + if err != nil { + fmt.Printf("fail to get net interfaces: %v", err) + return macAddress + } + + for _, netInterface := range netInterfaces { + if ( netInterface.Name == "WLAN" ) { + macAddress := netInterface.HardwareAddr + return macAddress.String() + } + } + return macAddress +} + +//获取本设备的IP地址 +func getIpAddress() (addr string, err error) { + var ( + ief *net.Interface + addrs []net.Addr + ipv4Addr net.IP + ) + if ief, err = net.InterfaceByName("WLAN"); err != nil { + return + } + if addrs, err = ief.Addrs(); err != nil { + return + } + for _, addr := range addrs { + if ipv4Addr = addr.(*net.IPNet).IP.To4(); ipv4Addr != nil { + break + } + } + if ipv4Addr == nil { + return "", errors.New(fmt.Sprintf("interface WLAN don't have an ipv4 address\n")) + } + return ipv4Addr.String(), nil +} + +//向服务器发送本设备的mac地址和IP地址 +func sendAddress(c *gin.Context) { + appG := app.Gin{C: c} + now := time.Now() + hour := now.Hour() + minute := now.Minute() + t := hour*100 + minute + macAddress := getMacAddress() + ipAddress, err := getIpAddress() + if err != nil { + fmt.Println(err) + } + db, err := sql.Open("sqlite3", "info.db") + if err != nil { + fmt.Println(err) + } + defer db.Close() + rows, err := db.Query(`select macAddress from Endpoint where macAddress = ?;`, macAddress) + defer rows.Close() + var MacAddress string + rows.Next() + rows.Scan(&MacAddress) + if MacAddress == "" { + db.Exec(`insert into Endpoint values (?,?,?,?);`, macAddress, ipAddress, 0, t) + } else { + db.Exec(`update Endpoint set ipAddress = ? where macAddress = ?;`, ipAddress, macAddress) + db.Exec(`update Endpoint set isOnLine = ? where macAddress = ?;`, t, macAddress) + } + appG.Response(http.StatusOK, "成功", null) +} + +func mediaPipe(c *gin.Context) { + appG := app.Gin{C: c} + //这里将视频流转化为自然语言 + data := + appG.Response(http.StatusOK, "成功", data) +} \ No newline at end of file diff --git a/src/webserver/api/android/word_input.go b/src/webserver/pkg/api/android/word_input.go similarity index 100% rename from src/webserver/api/android/word_input.go rename to src/webserver/pkg/api/android/word_input.go diff --git a/src/webserver/api/android/word_recv.go b/src/webserver/pkg/api/android/word_recv.go similarity index 100% rename from src/webserver/api/android/word_recv.go rename to src/webserver/pkg/api/android/word_recv.go diff --git a/src/webserver/api/android/word_send.go b/src/webserver/pkg/api/android/word_send.go similarity index 73% rename from src/webserver/api/android/word_send.go rename to src/webserver/pkg/api/android/word_send.go index f9cda2e..82d21f7 100644 --- a/src/webserver/api/android/word_send.go +++ b/src/webserver/pkg/api/android/word_send.go @@ -9,27 +9,25 @@ import ( "github.com/gin-gonic/gin" ) -func updateAverage (c *gin.Context) { - appG := app.Gin{C: c} +func updateAverage () { macAddress = getMacAddress() db, err := sql.Open("sqlite3", "info.db") if err != nil { fmt.Println(err) } defer db.Close() - db.Exec(`update Endpoint set times = times + 1 where macAddress = ?`, macAddress) + db.Exec(`update Endpoint set times = times + 1 where macAddress = ?;`, macAddress) rows, err := db.Query(`select SUM( times ) from Endpoint`) defer rows.Close() var sumOfTimes int rows.Next() rows.Scan(&sumOfTimes) - rows, err := db.Query(`select COUNT( macAddress ) from Endpoint`) + rows, err = db.Query(`select COUNT( macAddress ) from Endpoint;`) defer rows.Close() var countOfMacAddress int rows.Next() rows.Scan(&countOfMacAddress) - db.Exec(`update Average set average = ?/?`sendMacAddress, countOfMacAddress) - appG.Response(http.StatusOK, "成功", null) + db.Exec(`update Average set average = ?/?;`sumOfTimes, countOfMacAddress) } func wordSend (c *gin.Context) { diff --git a/src/webserver/api/android/word_show.go b/src/webserver/pkg/api/android/word_show.go similarity index 100% rename from src/webserver/api/android/word_show.go rename to src/webserver/pkg/api/android/word_show.go diff --git a/src/webserver/pkg/api/pc/load_alancing.go b/src/webserver/pkg/api/pc/load_alancing.go new file mode 100644 index 0000000..e69b370 --- /dev/null +++ b/src/webserver/pkg/api/pc/load_alancing.go @@ -0,0 +1,62 @@ +package pc + +import ( + "database/sql" + _ "github.com/mattn/go-sqlite3" + "time" + "math/rand" + "strings" +) + +//拉取在线的算力设备 +func ipAddress() []string { + now := time.Now() + hour := now.Hour() + minute := now.Minute() + t := hour*100 + minute -100 + db, err := sql.Open("sqlite3", "info.db") + if err != nil { + fmt.Println(err) + } + defer db.Close() + rows, _ := db.Query(`select average from Average;`) + defer rows.Close() + var average int + rows.Next() + rows.Scan(&average) + rows, _ = db.Query(`select ipAddress from Endpoint where times <= ? and isOnLine >= ?;`, average, t) + defer rows.Close() + var endpoint Endpoint + ipAddress := "" + for rows.Next() { + rows.Scan(&endpoint.ipAddress) + ipAddress += endpoint.ipAddress + "," + } + rows.Close() + sep := "," + IpAddress := strings.Split(ipAddress, sep) + return IpAddress +} + +func init() { + rand.Seed(time.Now().UnixNano()) +} + + +//洗牌算法 +func shuffle(n []string) { + for i := 0; i < len(n); i++ { + a := rand.Intn(len(n)) + b := rand.Intn(len(n)) + n[a], n[b] = n[b], n[a] + } +} + +//返回可进行运算的设备地址 +func request() []string { + var ipAddress = ipAddress() + for i := 0; i < 3; i++ { + shuffle(ipAddress) + } + return ipAddress +} \ No newline at end of file diff --git a/src/webserver/api/pc/nginx-rtmp.go b/src/webserver/pkg/api/pc/nginx-rtmp.go similarity index 100% rename from src/webserver/api/pc/nginx-rtmp.go rename to src/webserver/pkg/api/pc/nginx-rtmp.go diff --git a/src/webserver/api/pc/webserver.go b/src/webserver/pkg/api/pc/webserver.go similarity index 100% rename from src/webserver/api/pc/webserver.go rename to src/webserver/pkg/api/pc/webserver.go diff --git a/src/webserver/pkg/sql.go b/src/webserver/pkg/sql.go index 3befa86..33f6726 100644 --- a/src/webserver/pkg/sql.go +++ b/src/webserver/pkg/sql.go @@ -1,8 +1,10 @@ package pkg type Endpoint struct { - macAddress string //设备地址 + macAddress string //设备mac地址 + ipAddress string //设备IP地址 times int //贡献算力次数 + isOnLine int //设备登录时间 } type Average struct { diff --git a/src/webserver/routers/router.go b/src/webserver/routers/router.go index 99d640d..0a88402 100644 --- a/src/webserver/routers/router.go +++ b/src/webserver/routers/router.go @@ -3,8 +3,8 @@ package routers import ( "net/http" - android "application/api/android" - pc "application/api/pc" + android "webserver/api/android" + pc "webserver/api/pc" "github.com/gin-gonic/gin" ) diff --git a/src/webserver/static/js/mediaToWords.js b/src/webserver/static/js/mediaToWords.js new file mode 100644 index 0000000..0d39b5b --- /dev/null +++ b/src/webserver/static/js/mediaToWords.js @@ -0,0 +1,8 @@ +//获取手语视频 +func getMedia() {} + +//翻译视频为自然语言 +func mediaToWords() {} + +//显示自然语言 +func display() {} \ No newline at end of file diff --git a/src/webserver/static/js/homepage.js b/src/webserver/static/js/setting.js similarity index 100% rename from src/webserver/static/js/homepage.js rename to src/webserver/static/js/setting.js diff --git a/src/webserver/static/js/wordsToMedia.js b/src/webserver/static/js/wordsToMedia.js new file mode 100644 index 0000000..c4acf77 --- /dev/null +++ b/src/webserver/static/js/wordsToMedia.js @@ -0,0 +1,24 @@ +//输入自然语言 +function enterWords() { + words = infos.words.value + wordsToMedia(words) +} + +//翻译自然语言为动画 +function wordsToMedia(words) {} + +//显示动画 +function display(media) { + var xmlhttp; + if (window.XMLHttpRequest) xmlhttp=new XMLHttpRequest(); + else xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); + xmlhttp.onreadystatechange=function() + { + if (xmlhttp.readyState==4 && xmlhttp.status==200) { + if (xmlhttp.responseText=="成功") alert("删除成功!") + else alert("操作失败") + } + } + xmlhttp.open("GET","http://127.0.0.1:8000//"+"?user="+user+"&ps="+ps,true); + xmlhttp.send(); +} \ No newline at end of file