[觸動精靈]觸動精靈官方手冊流水帳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個視頻去

相關文章
相關標籤/搜索