feat: 实现电脑端被控与控制界面
This commit is contained in:
@@ -0,0 +1,157 @@
|
||||
// Package protocol 是设备之间的消息正文。定时和离线保留不写进正文。
|
||||
package protocol
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
const Version = 1
|
||||
|
||||
const (
|
||||
TypeShell = "shell"
|
||||
TypeShortcut = "shortcut"
|
||||
TypeText = "text"
|
||||
TypeShortcuts = "shortcuts"
|
||||
TypeShortcutsGet = "shortcuts_get"
|
||||
TypeResult = "result"
|
||||
TypeLogs = "logs"
|
||||
TypeLogsGet = "logs_get"
|
||||
)
|
||||
|
||||
const ErrNotTrusted = "not_trusted"
|
||||
|
||||
// Shortcut 是一条快捷命令。
|
||||
type Shortcut struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Shell string `json:"shell"`
|
||||
Confirm bool `json:"confirm"`
|
||||
}
|
||||
|
||||
// Log 是一条执行记录。
|
||||
type Log struct {
|
||||
At string `json:"at"`
|
||||
Summary string `json:"summary"`
|
||||
OK bool `json:"ok"`
|
||||
Detail string `json:"detail"`
|
||||
}
|
||||
|
||||
// Message 是收到的正文。不认识的字段忽略。
|
||||
type Message struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Shell string `json:"shell"`
|
||||
ShortcutID string `json:"shortcut_id"`
|
||||
Text string `json:"text"`
|
||||
Items []Shortcut `json:"items"`
|
||||
Logs []Log `json:"items_logs"`
|
||||
OK *bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
Duplicate bool `json:"duplicate"`
|
||||
}
|
||||
|
||||
// Parse 解析正文。logs 与 shortcuts 都使用 items,由类型区分。
|
||||
func Parse(data string) (Message, error) {
|
||||
var m Message
|
||||
if err := json.Unmarshal([]byte(data), &m); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if m.Type == TypeLogs {
|
||||
var wrap struct {
|
||||
Items []Log `json:"items"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(data), &wrap); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
m.Logs = wrap.Items
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// ResultBody 编码执行结果。
|
||||
func ResultBody(id string, ok bool, errText string, duplicate bool) ([]byte, error) {
|
||||
r := struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Duplicate bool `json:"duplicate,omitempty"`
|
||||
}{V: Version, Type: TypeResult, ID: id, OK: ok, Error: errText, Duplicate: duplicate}
|
||||
return json.Marshal(r)
|
||||
}
|
||||
|
||||
// ShellBody 编码临时命令或本端快捷命令。
|
||||
func ShellBody(id, shell string) ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Shell string `json:"shell"`
|
||||
}{Version, TypeShell, id, shell})
|
||||
}
|
||||
|
||||
// ShortcutBody 编码执行远端快捷命令的请求。
|
||||
func ShortcutBody(id, shortcutID string) ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
ShortcutID string `json:"shortcut_id"`
|
||||
}{Version, TypeShortcut, id, shortcutID})
|
||||
}
|
||||
|
||||
// TextBody 编码文字。
|
||||
func TextBody(id, text string) ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
Text string `json:"text"`
|
||||
}{Version, TypeText, id, text})
|
||||
}
|
||||
|
||||
// ShortcutsBody 编码快捷命令列表。id 用来对应一次拉取。
|
||||
func ShortcutsBody(id string, items []Shortcut) ([]byte, error) {
|
||||
if items == nil {
|
||||
items = []Shortcut{}
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id,omitempty"`
|
||||
Items []Shortcut `json:"items"`
|
||||
}{Version, TypeShortcuts, id, items})
|
||||
}
|
||||
|
||||
// ShortcutsGetBody 向远端要它发布的快捷命令。
|
||||
func ShortcutsGetBody(id string) ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
}{Version, TypeShortcutsGet, id})
|
||||
}
|
||||
|
||||
// LogsGetBody 向在线的对方要本地长日志。
|
||||
func LogsGetBody(id string) ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id"`
|
||||
}{Version, TypeLogsGet, id})
|
||||
}
|
||||
|
||||
// LogsBody 编码日志列表。
|
||||
func LogsBody(id string, ok bool, errText string, items []Log) ([]byte, error) {
|
||||
if items == nil {
|
||||
items = []Log{}
|
||||
}
|
||||
return json.Marshal(struct {
|
||||
V int `json:"v"`
|
||||
Type string `json:"type"`
|
||||
ID string `json:"id,omitempty"`
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Items []Log `json:"items"`
|
||||
}{Version, TypeLogs, id, ok, errText, items})
|
||||
}
|
||||
Reference in New Issue
Block a user