forked from fforchino/vector-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sdk-wrapper-settings.go
251 lines (218 loc) · 6.96 KB
/
sdk-wrapper-settings.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package sdk_wrapper
import (
"bytes"
"encoding/json"
"image/color"
"log"
"net/http"
"os"
"strings"
"github.com/PerformLine/go-stockutil/colorutil"
"github.com/kirillgrishin-tech/vector-go-sdk/pkg/vectorpb"
)
type CustomSettings struct {
RobotName string `json:"RobotName"`
ChatTarget string `json:"ChatTarget"`
LoggedInToChat bool `json:"LoggedInToChat"`
LastChatMessageRead int32 `json:"LastChatMessageRead"`
TTSEngine int `json:"TTSEngine"`
TTSVoice string `json:"TTSVoice"`
CurrentGamedata string `json:"CurrentGamedata"` // Put current game JSON data here, so that it will be preserved across multiple runs
}
var settings map[string]interface{}
var customSettings CustomSettings
/*
{
"button_wakeword" : 0,
"clock_24_hour" : false,
"custom_eye_color" : {
"enabled" : false,
"hue" : 0,
"saturation" : 0
},
"default_location" : "San Francisco, California, United States",
"dist_is_metric" : true,
"eye_color" : 0,
"locale" : "en-US",
"master_volume" : 5,
"temp_is_fahrenheit" : false,
"time_zone" : "Europe/Paris"
}
*/
func RefreshSDKSettings() error {
settingsJSON, err := getSDKSettings()
if err != nil {
log.Println("ERROR: Could not load Vector settings from JDOCS")
return err
}
customSettingsJSON, err := getCustomSettings()
if err != nil {
log.Println("WARNING: Could not load Vector custom settings, creating a blank file")
customSettings = CustomSettings{
RobotName: "",
ChatTarget: "",
LoggedInToChat: false,
TTSEngine: TTS_ENGINE_NATIVE,
TTSVoice: TTS_ENGINE_VOICESERVER_VOICE_ENGLISH_DEFAULT,
}
}
//println(string(customSettingsJSON))
//println(string(settingsJSON))
json.Unmarshal([]byte(settingsJSON), &settings)
json.Unmarshal([]byte(customSettingsJSON), &customSettings)
refreshLanguage()
return nil
}
func GetVectorSettings() map[string]interface{} {
RefreshSDKSettings()
return settings
}
func GetEyeColor() color.RGBA {
eyeColor := color.RGBA{0, 0, 0, 0xff}
presetEyeColor := settings["eye_color"].(float64)
var customEyeColor map[string]interface{} = (settings["custom_eye_color"]).(map[string]interface{})
customEyeColorEnabled := customEyeColor["enabled"].(bool)
if customEyeColorEnabled {
customEyeColorHue := customEyeColor["hue"].(float64)
customEyeColorSaturation := customEyeColor["saturation"].(float64)
//println(fmt.Sprintf("HUE/SAT: %0f,%f", customEyeColorHue, customEyeColorSaturation))
eyeColor.R, eyeColor.G, eyeColor.B = colorutil.HslToRgb(customEyeColorHue*360, customEyeColorSaturation, 0.5)
} else {
switch presetEyeColor {
case float64(vectorpb.EyeColor_value["TIP_OVER_TEAL"]):
eyeColor = color.RGBA{0x29, 0xae, 0x70, 0xff}
break
case float64(vectorpb.EyeColor_value["OVERFIT_ORANGE"]):
eyeColor = color.RGBA{0xfe, 0x76, 0x14, 0xff}
break
case float64(vectorpb.EyeColor_value["UNCANNY_YELLOW"]):
eyeColor = color.RGBA{0xf7, 0xcb, 0x04, 0xff}
break
case float64(vectorpb.EyeColor_value["NON_LINEAR_LIME"]):
eyeColor = color.RGBA{0xa8, 0xd3, 0x04, 0xff}
break
case float64(vectorpb.EyeColor_value["SINGULARITY_SAPPHIRE"]):
eyeColor = color.RGBA{0x0d, 0x97, 0xf0, 0xff}
break
case float64(vectorpb.EyeColor_value["FALSE_POSITIVE_PURPLE"]):
eyeColor = color.RGBA{0xc6, 0x61, 0xfc, 0xff}
break
case float64(vectorpb.EyeColor_value["CONFUSION_MATRIX_GREEN"]):
// Color unknown
eyeColor = color.RGBA{0x00, 0xff, 0x00, 0xff}
break
}
}
//println(fmt.Sprintf("%02x,%02x,%02x", eyeColor.R, eyeColor.G, eyeColor.B))
return eyeColor
}
func GetTemperatureUnit() string {
unit := WEATHER_UNIT_CELSIUS
isFaranheit := settings["temp_is_fahrenheit"].(bool)
if isFaranheit {
unit = WEATHER_UNIT_FARANHEIT
}
return unit
}
func SetCustomEyeColor(hue string, sat string) {
payload := `{"custom_eye_color": {"enabled": true, "hue": ` + hue + `, "saturation": ` + sat + `} }`
setSettingSDKStringHelper(payload)
}
func SetPresetEyeColor(value string) {
payload := `{"custom_eye_color": {"enabled": false}, "eye_color": ` + value + `}`
setSettingSDKStringHelper(payload)
}
func SetLocale(locale string) {
SetSettingSDKstring("locale", locale)
}
func GetLocale() string {
locale := settings["locale"].(string)
return locale
}
func SetLocation(location string) {
SetSettingSDKstring("default_location", location)
}
func SetTimeZone(timezone string) {
SetSettingSDKstring("time_zone", timezone)
}
func SetRobotName(name string) {
customSettings.RobotName = name
SaveCustomSettings()
}
func GetRobotName() string {
return customSettings.RobotName
}
func SetChatTarget(name string) {
customSettings.ChatTarget = name
SaveCustomSettings()
}
func GetChatTarget() string {
return customSettings.ChatTarget
}
func SetTTSConfiguration(ttsEngine int, ttsVoice string) {
customSettings.TTSEngine = ttsEngine
customSettings.TTSVoice = ttsVoice
refreshLanguage()
SaveCustomSettings()
}
func GetTTSConfiguration() (int, string) {
return customSettings.TTSEngine, customSettings.TTSVoice
}
func GetCurrentGameData() string {
return customSettings.CurrentGamedata
}
func SetCurrentGameData(gameData string) {
customSettings.CurrentGamedata = gameData
SaveCustomSettings()
}
func GetCustomSettings() *CustomSettings {
return &customSettings
}
func SaveCustomSettings() {
refreshLanguage()
file, _ := json.Marshal(GetCustomSettings())
_ = os.WriteFile(GetMyStoragePath("custom_settings.json"), file, 0644)
}
func SetSettingSDKstring(setting string, value string) {
payload := `{"` + setting + `": "` + value + `" }`
setSettingSDKStringHelper(payload)
RefreshSDKSettings()
}
/********************************************************************************************************/
/* PRIVATE FUNCTIONS */
/********************************************************************************************************/
func getSDKSettings() ([]byte, error) {
resp, err := Robot.Conn.PullJdocs(ctx, &vectorpb.PullJdocsRequest{
JdocTypes: []vectorpb.JdocType{vectorpb.JdocType_ROBOT_SETTINGS},
})
if err != nil {
return nil, err
}
json := resp.NamedJdocs[0].Doc.JsonDoc
return []byte(json), nil
}
func setSettingSDKStringHelper(payload string) {
if !strings.Contains(Robot.Cfg.Token, "error") {
url := "https://" + Robot.Cfg.Target + "/v1/update_settings"
var updateJSON = []byte(`{"update_settings": true, "settings": ` + payload + ` }`)
req, _ := http.NewRequest("POST", url, bytes.NewBuffer(updateJSON))
req.Header.Set("Authorization", "Bearer "+Robot.Cfg.Token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{Transport: transCfg}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
} else {
log.Println("GUID not there")
}
RefreshSDKSettings()
}
func getCustomSettings() ([]byte, error) {
json, err := os.ReadFile(GetMyStoragePath("custom_settings.json"))
if err != nil {
return nil, err
}
return []byte(json), nil
}