class Apiotics::Portal

Public Class Methods

ca_certificate() click to toggle source
# File lib/apiotics/portal.rb, line 200
def self.ca_certificate
  response = HTTParty.post("#{Apiotics.configuration.portal}api/ca_certificate", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key}).body
  hash = JSON.parse(response)
  return hash["ca_certificate"]
end
download_firmware(worker_name) click to toggle source
# File lib/apiotics/portal.rb, line 214
def self.download_firmware(worker_name)
  response = HTTParty.post("#{Apiotics.configuration.portal}api/download", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key, :worker => worker_name}).body
end
generate_certificate(csr) click to toggle source
# File lib/apiotics/portal.rb, line 185
def self.generate_certificate(csr)
  csr_file = Tempfile.new
  csr_file.write(csr)
  csr_file.rewind
  payload = {
    :multipart => true,
    :csr => csr_file,
    :public_key => Apiotics.configuration.public_key,
    :private_key => Apiotics.configuration.private_key
  }
  r = RestClient.post("#{Apiotics.configuration.portal}api/generate_certificate", payload)
  instance_hash = JSON.parse(r.body)
  return instance_hash["certificate"]
end
get_attributes(parent_name, model_name) click to toggle source
# File lib/apiotics/portal.rb, line 8
def self.get_attributes(parent_name, model_name)
  configuration = Portal.retrieve_configuration
  attributes = Portal.parse_drivers_and_scripts(configuration, parent_name, model_name)
end
openocd(system) click to toggle source
# File lib/apiotics/portal.rb, line 206
def self.openocd(system)
  response = HTTParty.post("#{Apiotics.configuration.portal}api/openocd", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key, :system => system}).body
end
openocd_worker_config(worker_name) click to toggle source
# File lib/apiotics/portal.rb, line 210
def self.openocd_worker_config(worker_name)
  response = HTTParty.post("#{Apiotics.configuration.portal}api/openocd_details", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key, :worker => worker_name}).body
end
parse_all_interfaces() click to toggle source
# File lib/apiotics/portal.rb, line 63
def self.parse_all_interfaces
  configuration = Portal.retrieve_configuration
  hash = Hash.new
  configuration["workers"].each do |key, value|
    hash[key] = Hash.new
    configuration["workers"][key]["drivers"].each do |k,v|
      a = Array.new
      if v["interfaces"] != nil
        v["interfaces"].each do |key, value|
          a << key
        end
      end
      hash[key][v["metadata"]["common name"]] = a
    end
    configuration["workers"][key]["scripts"].each do |k,v|
      a = Array.new
      if v["interfaces"] != nil
        v["interfaces"].each do |key, value|
          a << key
        end
      end
      hash[key][v["metadata"]["common name"]] = a
    end
  end
  hash
end
parse_drivers_and_scripts(hash, parent_name, model_name) click to toggle source
# File lib/apiotics/portal.rb, line 13
def self.parse_drivers_and_scripts(hash, parent_name, model_name)
  attributes_hash = nil
  hash["workers"][parent_name]["drivers"].each do |k,v|
    name = v["metadata"]["common name"].classify
    if name == model_name.classify
      attributes_hash = Hash.new
      attributes_hash[:kind] = "driver"
      attributes_hash[:attributes] = Hash.new
      v["interfaces"].each do |k,v|
        attributes_hash[:attributes][k] = {
          type: v["type"],
          accessor: v["accessor type"],
          units: v["units"],
          noise: v["noise threshold"],
          values: v["values"],
          range: v["range"]
        }
      end
    end     
  end
  hash["workers"][parent_name]["scripts"].each do |k,v|
    name = v["metadata"]["common name"].classify
    if name == model_name.classify
      attributes_hash = Hash.new
      attributes_hash[:kind] = "script"
      attributes_hash[:attributes] = Hash.new
      v["interfaces"].each do |key,value|
        attributes_hash[:attributes][key] = {
          type: value["type"],
          accessor: value["accessor type"],
          units: value["units"],
          noise: value["noise threshold"],
          values: value["values"],
          range: value["range"]
        }
      end
      attributes_hash[:inputs] = Hash.new
      v["inputs"].each do |key,value|
        attributes_hash[:inputs][key] = value
      end
    end
  end
  return attributes_hash
