-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
169 lines (160 loc) · 2.63 KB
/
Copy pathmain.go
File metadata and controls
169 lines (160 loc) · 2.63 KB
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
package main
import "fmt"
const (
S1 = iota
S2
S3
S3Sub1
s3Sub2
S3Out
S2Sub1
S2Sub2
S2Sub3
S2Out
)
var PS = S1
var NS = S1
var S3PS = S3Sub1
var S3NS = S3Sub1
var S2PS = S2Sub1
var S2NS = S2Sub1
var e string
var c Cooler
var h Heater
func main() {
c = Cooler{}
h = Heater{}
for true {
switch PS {
case S1:
stateName("S1")
fmt.Println("Actions:")
c.TurnOff()
h.TurnOff()
fmt.Println("valid events: t<15 or t>35 ")
e = getEvent()
if e == "t<15" {
NS = S3
} else if e == "t>35" {
NS = S2
} else {
errorOutput()
}
case S2:
CoolerSuperState(true)
if e == "t<25" {
NS = S1
} else {
errorOutput()
}
case S3:
HeaterSuperState(true)
if e == "t>30" {
NS = S1
} else {
errorOutput()
}
}
PS = NS
}
}
func HeaterSuperState(start bool) {
for S3PS != S3Out {
switch S3PS {
case S3Sub1:
stateName("S3_Sub1")
fmt.Println("Actions:")
if start {
h.TurnOn()
c.TurnOff()
start = false
}
h.SetLevel("low")
fmt.Println("valid events: t<10 or t>30 ")
e = getEvent()
if e == "t<10" {
S3NS = s3Sub2
} else if e == "t>30" {
S3NS = S3Out
} else {
errorOutput()
}
case s3Sub2:
stateName("S3_Sub2")
fmt.Println("Actions:")
h.SetLevel("high")
fmt.Println("valid events: t>15 ")
e = getEvent()
if e == "t>15" {
S3NS = S3Sub1
} else {
errorOutput()
}
}
S3PS = S3NS
}
S3PS = S3Sub1
}
func CoolerSuperState(start bool) {
for S2PS != S2Out {
switch S2PS {
case S2Sub1:
stateName("S2_Sub1")
fmt.Println("Actions:")
if start {
h.TurnOff()
c.TurnOn()
start = false
}
c.SetLevel("4RPS")
fmt.Println("valid events: t>40 or t<25 ")
e = getEvent()
if e == "t>40" {
S2NS = S2Sub2
} else if e == "t<25" {
S2NS = S2Out
} else {
errorOutput()
}
case S2Sub2:
stateName("S2_Sub2")
fmt.Println("Actions:")
c.SetLevel("6RPS")
fmt.Println("valid events: t>45 or t<35 ")
e = getEvent()
if e == "t>45" {
S2NS = S2Sub3
} else if e == "t<35" {
S2NS = S2Sub1
} else {
errorOutput()
}
case S2Sub3:
stateName("S2_Sub3")
fmt.Println("Actions:")
c.SetLevel("8RPS")
fmt.Println("valid events: t<40 ")
e = getEvent()
if e == "t<40" {
S2NS = S2Sub2
} else {
errorOutput()
}
}
S2PS = S2NS
}
S2PS = S2Sub1
}
func getEvent() string {
fmt.Printf("\tEnter an event: ")
var event string
fmt.Scanln(&event)
fmt.Println()
return event
}
func stateName(name string) {
fmt.Println("Current state: " + Green + name + Reset)
}
func errorOutput() {
fmt.Println("this event couldn't be handled!\n")
}