-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
actor.go
187 lines (154 loc) · 4.54 KB
/
actor.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
// This file is part of the Smart Home
// Program complex distribution https://github.com/e154/smart-home
// Copyright (C) 2016-2021, Filippov Alex
//
// This library is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library. If not, see
// <https://www.gnu.org/licenses/>.
package sun
import (
"fmt"
"math"
"sort"
"sync"
"time"
"github.com/e154/smart-home/common/events"
"github.com/e154/smart-home/common"
"github.com/e154/smart-home/common/astronomics/suncalc"
m "github.com/e154/smart-home/models"
"github.com/e154/smart-home/plugins/zone"
"github.com/e154/smart-home/system/bus"
"github.com/e154/smart-home/system/entity_manager"
)
// Actor ...
type Actor struct {
entity_manager.BaseActor
eventBus bus.Bus
positionLock *sync.Mutex
lat, lon, elevation float64
solarAzimuth float64
solarElevation float64
phase string
horizonState string
}
// NewActor ...
func NewActor(entity *m.Entity,
entityManager entity_manager.EntityManager,
eventBus bus.Bus) *Actor {
name := entity.Id.Name()
actor := &Actor{
BaseActor: entity_manager.BaseActor{
Id: common.EntityId(fmt.Sprintf("%s.%s", EntitySun, name)),
Name: name,
Description: "sun plugin",
EntityType: EntitySun,
AttrMu: &sync.RWMutex{},
Attrs: entity.Attributes,
Setts: entity.Settings,
ParentId: common.NewEntityId(fmt.Sprintf("%s.%s", zone.Name, name)),
Manager: entityManager,
States: NewStates(),
},
eventBus: eventBus,
positionLock: &sync.Mutex{},
}
if actor.Setts == nil {
actor.Setts = NewSettings()
}
actor.setPosition(entity.Settings)
return actor
}
// Spawn ...
func (e *Actor) Spawn() entity_manager.PluginActor {
return e
}
func (e *Actor) setPosition(settings m.Attributes) {
if settings == nil {
return
}
e.positionLock.Lock()
defer e.positionLock.Unlock()
if settings[AttrLat] != nil {
e.lat = settings[AttrLat].Float64()
}
if settings[AttrLon] != nil {
e.lon = settings[AttrLon].Float64()
}
}
// UpdateSunPosition ...
func (e *Actor) UpdateSunPosition(now time.Time) {
e.positionLock.Lock()
defer e.positionLock.Unlock()
oldState := e.GetEventState(e)
e.Now(oldState)
sunrisePos := suncalc.GetSunPosition(now, e.lat, e.lon)
e.solarAzimuth = sunrisePos.Azimuth*180/math.Pi + 180
e.solarElevation = sunrisePos.Altitude * 180 / math.Pi
times := suncalc.GetTimes(now, e.lat, e.lon)
//log.Debugf("Sun pos azimuth(%f), elevation(%f)", e.solarAzimuth, e.solarElevation)
var attributeValues = make(m.AttributeValue)
attributeValues[AttrAzimuth] = e.solarAzimuth
attributeValues[AttrElevation] = e.solarElevation
var dayTimes = make(DayTimes, 0)
for _, t := range times {
switch t.MorningName {
case suncalc.Sunrise:
case suncalc.Sunset:
case suncalc.SunriseEnd:
case suncalc.SunsetStart:
case suncalc.Dawn:
case suncalc.Dusk:
case suncalc.NauticalDawn:
case suncalc.NauticalDusk:
case suncalc.NightEnd, suncalc.Night:
continue
case suncalc.GoldenHourEnd:
case suncalc.GoldenHour:
case suncalc.SolarNoon:
case suncalc.Nadir:
default:
log.Warnf("unknown name %v", t.MorningName)
continue
}
attributeValues[string(t.MorningName)] = t.Time
dayTimes = append(dayTimes, DayTime{
MorningName: string(t.MorningName),
Time: t.Time,
})
}
sort.Sort(dayTimes)
for _, t := range dayTimes {
if now.Sub(t.Time).Minutes() > 0 {
e.phase = t.MorningName
if state, ok := e.States[t.MorningName]; ok {
e.State = &state
}
}
}
//log.Debugf("Sun phase %v", e.phase)
attributeValues[AttrPhase] = e.phase
if e.solarElevation > -0.833 {
e.horizonState = StateAboveHorizon
} else {
e.horizonState = StateBelowHorizon
}
attributeValues[AttrHorizonState] = e.horizonState
//log.Debugf("Sun horizonState %v", e.horizonState)
e.DeserializeAttr(attributeValues)
e.eventBus.Publish(bus.TopicEntities, events.EventStateChanged{
PluginName: e.Id.PluginName(),
EntityId: e.Id,
OldState: oldState,
NewState: e.GetEventState(e),
})
}