projectsimulator 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fb11152fdb739b57353cb3a2c426465b20ecde48075f0e25bd4a3212c2d47d1b
4
- data.tar.gz: 854bf9dd30fcc841211ab8c6a8424e9fc2a0e214755cc8b80951d5c35afd9905
3
+ metadata.gz: 74f715665b457881348502fb3cc58e1103faa8408b2e2e75d0d9f84eaade8020
4
+ data.tar.gz: 0b8e799a7f519c100763d78599b4971b4974bbad6e6df14e1d3d7b0f01b60248
5
5
  SHA512:
6
- metadata.gz: 262fbd34450b87dff84eb25f0d65857e524f8aa16d2ad1e1f1079ce13c35c3793b056f40b974008d67f38cfa38e3ae91c95b2b5df496ae16500d41d4466acd72
7
- data.tar.gz: 6c2db15265e494622f67b285ecd509ab656248ac01199a47d8796f41d9226c0e3fe7fa6b4fbae781972f169faa848b0577e1a51e75165422bf7c90c519b491d3
6
+ metadata.gz: 99827c8f22a234833341f5a466f006d83d5f369cbaca01fe6420e2911dc78dd564b480e428b2c70e8ad265b37516bc155b8ced72257a0d742d94921a023e4a3e
7
+ data.tar.gz: fc4e4e4ab3f71273763094f04bf777c98fbf02c57cad03a9ce3af090783e171a2346f3c46d6038b5cd084fef66bb157fc5409861ffc90406f4aa43f68d9d0779
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -12,12 +12,22 @@ module ProjectSimulator
12
12
  class Model
13
13
  include AppRoutes
14
14
 
15
- def initialize(obj=nil, root: 'building1')
15
+ def initialize(obj=nil, root: 'building1', debug: false)
16
16
 
17
17
  super()
18
- @root = root
18
+ @root, @debug = root, debug
19
19
  @location = nil
20
- build(obj, root: root) if obj
20
+
21
+ if obj then
22
+
23
+ s = obj.strip
24
+ if s[0] == '<' or s.lines[1][0..1] == ' ' then
25
+ @ed = EasyDom.new(s)
26
+ else
27
+ build(obj, root: root)
28
+ end
29
+
30
+ end
21
31
 
22
32
  end
23
33
 
@@ -35,8 +45,27 @@ module ProjectSimulator
35
45
  status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action])
36
46
  "The %s %s is %s." % [h[:location], h[:device], status]
37
47
 
38
- end
48
+ end
49
+
50
+ def get_service(h)
51
+
52
+ a = []
53
+ a << h[:location].split(/ /) if h.has_key? :location
54
+ a << h[:service]
55
+ status = a.inject(@ed) {|r,x| r.send(x)}.send(h[:action])
56
+
57
+ if h.has_key? :location then
58
+ "The %s %s is %s." % [h[:location], h[:service], status]
59
+ else
60
+ "%s is %s." % [h[:service].capitalize, status]
61
+ end
62
+
63
+ end
39
64
 
65
+ # Object Property (op)
66
+ # Helpful for accessing properites in dot notation
67
+ # e.g. op.livingroom.light.switch = 'off'
68
+ #
40
69
  def op()
41
70
  @ed
42
71
  end
@@ -45,13 +74,16 @@ module ProjectSimulator
45
74
  @ed.e.element(s)
46
75
  end
47
76
 
77
+ # request accepts a string in plain english
78
+ # e.g. request 'switch the livingroom light on'
79
+ #
48
80
  def request(s)
49
81
 
50
82
  params = {request: s}
51
83
  requests(params)
52
84
  h = find_request(s)
53
85
 
54
- method(h.first[-1]).call(h)
86
+ method(h.first[-1]).call(h).gsub(/_/,' ')
55
87
 
56
88
  end
57
89
 
@@ -62,14 +94,27 @@ module ProjectSimulator
62
94
  a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value])
63
95
 
64
96
  end
