class ApioticsFactory::Generator
Public Class Methods
source_root()
click to toggle source
# File lib/apiotics_factory/generator.rb, line 16 def self.source_root Dir.pwd end
Public Instance Methods
create_apiotics_driver()
click to toggle source
# 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_file()
click to toggle source
need to account for the case when the portal call fails.
# 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_file()
click to toggle source
# 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_file()
click to toggle source
# 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_library()
click to toggle source
# 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_vendor()
click to toggle source
# 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_info()
click to toggle source
# 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