mirror of
https://github.com/IceWhaleTech/CasaOS.git
synced 2025-06-16 05:55:33 +00:00
Remove 9527 (#1162)
This commit is contained in:
parent
7ecfea71ab
commit
1aa8fb600b
69
main.go
69
main.go
@ -5,27 +5,21 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/model"
|
"github.com/IceWhaleTech/CasaOS-Common/model"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/constants"
|
||||||
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
|
||||||
"github.com/tidwall/gjson"
|
|
||||||
"golang.org/x/net/websocket"
|
|
||||||
|
|
||||||
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
|
util_http "github.com/IceWhaleTech/CasaOS-Common/utils/http"
|
||||||
|
|
||||||
"github.com/IceWhaleTech/CasaOS/codegen/message_bus"
|
"github.com/IceWhaleTech/CasaOS/codegen/message_bus"
|
||||||
"github.com/IceWhaleTech/CasaOS/common"
|
"github.com/IceWhaleTech/CasaOS/common"
|
||||||
model2 "github.com/IceWhaleTech/CasaOS/model"
|
|
||||||
"github.com/IceWhaleTech/CasaOS/pkg/cache"
|
"github.com/IceWhaleTech/CasaOS/pkg/cache"
|
||||||
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
"github.com/IceWhaleTech/CasaOS/pkg/config"
|
||||||
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
|
"github.com/IceWhaleTech/CasaOS/pkg/sqlite"
|
||||||
@ -86,7 +80,7 @@ func init() {
|
|||||||
service.GetCPUThermalZone()
|
service.GetCPUThermalZone()
|
||||||
|
|
||||||
route.InitFunction()
|
route.InitFunction()
|
||||||
go SendToSocket(service.MyService.System().GetDeviceInfo())
|
|
||||||
//service.MyService.System().GenreateSystemEntry()
|
//service.MyService.System().GenreateSystemEntry()
|
||||||
///
|
///
|
||||||
//service.MountLists = make(map[string]*mountlib.MountPoint)
|
//service.MountLists = make(map[string]*mountlib.MountPoint)
|
||||||
@ -108,7 +102,6 @@ func main() {
|
|||||||
if *versionFlag {
|
if *versionFlag {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go Special(service.MyService)
|
|
||||||
v1Router := route.InitV1Router()
|
v1Router := route.InitV1Router()
|
||||||
|
|
||||||
v2Router := route.InitV2Router()
|
v2Router := route.InitV2Router()
|
||||||
@ -238,63 +231,3 @@ func main() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func Special(myservice service.Repository) {
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
m := myservice.System().GetDeviceInfo()
|
|
||||||
jsonData, err := json.Marshal(m)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error:", err)
|
|
||||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
|
||||||
fmt.Fprintln(w, string(jsonData))
|
|
||||||
})
|
|
||||||
|
|
||||||
if err := http.ListenAndServe(":9527", nil); err != nil {
|
|
||||||
fmt.Println("Error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func SendToSocket(m model2.DeviceInfo) {
|
|
||||||
if len(m.DeviceSN) == 0 {
|
|
||||||
//TODO:需要放开sn的判断
|
|
||||||
//return
|
|
||||||
}
|
|
||||||
by, _ := json.Marshal(m)
|
|
||||||
base64Str := base64.StdEncoding.EncodeToString(by)
|
|
||||||
var count int = 1
|
|
||||||
for i := 0; i < 10; i++ {
|
|
||||||
wsURL := fmt.Sprintf("wss://%s/server/zima%s", "www.findzima.com/ws", "?device="+base64Str)
|
|
||||||
ws, err := websocket.Dial(wsURL, "", "http://localhost")
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("connect websocket err"+strconv.Itoa(i), zap.Any("error", err))
|
|
||||||
time.Sleep(time.Second * 1)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
defer ws.Close()
|
|
||||||
|
|
||||||
logger.Info("subscribed to", zap.Any("url", wsURL))
|
|
||||||
for {
|
|
||||||
msg := make([]byte, 1024)
|
|
||||||
n, err := ws.Read(msg)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("err", zap.Any("err", err.Error()))
|
|
||||||
break
|
|
||||||
}
|
|
||||||
message := msg[:n]
|
|
||||||
t := gjson.GetBytes(message, "type")
|
|
||||||
if t.Str == "ping" {
|
|
||||||
ws.Write([]byte(`{"type":"pong"}`))
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
if count > 600 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
logger.Error("error when try to connect to message bus")
|
|
||||||
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user