Toggle navigation
Wialon Playground
Add library
jQuery latest
Bootstrap 3.3.1
Wialon Javascript SDK
Run
Get code
Fork
Save
Examples
Login
Get units
Change unit icon
Get messages
Get sensors
Edit sensors
Commands
Monitoring notification
Track layer
Get resources
Resources and accounts
Account parameters
Do payment
Management Driver
Get POIs
Create POI
Create notification
Get geofences
Geofence parameters
Create geofence
Export geofences
Gurtam map
Units on map
Unit trace
Create report template
Execute report
Execute custom report
Remaining SMS
Create driver
Bind driver to unit
Export resource props
Import resource props
Unit edit fields
Import fillings
Fuel by mail
Token login for site
Advanced authorization form
Token usage in app
Get location's geofences
Account hierarchy
Nearest units
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Wialon Playground - Remaining SMS</title> <script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script> <script type="text/javascript" src="//hst-api.wialon.com/wsdk/script/wialon.js"></script> </head> <body> <h1>Remaining sms</h1> <form> <p> <label for="token">Token:</label> <input id="token" name="token" type="text"> </p> <p> <input id="login" type="button" value="Login"> <input id="logout" type="button" value="logout"> <input id="clear" type="button" value="Clear all"> </p> <p><input id="fetch_sms" type="button" value="Get remaining sms"></p> </form> <p> Report: <span id="remaining_sms"></span> </p> log: <div id="log"> </div> </body> </html>
HTML
CSS
var RemainingSMS = function(){ this.sess = null; this.user = null; this.init = function(){ /// local variables this.addEventsListeners(); this.sess = wialon.core.Session.getInstance(); }; this.addEventsListeners = function(){ var self = this; // save content of this $('#login').on('click', function(){ self.login(); }); $('#logout').on('click', function(){ self.logout(); }); $('#clear').on('click', function(){ $('#log').html(''); $('#remaining_sms').html(''); }); $('#fetch_sms').on('click', function(){ self.fetchSMSInfo(); }); }; this.log = function(msg){ $('#log').prepend( msg +'<br/>'); }; /// autorization method this.login = function(){ var self = this; if( this.user ) { // if user exists - you are already logged, print username to log this.log("You are logged as '" + this.user.getName()+"', click logout button first"); return; } // if not logged var token = $("#token").val(); // get token from input if( !token ){ // if token is empty - print message to log this.log( 'ERROR: please check fields of login form.' ); } this.log("Trying to login with token '"+ token +"'"); this.sess.initSession("https://hst-api.wialon.com"); // initialize Wialon session // For more info about how to generate token check // http://sdk.wialon.com/playground/demo/app_auth_token this.sess.loginToken(token, "", // try to login function (code) { // login callback if (code) self.log(wialon.core.Errors.getErrorText(code)); // login failed, print error else { self.log("Logged successfully!"); // login succeed self.user = self.sess.getCurrUser(); } } ); }; this.logout = function(){ var self = this; if (!this.user){ this.log("You are not logged, click 'login' button"); return; } this.sess.logout( // if user exist - logout function (code) { // logout callback if (code) self.log(wialon.core.Errors.getErrorText(code)); // logout failed, print error else { self.log("Logout successfully"); // logout suceed self.user = null; } } ); $('#remaining_sms').html(''); // reset result field }; this.fetchSMSInfo = function(){ var self = this; if (!this.user) { this.log("You are not logged!"); return; } var remaining_sms = $('#remaining_sms'); remaining_sms.html(''); // reset result field this.sess.getAccountData(true, function (code, data) { if (code) { self.log(wialon.core.Errors.getErrorText(code)); // login failed, print error return; } // check exist service if ( (!data.settings || !data.settings.combined || !data.settings.combined.services || !data.settings.combined.services.sms) || (!data.settings || !data.settings.plan || !data.settings.plan.services || !data.settings.plan.services.sms) ) { self.log('SMS service is not connected!'); return; } $('#remaining_sms').html( getCount( data ) ); }); function getCount( data ){ // detect maxUsage if (!data.settings || !data.settings.combined || !data.settings.combined.services || !data.settings.combined.services.sms) { self.log('Error load services'); return; } var balance = data.settings.balance; var maxUsage = data.settings.combined.services.sms.maxUsage; var usage = data.settings.combined.services.sms.usage; var costFull = (data.settings.combined.services.sms.cost).split(';'); // get cost of sms var cost = 0; for (var i = 0; i < costFull.length; i++) { var m = parseFloat( costFull[i].split(':')[1] ); if (m) { cost = m; break; } } if (maxUsage != -1 && !cost) { // limited and free sms return 'max: ' + maxUsage + '; usage: ' + usage + '; remaining sms: ' + (maxUsage - usage) + '.'; } else if (maxUsage == -1) { // unlimited var remainingSMS = Math.floor( balance / cost ); remainingSMS = ( '' + remainingSMS === 'Infinity') ? 'unlimited' : remainingSMS; return 'balance: ' + balance + ' cost: ' + cost + ' remaining sms: ' + remainingSMS; } else if (maxUsage != -1 && cost) { // limited with cost var lastOfLimit = maxUsage - usage; var remainingSMS = Math.floor( balance / cost ); if (remainingSMS == 'Infinity') { return 'max: ' + maxUsage + '; usage: ' + usage + '; cost: free; remaining sms: ' + (maxUsage - usage) + '.'; } else if (lastOfLimit >= remainingSMS) { return 'max: ' + maxUsage + ' usage: ' + usage + ' balance: ' + balance + ' cost: ' + cost + '; remaining sms: ' + remainingSMS; } else if (lastOfLimit < remainingSMS) { return 'max: ' + maxUsage + '; usage: ' + usage + '; remaining sms: ' + (maxUsage - usage) + '.'; } } self.log('Error define billing plan'); return ''; } }; // run initialization method return this. init(); } /// execute when DOM ready $(document).ready(function () { $("#token").val("5dce19710a5e26ab8b7b8986cb3c49e58C291791B7F0A7AEB8AFBFCEED7DC03BC48FF5F8"); var remainingSMS = new RemainingSMS(); });
JS
Result
Source code of example
Close ✕
×
Source code