Espruino/libs/js/ATSMS.min.js
2019-03-13 14:44:34 +00:00

4 lines
1.7 KiB
JavaScript

function d(a){var b=this;this.serial=a;this.at=require("AT").connect(a);this.at.registerLine('+CMTI: "SM",',function(a){b.emit("message",a.substr(12));return""})}function g(a){if(0!=(a.length&3))return a;for(var b="",e=0;e<a.length;e+=4){var c=parseInt(a.substr(e,4),16);if(isNaN(c))return a;b+=String.fromCharCode(c)}return b}d.prototype.init=function(a){var b=this.at;this.at.cmd("ATE0\r\n",3E3,function k(c){if("ATE0"==c)return k;"OK"!=c&&a("ATE0 ERROR "+c);b.cmd("AT+CMGF=1\r\n",1E3,function(b){a&&
a("OK"==b?null:"CMGF ERROR "+b)})})};d.prototype.send=function(a,b,e){var c=this.at;c.register(">",function(){c.unregister(">");c.write(b+"\u001a\r");return""});c.cmd('AT+CMGS="'+a+'"\r\n',1E4,function f(a){c.unregister(">");if(a&&"+CMGS"==a.substr(0,5))return f;e&&e("OK"==a?null:"CMGS ERROR "+a)})};d.prototype.list=function(a,b){var e=[],c;this.at.cmd('AT+CMGL="'+a+'"\r\n',1E4,function f(a){if(void 0!==c&&void 0!==a)return c.text=g(a),c=void 0,f;if(a&&"+CMGL: "==a.substr(0,7)){try{var d=JSON.parse("["+
a.substr(7)+"]");c={index:d[0],isRead:"REC READ"==d[1],oaddr:d[2],oname:d[3],time:d[4],text:""};e.push(c)}catch(m){}return f}b&&b("OK"==a?null:"CMGL ERROR "+a,e)})};d.prototype.get=function(a,b){var e;this.at.cmd("AT+CMGR="+a+"\r\n",1E4,function h(a){if("OK"==a)return b(null,e);if(void 0!==e&&void 0!==a&&"OK"!=a)return e.text=g(a),h;if(a&&"+CMGR: "==a.substr(0,7)){try{var d=JSON.parse("["+a.substr(7)+"]");e={isRead:"REC READ"==d[0],oaddr:d[1],oname:d[2],time:d[3],text:""}}catch(l){}return h}b&&b("OK"==
a?null:"CMGR ERROR "+a,list)})};d.prototype["delete"]=function(a,b){"ALL"==a&&(a="1,4");this.at.cmd("AT+CMGD="+a+"\r\n",1E3,function(a){b&&b("OK"==a?null:"CMGD ERROR "+a)})};exports=d