require 'mongo' require 'json' require 'rubygems' require 'freewifi/additional/additional_functions' #class for communication with mongo database for iot API class MongoWifi attr_accessor :mongo_ip, :mongo_port, :client, :mongo_database, :additional_func def initialize(mongo_ip, mongo_port, mongo_database) @mongo_database = mongo_database @mongo_ip = mongo_ip @mongo_port = mongo_port client_host = [mongo_ip + ":" + mongo_port] @client = Mongo::Client.new(client_host, :database => mongo_database) @additional_func = InternalFunc.new end def audit_logger(proc_name, src_ip, input_json, output_json, real_ip, subscriber_ip) out_resp = {} begin current = additional_func.datetimenow collection = client[:audit] doc = { :proc_name => proc_name, :date => current, :sender => {:src_ip => src_ip, :real_ip => real_ip, :subscriber_ip => subscriber_ip}, :input_params => input_json, :output_params => output_json, :sdk_version => Freewifi::VERSION } result = collection.insert_one(doc) out_resp = {:code => 200, :result => "audit_logger: Request completed successfully", :body => result} rescue out_resp = {:code => 507, :result => "audit_logger: Unknown SDK error"} end additional_func.printer_texter(out_resp, "debug") out_resp end def data_ap_auto_created_insert(data) input_params = {:data => data} output_params = {} begin collection = client[:ap_auto_created] result = collection.insert_one(data) output_params = {:code => 200, :result => "data_ap_auto_created_insert: Request completed successfully", :body => result} rescue output_params = {:code => 500, :result => "data_ap_auto_created_insert: failed insert data to mongo"} end additional_func.printer_texter({:input_params => input_params, :output_params => output_params, :procedure => "data_ap_auto_created_insert", :file => "freewifi/database/mongo/mongoconnector.rb"}, "debug") output_params end def get_ap_info_sim_ip(sim_ipaddress, sim_msisdn) begin result_ps = [] collection = client[:ap_auto_created] collection.find({"sim_ipaddress" => sim_ipaddress, "sim_msisdn" => sim_msisdn}).each {|row| result_ps << row #result_ps.append(row) } result_ps[0] rescue [] end end def tunnelid_remove_single_mongo(tunnel_id) out_resp = {} begin collection = client[:ap_auto_created] doc = { "tunnel_id" => tunnel_id } result = collection.delete_many(doc) p result out_resp = {:code => 200, :result => "tunnelid_remove_single_mongo: Request completed successfully"} rescue out_resp = {:code => 507, :result => "tunnelid_remove_single_mongo: Unknown SDK error"} end #internal_func.printer_texter(out_resp, "debug") out_resp end end