diff --git a/src/webserver/api/android/media_pipe.go b/src/webserver/api/android/media_pipe.go index e69de29..3117155 100644 --- a/src/webserver/api/android/media_pipe.go +++ b/src/webserver/api/android/media_pipe.go @@ -0,0 +1,47 @@ +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/android/word_send.go b/src/webserver/api/android/word_send.go index e69de29..f9cda2e 100644 --- a/src/webserver/api/android/word_send.go +++ b/src/webserver/api/android/word_send.go @@ -0,0 +1,41 @@ +package android + +import ( + "fmt" + "database/sql" + _ "go-sqlite3-master" + "application/pkg/app" + + "github.com/gin-gonic/gin" +) + +func updateAverage (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(`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`) + defer rows.Close() + var countOfMacAddress int + rows.Next() + rows.Scan(&countOfMacAddress) + db.Exec(`update Average set average = ?/?`sendMacAddress, countOfMacAddress) + appG.Response(http.StatusOK, "成功", null) +} + +func wordSend (c *gin.Context) { + appG := app.Gin{C: c} + //这里获取转化好的数据 + data := mediaPipe().data + updateAverage() + 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 index e69de29..74df7f2 100644 --- a/src/webserver/api/pc/load_alancing.go +++ b/src/webserver/api/pc/load_alancing.go @@ -0,0 +1,54 @@ +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/main.go b/src/webserver/main.go index dee48c7..2aa77d1 100644 --- a/src/webserver/main.go +++ b/src/webserver/main.go @@ -4,18 +4,11 @@ import ( "fmt" "log" "net/http" - "time" "application/routers" ) func main() { - timeLocal, err := time.LoadLocation("Asia/Shanghai") - if err != nil { - log.Printf("时区设置失败 %s", err) - } - time.Local = timeLocal - endPoint := fmt.Sprintf("0.0.0.0:%d", 8000) server := &http.Server{ Addr: endPoint, diff --git a/src/webserver/pkg/app/response.go b/src/webserver/pkg/app/response.go index 40b8e13..80b714d 100644 --- a/src/webserver/pkg/app/response.go +++ b/src/webserver/pkg/app/response.go @@ -1,4 +1,4 @@ -package app +package pkg import ( "github.com/gin-gonic/gin" diff --git a/src/webserver/pkg/sql.go b/src/webserver/pkg/sql.go new file mode 100644 index 0000000..3befa86 --- /dev/null +++ b/src/webserver/pkg/sql.go @@ -0,0 +1,10 @@ +package pkg + +type Endpoint struct { + macAddress string //设备地址 + times int //贡献算力次数 +} + +type Average struct { + average int //平均使用次数 +} \ No newline at end of file