end
parse_parents() click to toggle source
# File lib/apiotics/portal.rb, line 90
def self.parse_parents
  configuration = Portal.retrieve_configuration
  hash = Hash.new
  configuration["workers"].each do |key, value|
    if hash[key] == nil
      hash[key] = Hash.new
    end
    value["drivers"].each do |k, v|
      if hash[key][k] == nil
        hash[key][k] = Hash.new
      end
      if v["parents"] != {}
        unless v["parents"] == nil
          v["parents"].each do |parent_driver, parent_hash|
            parent_hash.each do |parent_interface, parent_value|
              if parent_value.is_a? Hash
                hash[key][k][parent_value["interface"]] = Hash.new
                hash[key][k][parent_value["interface"]]["interface"] = parent_interface
                hash[key][k][parent_value["interface"]]["driver"] = parent_driver
                if hash[key][parent_driver] == nil
                  hash[key][parent_driver] = Hash.new
                  hash[key][parent_driver][parent_interface] = Hash.new
                end
                hash[key][parent_driver][parent_interface]["interface"] = parent_value["interface"]
                hash[key][parent_driver][parent_interface]["driver"] = k
                if parent_value.keys.count > 2
                  parent_value.each do |pv_key, pv_value|
                    unless pv_key == "interface"
                      hash[key][parent_driver][parent_interface][pv_key] = pv_value
                      hash[key][k][parent_value["interface"]][pv_value] = pv_key
                    end
                  end
                end
              end
            end
          end
        end
      end
    end 
  end
  hash
end
retrieve_configuration() click to toggle source
# File lib/apiotics/portal.rb, line 58
def self.retrieve_configuration
  json = HTTParty.post("#{Apiotics.configuration.portal}api/hive", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key}).body
  hash = Apiotics::Parse.portal(json)
end
sync_device_instances(worker_name, sync_data) click to toggle source
# File lib/apiotics/portal.rb, line 133
def self.sync_device_instances(worker_name, sync_data)
  instance_hash = JSON.parse(HTTParty.post("#{Apiotics.configuration.portal}api/workers", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key}).body)      
  klass = (worker_name.to_s + "::" + worker_name.to_s).constantize rescue nil
  unless klass == nil
    instance_ids = Hash.new
    instance_id_array = Array.new
    instance_hash[worker_name].each do |instance_id,status_hash|
      if status_hash["status"] == "Active"
        instance_ids[instance_id] = status_hash
        instance_id_array << instance_id
      end
    end
    stale_instances = klass.where.not(apiotics_instance: instance_id_array)
    stale_instances.destroy_all
    instance_ids.each do |instance_id, status_hash|
      i = klass.find_by(apiotics_instance: instance_id)
      if i == nil
        i = klass.new
        i.apiotics_instance = instance_id
        i.name = status_hash["name"]
        i.save(:validate => false)
        Apiotics.configuration.targets[worker_name].keys.each do |key|
          subklass = (worker_name + "::" + key).constantize rescue nil
          unless subklass == nil
            s = subklass.new
            k = worker_name.underscore.gsub(" ","_").to_s + "_id="
            s.send((k).to_sym, i.id)
            s.skip_extract = true
            if sync_data == true
              s.sync
            end
            s.save(:validate => false)
            s.skip_extract = false
          end
        end 
      end
    end
  end
end
upload_script(worker, script) click to toggle source
# File lib/apiotics/portal.rb, line 173
def self.upload_script(worker, script)
  payload = {
    :multipart => true,
    :script_file => File.open("#{Rails.root}/lib/scripts/apiotics/#{worker.underscore.downcase}/#{script.underscore.downcase}.rb", 'rb'),
    :public_key => Apiotics.configuration.public_key,
    :private_key => Apiotics.configuration.private_key,
    :worker_name => worker,
    :script_name => script
  }
  r = RestClient.post("#{Apiotics.configuration.portal}api/upload_script", payload)
end