[触动精灵]触动精灵官方手册流水帐4


函数: ts.ping 测试网络链接状况   html

有点意思  好比脚本须要链接某个平台获取受权或者要进行使用验证 先用这个命令ping下目标网站接口 测试是否能够链接  而后再进行对应的操做 链接不到天然提示 并中止脚本 sql

能够ping ip 局域网ip更是能够 例子直接看官方例子 很清楚数据库

local ts = require("ts")
status = ts.ping("www.baidu.com",3)  --也能够是 IP 地址 返回的是个表  key是第几回链接 value是消耗的时间 单位毫秒
if status then
     for i,v in pairs(status) do
     dialog(string.format("%s = %s",i,v))
     mSleep(3000)
end
else
dialog(status, 0)
endjson


函数: ts.smtp 经过 smtp 发送邮件 服务器

测试过了用起来没问题 若是对脚本运行有担忧  每次脚本中止前  能够在log目录下对应的日志文件以邮件的方式发送给做者或者须要的人 做为备份或者查看问题的依据 不过很明显这个发送邮件不支持附件 没法把日志文件做为附件发送 只能读取日志文件内容写入到邮件 也没办法网络

local ts = require("ts")
status = ts.smtp("10879433@qq.com","紧急通知","今天不上班","smtp.163.com","test@163.com","testpassword")
if (status) then          
     dialog("Success", 0) --发送成功
else
     dialog("False", 0) --发送失败
endapp


post知识很强大 不过没法说太多函数

函数:ts.qrEncode 二维码图片生成    函数:ts.qrDecode 二维码图片解析 post

没什么说的 好比本身的淘宝店 或者广告信息 均可以作成二维码放到脚本界面 以图片框的形式 或者 app有提供一些二维码供咱们解析测试


ts.json.encode json 串编码  ts.json.decode json 串解码  

json字符串和表之间的转化 没什么说的 仍是那个 字符串转表 若是字符串不符合json规则出错的问题  之前写的那个函数已经解决了这个问题


彷佛触动在string库的原有基础上添加了一些子方法  string.md5  string.trim()  等等 省事了


函数: ts.ms 毫秒级时间戳

没什么说的 很是有价值

虽然和os.clcok相比 效率慢  可是连续运行20次 占用2-8毫秒 也挺好


函数:ts.tsDownload 下载文件

没什么说的 挺好使


根据ts库的文件和文件夹函数又调整下封装的自定义函数

不过注意的是 咱们操做的文件和文件夹是手机上模拟器上的 而不是咱们电脑上工程目录下的那些东西 千万注意

为了和官方的命令区分开 把全部的文件操做函数写入file表  全部目录操做函数写入dir表 这样就不会和官方命令冲突

file={}
dir={}
--都须要ts库和TSLib库
--[[
表的基本结构:主要是为了我的用的习惯和避免名字冲突
file:isFile
file:readFile
file:writeTableToFile
file:readLines
file:writeFile
file:appendFile
file:copyfile
file:moveFile
file:delFile

dir:isFolder
dir:createFolder
dir:delFolder
dir:copyFolder
dir:scanPath
--]]
--判断文件(夹)是否存在 
--其实就是isFileExist 不过就是我用不习惯这个函数名 换了个名字而已
--返回值true false
function  file:isFile(path)
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result,result1,result2            
            result=false
            if path=="" or path==nil then
                error("file:isFile路径为空 请检查")
            else
                result1,result2=isFileExist(path)
                --return result1,result2
                if result1==true and result2==true then
                    result=true
                else
                    result=false
                end
            end
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("fileExist") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }    
end

--读取指定路径文件所有内容 成功返回内容 读取失败返回false/nil 
function  file:readFile(path)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result
            if path=="" or path==nil then
                error("file:readFile路径为空 请检查")
            else
                result=readFileString(path)
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:readFile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }    
end
--把表元素的做为文件的一行覆盖写入到文件里面
--返回true false/nil
function  file:writeTableToFile(path,temptable)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result
            --temptable=temptable or {}
            if type(temptable)=="table" then
            else
                error("file:writeTableToFile第二参数表不是表 请检查")
            end
            if path=="" or path==nil  then
                error("file:writeTableToFile路径为空 请检查")
            else
                result=writeFile(path,temptable,"w") --将 table 内容存入文件,成功返回 true
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:writeTableToFile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end

--读取指定路径文件所有内容到表中 
--成功返回表 读取失败返回false/nil 不过要注意 返回值表只是传址
function  file:readLines(path)    
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result
            if path=="" or path==nil  then
                error("file:readLines 请检查")
            else
                result=readFile(path)
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:readLines") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }    
end
--覆盖写入文件内容 
--nil
function  file:writeFile(path,str)
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result
            local file
            if  str=="" or str==nil then
                error("file:writeFile第二参数为空 请检查")
            end
            if path=="" or path==nil  then
                error("file:writeFile路径为空 请检查")
            else
                result=writeFileString(path,str,"w")
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:writeFile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end

