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


函数: ts.ping 测试网络连接情况  

有点意思  比如脚本需要连接某个平台获取授权或者要进行使用验证 先用这个命令ping下目标网站接口 测试是否可以连接  然后再进行对应的操作 连接不到自然提示 并停止脚本

可以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)
end


函数: ts.smtp 通过 smtp 发送邮件

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

local ts = require("ts")
status = ts.smtp("[email protected]","紧急通知","今天不上班","smtp.163.com","[email protected]","testpassword")
if (status) then          
     dialog("Success", 0) --发送成功
else
     dialog("False", 0) --发送失败
end


post知识很强大 不过无法说太多

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

没什么说的 比如自己的淘宝店 或者广告信息 都可以做成二维码放到脚本界面 以图片框的形式 或者 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个视频去

猜你喜欢

转载自www.cnblogs.com/zjl8455482/p/11431376.html