Friday, 29 March 2019

How to properly reuse connection to Mongodb across NodeJs application and modules

I've been reading and reading and still am confused on what is the best way to share the same database (MongoDb) connection across whole NodeJs app. As I understand connection should be open when app starts and reused between modules. My current idea of the best way is that server.js (main file where everything starts) connects to database and creates object variable that is passed to modules. Once connected this variable will be used by modules code as necessary and this connection stays open. E.g.:

    var MongoClient = require('mongodb').MongoClient;
    var mongo = {}; // this is passed to modules and code

    MongoClient.connect("mongodb://localhost:27017/marankings", function(err, db) {
        if (!err) {
            console.log("We are connected");

            // these tables will be passed to modules as part of mongo object
            mongo.dbUsers = db.collection("users");
            mongo.dbDisciplines = db.collection("disciplines");

            console.log("aaa " + users.getAll()); // displays object and this can be used from inside modules

        } else
            console.log(err);
    });

    var users = new(require("./models/user"))(app, mongo);
    console.log("bbb " + users.getAll()); // not connected at the very first time so displays undefined

then another module models/user looks like that:

Users = function(app, mongo) {

Users.prototype.addUser = function() {
    console.log("add user");
}

Users.prototype.getAll = function() {

    return "all users " + mongo.dbUsers;

    }
}

module.exports = Users;

Now I have horrible feeling that this is wrong so are there any obvious problems with this approach and if so how to make it better?



from How to properly reuse connection to Mongodb across NodeJs application and modules

Calibration of magnetometer doesn't give expected results

I want to have a "compass heading" (I want to know the angle upon the north) using a magnetometer. I have seen on several tutorials that first, I need to calibrate it. When I looked up on how to do it I saw graphics comparisons of magnetometer value with and without calibration.

Here are the links I used : link_one and link_two

Both links shows that an uncalibrated magnetometer should display several clusters on the graph as bellow :

This is the graph that I should have for an uncalibrated magnetometer

And calibrated mangnetometer should have both 3-axis on the same point around zero like this :

This is the graph that I should have after the calibration


I am using the Adafruit LSM9DS1. I tried to get the same graphics with the following python code :

def save_mag_values():
    f = open("magnetometer.csv","w")
    for i in range(10000):
        value = sensor.magnetic
        f.write(",".join(map(str,value)))
        f.write("\n")

Then I use the following gnuplot command to print :

gnuplot> plot "magnetometer.csv" using 1:2 title "XY" pointsize 2 pointtype 7, \
              "magnetometer.csv" using 1:3 title "XZ" pointsize 2 pointtype 7, \
              "magnetometer.csv" using 2:3 title "YZ" pointsize 2 pointtype 7

As it's written in the tutorial I just slowly move the sensor and after 1 min I print the values. Here is what I have for the uncalibrated magnetometer : My graph of non calibrated magnetometer

As you can see, the global shape is not a circle and I don't know why. I tried to calibrate it and here is what I have :

This is my graph with calibration

Can anyone tell me what I did wrong and why can't I have "circle shape" values like it should be ? Thanks



from Calibration of magnetometer doesn't give expected results

How to detect AVplayer and get url of current video from WKWebView?

I'm using below code for get url from UIWebView it is working fine but, this same code using for WKWebView it's not working anymore. can anyone help me ?

my code is :

 NotificationCenter.default.addObserver(self, selector: #selector(self.playerItemBecameCurrent(_:)), name: NSNotification.Name("AVPlayerItemBecameCurrentNotification"), object: nil)

 @objc func playerItemBecameCurrent(_ sender : NSNotification){
    let playerItem: AVPlayerItem? = sender.object as? AVPlayerItem
    if playerItem == nil {
        print("player item nil")
        return
    }
    // Break down the AVPlayerItem to get to the path
    let asset = playerItem?.asset as? AVURLAsset
    let url: URL? = asset?.url
    let path = url?.absoluteString

    print(path!,"video url")
}

so please help me how to get this. Thanks.



from How to detect AVplayer and get url of current video from WKWebView?