--追加写入文件内容
function  file:appendFile(path,str)
    return try{
        function ()
            --下面代码随便写 有可能抛出异常便可
            local result
            path=path or ""
            path=string.trim(path)
            if   str=="" or str==nil then
                error("file:appendFile第二参数为空 请检查")
            end
            if  path=="" or path==nil then
                error("file:appendFile路径为空字符串 请检查")
            else
                result=writeFileString(path,str,"a")
                return result
            end            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:appendFile") .. "] 错误信息:".. tostring(errors)    
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end

--复制文件
--支持须要ts库
--返回true false/nil
function file:copyfile(path,to)
    --os.execute("cp -rf "..path.." "..to);
        return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            local ts = require("ts")
            local result
            path=string.trim(path)
            to=string.trim(to)
            --os.execute("cp -rf "..path.." "..to)--复制文件
            result = ts.hlfs.copyFile(path,to)  --复制文件
            return result
            
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:copyfile") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end
--剪切文件
--返回值 无或者nol
function file:moveFile(path,to)
    --os.execute("cp -rf "..path.." "..to);
        return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            path=string.trim(path)
            to=string.trim(to)
            os.execute("mv "..path.." "..to)--剪切文件
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:moveFile") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end
--删除文件 删除文件已经有了  单独的 delFile
--返回值 nil
function file:delFile(path)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            path=string.trim(path)
            os.execute("rm -rf ".. path)
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("file:delFile") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                dialog(tempStr, 3)
            end            
        }
    }
end
--是否文件夹
--支持 须要ts TSLib库
--返回true false/nil
function dir:isFolder(path)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            local ts = require("ts")
            local result=false
            local result1,result2
            path=string.trim(path)
            --os.execute("mkdir "..path)--建立文件夹
            --result = ts.hlfs.isDir(path)--这个函数没法准确判断是文件仍是文件夹  败了 换TSLib库的函数检测
            result1,result2=isFileExist(path)
                --return result1,result2
                if result1==true and result2==false then
                    result=true
                else
                    result=false
                end
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dir:isFolder") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end

--新建文件夹 
--支持 须要ts TSLib库
--问题 若是写入的路径是个文件也会反馈true 算个bug
--返回true false/nil
function dir:createFolder(path)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            --require "zjlLib"
            local ts = require("ts")            
            local result
            local result1,result2
            path=string.trim(path)
            result = ts.hlfs.makeDir(path)
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dir:createFolder") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end
--删除文件夹及旗下的文件和目录
--支持 须要ts TSLib库
--返回true false
function dir:delFolder(path)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            local ts = require("ts")
            local result=false
            path=string.trim(path)
            --os.execute("mkdir "..path)--建立文件夹
            result = ts.hlfs.removeDir(path)--删除 文件夹及全部文件
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dir:delFolder") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end
--复制文件夹及旗下的文件和目录
--支持 须要ts TSLib库
--返回true false
function dir:copyFolder(path,to)
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            local ts = require("ts")
            local result=false
            path=string.trim(path)
            to=string.trim(to)
            --os.execute("mkdir "..path)--建立文件夹
            result = ts.hlfs.copyDir(path,to)
            return result
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dir:copyFolder") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end

--遍历目录 
--返回表 或者出错就nil
function dir:scanPath(path)    
    return try{
        function ()            
            --下面代码随便写 有可能抛出异常便可    
            local a 
            local f = {};
            if string.sub(path,-1,-1) ~= "/"then
                path = path .. "/"
            end        
            a = io.popen("ls "..path)            
            for l in a:lines() do
                    table.insert(f,l)
            end
            a:close()
            return f
        end,
        catch{
            function (errors)
                --这里对应函数名要改
                local tempStr=""
                tempStr="函数[" .. tostring("dir:scanPath") .. "] 错误信息:".. tostring(errors)
                traceprint(tempStr)
                --dialog(tempStr, 3)
                toast(tempStr) mSleep(3000)
            end            
        }
    }
end


触动的本地数据库

天然是用SQLite  由于它最简单 不须要配置 不须要外部支持  简单说咱们只须要把db数据库文件扔到触动指定目录 就能够对这个数据库进行操做了 注意他是本地数据库 不是客户端-服务器模式

https://pc.qq.com/detail/7/detail_163887.html  SQLite Expert 我的版下载

https://www.jianshu.com/p/7210fb9d5bea   SQLite Expert 我的版基础操做 创建数据库 创建表 增删改查操做  字段类型之类的百度下就好

若是有自增字段 那么添加记录咱们能够指定字段添加就好   INSERT into test_account ('key','value')values('shebei003','{123w22}')  还有一个自增字段id 不写这个字段就自增了


注意 我实际测试发现 除了insert的sql语句能够正常使用 其余语句都会报错 不知道是否是我用的雷电模拟器做为开发环境 仍是官方没有对这个命令完善


tsting库 是对图片文件操做的 有点相似ps的功能 短时间内用不上   先看下官方推荐的另外3个视频去

相关文章
相关标签/搜索