-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
208 lines (186 loc) · 4.65 KB
/
main.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
package main
import (
mqtt "github.com/eclipse/paho.mqtt.golang"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"smart-analog-intercom/baresip"
"smart-analog-intercom/gpiowrapper"
mqtt_client "smart-analog-intercom/mqtt-client"
"smart-analog-intercom/utils"
"sync"
"time"
)
type Call struct {
mutex *sync.Mutex
active bool
StartedAt time.Time
BaresipCli *baresip.BaresipClient
}
func (c *Call) Toggle(number string) error {
if c.active && c.StartedAt.Before(time.Now().Add(-time.Second*5)) {
return c.Hangup()
}
if !c.active {
return c.Dial(number)
}
return nil
}
func (c *Call) Dial(number string) error {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Info().Msgf("dial %s", number)
if err := c.BaresipCli.Dial(number); err != nil {
c.BaresipCli.Play("error.wav")
return err
}
c.active = true
c.StartedAt = time.Now()
c.BaresipCli.Play("ringback.wav")
return nil
}
func (c *Call) Hangup() error {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Info().Msg("hangup")
if err := c.BaresipCli.Hangup(); err != nil {
return err
}
c.active = false
return nil
}
func (c *Call) SetActive(value bool) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.active = value
}
func (c *Call) IsActive() bool {
return c.active
}
type Intercom struct {
Call *Call
MQTTClient *mqtt_client.Client
GPIO *gpiowrapper.GPIO
Config *utils.Config
}
func (i *Intercom) UnlockDoor() {
if i.Config.BareSIPEnabled {
i.Call.BaresipCli.Play("unlockdoor.wav")
}
go i.GPIO.UnlockDoor(time.Second * 5)
log.Info().Msgf("unlocking door")
}
func (c *Call) Play(filename string) error {
return c.BaresipCli.Play(filename)
}
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
config := utils.GetConfig()
intercom := &Intercom{
Config: config,
}
intercom.GPIO = gpiowrapper.NewGPIO()
go intercom.GPIO.WatchInput()
go intercom.GPIO.WatchDoorFeedback()
if config.BareSIPEnabled {
log.Info().Msgf("%s will be call on signal", config.PhoneNumber)
baresipCli, err := baresip.NewBaresipCLient(config.BareSIPHost, 4444)
if err != nil {
log.Fatal().Err(err).Msg("failed to establish connection to baresip")
}
go baresipCli.ReadLoop()
intercom.Call = &Call{
mutex: &sync.Mutex{},
BaresipCli: baresipCli,
}
go func() {
for {
resp := <-intercom.Call.BaresipCli.ResponseChan
log.Debug().Msgf("%v", resp)
}
}()
go func() {
for {
event := <-intercom.Call.BaresipCli.EventChan
switch event.Type {
case "CALL_LOCAL_SDP":
intercom.Call.SetActive(true)
go intercom.GPIO.RedLight(true)
case "CALL_CLOSED":
intercom.Call.SetActive(false)
go intercom.GPIO.RedLight(false)
case "CALL_DTMF_START":
log.Info().Msgf("button %s pressed", event.Param)
if event.Param == "5" {
intercom.UnlockDoor()
}
}
log.Debug().Msgf("%v", event)
}
}()
}
if config.MQTT.Enabled {
intercom.MQTTClient = mqtt_client.NewMQTT(config)
go intercom.MQTTClient.WatchTopicUnlock(func(client mqtt.Client, message mqtt.Message) {
log.Info().Msgf("unlocking from MQTT")
if intercom.Call.IsActive() {
log.Info().Msgf("hangup sip call because handle by MQTT")
intercom.Call.Hangup()
}
intercom.UnlockDoor()
})
if config.BareSIPEnabled {
go intercom.MQTTClient.WatchTopicSound(func(client mqtt.Client, message mqtt.Message) {
log.Info().Msgf("play %s", message.Payload())
if err := intercom.Call.Play(string(message.Payload())); err != nil {
log.Error().Err(err).Msgf("failed to play %s", message.Payload())
}
})
go intercom.MQTTClient.WatchCallAction(func(client mqtt.Client, message mqtt.Message) {
intercom.Call.Toggle(config.PhoneNumber)
})
}
}
go func() {
// CallSignal loop
for {
<-intercom.GPIO.CallSignalChan
log.Info().Msgf("Signal detected")
if config.BareSIPEnabled {
intercom.Call.Toggle(config.PhoneNumber)
}
if config.MQTT.Enabled {
intercom.MQTTClient.PublishCall()
}
}
}()
// Door Unlock feedback
go func() {
for {
<-intercom.GPIO.DoorReleaseChan
if config.BareSIPEnabled && !intercom.Call.IsActive() {
log.Info().Msgf("door unlock without signal calling")
continue
}
// if the release come from this app
if intercom.GPIO.DoorUnlocked {
log.Info().Msgf("door unlocked")
continue
}
log.Info().Msgf("door unlocked from physical button, cancelling the call")
if config.BareSIPEnabled {
intercom.Call.Hangup()
}
}
}()
for {
if config.BareSIPEnabled {
<-intercom.Call.BaresipCli.PingChan
} else {
time.Sleep(2 * time.Second)
}
if config.MQTT.Enabled {
intercom.MQTTClient.PublishAvailability()
}
intercom.GPIO.BlinkGreen(time.Second)
}
}