require 'mongo' require 'json' require 'rubygems' require 'freewifi/additional/additional_functions' #class for communication with mongo database for iot API class MongoWifiCl_1 attr_accessor :mongo_ip, :mongo_port, :client, :mongo_database, :additional_func_1 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, :connect_timeout => 2) @additional_func_1 = InternalFunc_1.new end def audit_logger_wifi_1(proc_name, src_ip, input_json, output_json, real_ip, subscriber_ip) out_resp = {} begin current = additional_func_1.datetimenow_1 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_1::VERSION } result = collection.insert_one(doc) out_resp = {:code => 200, :result => "audit_logger_wifi_1: Request completed successfully", :body => result, :sdk => "freewifi"} rescue out_resp = {:code => 507, :result => "audit_logger_wifi_1: Unknown SDK error", :sdk => "freewifi"} end additional_func_1.printer_texter_wifi_1(out_resp, "debug") out_resp end def audit_logger_wifi_portal_1(proc_name, src_ip, input_json, output_json, real_ip) out_resp = {} begin current = additional_func_1.datetimenow_1 collection = client[:audit_portal] doc = { :proc_name => proc_name, :date => current, :sender => {:src_ip => src_ip, :real_ip => real_ip}, :input_params => input_json, :output_params => output_json, :sdk_version => Freewifi_1::VERSION } result = collection.insert_one(doc) out_resp = {:code => 200, :result => "audit_logger_wifi_1: Request completed successfully", :body => result, :sdk => "freewifi"} rescue out_resp = {:code => 507, :result => "audit_logger_wifi_1: Unknown SDK error", :sdk => "freewifi"} end additional_func_1.printer_texter_wifi_1(out_resp, "debug") out_resp end def data_ap_auto_created_insert_1(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_1: Request completed successfully", :body => result, :sdk => "freewifi"} rescue output_params = {:code => 500, :result => "data_ap_auto_created_insert_1: failed insert data to mongo", :sdk => "freewifi"} end additional_func_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "data_ap_auto_created_insert_1", :file => "freewifi/database/mongo/mongoconnector.rb", :sdk => "freewifi"}, "debug") output_params end def get_ap_info_sim_ip_wifi_1(sim_ipaddress, sim_msisdn) input_params = {:sim_ipaddress => sim_ipaddress, :sim_msisdn => sim_msisdn} output_params = {} 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) } tun_id = "" if result_ps != [] if result_ps[0].key?("tunnel_id") tun_id = result_ps[0]["tunnel_id"] end end output_params = {:code => 200, :result => "get_ap_info_sim_ip_wifi_1: Request completed successfully", :body => {:all_mongo => result_ps, :tunnel_id => tun_id }, :sdk => "freewifi"} rescue output_params = {:code => 500, :result => "get_ap_info_sim_ip_wifi_1: failed ", :sdk => "freewifi"} end additional_func_1.printer_texter_wifi_1({:input_params => input_params, :output_params => output_params, :procedure => "get_ap_info_sim_ip_wifi_1", :file => "freewifi/database/mongo/mongoconnector.rb", :sdk => "freewifi"}, "debug") output_params end def tunnelid_remove_single_mongo_1(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_1: Request completed successfully", :sdk => "freewifi"} rescue out_resp = {:code => 507, :result => "tunnelid_remove_single_mongo_1: Unknown SDK error", :sdk => "freewifi"} end #internal_func.printer_texter(out_resp, "debug") out_resp end end