-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.go
158 lines (130 loc) · 4.06 KB
/
script.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package lxlib
import (
"encoding/json"
"github.com/lxbot/lxlib/v2/common"
"github.com/lxbot/lxlib/v2/lxtypes"
"github.com/mitchellh/mapstructure"
)
type (
Script struct {
common *common.LxCommon
events map[string]Event
eventCh *chan *lxtypes.Event
messageCh *chan *lxtypes.Message
}
Event struct {
eventCh *chan *lxtypes.Event
}
)
func NewScript() (*Script, *chan *lxtypes.Message) {
common.TraceLog("(script)", "lxlib.NewScript()", "start")
defer common.TraceLog("(script)", "lxlib.NewScript()", "end")
messageCh := make(chan *lxtypes.Message)
eventCh := make(chan *lxtypes.Event)
c := common.NewLxCommon()
script := &Script{
common: c,
events: make(map[string]Event),
eventCh: &eventCh,
messageCh: &messageCh,
}
go c.Listen(&eventCh)
go script.listen()
script.Raw(lxtypes.NewEvent(lxtypes.ReadyEvent, lxtypes.ReadyEventPayload{
Mode: lxtypes.StdIOMode,
Endpoint: "",
}))
return script, &messageCh
}
func (this *Script) listen() {
common.TraceLog("(script)", "lxlib.listen()", "start")
defer common.TraceLog("(script)", "lxlib.listen()", "end")
for {
common.TraceLog("(script)", "lxlib.listen()", "waiting event...")
eventPtr := <-*this.eventCh
switch eventPtr.Event {
case lxtypes.IncomingMessageEvent:
json := eventPtr.Payload.(json.RawMessage)
common.TraceLog("(script)", "lxlib.listen()", "event received", "type:", eventPtr.Event)
payload, err := common.FromJSON(json)
if err != nil {
common.ErrorLog(err)
continue
}
message, err := lxtypes.NewLXMessage(payload)
if err != nil {
common.ErrorLog(err)
continue
}
*this.messageCh <- message
case lxtypes.GetStorageEvent:
common.TraceLog("(script)", "lxlib.listen()", "event received", "type:", eventPtr.Event)
if event, ok := this.events[eventPtr.ID]; ok {
common.TraceLog("(script)", "lxlib.listen()", "found registered event", "id:", eventPtr.ID)
*event.eventCh <- eventPtr
}
default:
common.TraceLog("(script)", "lxlib.listen()", "unknown event received", "type:", eventPtr.Event)
}
}
}
func (this *Script) Raw(event *lxtypes.Event) {
common.TraceLog("(script)", "lxlib.Raw()", "start")
defer common.TraceLog("(script)", "lxlib.Raw()", "end")
go this.common.Send(event)
}
func (this *Script) SendMessage(message *lxtypes.Message) error {
common.TraceLog("(script)", "lxlib.SendMessage()", "start")
defer common.TraceLog("(script)", "lxlib.SendMessage()", "end")
m, err := message.ToMap()
if err != nil {
return err
}
common.TraceLog("(script)", "lxlib.SendMessage()", "payload:", m)
this.Raw(lxtypes.NewEvent(lxtypes.OutgoingMessageEvent, m))
return nil
}
func (this *Script) GetStorage(key string) interface{} {
common.TraceLog("(script)", "lxlib.GetStorage()", "start")
defer common.TraceLog("(script)", "lxlib.GetStorage()", "end")
event := lxtypes.NewEvent(lxtypes.GetStorageEvent, lxtypes.KV{
Key: key,
})
common.TraceLog("(script)", "lxlib.GetStorage()", "payload:", event.Payload)
eventCh := make(chan *lxtypes.Event)
this.events[event.ID] = Event{
eventCh: &eventCh,
}
defer delete(this.events, event.ID)
this.Raw(event)
common.TraceLog("(script)", "lxlib.GetStorage()", "waiting response...")
result := <-eventCh
json := result.Payload.(json.RawMessage)
payload, err := common.FromJSON(json)
if err != nil {
common.WarnLog(err)
}
resultKV := new(lxtypes.KV)
if err := mapstructure.WeakDecode(payload, resultKV); err != nil {
common.WarnLog(err)
resultKV = &lxtypes.KV{
Key: key,
Value: nil,
}
}
common.TraceLog("(script)", "lxlib.GetStorage()", "response received", "result:", resultKV)
return resultKV.Value
}
func (this *Script) SetStorage(key string, value interface{}) {
common.TraceLog("(script)", "lxlib.SetStorage()", "start")
defer common.TraceLog("(script)", "lxlib.SetStorage()", "end")
this.Raw(lxtypes.NewEvent(lxtypes.SetStorageEvent, lxtypes.KV{
Key: key,
Value: value,
}))
}
func (this *Script) Close() {
common.TraceLog("(script)", "lxlib.Close()", "start")
defer common.TraceLog("(script)", "lxlib.Close()", "end")
go this.common.Close()
}