-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.go
69 lines (60 loc) · 1.75 KB
/
prefs.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/chinenual/synergize/logger"
)
type Preferences struct {
LibraryPath string
OscAutoConfig bool
OscCSurfaceAddress string
OscCSurfacePort uint
OscPort uint
SerialBaud uint
SerialPort string
SerialFlowControl bool
UseOsc bool
UseSerial bool
// hidden from user
HTTPDebug bool
}
var preferencesPathname = getWorkingDirectory() + "/preferences.json"
var prefsUserPreferences = Preferences{
OscAutoConfig: false,
OscCSurfacePort: 9000,
OscPort: 8000,
SerialBaud: 9600,
SerialFlowControl: true,
UseOsc: false,
UseSerial: true,
}
func prefsLoadPreferences() (err error) {
var b []byte
if b, err = ioutil.ReadFile(preferencesPathname); err != nil {
logger.Errorf("Error loading preferences. Using defaults %#v: %v", prefsUserPreferences, err)
return
}
if err = json.Unmarshal(b, &prefsUserPreferences); err != nil {
logger.Errorf("Error parsing preferences. Using defaults %#v: %v", prefsUserPreferences, err)
return
}
logger.Infof("Loaded preferences %#v from file %s\n", prefsUserPreferences, preferencesPathname)
return
}
func prefsSavePreferences() (err error) {
var b []byte
if b, err = json.MarshalIndent(prefsUserPreferences, "", " "); err != nil {
logger.Error("Error saving preferences", err)
}
logger.Infof("Save preferences %#v to file %s\n", prefsUserPreferences, preferencesPathname)
if err = ioutil.WriteFile(preferencesPathname, b, 0644); err != nil {
logger.Error("Error saving preferences", err)
}
return
}
func prefsSynergyName() string {
hostname, _ := os.Hostname()
return fmt.Sprintf("%s [%s]", hostname, prefsUserPreferences.SerialPort)
}