This repository was archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
136 lines (116 loc) · 2.57 KB
/
app.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'homebus'
require 'homebus_app'
require 'summer'
require 'mqtt'
require 'json'
class DoorBot < Summer::Connection
def initialize(callback)
@callback = callback
super('irc.freenode.net')
end
def channel_message(sender, channel, message)
@callback.call(sender, channel, message)
end
end
class DoorHomeBusApp < HomeBusApp
def initialize(options)
@options = options
super
end
def setup!
end
def work!
@irc = DoorBot.new(lambda {|sender, channel, message| self.irc_message(sender, channel, message); })
end
def irc_message(sender, channel, message)
puts "got a message #{message}"
obj = {}
# May 9 11:51:09 Isaac P. has opened unit2 front door
obj = case message
when /(\S+ \S\.) has (\S+) (unit\d) (\S+ door)/
{ id: @uuid,
timestamp: Time.now.to_i,
access: {
person: $1,
action: $2,
unit: $3,
door: $4
}
}
when /(\S+ \S\.) has (\S+) (front craft lab)/
{ id: @uuid,
timestamp: Time.now.to_i,
access: {
person: $1,
action: $2,
unit: 'unit3',
door: $3
}
}
when /(front craft lab) (\S+) by (\S+ \S\.)/
{ id: @uuid,
timestamp: Time.now.to_i,
access: {
person: $3,
action: $2,
unit: 'unit3',
door: $1
}
}
# "May 16 21:35:39 unit2 front door locked by John R."
when /(unit\d) (\S+ door) (\S+) by (\S+ \S\.)/
{ id: @uuid,
timestamp: Time.now.to_i,
access: {
person: $4,
action: $3,
unit: $1,
door: $2
}
}
else
obj = { id: @uuid,
timestamp: Time.now.to_i,
access: {
message: message
}
}
end
# messages look like "FIRSTNAME INITIAL. has opened unit3 back door"
# parse them into "(PERSON) has (VERBED) (DOOR)"
# may also look like "unit3 access control is online"
@mqtt.publish "/door", JSON.generate(obj), true
end
def manufacturer
'HomeBus'
end
def model
'D'
end
def friendly_name
'Door activity'
end
def friendly_location
'PDX Hackerspace'
end
def serial_number
''
end
def pin
''
end
def devices
[
{ friendly_name: 'Who goes there',
friendly_location: 'PDX Hackerspace',
update_frequency: 60,
index: 0,
accuracy: 0,
precision: 0,
wo_topics: [ 'door' ],
ro_topics: [],
rw_topics: []
}
]
end
end