Class: MacroDroid

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj = nil, deviceid: nil, remote_url: nil, debug: false) ⇒ MacroDroid

note: The deviceid can only be found from an existing Webhook trigger, generated from MacroDroid itself.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/ruby-macrodroid.rb', line 117

def initialize(obj=nil, deviceid: nil, remote_url: nil, debug: false)

  @deviceid, @remote_url, @debug = deviceid, remote_url, debug    
  
  @geofences = {}
  
  if obj then
    
    raw_s, _ = RXFHelper.read(obj)    
    
    s = raw_s.strip
    
    if s[0] == '{' then
      
      import_json(s) 
      puts 'after import_json' if @debug
      
    elsif  s[0] == '<'
      
      import_xml(s)
      @h = build_h
      
    else

      puts 's: ' + s.inspect if @debug
      
      if s =~ /m(?:acro)?:\s/ then
        
        puts 'before RowX.new' if @debug

        s2 = s.gsub(/^g:/,'geofence:').gsub(/^m:/,'macro:')\
            .gsub(/^v:/,'variable:').gsub(/^t:/,'trigger:')\
            .gsub(/^a:/,'action:').gsub(/^c:/,'constraint:').gsub(/^#.*/,'')
        
        a = s2.split(/(?=^macro:)/)
        
        raw_geofences = a.shift if a.first =~ /^geofence/
        raw_macros = a.join
        #raw_macros, raw_geofences .reverse
        
        puts 'raw_macros: ' + raw_macros.inspect if @debug
        
        if raw_geofences then
          
          geoxml = RowX.new(raw_geofences).to_xml
          
          geodoc = Rexle.new(geoxml)  
          geofences = geodoc.root.xpath('item/geofence')        
          @geofences = fetch_geofences(geofences) if geofences.any?          
          
        end
        
        xml = RowX.new(raw_macros, allow_lonely_keyfield: true).to_xml
        puts 'xml: ' + xml if @debug
        import_rowxml(xml)
        
      elsif s =~ /^# / 
        xml = pc_to_xml(s)
        import_xml(xml)
      else
        raise MacroDroidError, 'invalid input'
      end
      
      @h = build_h
      
    end
    
  else
    
    @h = build_h()
    
    @macros = []
    
  end
end

Instance Attribute Details

#deviceidObject

Returns the value of attribute deviceid.



112
113
114
# File 'lib/ruby-macrodroid.rb', line 112

def deviceid
  @deviceid
end

#geofencesObject (readonly)

Returns the value of attribute geofences.



111
112
113
# File 'lib/ruby-macrodroid.rb', line 111

def geofences
  @geofences
end

#macrosObject (readonly)

Returns the value of attribute macros.



111
112
113
# File 'lib/ruby-macrodroid.rb', line 111

def macros
  @macros
end

#remote_urlObject

Returns the value of attribute remote_url.



112
113
114
# File 'lib/ruby-macrodroid.rb', line 112

def remote_url
  @remote_url
end

#yamlObject (readonly)

Returns the value of attribute yaml.



111
112
113
# File 'lib/ruby-macrodroid.rb', line 111

def yaml
  @yaml
end

Instance Method Details

#add(macro) ⇒ Object



193
194
195
# File 'lib/ruby-macrodroid.rb', line 193

def add(macro)
  @macros << macro
end

#build_hObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/ruby-macrodroid.rb', line 197

def build_h()
  
  puts 'inside Macro#build_h' if @debug
  {
    cell_tower_groups: [],
    cell_towers_ignore: [],
    drawer_configuration: {
      drawer_items: [],
      background_color: -1,
      header_color: 12692882,
      left_side: false,
      swipe_area_color: -7829368,
      swipe_area_height: 20,
      swipe_area_offset: 40,
      swipe_area_opacity: 80,
      swipe_area_width: 14,
      visible_swipe_area_width: 0
    },
    variables: [],
    user_icons: [],
    geofence_data: {
      geofence_map: {}
    },
    macro_list: []

  }    
end

#export(filepath) ⇒ Object



225
226
227
# File 'lib/ruby-macrodroid.rb', line 225

def export(filepath)
  FileX.write filepath, to_json
end

#to_hObject



236
237
238
239
240
241
242
243
244
245
246
# File 'lib/ruby-macrodroid.rb', line 236

def to_h()

  h = {
    geofence_data: {
      geofence_map: @geofences.map {|key, value| [key, value.to_h] }.to_h
    },
    macro_list:  @macros.map(&:to_h)
  }
  @h.merge(h).to_camelcase

end

#to_jsonObject



229
230
231
232
233
# File 'lib/ruby-macrodroid.rb', line 229

def to_json()

  to_h.to_json

end

#to_pcObject

returns pseudocode



250
251
252
# File 'lib/ruby-macrodroid.rb', line 250

def to_pc()
  @macros.map(&:to_pc).join("\n\n")
end

#to_s(colour: false) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/ruby-macrodroid.rb', line 254

def to_s(colour: false)
  
  lines = []
  
  if @geofences.any? then
    lines << @geofences.map {|_, value| (colour ? "g".green.bold : 'g') \
                             + ': ' + value.to_s}.join("\n\n") + "\n"
  end
  
  lines << @macros.map {|x| x.to_s(colour: colour)}.join("\n")
  lines.join("\n")
  
end

#to_summary(colour: false) ⇒ Object



268
269
270
# File 'lib/ruby-macrodroid.rb', line 268

def to_summary(colour: false)
  @macros.map {|x| x.to_summary(colour: colour)}.join("\n")
end