Skip to content
This repository has been archived by the owner on Oct 16, 2024. It is now read-only.

Commit

Permalink
优个化吧
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Aug 11, 2024
1 parent e4fe77e commit 81f8c78
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 27 deletions.
27 changes: 1 addition & 26 deletions docs/protocol_fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,33 +124,8 @@
```


## 7、查看磁盘列表
```json5
{
"module": "fs",
"command": "disk",
}
```

响应

```json5
{
"result": "ok/fail", //结果
"reason": "", //错误原因
"data": [
{
"disk": "/sd0",
"total": 1300000,
"used": 100232,
"free": 1200030,
}
]
}
```


## 8、格式化
## 7、格式化
```json5
{
"module": "fs",
Expand Down
14 changes: 13 additions & 1 deletion rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"net/url"
)

const CLIENT_BUFFER_SIZE = 1024

type Client struct {
Url string

Expand All @@ -17,6 +19,10 @@ type Client struct {
id uint16
conn net.Conn
requests map[uint16]any

inHeader [8]byte
outHeader [8]byte
buffer [CLIENT_BUFFER_SIZE]byte
}

func (c *Client) Write(pack *Pack) error {
Expand Down Expand Up @@ -54,7 +60,13 @@ func (c *Client) receive() {

l := int(binary.BigEndian.Uint16(buf[6:])) //长度
if l > 0 {
b := make([]byte, l)
var b []byte
if l > CLIENT_BUFFER_SIZE {
b = make([]byte, l)
} else {
b = c.buffer[:] //使用默认buffer
}

//_ = c.conn.SetReadDeadline(time.Now().Add(time.Second * 30))
n, err = io.ReadAtLeast(c.conn, b, l)
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions rpc/device.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package rpc

type DeviceId struct {
Id string `json:"id"`
}

type DeviceItem struct {
Id string `json:"id"`
Name string `json:"name"`
ProductId string `json:"product_id"`
Station map[string]any `json:"station,omitempty"`
}

type DeviceListResponse []DeviceItem

type DeviceCreateRequest DeviceItem

type DeviceDeleteRequest DeviceId

type DevicePropertyRequest struct {
Id string `json:"id"`
ProductId string `json:"product_id,omitempty"`
Properties map[string]any `json:"properties"`
}

type DevicePropertyModifyRequest struct {
Id string `json:"id"`
Properties map[string]any `json:"properties"`
}

type DeviceEventRequest struct {
Id string `json:"id"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
Level int `json:"level,omitempty"`
}

type DeviceActionRequest struct {
Id string `json:"id"`
Name string `json:"name"`
Parameters map[string]any `json:"parameters"`
}

type DeviceActionResponse struct {
Return map[string]any `json:"return,omitempty"`
}
44 changes: 44 additions & 0 deletions rpc/fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package rpc

type FsPath struct {
Path string `json:"path"`
}

type FsPathMove struct {
Path string `json:"path"`
Move string `json:"move"`
}

type FsItem struct {
Name string `json:"name"`
Dir bool `json:"dir,omitempty"`
Size int64 `json:"size,omitempty"`
Time int64 `json:"time,omitempty"`
}

type FsStream struct {
Stream uint16 `json:"stream"`
}

type FsSearchRequest FsPath

type FsSearchResponse []FsItem

type FsDownloadRequest FsPath

type FsDownloadResponse FsStream

type FsUploadRequest FsPath

type FsUploadResponse FsStream

type FsDeleteRequest FsPath

type FsMoveRequest FsPathMove

type FsMakeDirectoryRequest FsPath

type FsFormatRequest struct {
Disk string `json:"disk"`
Type string `json:"type"`
}
27 changes: 27 additions & 0 deletions rpc/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,33 @@ func RegisterEncoding(typ uint8, encoder Encoder, decoder Decoder) {
decoders[typ] = decoder
}

type Connect struct {
Username string `json:"username"`
Password string `json:"password"`
}

type ConnectAgain struct {
Id string `json:"id"`
Token string `json:"token"`
}

type ConnectAck struct {
Id string `json:"id"`
Token string `json:"token"`
}

type Request[T any] struct {
Module string `json:"module"`
Command string `json:"command"`
Data T `json:"data,omitempty"`
}

type Response[T any] struct {
Result string `json:"result"`
Reason string `json:"reason,omitempty"`
Data T `json:"data,omitempty"`
}

type Pack struct {
Type uint8
Encoding uint8
Expand Down
1 change: 1 addition & 0 deletions rpc/payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package rpc

0 comments on commit 81f8c78

Please sign in to comment.