-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_status.go
123 lines (98 loc) · 2.23 KB
/
env_status.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
package main
//refer : https://gist.github.com/ImJasonH/6713e855b431a0c067afea4b74cbf504
import (
"encoding/json"
"io/ioutil"
"log"
"sync"
"github.com/sibeshkar/jiminy-env/utils"
"github.com/spf13/viper"
)
type EnvState struct {
//access lock for envID
EnvId string `json:"env_id"`
//access lock for status
sync.Mutex
EnvStatus string `json:"env_status"`
EpisodeId int64 `json:"episode_id"`
Fps float32 `json:"fps"`
}
//Creates new EnvState struct type to store, environment ID, env status,
//episode ID and global FPS.
func NewEnvState(EnvId string, EnvStatus string, EpisodeId int64, Fps float32) (*EnvState, error) {
var err error //replace with proper function for errors later
envStatus := EnvState{
EnvId: EnvId,
EnvStatus: EnvStatus,
EpisodeId: EpisodeId,
Fps: Fps,
}
envStatus.WriteEnvState()
return &envStatus, err
}
func (e *EnvState) LoadEnvState() {
e.Lock()
defer e.Unlock()
viper.SetConfigName("env")
viper.AddConfigPath(utils.TempDir())
viper.SetConfigType("json")
if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
err := viper.Unmarshal(e)
if err != nil {
log.Fatalf("unable to decode into struct, %v", err)
}
}
func (e *EnvState) WriteEnvState() {
file, _ := json.MarshalIndent(e, "", " ")
_ = ioutil.WriteFile(utils.TempDir()+"env.json", file, 0644)
}
func (e *EnvState) SetEnvId(EnvId string) bool {
e.Lock()
defer e.Unlock()
e.EnvId = EnvId
e.WriteEnvState()
return true
}
func (e *EnvState) SetEnvStatus(EnvStatus string) bool {
e.Lock()
defer e.Unlock()
e.EnvStatus = EnvStatus
e.WriteEnvState()
return true
}
func (e *EnvState) SetEpisodeId(EpisodeId int64) bool {
e.Lock()
defer e.Unlock()
e.EpisodeId = EpisodeId
e.WriteEnvState()
return true
}
func (e *EnvState) SetFps(Fps float32) bool {
e.Lock()
defer e.Unlock()
e.Fps = Fps
e.WriteEnvState()
return true
}
func (e *EnvState) GetEnvId() string {
e.Lock()
defer e.Unlock()
return e.EnvId
}
func (e *EnvState) GetEnvStatus() string {
e.Lock()
defer e.Unlock()
return e.EnvStatus
}
func (e *EnvState) GetEpisodeId() int64 {
e.Lock()
defer e.Unlock()
return e.EpisodeId
}
func (e *EnvState) GetFps() float32 {
e.Lock()
defer e.Unlock()
return e.Fps
}