<file_sep>install:
	npm i
	npm install axios
	npm install net-snmp

run:
	npm start<file_sep>var express = require('express');
var router = express.Router();

var snmp = require ('net-snmp');
var hostip = "127.0.0.1";
var options = {
    port: '4161'
};
var session = snmp.createSession(hostip, "public", options);
var maxRepetitions = 20;
var musics = [];
var re;
var hosts = [
    '127.0.0.1',
    '192.168.1.67',
    '192.168.1.1',
];
//var oid = "1.3.6.1.4.1.2";
var mount = [[],[],[],[]];
var oids = [
    "1.3.6.1.4.1.2.1"
];

var nonRepeaters = 0;

/* GET home page. */
router.get('/', function(req, res, next) {
    re = res;
    // The maxRepetitions argument is optional, and will be ignored unless using
    // SNMP verison 2c
    //session.table(oid, maxRepetitions, getMusics);

    session.getBulk (oids, nonRepeaters,60, function (error, varbinds) {
        if (error) {
            console.error (error.toString ());
        } else {
            // step through the non-repeaters which are single varbinds
            for (var i = 0; i < nonRepeaters; i++) {
                if (i >= varbinds.length)
                    break;

                if (snmp.isVarbindError (varbinds[i]))
                    console.error (snmp.varbindError (varbinds[i]));
                else
                    console.log (varbinds[i].oid + "|" + varbinds[i].value);
            }

            // then step through the repeaters which are varbind arrays
            for (var i = nonRepeaters; i < varbinds.length; i++) {
                for (var j = 0, nm = ((varbinds[i].length)/4), p = 0, m = 0; j < varbinds[i].length; j++, p++) {
                    if (snmp.isVarbindError (varbinds[i][j]))
                        console.error (snmp.varbindError (varbinds[i][j]));
                    else {
                        console.log ((varbinds[i].length) + '------' + varbinds[i][j].value.toString() + '------' + j);
                        if(p === nm) { p = 0; m++ }
                        mount[m].push(varbinds[i][j].value.toString());

                        console.log (varbinds[i][j].oid + "|" + varbinds[i][j].value);
                    }
                }
            }
            for (var i = 0; i < mount[0].length; i++){
                var music = {
                    id: mount[0][i],
                    artist: mount[1][i],
                    title: mount[2][i],
                    path: mount[3][i]
                }
                musics.push(music);
            }
            mount = [[],[],[],[]];
        }
        
        res.jsonp(musics);
        musics = [];
    });
  });


/* GET home page. */
router.get('/music/:musica', function(req, res, next) {
    // The maxRepetitions argument is optional, and will be ignored unless using
    // SNMP verison 2c
    var varbinds = [
        {
            oid: "1.3.6.1.4.1.1.0",
            type: snmp.ObjectType.OctetString,
            value: '*path*' + req.params.musica
        }
    ];
    session.set (varbinds, (error, varbinds) => { 
        if (error) {
            console.error (error.toString ());
        } else {
            for (var i = 0; i < varbinds.length; i++) {
             // for version 2c we must check each OID for an error condition
                if (snmp.isVarbindError (varbinds[i]))
                    console.error (snmp.varbindError (varbinds[i]));
                else
                    console.log (varbinds[i].oid + "|" + varbinds[i].value);

                    session.get(["1.3.6.1.4.1.3.0"], (error, varbinds) => {    
                        if (error) { console.error (error.toString ());
                        } else {
                            for (var i = 0; i < varbinds.length; i++) {
                                // for version 2c we must check each OID for an error condition
                                if (snmp.isVarbindError (varbinds[i]))
                                    console.error (snmp.varbindError (varbinds[i]));
                                else {
                                     res.send( varbinds[i].value.toString() );
                                    console.log (varbinds[i].oid + "|" + varbinds[i].value);
                                }
                            }
                        }
                    });
                }
            }
    });
});

/* GET home page. */
router.get('/hosts', function(req, res, next) {
    // The maxRepetitions argument is optional, and will be ignored unless using
    // SNMP verison 2c
    res.jsonp(hosts)
});

