Class: Apiotics::Portal

Inherits:
Object
  • Object
show all
Defined in:
lib/apiotics/portal.rb

Class Method Summary collapse

Class Method Details

.ca_certificateObject



198
199
200
201
202
# File 'lib/apiotics/portal.rb', line 198

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) ⇒ Object



212
213
214
# File 'lib/apiotics/portal.rb', line 212

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) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/apiotics/portal.rb', line 183

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) ⇒ Object



8
9
10
11
# 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) ⇒ Object



204
205
206
# File 'lib/apiotics/portal.rb', line 204

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) ⇒ Object



208
209
210
# File 'lib/apiotics/portal.rb', line 208

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_interfacesObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



13
14
15
16
17
18
19
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
# 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_parentsObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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"] != {}
        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
  hash
end

.retrieve_configurationObject



58
59
60
61
# 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) ⇒ Object



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
# File 'lib/apiotics/portal.rb', line 131

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) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/apiotics/portal.rb', line 171

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