class Apiotics::Portal
Public Class Methods
ca_certificate()
click to toggle source
# File lib/apiotics/portal.rb, line 243 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 261 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}) data = { "file_name" => response.headers["content-disposition"].split[1].split("\"")[1], "file" => response.body } end
generate_certificate(csr)
click to toggle source
# File lib/apiotics/portal.rb, line 226 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) csr_file.close csr_file.unlink 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
get_image_size(worker_name)
click to toggle source
# File lib/apiotics/portal.rb, line 299 def self.get_image_size(worker_name) response = HTTParty.post("#{Apiotics.configuration.portal}api/image_size", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key, :worker => worker_name}).body response = JSON.parse(response) if response["size"] != nil response["size"] else nil end end
get_sd_image(download_directory, worker_name, image_name)
click to toggle source
# File lib/apiotics/portal.rb, line 271 def self.get_sd_image(download_directory, worker_name, image_name) filename = "#{download_directory}/#{image_name}.img.gz" 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 response = JSON.parse(response) url = response["url"] size = response["size"].to_i #puts url total_downloaded = 0 chunk = size / 50 back = "\b" * 52 todo = " " * 50 File.open(filename, "wb") do |file| puts "Downloading image..\n" print "[#{todo}]" HTTParty.get(url, stream_body: true) do |fragment| total_downloaded = total_downloaded + fragment.length done = "#" * (total_downloaded / chunk) todo = " " * (50 - total_downloaded / chunk) print "#{back}" print "[#{done}#{todo}]" file.write(fragment) end end fin = "#" * 50 print "#{back}" print "[#{fin}]\n" end
openocd(system)
click to toggle source
# File lib/apiotics/portal.rb, line 249 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 257 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 72 def self.parse_all_interfaces configuration = Portal.retrieve_configuration hash = Hash.new configuration["workers"].each do |key, value| hash[key] = Hash.new if configuration["workers"][key]["drivers"] != nil 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 end if configuration["workers"][key]["scripts"] != nil 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 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 if hash["workers"][parent_name]["drivers"] != 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 end if hash["workers"][parent_name]["scripts"] != nil 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 end return attributes_hash end
parse_parents()
click to toggle source
# File lib/apiotics/portal.rb, line 103 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
register_client()
click to toggle source
# File lib/apiotics/portal.rb, line 309 def self.register_client response = HTTParty.post("#{Apiotics.configuration.portal}api/register_client", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key}).body hash = JSON.parse(response) gem_setting = ApioticsSetting.find_by(key: "gem_id") if gem_setting == nil gem_setting = ApioticsSetting.new gem_setting.key = "gem_id" gem_setting.value = hash["public_hash"] gem_setting.save else gem_setting.value = hash["public_hash"] gem_setting.save end s = ApioticsSetting.find_by(key: "private_gem_hash") if s == nil s = ApioticsSetting.new s.key = "private_gem_hash" s.value = hash["private_hash"] s.save end s = ApioticsSetting.find_by(key: "public_key") if s == nil s = ApioticsSetting.new s.key = "public_key" s.value = hash["public"] s.save end s = ApioticsSetting.find_by(key: "private_key") if s == nil s = ApioticsSetting.new s.key = "private_key" s.value = hash["private"] s.save end s = ApioticsSetting.find_by(key: "cert") if s == nil s = ApioticsSetting.new s.key = "cert" s.value = hash["cert"] s.save end hash["public_hash"] end
retrieve_configuration()
click to toggle source
# File lib/apiotics/portal.rb, line 62 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) if Apiotics.configuration.server_type == nil Apiotics.configuration.server_type = hash["server_version"] Apiotics.configuration.aws_endpoint = hash["aws_endpoint"] end hash end
sd_scripts(system)
click to toggle source
# File lib/apiotics/portal.rb, line 253 def self.sd_scripts(system) response = HTTParty.post("#{Apiotics.configuration.portal}api/sd_scripts", :query => {:public_key => Apiotics.configuration.public_key, :private_key => Apiotics.configuration.private_key, :system => system}).body end
sync_device_instances(worker_name, sync_data)
click to toggle source
# File lib/apiotics/portal.rb, line 146 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) configuration = Portal.retrieve_configuration 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) configuration["workers"][worker_name]["drivers"][key]["interfaces"].each do |interface_name,interface_spec| if interface_spec["default_value"] == "" value = nil else value = interface_spec["default_value"] end if Apiotics.configuration.interface_kinds[interface_spec["type"]] == "integer" value = value.to_i elsif Apiotics.configuration.interface_kinds[interface_spec["type"]] == "float" value = value.to_f elsif Apiotics.configuration.interface_kinds[interface_spec["type"]] == "boolean" if value == "nil" value = nil elsif value == "true" value = true elsif value == "false" value = false end elsif Apiotics.configuration.interface_kinds[interface_spec["type"]] == "json" value = value.to_json end s.send(interface_name + "=", value) end s.skip_extract = true if sync_data == true begin s.sync rescue StandardError => e puts e.inspect end 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 214 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