projectsimulator 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/projectsimulator.rb +270 -1
- data.tar.gz.sig +0 -0
- metadata +42 -2
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7498ee0529a8c6caa6f82119db104e656f17da58655bb6258fbca979af7819d8
|
4
|
+
data.tar.gz: 66fe74f176dfe6aac2c00de2c2451dea44955be5a7c88bb1e8369207bc8ce787
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2bb64a67834ac13c34698f12ccd95a98b9f7dd8c70b313623c6e3c1b164c3d68e0d56d395113e29aac7b697c38e4b0fbbe2c0da5baedb5015c52d8f84b5edf03
|
7
|
+
data.tar.gz: 4a11a714877afd064f3b414662a9d19ad2dd34f7e86eaab63cb43e9fb54c13f797fa0d442029bfbfb11fc4bf4faaaf24ca0c713147180444b3b4c4516327b86f
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/lib/projectsimulator.rb
CHANGED
@@ -2,8 +2,10 @@
|
|
2
2
|
|
3
3
|
# file: projectsimulator.rb
|
4
4
|
|
5
|
+
require 'rowx'
|
5
6
|
require 'easydom'
|
6
7
|
require 'app-routes'
|
8
|
+
require 'chronic_between'
|
7
9
|
|
8
10
|
|
9
11
|
module ProjectSimulator
|
@@ -47,7 +49,7 @@ module ProjectSimulator
|
|
47
49
|
def request(s)
|
48
50
|
|
49
51
|
params = {request: s}
|
50
|
-
requests(
|
52
|
+
requests(params)
|
51
53
|
h = find_request(s)
|
52
54
|
|
53
55
|
method(h.first[-1]).call(h)
|
@@ -99,6 +101,19 @@ module ProjectSimulator
|
|
99
101
|
location = dev_location(device)
|
100
102
|
{type: :get_device, action: 'switch', location: location, device: device}
|
101
103
|
end
|
104
|
+
|
105
|
+
# e.g. fetch the livingroom temperature reading
|
106
|
+
#
|
107
|
+
get /fetch the ([^ ]+) +([^ ]+) +(?:reading)$/ do |location, device|
|
108
|
+
{type: :get_device, action: 'reading', location: location, device: device}
|
109
|
+
end
|
110
|
+
|
111
|
+
# e.g. fetch the temperature reading
|
112
|
+
#
|
113
|
+
get /fetch the ([^ ]+) +(?:reading)$/ do |device|
|
114
|
+
location = dev_location(device)
|
115
|
+
{type: :get_device, action: 'reading', location: location, device: device}
|
116
|
+
end
|
102
117
|
|
103
118
|
end
|
104
119
|
|
@@ -112,4 +127,258 @@ module ProjectSimulator
|
|
112
127
|
alias find_request run_route
|
113
128
|
|
114
129
|
end
|
130
|
+
|
131
|
+
|
132
|
+
class Event
|
133
|
+
|
134
|
+
attr_accessor :title
|
135
|
+
attr_reader :triggers, :actions, :constraints, :messages
|
136
|
+
|
137
|
+
def initialize(e, time: nil, debug: false)
|
138
|
+
|
139
|
+
@time, @debug = time, debug
|
140
|
+
@title = e.text('event')
|
141
|
+
@actions = []
|
142
|
+
@triggers = []
|
143
|
+
@constraints = []
|
144
|
+
|
145
|
+
e.xpath('trigger/text()').each do |x|
|
146
|
+
@triggers << Trigger.new(x, time: time, debug: debug).to_type
|
147
|
+
end
|
148
|
+
|
149
|
+
e.xpath('action/text()').each do |x|
|
150
|
+
@actions << Action.new(x, debug: debug).to_type
|
151
|
+
end
|
152
|
+
|
153
|
+
e.xpath('constraint/text()').each do |x|
|
154
|
+
puts 'before Constraints.new'
|
155
|
+
@constraints << Constraint.new(x, time: time, debug: debug).to_type
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
def match(trigger: nil, location: '')
|
161
|
+
|
162
|
+
@messages = []
|
163
|
+
|
164
|
+
h = {motion: MotionTrigger}
|
165
|
+
if @triggers.any? {|x| x.is_a? h[trigger.to_sym] and x.match } and \
|
166
|
+
@constraints.all?(&:match) then
|
167
|
+
|
168
|
+
@messages = @actions.map(&:call)
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
end
|
175
|
+
|
176
|
+
class Action
|
177
|
+
include AppRoutes
|
178
|
+
|
179
|
+
attr_reader :to_type
|
180
|
+
|
181
|
+
def initialize(s, event: '', debug: false)
|
182
|
+
|
183
|
+
super()
|
184
|
+
@debug = debug
|
185
|
+
params = {s: s, event: event}
|
186
|
+
actions(params)
|
187
|
+
@to_type = find_action(s) || {}
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
protected
|
192
|
+
|
193
|
+
def actions(params)
|
194
|
+
|
195
|
+
puts 'inside actions'
|
196
|
+
# e.g. Say 'Good morning'
|
197
|
+
#
|
198
|
+
get /say ['"]([^'"]+)/i do |s|
|
199
|
+
puts 's: ' + s.inspect if @debug
|
200
|
+
SayAction.new(s)
|
201
|
+
end
|
202
|
+
|
203
|
+
# e.g. webhook entered_kitchen
|
204
|
+
#
|
205
|
+
get /webhook (.*)/i do |name|
|
206
|
+
WebhookAction.new(name)
|
207
|
+
end
|
208
|
+
|
209
|
+
get /.*/ do
|
210
|
+
puts 'action unknown' if @debug
|
211
|
+
{}
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
private
|
217
|
+
|
218
|
+
alias find_action run_route
|
219
|
+
|
220
|
+
end
|
221
|
+
|
222
|
+
class Trigger
|
223
|
+
include AppRoutes
|
224
|
+
|
225
|
+
attr_reader :to_type
|
226
|
+
|
227
|
+
def initialize(s, time: nil, debug: false)
|
228
|
+
|
229
|
+
super()
|
230
|
+
@time, @debug = time, debug
|
231
|
+
params = {s: s}
|
232
|
+
puts 'inside Trigger'
|
233
|
+
puts 'params: ' + params.inspect
|
234
|
+
triggers(params)
|
235
|
+
@to_type = find_trigger(s)
|
236
|
+
|
237
|
+
end
|
238
|
+
|
239
|
+
protected
|
240
|
+
|
241
|
+
def triggers(params)
|
242
|
+
|
243
|
+
puts 'inside triggers'
|
244
|
+
|
245
|
+
# e.g. Motion detected in the kitchen
|
246
|
+
#
|
247
|
+
get /motion detected in the (.*)/i do |location|
|
248
|
+
puts 'motion detection trigger'
|
249
|
+
MotionTrigger.new(location)
|
250
|
+
end
|
251
|
+
|
252
|
+
end
|
253
|
+
|
254
|
+
private
|
255
|
+
|
256
|
+
alias find_trigger run_route
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
class Constraint
|
261
|
+
include AppRoutes
|
262
|
+
|
263
|
+
attr_reader :to_type
|
264
|
+
|
265
|
+
def initialize(s, time: nil, debug: false)
|
266
|
+
|
267
|
+
super()
|
268
|
+
@time, @debug = time, debug
|
269
|
+
|
270
|
+
params = {s: s }
|
271
|
+
constraints(params)
|
272
|
+
@to_type = find_constraint(s)
|
273
|
+
|
274
|
+
end
|
275
|
+
|
276
|
+
protected
|
277
|
+
|
278
|
+
def constraints(params)
|
279
|
+
|
280
|
+
puts 'inside constraints' if @debug
|
281
|
+
# e.g. Between 8am and 10am
|
282
|
+
#
|
283
|
+
get /^between (.*)/i do |s|
|
284
|
+
TimeConstraint.new(s, time: @time)
|
285
|
+
end
|
286
|
+
|
287
|
+
get /^on a (.*)/i do |s|
|
288
|
+
TimeConstraint.new(s, time: @time)
|
289
|
+
end
|
290
|
+
|
291
|
+
get /^(after .*)/i do |s|
|
292
|
+
TimeConstraint.new(s, time: @time)
|
293
|
+
end
|
294
|
+
|
295
|
+
end
|
296
|
+
|
297
|
+
private
|
298
|
+
|
299
|
+
alias find_constraint run_route
|
300
|
+
end
|
301
|
+
|
302
|
+
class MotionTrigger
|
303
|
+
|
304
|
+
attr_reader :location
|
305
|
+
|
306
|
+
def initialize(location)
|
307
|
+
@location = location
|
308
|
+
end
|
309
|
+
|
310
|
+
def match()
|
311
|
+
@location.downcase == location.downcase
|
312
|
+
end
|
313
|
+
|
314
|
+
end
|
315
|
+
|
316
|
+
class SayAction
|
317
|
+
|
318
|
+
def initialize(s)
|
319
|
+
@s = s
|
320
|
+
end
|
321
|
+
|
322
|
+
def call()
|
323
|
+
"say: %s" % @s
|
324
|
+
end
|
325
|
+
|
326
|
+
end
|
327
|
+
|
328
|
+
class WebhookAction
|
329
|
+
|
330
|
+
attr_accessor :url
|
331
|
+
|
332
|
+
def initialize(name)
|
333
|
+
@name = name
|
334
|
+
@url = '127.0.0.1'
|
335
|
+
end
|
336
|
+
|
337
|
+
def call()
|
338
|
+
"webhook: %s" % @url
|
339
|
+
end
|
340
|
+
|
341
|
+
end
|
342
|
+
|
343
|
+
class TimeConstraint
|
344
|
+
|
345
|
+
attr_reader :match
|
346
|
+
|
347
|
+
def initialize(times, time: nil)
|
348
|
+
@match = ChronicBetween.new(times).within?(time)
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
class Controller
|
354
|
+
|
355
|
+
attr_reader :events
|
356
|
+
attr_accessor :time
|
357
|
+
|
358
|
+
def initialize(s, time: Time.now, debug: false)
|
359
|
+
|
360
|
+
@time, @debug = time, debug
|
361
|
+
|
362
|
+
doc = Rexle.new(RowX.new(s).to_xml)
|
363
|
+
|
364
|
+
@events = doc.root.xpath('item')\
|
365
|
+
.map {|e| Event.new(e, time: @time, debug: debug) }
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
def trigger(name, location: '')
|
370
|
+
|
371
|
+
events = @events.select do |event|
|
372
|
+
|
373
|
+
puts 'event: ' + event.inspect if @debug
|
374
|
+
|
375
|
+
event.match(trigger: 'motion', location: location='kitchen')
|
376
|
+
|
377
|
+
end
|
378
|
+
|
379
|
+
events.flat_map(&:messages)
|
380
|
+
|
381
|
+
end
|
382
|
+
|
383
|
+
end
|
115
384
|
end
|
data.tar.gz.sig
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: projectsimulator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Robertson
|
@@ -35,8 +35,28 @@ cert_chain:
|
|
35
35
|
2GrtfOXGVOS+2jvmCQC6vU+ew9WBxiDUbebI95TeTwMs2o0cs3IASXX5rIn4TPcz
|
36
36
|
SCccB3fVg2yfsy5DivaWaZwg
|
37
37
|
-----END CERTIFICATE-----
|
38
|
-
date: 2020-05-
|
38
|
+
date: 2020-05-06 00:00:00.000000000 Z
|
39
39
|
dependencies:
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: rowx
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 0.7.0
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0.7'
|
50
|
+
type: :runtime
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 0.7.0
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0.7'
|
40
60
|
- !ruby/object:Gem::Dependency
|
41
61
|
name: easydom
|
42
62
|
requirement: !ruby/object:Gem::Requirement
|
@@ -77,6 +97,26 @@ dependencies:
|
|
77
97
|
- - ">="
|
78
98
|
- !ruby/object:Gem::Version
|
79
99
|
version: 0.1.19
|
100
|
+
- !ruby/object:Gem::Dependency
|
101
|
+
name: chronic_between
|
102
|
+
requirement: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - "~>"
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0.3'
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: 0.3.1
|
110
|
+
type: :runtime
|
111
|
+
prerelease: false
|
112
|
+
version_requirements: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0.3'
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: 0.3.1
|
80
120
|
description:
|
81
121
|
email: james@jamesrobertson.eu
|
82
122
|
executables: []
|
metadata.gz.sig
CHANGED
Binary file
|