Class: ApioticsFactory::Generator

Inherits:
Thor::Group
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/apiotics_factory/generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



16
17
18
# File 'lib/apiotics_factory/generator.rb', line 16

def self.source_root
  Dir.pwd
end

Instance Method Details

#create_apiotics_driverObject



96
97
98
99
100
101
# File 'lib/apiotics_factory/generator.rb', line 96

def create_apiotics_driver
  if @status == true
    puts "Creating apiotics_driver.rb"
    template("#{@template_path}/apiotics_driver.rb.erb", "#{@driver_name.downcase}/rootfs/apiotics_driver.rb")
  end
end

#create_config_fileObject

need to account for the case when the portal call fails.



75
76
77
78
79
80
# File 'lib/apiotics_factory/generator.rb', line 75

def create_config_file
  if @status == true
    puts "Creating driver config file..."
    template("#{@template_path}/config.json.erb", "#{@driver_name.downcase}/config.json")
  end
end

#create_exec_fileObject



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/apiotics_factory/generator.rb', line 82

def create_exec_file
  if @status == true
    puts "Creating driver executable..."
    @exec_name = @driver_name
    @exec_name = @exec_name.gsub(/::/, '/')
    @exec_name = @exec_name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
    @exec_name = @exec_name.gsub(/([a-z\d])([A-Z])/,'\1_\2')
    @exec_name = @exec_name.tr("-", "_")
    @exec_name = @exec_name.downcase
    template("#{@template_path}/exec.rb.erb", "#{@driver_name.downcase}/rootfs/#{@driver_name.downcase}")
    chmod "#{@driver_name.downcase}/rootfs/#{@driver_name.downcase}", 0755
  end
end

#create_interface_fileObject



103
104
105
106
107
108
# File 'lib/apiotics_factory/generator.rb', line 103

def create_interface_file
  if @status == true
    "Creating #{@exec_name}.rb"
    template("#{@template_path}/apiotics_interfaces.rb.erb", "#{@driver_name.downcase}/rootfs/#{@exec_name}.rb")
  end
end

#create_libraryObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/apiotics_factory/generator.rb', line 110

def create_library
  if @status == true
    if options[:library] == "grove_pi"
      puts "Adding GrovePi library..."
      insert_into_file "#{@driver_name.downcase}/rootfs/#{@exec_name}.rb", "require_relative './grove_pi.rb'\n", :after => "require_relative './apiotics_driver'\n"
      copy_file "#{@template_path}/grove_pi/grove_pi.rb", "#{@driver_name.downcase}/rootfs/grove_pi.rb"
      copy_file "#{@template_path}/grove_pi/i2c/i2c.rb", "#{@driver_name.downcase}/rootfs/i2c.rb"
      copy_file "#{@template_path}/grove_pi/i2c/driver.rb", "#{@driver_name.downcase}/rootfs/i2c/driver.rb"
      copy_file "#{@template_path}/grove_pi/i2c/driver/i2c-dev.rb", "#{@driver_name.downcase}/rootfs/i2c/driver/i2c-dev.rb"
      copy_file "#{@template_path}/grove_pi/i2c/driver/gpio.rb", "#{@driver_name.downcase}/rootfs/i2c/driver/gpio.rb"
      directory "#{@template_path}/grove_pi/i2c/device", "#{@driver_name.downcase}/rootfs/i2c/device"
    end
  end
end

#create_vendorObject



125
126
127
128
129
130
# File 'lib/apiotics_factory/generator.rb', line 125

def create_vendor
  if @status == true
    puts "Creating vendor directory..."
    empty_directory "#{@driver_name.downcase}/rootfs/vendor"
  end
end

#fetch_infoObject



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
# File 'lib/apiotics_factory/generator.rb', line 20

def fetch_info
  config_dir = File.dirname(__FILE__).chomp("/lib/apiotics_factory") + "/var"
  #puts config_dir
  if File.exist?(config_dir + "/config.json")
    puts "Found config file #{config_dir}" + "/config.json"
    config = JSON.parse(File.read(config_dir + "/config.json"))
    ApioticsFactory.configuration.public_key = config["public_key"]
    ApioticsFactory.configuration.private_key = config["private_key"]
    ApioticsFactory.configuration.portal = config["portal"]
  else
    say("Please enter your vendor public key from the Apiotics Portal.")
    ApioticsFactory.configuration.public_key = ask("Vendor Public Key: ")
    say("Please enter your vendor private key from the Apiotics Portal.")
    ApioticsFactory.configuration.private_key = ask("Vendor Private Key: ")
    say("Please confirm the Apiotics Portal web address.")
    ApioticsFactory.configuration.portal = ask("Portal: ", default: "https://portal.apiotics.com/")
    unless ApioticsFactory.configuration.portal[-1] == "/"
      ApioticsFactory.configuration.portal = ApioticsFactory.configuration.portal + "/"
    end
    config = {
      "public_key" => ApioticsFactory.configuration.public_key,
      "private_key" => ApioticsFactory.configuration.private_key,
      "portal" => ApioticsFactory.configuration.portal
    }
    #puts config
    unless Dir.exist?(config_dir)
      Dir.mkdir(config_dir)
    end
    File.write(config_dir + "/config.json", config.to_json)
  end
  puts "Fetching driver data from the portal..."
  data = ApioticsFactory::Portal.driver(id)
  #puts data
  data = JSON.parse(data)
  @driver_name = data["name"]
  @status = true
  unless data.keys.include?("errors")
    @interfaces = data["interfaces"]# a hash with "read", "write" and "all".  "read" is an array of read and read/write interfaces. "write" is an array of write and read/write interfaces.  "all" is a hash with interface names as the keys.
    # the value is a hash with an "accessor" key and a "type" key
    @delay_default = data["delay_default"]
    if @delay_default == nil
      @delay_default = Hash.new
      @interfaces["read"].each do |interface|
        @delay_default[interface] = 1.0
      end
    end
  else
    @status = false
    say("#{data['errors']['detail']}")
  end
  @template_path = File.dirname(__FILE__).chomp("/apiotics_factory") + "/templates"
end