/* GET home page. */
router.post('/hostip', function(req, res, next) {
    hostip = req.body.host;
    session = snmp.createSession(hostip, "public", options);
    // The maxRepetitions argument is optional, and will be ignored unless using
    // SNMP verison 2c
    res.send({'udp':'done'})
});

/* GET home page. */
router.get('/sysname', function(req, res, next) {
    session.get(["1.3.6.1.2.1.1.5.0"], (error, varbinds) => {    
        if (error) { 
            console.error (error.toString ());
        } else {
            for (var i = 0; i < varbinds.length; i++) {
                // for version 2c we must check each OID for an error condition
                if (snmp.isVarbindError (varbinds[i]))
                    console.error (snmp.varbindError (varbinds[i]));
                else {
                    res.send( varbinds[i].value.toString() );
                    console.log (varbinds[i].oid + "|" + varbinds[i].value);
                }
            }
        }
    });
});

module.exports = router;<file_sep>const Host = require('../models/hosts');

var Hosts = module.exports;

//Devolve lista dos Hosts
Hosts.listar = () => {
    return Host
        .find()
        .exec();
}

//Devolve lista dos Hosts
Hosts.consultar = id => {
    return Host
        .findOne({_id: id})
        .exec();
}

//Devolve lista dos Hosts
Hosts.contar = () => {
    return Host
        .countDocuments()
        .exec();
}

Hosts.inserir = host => {
    var novo = new Host(host);
    return novo.save();
}<file_sep>install:
	npm i
	npm install axios
	npm install os
	npm install mpg123
	npm install net-snmp

run:
	node jssnmpd<file_sep>var snmp = require('net-snmp');
var os = require('os');
var mpg = require('mpg123');
player = new mpg.MpgPlayer();

// Default options
var options = {
    port: 4161,
    disableAuthorization: true,
    transport: "udp4"
};
var lastMusic = "default.mp3"
var pause = false;
var callback = function (error, data) {
    if ( error ) {
        console.error (error);
    } else {
        if (data.pdu.varbinds[0].value) {
            name = data.pdu.varbinds[0].value.toString();
            if(name.includes('*path*')) {
                var nome = name.slice(6);
                console.log (lastMusic + ' ' + nome);
                if(lastMusic != nome) {
                    player.play('./musicas/'+ nome);
                    pause = false;
                } else if (pause) {
                    player.pause();
                } else {
                    player.pause();
                    pause = true;
                }
            }
        }
        // console.log (JSON.stringify(data, null, 2));
    }
};

agent = snmp.createAgent (options, callback);
var mib = agent.getMib();
console.log('SNMP agent running on port: ' + options.port)
player.on('resume', function(data){ 
    mib.setScalarValue ("playedLength", a = parseInt(player.length)); 
})



// Create a module store, load a MIB module, and fetch its JSON representation
var store = snmp.createModuleStore ();
store.loadFromFile ("/usr/local/share/snmp/mibs/SNMPv2-MIB.txt");
//var jsonModule = store.getModule ("SNMPv2-MIB");

// Fetch MIB providers, create an agent, and register the providers with your agent
var providersMIB = store.getProvidersForModule ("SNMPv2-MIB");

mib.registerProviders (providersMIB);


// Create a module store, load a MIB module, and fetch its JSON representation
var store = snmp.createModuleStore ();
store.loadFromFile ("/usr/local/share/snmp/mibs/SNMPv2-SMI.txt");
//var jsonModule = store.getModule ("SNMPv2-SMI");

// Fetch SMI providers, create an agent, and register the providers with your agent
var providersSMI = store.getProvidersForModule ("SNMPv2-SMI");

mib.registerProviders (providersSMI);


// Create a module store, load a MIB module, and fetch its JSON representation
var store = snmp.createModuleStore ();
store.loadFromFile ("/usr/local/share/snmp/mibs/SNMPv2-CONF.txt");
//var jsonModule = store.getModule ("SNMPv2-CONF");

// Fetch CONF providers, create an agent, and register the providers with your agent
var providersCONF = store.getProvidersForModule ("SNMPv2-CONF");

mib.registerProviders (providersCONF);


// Create a module store, load a MIB module, and fetch its JSON representation
var store = snmp.createModuleStore ();
store.loadFromFile ("/usr/local/share/snmp/mibs/SNMPv2-TC.txt");
//var jsonModule = store.getModule ("SNMPv2-TC");

