Class: Macro

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-macrodroid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, debug: false) ⇒ Macro



118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ruby-macrodroid.rb', line 118

def initialize(name=nil, debug: false)

  @title, @debug = name, debug
  
  puts 'inside Macro#initialize' if @debug
  
  lv=[], triggers=[], actions=[]
  @local_variables, @triggers, @actions = lv, triggers, actions
        
  @triggers, @actions, @constraints = [], [], []
  @h = {}
  
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



115
116
117
# File 'lib/ruby-macrodroid.rb', line 115

def actions
  @actions
end

#local_variablesObject (readonly)

Returns the value of attribute local_variables.



115
116
117
# File 'lib/ruby-macrodroid.rb', line 115

def local_variables
  @local_variables
end

#titleObject

Returns the value of attribute title.



116
117
118
# File 'lib/ruby-macrodroid.rb', line 116

def title
  @title
end

#triggersObject (readonly)

Returns the value of attribute triggers.



115
116
117
# File 'lib/ruby-macrodroid.rb', line 115

def triggers
  @triggers
end

Instance Method Details

#add(obj) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ruby-macrodroid.rb', line 132

def add(obj)

  if obj.kind_of? Trigger then
    
    puts 'trigger found' if @debug
    @triggers << obj
    
  elsif obj.kind_of? Action
    
    puts 'action found' if @debug
    @actions << obj
    
  elsif obj.kind_of? Constraint
    
    puts 'constraint found' if @debug
    @constraints << obj
    
  end
  
end

#import_h(h) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/ruby-macrodroid.rb', line 175

def import_h(h)

  # fetch the local variables
  @local_variables = h['local_variables']
  
  # fetch the triggers
  @triggers = h[:trigger_list].map do |trigger|
    
    object(trigger.to_snake_case)

  end

  @actions = h[:action_list].map do |action|
    object(action.to_snake_case)
  end

  # fetch the constraints (not yet implemented)
  
  @h = h

  i(local_variables m_trigger_list m_action_list).each do |x|
    @h[x] = []
  end

  @h

end

#import_xml(node) ⇒ Object



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/ruby-macrodroid.rb', line 203

def import_xml(node)
  
  if @debug then
    puts 'inside Macro#import_xml'
    puts 'node: ' + node.xml.inspect
  end
  
  @title = node.attributes[:name]

  if node.element('triggers') then
    
    # level 2
    
    # get all the triggers
    @triggers = node.xpath('triggers/*').map do |e|
      
      puts 'e.name: ' + e.name.inspect if @debug
      {timer: TimerTrigger}[e.name.to_sym].new(e.attributes.to_h)
      
    end

    # get all the actions
    @actions = node.xpath('actions/*').map do |e|
      
      if e.name == 'notification' then
        
        case e.attributes[:type].to_sym
        when :popup          
          e.attributes.delete :type
          ToastAction.new e.attributes.to_h
        end
        
      end

    end    
    
  else
    
    # Level 1
    
    tp = TriggersNlp.new      
    
    @triggers = node.xpath('trigger').map do |e|
      
      r = tp.find_trigger e.text
      
      puts 'found trigger ' + r.inspect if @debug
      
      if r then
        r[0].new(r[1])
      end
      
    end
    
    ap = ActionsNlp.new      
    
    @actions = node.xpath('action').map do |e|
      
      r = ap.find_action e.text
      puts 'found action ' + r.inspect if @debug
      
      if r then
        r[0].new(r[1])
      end
      
    end      
    
  end
  
  self
  
end

#match?(triggerx, detail = {time: $env[:time]}) ⇒ Boolean



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ruby-macrodroid.rb', line 276

def match?(triggerx, detail={time: $env[:time]} )
              
  if @triggers.any? {|x| x.type == triggerx and x.match?(detail) } then
    
    if @debug then
      puts 'checking constraints ...' 
      puts '@constraints: ' + @constraints.inspect
    end
    
    if @constraints.all? {|x| x.match?($env.merge(detail)) } then
    
      true
      
    else

      return false
      
    end
    
  end
  
end

#runObject



299
300
301
# File 'lib/ruby-macrodroid.rb', line 299

def run()
  @actions.map(&:invoke)
end

#to_hObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ruby-macrodroid.rb', line 153

def to_h()

  h = {
    local_variables: @local_variables,
    m_trigger_list: @triggers.map(&:to_h),
    m_action_list: @actions.map(&:to_h),
    m_constraint_list: [], 
    m_description: '',
    m_name: @title,
    m_excludeLog: false,
    m_GUID: guid(),
    m_isOrCondition: false,
    m_enabled: false,
    m_descriptionOpen: false,
    m_headingColor: 0
  }
  
  puts 'h: ' + h.inspect if @debug

  @h.merge(h)
end