97
+
98
+ def set_service(h)
99
+
100
+ a = []
101
+ a += h[:location].split(/ /) if h[:location]
102
+ a << h[:service]
103
+ a.inject(@ed) {|r,x| r.send(x)}.send(h[:action], h[:value])
104
+
105
+ end
65
106
 
66
107
  def to_sliml()
67
108
  @ed.to_sliml
68
109
  end
69
110
 
70
- def xml(options=nil)
111
+ def to_xml(options=nil)
71
112
  @ed.xml(pretty: true).gsub(' style=\'\'','')
72
113
  end
114
+
115
+ alias xml to_xml
116
+
117
+ # to_xml() is the preferred method
73
118
 
74
119
  protected
75
120
 
@@ -84,20 +129,44 @@ module ProjectSimulator
84
129
  # e.g. switch the gas _fire off
85
130
  #
86
131
  get /(?:switch|turn) the ([^ ]+) +(on|off)$/ do |device, onoff|
87
- location = dev_location(device)
132
+ location = find_path(device)
88
133
  {type: :set_device, action: 'switch=', location: location, device: device, value: onoff}
89
- end
134
+ end
90
135
 
91
136
  # e.g. is the livingroom gas_fire on?
92
137
  #
93
138
  get /is the ([^ ]+) +([^ ]+) +(?:on|off)\??$/ do |location, device|
94
139
  {type: :get_device, action: 'switch', location: location, device: device}
95
140
  end
141
+
142
+ # e.g. enable airplane mode
143
+ #
144
+ get /((?:dis|en)able) ([^$]+)$/ do |state, rawservice|
145
+ service = rawservice.gsub(/ /,'_')
146
+ location = find_path(service)
147
+ {type: :set_service, action: 'switch=', location: location, service: service, value: state + 'd'}
148
+ end
149
+
150
+ # e.g. switch airplane mode off
151
+ #
152
+ get /switch (.*) (on|off)/ do |rawservice, rawstate|
153
+
154
+ state = rawstate == 'on' ? 'enabled' : 'disabled'
155
+ service = rawservice.gsub(/ /,'_')
156
+ location = find_path(service)
157
+ {type: :set_service, action: 'switch=', location: location, service: service, value: state}
158
+ end
159
+
160
+ # e.g. is airplane mode enabed?
161
+ #
162
+ get /is (.*) +(?:(?:dis|en)abled)\??$/ do |service|
163
+ {type: :get_service, action: 'switch', service: service.gsub(/ /,'_')}
164
+ end
96
165
 
97
166
  # e.g. is the gas_fire on?
98
167
  #
99
168
  get /is the ([^ ]+) +(?:on|off)\??$/ do |device|
100
- location = dev_location(device)
169
+ location = find_path(device)
101
170
  {type: :get_device, action: 'switch', location: location, device: device}
102
171
  end
103
172
 
@@ -110,7 +179,7 @@ module ProjectSimulator
110
179
  # e.g. fetch the temperature reading
111
180
  #
112
181
  get /fetch the ([^ ]+) +(?:reading)$/ do |device|
113
- location = dev_location(device)
182
+ location = find_path(device)
114
183
  {type: :get_device, action: 'reading', location: location, device: device}
115
184
  end
116
185
 
@@ -118,8 +187,11 @@ module ProjectSimulator
118
187
 
119
188
  private
120
189
 
121
- def dev_location(device)
122
- a = query('//'+ device).backtrack.to_xpath.split('/')
190
+ def find_path(s)
191
+ puts 'find_path s: ' + s.inspect if @debug
192
+ found = query('//'+ s)
193
+ return unless found
194
+ a = found.backtrack.to_xpath.split('/')
123
195
  a[1..-2].join(' ')
124
196
  end
125
197
 
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.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  2GrtfOXGVOS+2jvmCQC6vU+ew9WBxiDUbebI95TeTwMs2o0cs3IASXX5rIn4TPcz
36
36
  SCccB3fVg2yfsy5DivaWaZwg
37
37
  -----END CERTIFICATE-----
38
- date: 2020-08-19 00:00:00.000000000 Z
38
+ date: 2020-08-20 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: easydom
metadata.gz.sig CHANGED
Binary file