// Fetch TC providers, create an agent, and register the providers with your agent
var providersTC = store.getProvidersForModule ("SNMPv2-TC");

mib.registerProviders (providersTC);

var play = {
    name: "playedMusic",
    type: snmp.MibProviderType.Scalar,
    oid: "1.3.6.1.4.1.1",
    scalarType: snmp.ObjectType.OctetString,
    handler: function (mibRequest) {
        var nome = mibRequest.instanceNode.value.slice(6);
        lastMusic = nome;
        mibRequest.done ();
    }
};

var size = {
    name: "playedLength",
    type: snmp.MibProviderType.Scalar,
    oid: "1.3.6.1.4.1.3",
    scalarType: snmp.ObjectType.Integer,
    handler: function (mibRequest) {
        mibRequest.done ();
    }
};

var musicsTable = {
    name: "playListTable",
    type: snmp.MibProviderType.Table,
    oid: "1.3.6.1.4.1.2",
    tableColumns: [
        {
            number: 1,
            name: "musicIndex",
            type: snmp.ObjectType.Integer
        },
        {
            number: 2,
            name: "musicArtist",
            type: snmp.ObjectType.OctetString
        },
        {
            number: 3,
            name: "musicTitle",
            type: snmp.ObjectType.OctetString
        },
        {
            number: 4,
            name: "musicPath",
            type: snmp.ObjectType.OctetString
        }
    ],
    tableIndex: [
        {
            columnName: "musicIndex"
        }
    ]
};

mib.registerProvider (play);
mib.registerProvider (size);
mib.registerProvider (musicsTable);

mib.setScalarValue ("playedMusic", "default.mp3");
mib.setScalarValue ("playedLength", 0);
mib.addTableRow ("playListTable", [1, "<NAME>", "Escape", "PinaColada.mp3"]);
mib.addTableRow ("playListTable", [2, "Toploader", "Dancing in the Moonlight", "DancingInTheMoonlight.mp3"]);
mib.addTableRow ("playListTable", [3, "Tame Impala", "BorderLine", "Borderline.mp3"]);
mib.addTableRow ("playListTable", [4, "Kodaline", "HighHopes", "HighHopes.mp3"]);
mib.addTableRow ("playListTable", [5, "<NAME>", "God's Gonna Cut You Down", "GodsGonnaCutYouDown.mp3"]);
mib.addTableRow ("playListTable", [6, "<NAME>", "Summer Of '69", "SummerOf69.mp3"]);
mib.addTableRow ("playListTable", [7, "<NAME>", "Runaway", "Runaway.mp3"]);
mib.addTableRow ("playListTable", [8, "<NAME>", "When a blind man cries", "Whenablindmancries.mp3"]);
mib.addTableRow ("playListTable", [9, "R.E.M.", "Losing My Religion", "LosingMyReligion.mp3"]);
mib.addTableRow ("playListTable", [10, "System Of A Down", "Lonely Day", "LonelyDay.mp3"]);
mib.addTableRow ("playListTable", [11, "Linkin Park", "Breaking The Habit", "BreakingtheHabit.mp3"]);
mib.addTableRow ("playListTable", [12, "Linkin Park", "In The End", "InTheEnd.mp3"]);
mib.addTableRow ("playListTable", [13, "Red Hot Chili Peppers", "Can't Stop", "CantStop.mp3"]);
mib.addTableRow ("playListTable", [14, "Foo Fighters", "The Pretender", "ThePretender.mp3"]);
mib.addTableRow ("playListTable", [15, "System Of A Down", "Toxicity", "Toxicity.mp3"]);

// Start manipulating the MIB through the registered providers using the `Mib` API calls
mib.setScalarValue ("sysDescr", "The most powerful system you can think of");
mib.setScalarValue ("sysName", os.hostname());
mib.addTableRow ("sysOREntry", [1, "1.3.6.1.4.1.47491.192.168.127.12", "I've dreamed up this MIB", 20]);
<file_sep># Remote-Music-Player
A simple player, over snmp protocol. This project contains a vue js interface, a node js api and a snmp agent in javascript.

## How to set up
You should run the snmp agent in the remote device and control it through the web interface.
