97 lines
2.1 KiB
Go
97 lines
2.1 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"github.com/r3labs/sse/v2"
|
||
|
"golang.org/x/text"
|
||
|
"gopkg.in/toast.v1"
|
||
|
"log"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
deviceId := os.Getenv("DEVICE_ID")
|
||
|
if deviceId == "" {
|
||
|
println("DEVICE_ID env is not set")
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
pushNotification("连接", "开始连接。")
|
||
|
|
||
|
for {
|
||
|
fmt.Println("开始连接。")
|
||
|
pushNotification("连接", "开始连接。")
|
||
|
client := sse.NewClient("https://ivampiresp.com/wp-content/rCMD/sse.php?device=" + deviceId)
|
||
|
|
||
|
err := client.Subscribe("message", func(msg *sse.Event) {
|
||
|
//fmt.Println(msg.Data)
|
||
|
|
||
|
// byte to json
|
||
|
var data map[string]interface{}
|
||
|
err := json.Unmarshal(msg.Data, &data)
|
||
|
|
||
|
//fmt.Println(data)
|
||
|
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if data["cmd"] == nil || data["cmd"] == "" {
|
||
|
//fmt.Println("cmd or params is nil")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// interface to string
|
||
|
command := fmt.Sprintf("%v", data["cmd"])
|
||
|
params := fmt.Sprintf("%v", data["param"])
|
||
|
|
||
|
if command != "press" {
|
||
|
fmt.Println("此程序只作为演示工具使用,只处理 press 情况,但是意外的接收到了 " + command + ", 所以将不会处理。")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if params == "back" {
|
||
|
pushNotification("返回", "返回。")
|
||
|
}
|
||
|
})
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
// 下个循环重新连接
|
||
|
continue
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
func pushNotification(title string, message string) {
|
||
|
//notification := toast.Notification{
|
||
|
// AppID: "手势操作",
|
||
|
// Title: title,
|
||
|
// Message: message,
|
||
|
// //Icon: "go.png"
|
||
|
// //Actions: []toast.Action{
|
||
|
// // {"protocol", "I'm a button", ""},
|
||
|
// // {"protocol", "Me too!", ""},
|
||
|
// //},
|
||
|
//}
|
||
|
|
||
|
utf8Reader := transform.NewReader(resp.Body, simplifiedchinese.GBK.NewDecoder())
|
||
|
|
||
|
notification := toast.Notification{
|
||
|
AppID: "ExampleApp",
|
||
|
Title: title,
|
||
|
Message: "Some message about how important something is...",
|
||
|
//Icon: "C:\\project\\notification\\notification\\assets\\icon.png",
|
||
|
//Actions: []toast.Action{
|
||
|
// {Type: "protocol", Label: "I'm a button", Arguments: ""},
|
||
|
// {Type: "protocol", Label: "Me too!", Arguments: ""},
|
||
|
//},
|
||
|
}
|
||
|
err := notification.Push()
|
||
|
if err != nil {
|
||
|
log.Fatalln(err)
|
||
|
}
|
||
|
|
||
|
}
|