-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_manager.rb
120 lines (105 loc) · 3.92 KB
/
server_manager.rb
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
#
# Copyright (C) Business Learning Incorporated (www.businesslearninginc.com)
# Use of this source code is governed by an MIT-style license, details of
# which can be found in the project LICENSE file
#
require_relative '../lib/lib_config'
require_relative '../lib/lib_motion'
require_relative '../lib/lib_network'
require_relative '../lib/lib_audio'
require_relative 'server_config'
require_relative 'server_logging'
# -----------------------------------------------------------------------------
# server-side methods to determine enable/disable state of the motion daemon
#
class ServerManager
# ---------------------------------------------------------------------------
# initialize, set @motion_state which determines state of motion daemon for
# all network device clients
#
def initialize
@motion_state = 'disable'
@check_interval_time = Time.new
@server = ServerLogging.new
@server.logging "BEGIN #{self.class}"
end
# ---------------------------------------------------------------------------
# determine whether to start motion based device presence/time logic
# note: the return of this method is pushed to clients at the server
#
def determine_motion_state
@server.logging 'checking motion state'
return @motion_state unless update_motion_state?
if time_in_range? || !device_on_lan?
enable_motion_daemon
else
disable_motion_daemon
end
end
# ---------------------------------------------------------------------------
# update motion_state if check interval (in seconds) is greater than the
# last update (check interval expired)
#
def update_motion_state?
cur_time = Time.new
if (cur_time - @check_interval_time).to_i >= ServerConfig::CHECK_INTERVAL
@check_interval_time = cur_time
@server.logging 'updating motion_state...'
true
else
@server.logging 'no update to motion_state'
false
end
end
# ---------------------------------------------------------------------------
#
def enable_motion_daemon
return @motion_state unless @motion_state == 'disable'
@server.logging 'motion_state set to ENABLE'
LibAudio.play_audio(ServerConfig::AUDIO_MOTION_START) if
ServerConfig::PLAY_AUDIO == 1
@motion_state = 'enable'
end
# ---------------------------------------------------------------------------
#
def disable_motion_daemon
return @motion_state unless @motion_state == 'enable'
@server.logging 'motion_state set to DISABLE'
LibAudio.play_audio(ServerConfig::AUDIO_MOTION_STOP) if
ServerConfig::PLAY_AUDIO == 1
@motion_state = 'disable'
end
# ---------------------------------------------------------------------------
# checks to see if the configured time range crosses into the next day, and
# determines time range accordingly
#
def calc_date_range?
cur_time = Time.new.strftime('%H%M')
if ServerConfig::ALWAYS_ON_START_TIME > ServerConfig::ALWAYS_ON_END_TIME
cur_time >= ServerConfig::ALWAYS_ON_START_TIME ||
cur_time < ServerConfig::ALWAYS_ON_END_TIME
else
cur_time >= ServerConfig::ALWAYS_ON_START_TIME &&
cur_time < ServerConfig::ALWAYS_ON_END_TIME
end
end
# ---------------------------------------------------------------------------
# checks to see if the current time is within the bounds of the 'always on'
# range (if that option is enabled)
#
def time_in_range?
return false if ServerConfig::SCAN_FOR_TIME.zero?
@server.logging 'scan for time in range'
calc_date_range?
end
# ---------------------------------------------------------------------------
# checks to see if device mac exists on LAN
#
def device_on_lan?
# freshen local arp cache to guarantee good results when searching for
# devices by MAC address
LibNetwork.ping_hosts(ServerConfig::IP_BASE, ServerConfig::IP_RANGE)
@server.logging 'look for device(s) on LAN'
LibNetwork.find_macs?(ServerConfig::MACS_TO_FIND)
end
end