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 0000000..65142ad Binary files /dev/null and b/src/webserver/info.db differ 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