0

I have a code below that I need to transform to python code and there is a few problems for me. First of all not sure if I got the point with Buffer.

var dgram = require("dgram");
var async = require("async");
var util = require("util");

var server = dgram.createSocket('dod3');
var domestic = require('./utils/domestic');

server.on("message", function (mess_data, remote) {
    var buf = new Buffer(mess_data, 'hex');

    if(buf[22] === 2) {
        return;
    }

    var log_date = new Date().toISOString();

    try {
        var adress_m = buf.slice(16, 22).toString('hex').toUpperCase();
        var status_id = buf.readUInt32BE(4);
        var usage = buf.slice(1,2).toString('hex');
        //var delta_time = buf.readUInt32BE(8);
        var time = parseInt((new Date().getTime() / 1000), 10);
    }
    catch(ex) {
        console.error(util.format('%s PARSE 500 \"%s\"', log_date, ex));
    }
    async.waterfall([
        function(callback){
            client.hgetall(ams_id, function (error, domestic){
                if(domestic) {
                    callback(null, domestic["domestic_id"]);
                }
                else {
                    console.error(util.format('%s  404 \" No data found for %s | %s\"', log_date, status_id, error));
                    callback(null, null)
                }
            })
        },
        function(domestic_id, callback){
            if(domestic_id) {
                callback(null, domestic_id);
            }
            else {
                domestic.get_domestic_id_by_ams(ams_id, function(result){
                        callback(null, result);
                });
            }
        }
    ], function (error, domestic_id) {
        if(domestic_id == null) {
            return;
        }

        if(ams_id.toString().length > 4) {
            ams_id = ams_id - 16777216;
        }

        var data = {
            "user": {
                "enter" : false
            },
            "device" : {
                "type" : "fev",
                "mac": adress_m
            },
            "event" : {
                "origin" : "call",
                "timestamp": time,
                "type": "zap",
                "product-type" : "domestic",
                "channel": {
                    "id" : domestic_id,
                    "ams-id": ams_id
                },
                "content": {
                    "usage": usage
                }
            }
        };

My python code:

def parse_jspy(mess_data):
    buf = (hashlib.sha256(bytearray.fromhex(mess_data)).hexdigest())
    buf = int(buf, 16)

    buf_check = str(buf)
    if buf_check[22] == 2:
        pass

    datetime_now = datetime.now()
    log_date = datetime_now.isoformat()
    adress_m = buf_check[16:22]
    status_id = buf_check[4:]
    usage = buf_check[1:2]
    time_a = int(time())
    dict_test = {
    "user": {
        "enter" : 'false'
    },
    "device" : {
        "type" : "net",
        "adress_m": adress_m
    },
    "event" : {
        "origin" : "flex",
        "timestamp": time_a,
        "type": "relevant",
        "product-type" : "info",
        "channel": {
            "id" : domestic_id,
            "status_id": status_id
        },
        "content": {
            "usage": usage
        }
    }
    };
    print dict_test



test_data = "02 01 01 dc 01 00 02 02 00 01 9a ba 02 01 9a bb 01 04 30 53 23 11 01"

The main problem is that I can't really well understand javascript code and not sure about this buffer function, and that causes problems in the following functions.
Also, I can't understand the code between "async.waterfall" and json (var data), help with this would be nice too, but not primary. Buffer and try/catch is the must important.

If there is any question or something I can additionaly provide, here I am. Any kind of help is welcomed, thanks in advance. :)

1
  • I would say that your question should be only about javascript, only about the part you don't understand. You should analyse it as much as you can and that ask about the part you don't know. When you make a minimal example and question with limited scope, it will be easier for everyone to answer. See also stackoverflow.com/help/mcve Commented Sep 5, 2018 at 18:55

1 Answer 1

1

I had to do a similar conversion but the other way, from python to java. I think that the buffer has bytes of data so the slices are access specific bytes and setting to variables. Sorry, I would have just commented but can't because don't have 50 reputation yet.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.