"use strict";(function(){var t=0;var e=0;"use strict";var r=require("util");var a=require("events");var s=require("./Logger");var n=s.getLogger("taskMgrDb.js");function i(){var t=this;a.EventEmitter.call(this);if(false===this instanceof i){return new i}t.taskMgr=null;t.init_taskMgr="init";t.create("taskMgrDb.db");return}r.inherits(i,a.EventEmitter);i.prototype.create=function(t){var e=this;var r=require("./jsUtil");var a=r.getDataPath()+"/db/";var s=a+t;r.ensurePathFile(a,s);var n=require("sqlite3").verbose();e.db=new n.Database(s);e.db.serialize();e.db.run("CREATE TABLE IF NOT EXISTS task_table("+"name VARCHAR(32),"+"status INTEGER,"+"start_time DATETIME,"+"period INTEGER,"+"create_time DATETIME,"+"PRIMARY KEY(name)"+");");e.db.run("CREATE TABLE IF NOT EXISTS task_dev_table("+"task VARCHAR(32),"+"dev VARCHAR(32),"+"PRIMARY KEY(task, dev)"+");")};i.prototype.close=function(){var t=this;if(t.taskMgr!=null){}t.db.close();t.db=null};i.prototype.setTaskMgr=function(t){var e=this;if(t==undefined||t==null){return false}if(e.taskMgr!=null){return true}e.taskMgr=t;return true};i.prototype.addTask=function(t,e){var r=this;if(r.taskMgr==null){return false}if(!r.taskMgr.addTask(t,e)){return false}var a=r.taskMgr.getTask(t);if(a==null){return false}r.taskMgr.on("start_"+t,function(t,e){r.emit("start_"+t,t,e)});r.taskMgr.on("end_"+t,function(t,e){r.emit("end_"+t,t,e)});r.taskMgr.on("forceend_"+t,function(t,e){r.emit("forceend_"+t,t,e)});r.db.run("REPLACE INTO task_table(name, status, start_time, period, create_time) "+"VALUES (?, ?, ?, ?, ?);",[a.name,a.status,a.start_time,a.period,a.create_time]);return true};i.prototype.delTask=function(t){var e=this;if(e.taskMgr==null){return false}if(!e.taskMgr.delTask(t)){return false}e.taskMgr.removeAllListener("start_"+t);e.taskMgr.removeAllListener("end_"+t);e.taskMgr.removeAllListener("forceend_"+t);e.db.run("DELETE FROM task_table WHERE name=?;",[t]);return true};i.prototype.enableTask=function(t){var e=this;if(e.taskMgr==null){return false}if(!e.taskMgr.startTask(t)){return false}var r=e.taskMgr.getTask(t);if(r==null){return false}e.db.run("REPLACE INTO task_table(name, status, start_time, period, create_time) "+"VALUES (?, ?, ?, ?, ?);",[r.name,r.status,r.start_time,r.period,r.create_time]);return true};i.prototype.disableTask=function(t){var e=this;if(e.taskMgr==null){return false}if(!e.taskMgr.stopTask(t)){return false}var r=e.taskMgr.getTask(t);if(r==null){return false}e.db.run("REPLACE INTO task_table(name, status, start_time, period, create_time) "+"VALUES (?, ?, ?, ?, ?);",[r.name,r.status,r.start_time,r.period,r.create_time]);return true};i.prototype.getAllTaskFromDb=function(t){var e=this;e.db.all("SELECT * FROM task_table;",function(e,r){t(e,r)})};i.prototype.addTaskDev=function(t,e){var r=this;if(r.taskMgr==null){return false}if(!r.taskMgr.addTaskDev(t,e)){return false}var a=r.taskMgr.getTaskDev(t,e);if(a==null){return false}r.db.run("REPLACE INTO task_dev_table(task, dev) VALUES (?, ?);",[a.task,a.dev]);return true};i.prototype.delTaskDev=function(t,e){var r=this;if(r.taskMgr==null){return false}if(!r.taskMgr.delTaskDev(t,e)){return false}r.db.run("DELETE FROM task_dev_table WHERE task=? AND dev=?;",[taskDev.task,taskDev.dev]);return true};i.prototype.getAllTaskDevFromDb=function(t){var e=this;e.db.all("SELECT * FROM task_dev_table;",function(e,r){t(e,r)})};i.prototype.restoreFromDb=function(t){var e=this;if(e.taskMgr==null){return false}if(e.init_taskMgr!="init"){return true}e.init_taskMgr="doing";e.getAllTaskFromDb(function(t,r){for(var a in r){e.taskMgr.addTask(r[a].name,r[a]);e.emit("restore",task_name)}});e.getAllTaskDevFromDb(function(t,r){for(var a in r){e.taskMgr.addTaskDev(r[a].task,r[a].dev)}});e.getAllTaskFromDb(function(r,a){for(var s in a){if(a[s].status){e.taskMgr.startTask(a[s].name)}}e.init_taskMgr="done";n.debug("taskMgrDb.restoreFromDb end");t()});return true};exports.TaskMgrDb=i;if(e){var u=require("./deviceLinkMgr");var l=new u;var o=require("./taskMgr");var k=new o.DevMgr(l);var f=[{name:"telnet",param:{deviceName:"telnet",protocal:"telnet",host:"192.168.23.111",port:23,username:"",password:"",enablePassword:""}},{name:"COM1",param:{deviceName:"COM1",protocal:"serial",comNum:"COM1",baudrate:115200,enablePassword:""}}];for(var d in f){k.addDevice(f[d].name,f[d].param)}var v=new o.TaskMgr;v.setDevMgr(k);var g=new i;g.setTaskMgr(v);g.restoreFromDb(function(){})}})();