rulexlib

package
v0.6.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2024 License: AGPL-3.0 Imports: 39 Imported by: 0

README

标准库规范

  • 最多传5个参数,需要资源的时候第一个参数永远是资源UUID
  • 函数必须两个返回值:data,error

文档

提倡每个LUA函数都有文档,下面提供一种快速写文档的方法:

//@desc:数据转发到HTTP服务器
func __RULEX_DataToHttp(
	uuid string, //@arg: HTTP UUID
	data string, //@arg: 数据
) error //@arg: 错误信息

其实是用 go 的语法来生成文档,@开头的文本会被复制进文档里面去。必须每一个字段写一行。

Documentation

Overview

Copyright (C) 2023 wwhai

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

if Duration, err := pingQ(ip, 2000*time.Millisecond); err != nil {
	glogger.GLogger.WithFields(Fields).Info(fmt.Sprintf(
		"[Count:%d] Ping Error:%s", i,
		err.Error()))
} else {

	glogger.GLogger.WithFields(Fields).Info(fmt.Sprintf(
		"[Count:%d] Ping Reply From %s: time=%v ms TTL=128", i,
		tt, Duration))
}

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ABCD

func ABCD(rx typex.RuleX) func(*lua.LState) int

* * 处理ABCD序 *

func ActionReplyFailed

func ActionReplyFailed(rx typex.RuleX) func(*lua.LState) int

* 设备行为调用失败 * { * "method": "action_reply", * "id": "20a4ccfd", * "code": 0, * "status": "message", * "out": {属性} * } *

func ActionReplySuccess

func ActionReplySuccess(rx typex.RuleX) func(*lua.LState) int

* 设备行为调用成功 * { * "method": "action_reply", * "id": "20a4ccfd", * "code": 0, * "status": "message", * "out": {属性} * } *

func AsByteSlice

func AsByteSlice(b string) []byte

* * 位串转字节 *

func AsHexSlice

func AsHexSlice(b string) []string

* * 位串转十六进制 *

func B64S2B

func B64S2B(rx typex.RuleX) func(*lua.LState) int

* * Base64 to byte: 用来处理golang的JSON转换byte[]问题 *

func BADC

func BADC(rx typex.RuleX) func(*lua.LState) int

BADC 将十六进制串里的N个字节"ABCD"逆序成"BADC"

func Base64J2T added in v0.6.6

func Base64J2T(rx typex.RuleX) func(l *lua.LState) int

func Bin2Str

func Bin2Str(rx typex.RuleX) func(l *lua.LState) int

func BinToFloat32

func BinToFloat32(rx typex.RuleX) func(*lua.LState) int

func BinToFloat32Little added in v0.6.5

func BinToFloat32Little(rx typex.RuleX) func(*lua.LState) int

* * 二进制转浮点小端 *

func BinToFloat64

func BinToFloat64(rx typex.RuleX) func(*lua.LState) int

* * 二进制转浮点数 *

func BinToFloat64Little added in v0.6.5

func BinToFloat64Little(rx typex.RuleX) func(*lua.LState) int

* * 二进制转8字节浮点小端 *

func BitStringToBytes

func BitStringToBytes(rx typex.RuleX) func(*lua.LState) int

func ByteToBitString

func ByteToBitString(rx typex.RuleX) func(*lua.LState) int

func ByteToInt

func ByteToInt(b []byte, order binary.ByteOrder) uint64

/

func ByteToInt64

func ByteToInt64(rx typex.RuleX) func(*lua.LState) int

func Bytes2Hexs

func Bytes2Hexs(rx typex.RuleX) func(*lua.LState) int

* * byte数组转十六进制字符串 *

func CDAB

func CDAB(rx typex.RuleX) func(*lua.LState) int

CDAB 将十六进制串里的2个字节"ABCD"转化成"CDAB"

func CRC16

func CRC16(rx typex.RuleX) func(*lua.LState) int

* * CRC16校验 *

func CtrlDevice

func CtrlDevice(rx typex.RuleX) func(*lua.LState) int

* * 控制操作[2023年4月16日新增, 需求来自总线控制多个不对等设备] *

func DCACall

func DCACall(rx typex.RuleX) func(*lua.LState) int

* * 设备功能调用 *

func DCBA

func DCBA(rx typex.RuleX) func(*lua.LState) int

DCBA 将十六进制串里的N个字节"ABCD"逆序成"DCBA"

func DataToHttp

func DataToHttp(rx typex.RuleX) func(*lua.LState) int

* * 数据转发到HTTP:local err: = data:ToHttp(uuid, data) *

func DataToMongo

func DataToMongo(rx typex.RuleX) func(*lua.LState) int

func DataToMqtt

func DataToMqtt(rx typex.RuleX) func(*lua.LState) int

func DataToMqttTopic

func DataToMqttTopic(rx typex.RuleX) func(*lua.LState) int

func DataToNats added in v0.6.4

func DataToNats(rx typex.RuleX) func(*lua.LState) int

* * 数据转发到HTTP:local err: = data:ToNats(uuid, data) *

func DataToTarget

func DataToTarget(rx typex.RuleX) func(*lua.LState) int

* 注意:该接口是通用的,如果觉得这个不清晰,可以尝试具体的'DataToXXX' * 数据转发到具体的目的地:local err: = data:ToTarget(uuid, data) *

func DataToTcp added in v0.6.4

func DataToTcp(rx typex.RuleX) func(*lua.LState) int

* * 数据转发到 UDP:local err: = data:ToTcp(uuid, data) *

func DataToTdEngine

func DataToTdEngine(rx typex.RuleX) func(*lua.LState) int

数据推送到Tdengine

func DataToUdp

func DataToUdp(rx typex.RuleX) func(*lua.LState) int

* * 数据转发到 UDP:local err: = data:ToUdp(uuid, data) *

func DataToUiComponent added in v0.6.3

func DataToUiComponent(rx typex.RuleX) func(l *lua.LState) int

func DebugAPP added in v0.6.1

func DebugAPP(rx typex.RuleX, uuid string) func(*lua.LState) int

* * APP debug输出, stdlib:Debug(".....") *

func DebugRule added in v0.6.4

func DebugRule(rx typex.RuleX, uuid string) func(*lua.LState) int

* * 辅助Debug使用, 用来向前端Dashboard打印日志的时候带上ID *

func DecodeValue

func DecodeValue(L *lua.LState, value interface{}) lua.LValue

DecodeValue converts the value to a Lua value.

This function only converts values that the encoding/json package decodes to. All other values will return lua.LNil.

func Endian

func Endian(endian byte) binary.ByteOrder

* * 大小端判断 *

func F1

func F1(rx typex.RuleX) func(l *lua.LState) int
--------------------------------------------

|Function | Register Type

--------------------------------------------

| 1 | Read Coil | 2 | Read Discrete Input | 3 | Read Holding Registers | 4 | Read Input Registers | 5 | Write Single Coil | 6 | Write Single Holding Register | 15 | Write Multiple Coils | 16 | Write Multiple Holding Registers

--------------------------------------------

* * Modbus Function1 *

func F15 added in v0.6.4

func F15(rx typex.RuleX) func(l *lua.LState) int

*

  • Modbus Function15 local error = modbus:F15("uuid1", 0, 1, "0001020304")

*

func F16 added in v0.6.4

func F16(rx typex.RuleX) func(l *lua.LState) int

* * Modbus Function16 * local error = modbus:F16("uuid1", 0, 1, "0001020304")

func F2

func F2(rx typex.RuleX) func(l *lua.LState) int

* * Modbus Function2 *

func F3

func F3(rx typex.RuleX) func(l *lua.LState) int

* * Modbus Function3 *

func F4

func F4(rx typex.RuleX) func(l *lua.LState) int

* * Modbus Function4 *

func F5 added in v0.6.4

func F5(rx typex.RuleX) func(l *lua.LState) int

func F6 added in v0.6.4

func F6(rx typex.RuleX) func(l *lua.LState) int

* * local error = modbus:F6("uuid1", 0, 1, "0001020304")

*

func FailedCmd

func FailedCmd(rx typex.RuleX) func(*lua.LState) int

func FinishCmd

func FinishCmd(rx typex.RuleX) func(*lua.LState) int

func GetABitOnByte

func GetABitOnByte(rx typex.RuleX) func(*lua.LState) int

func H3DI1Get added in v0.6.4

func H3DI1Get(rx typex.RuleX) func(*lua.LState) int

* * DI 1,2,3 -> gpio 8-9-10 *

func H3DI2Get added in v0.6.4

func H3DI2Get(rx typex.RuleX) func(*lua.LState) int

func H3DI3Get added in v0.6.4

func H3DI3Get(rx typex.RuleX) func(*lua.LState) int

func H3DO1Get added in v0.6.4

func H3DO1Get(rx typex.RuleX) func(*lua.LState) int

func H3DO1Set added in v0.6.3

func H3DO1Set(rx typex.RuleX) func(*lua.LState) int

* * DI2(0/1) *

func H3DO2Get added in v0.6.4

func H3DO2Get(rx typex.RuleX) func(*lua.LState) int

func H3DO2Set added in v0.6.3

func H3DO2Set(rx typex.RuleX) func(*lua.LState) int

func HToN

func HToN(rx typex.RuleX) func(*lua.LState) int

* * Hex to number *

func HexToNumber

func HexToNumber(s string) (int64, error)

* * 十六进制字符串转数字 *

func Hexs2Bytes

func Hexs2Bytes(rx typex.RuleX) func(*lua.LState) int

* * 十六进制字符串转byte数组 *

func HsubToN

func HsubToN(rx typex.RuleX) func(*lua.LState) int

* * 取某个Hex字符串的子串转换成数字 *

func HttpGet added in v0.6.5

func HttpGet(rx typex.RuleX) func(*lua.LState) int

* * HTTP GET *

func HttpPost added in v0.6.5

func HttpPost(rx typex.RuleX) func(*lua.LState) int

func Infer

func Infer(rx typex.RuleX) func(*lua.LState) int

func JQ

func JQ(jqExpression string, inputData interface{}) ([]interface{}, error)

JQ * * In either case, you cannot use custom type values as the query input. * The type should be []interface{} for an array and map[string]interface{} for a map (just like decoded to an interface{} using the encoding/json package). * You can't use []int or map[string]string, for example. * If you want to query your custom struct, marshal to JSON, unmarshal to interface{} and use it as the query input.

func JSOND

func JSOND(rx typex.RuleX) func(l *lua.LState) int

func JSONE

func JSONE(rx typex.RuleX) func(l *lua.LState) int

func JqSelect

func JqSelect(rx typex.RuleX) func(*lua.LState) int

func Led1Off added in v0.6.5

func Led1Off(rx typex.RuleX) func(*lua.LState) int

func Led1On added in v0.6.5

func Led1On(rx typex.RuleX) func(*lua.LState) int

User Gpio operation 注意:低电平亮

func LocalDBQuery added in v0.6.4

func LocalDBQuery(rx typex.RuleX) func(*lua.LState) int

* * 数据中心本地执行 *

func MakeUUID added in v0.6.6

func MakeUUID(rx typex.RuleX) func(l *lua.LState) int

* * 生成uuid: local uuid = uuid:make() *

func MatchBinary

func MatchBinary(rx typex.RuleX) func(*lua.LState) int

* * 二进制匹匹配, [<|> K1:LEN1 K2:LEN2... ]返回一个K-V table * 其中K是字符串, V是二进制字符串 *

func MatchBinaryHex

func MatchBinaryHex(rx typex.RuleX) func(*lua.LState) int

* * 二进制串转成十六进制串 *

func MatchHex

func MatchHex(rx typex.RuleX) func(*lua.LState) int

*--------------------------------------------------------------------------- * 十六进制字符串匹配: MatchHex("FFFFFF014CB2AA55", "age:[1,232];sex:[4,5]") *---------------------------------------------------------------------------

func MatchUInt added in v0.5.1

func MatchUInt(rx typex.RuleX) func(*lua.LState) int

* * 匹配十六进制转成整数 * MatchHex("FFFFFF014CB2AA55", "age:[1,1];sex:[4,5]") *

func NewJsonDecodeLib

func NewJsonDecodeLib() typex.XLib

func NewJsonEncodeLib

func NewJsonEncodeLib() typex.XLib

func NtpTime

func NtpTime(rx typex.RuleX) func(l *lua.LState) int

* * NTP Server Time * return: ntp time string, error *

func Output added in v0.6.3

func Output(rx typex.RuleX) func(l *lua.LState) int

* * 对外输出数据,这个函数的数据会被推到 yqueue 里面,然后yqueue会再次把数据推到其实现的各种pipe里, * 可能是个 websocket,也可能是个 TCP 或者 UDP Server。 *

func ParseGPS

func ParseGPS(rx typex.RuleX) func(*lua.LState) int

* * 解析GPS数据:"$GPRMC,220516,A,5133.82,N,00042.24,W,173.8,231.8,130694,004.2,W*70" *

func PingIp added in v0.6.5

func PingIp(rx typex.RuleX) func(l *lua.LState) int

* * Ping *

func PlayMusic added in v0.6.3

func PlayMusic(rx typex.RuleX) func(l *lua.LState) int

* * applib:PlayMusic('001.mp3') *

func Println added in v0.6.4

func Println(rx typex.RuleX) func(*lua.LState) int

* * Println *

func PropertyReplyFailed

func PropertyReplyFailed(rx typex.RuleX) func(l *lua.LState) int

* * 属性下发到设备,回复失败 *

func PropertyReplySuccess

func PropertyReplySuccess(rx typex.RuleX) func(l *lua.LState) int

* * 属性下发到设备,回复成功 *

func RASPI4_GPIOGet

func RASPI4_GPIOGet(rx typex.RuleX) func(*lua.LState) int

* * 读GPIO, lua的函数调用应该是这样: rhinopi:GPIOGet(pin) -> v,error *

func RASPI4_GPIOSet

func RASPI4_GPIOSet(rx typex.RuleX) func(*lua.LState) int

* * 写GPIO, lua的函数调用应该是这样: rhinopi:GPIOSet(pin, v) -> error *

func RPCDecode

func RPCDecode(rx typex.RuleX) func(*lua.LState) int

func RPCEncode

func RPCEncode(rx typex.RuleX) func(*lua.LState) int

* * GRPC 编码 *

func ReadDevice

func ReadDevice(rx typex.RuleX) func(*lua.LState) int

func ReadSource

func ReadSource(rx typex.RuleX) func(*lua.LState) int

* * 从资源里面读数据出来 *

func Request added in v0.6.4

func Request(rx typex.RuleX) func(*lua.LState) int

* * GRPC 解码 *

func ReverseBits

func ReverseBits(b byte) byte

* * 逆转位顺序 *

func ReverseByteOrder

func ReverseByteOrder(b []byte) []byte

* * 逆转字节顺序 *

func ReverseString

func ReverseString(text string) string

* * 逆转字符串 *

Reverse reverses the input while respecting UTF8 encoding and combined characters

func SelfRuleUUID

func SelfRuleUUID(rx typex.RuleX, uuid string) func(*lua.LState) int

* * 获取当前的规则UUID *

func SetModelValue

func SetModelValue(rx typex.RuleX) func(*lua.LState) int

* * 改变模型值 *

func Sleep

func Sleep(rx typex.RuleX) func(l *lua.LState) int

* * 等待毫秒 *

func StoreDelete

func StoreDelete(rx typex.RuleX) func(l *lua.LState) int

func StoreGet

func StoreGet(rx typex.RuleX) func(l *lua.LState) int

func StoreSet

func StoreSet(rx typex.RuleX) func(l *lua.LState) int

func StoreSetWithDuration added in v0.6.7

func StoreSetWithDuration(rx typex.RuleX) func(l *lua.LState) int

func SubStr added in v0.6.2

func SubStr(data string, start, end int) (string, error)

SubStr 字符串截取子串,遵循前闭后开原则[start:end)

func T2Str

func T2Str(rx typex.RuleX) func(l *lua.LState) int

* * Table 转成 String *

func Throw

func Throw(rx typex.RuleX) func(l *lua.LState) int

func Time

func Time(rx typex.RuleX) func(l *lua.LState) int

* * 时间字符串 2006-01-02 15:04:05 *

func TimeMs added in v0.6.3

func TimeMs(rx typex.RuleX) func(l *lua.LState) int

func TruncateFloat added in v0.6.3

func TruncateFloat(rx typex.RuleX) func(*lua.LState) int

* * 取小数位 applib:Float(number, decimalPlaces) -> float *

func TsUnix

func TsUnix(rx typex.RuleX) func(l *lua.LState) int

* * Unix 时间戳 *

func TsUnixNano

func TsUnixNano(rx typex.RuleX) func(l *lua.LState) int

* * Unix 纳秒时间戳 *

func UrlBuild

func UrlBuild(rx typex.RuleX) func(*lua.LState) int

* * build *

func UrlBuildQS

func UrlBuildQS(rx typex.RuleX) func(*lua.LState) int

func UrlParse

func UrlParse(rx typex.RuleX) func(*lua.LState) int

func UrlResolve

func UrlResolve(rx typex.RuleX) func(*lua.LState) int

func VerifyJqExpression

func VerifyJqExpression(jqExpression string) (*gojq.Query, error)

func WKYWS1608_GPIOGet added in v0.5.2

func WKYWS1608_GPIOGet(rx typex.RuleX) func(*lua.LState) int

* * 读GPIO, lua的函数调用应该是这样: ws1608:GPIOGet(pin) -> v,error *

func WKYWS1608_GPIOSet added in v0.5.2

func WKYWS1608_GPIOSet(rx typex.RuleX) func(*lua.LState) int

* * 写GPIO, lua的函数调用应该是这样: ws1608:GPIOSet(pin, v) -> error *

func WriteDevice

func WriteDevice(rx typex.RuleX) func(*lua.LState) int

* * 写数据 *

func WriteSource

func WriteSource(rx typex.RuleX) func(l *lua.LState) int

* * 向资源写入数据 *

func XOR

func XOR(rx typex.RuleX) func(*lua.LState) int

* * XOR 校验 *

Types

type Fun

type Fun struct {
	NameSpace   string        // 函数名
	FunName     string        // 函数名
	FunArgs     []FunArg      // 函数参数
	ReturnValue []ReturnValue // 函数返回值
	Description string        // 描述文本
	Example     string        // 示例
}

func (*Fun) BuildSection

func (fun *Fun) BuildSection() string

type FunArg

type FunArg struct {
	Pos         int
	Type        string
	Description string
}

* * 生成文档的最小单元 *

type HexSegment

type HexSegment struct {
	Name  string
	Value []byte
}

* * 十六进制字节表示字符串 *

func MatchHexLib

func MatchHexLib(regExpr, hexStr string) []HexSegment

* * 匹配十六进制字符 *

func (HexSegment) ToHexString

func (sgm HexSegment) ToHexString() string

func (HexSegment) ToUInt64 added in v0.5.1

func (sgm HexSegment) ToUInt64() uint64

* * 大端表示法 *

func (HexSegment) ToUint16 added in v0.5.1

func (sgm HexSegment) ToUint16() uint16

* * 大端表示法 *

func (HexSegment) ToUint32 added in v0.5.1

func (sgm HexSegment) ToUint32() uint32

* * 大端表示法 *

func (HexSegment) ToUint8 added in v0.6.5

func (sgm HexSegment) ToUint8() uint8

type JsonDecodeLib

type JsonDecodeLib struct {
}

* * JsonDecodeLib, _Decode converts the JSON encoded data to Lua values. *

func (*JsonDecodeLib) LibFun

func (l *JsonDecodeLib) LibFun(rx typex.RuleX) func(*lua.LState) int

func (*JsonDecodeLib) Name

func (l *JsonDecodeLib) Name() string

type JsonEncodeLib

type JsonEncodeLib struct {
}

* * JsonEncodeLib,_Encode converts Lua values to JSON. *

func (*JsonEncodeLib) LibFun

func (l *JsonEncodeLib) LibFun(rx typex.RuleX) func(*lua.LState) int

func (*JsonEncodeLib) Name

func (l *JsonEncodeLib) Name() string

type Kl

type Kl struct {
	K  string //Key
	L  uint   //Length
	BS string //BitString
}

func Match

func Match(expr string, data []byte, returnMore bool) []Kl

func (Kl) String

func (k Kl) String() string

func (Kl) ToByte

func (k Kl) ToByte() []byte

func (Kl) ToHexString

func (k Kl) ToHexString() string

type ModbusW

type ModbusW struct {
	SlaverId byte   // 从机ID
	Function int    // 功能码
	Address  uint16 // 地址
	Quantity uint16 // 读写数量
	Value    []byte // 值
}

* * 这里规定一些LUA和golang的类型映射 * local t = { * ["type"] = 5, * ["params"] = { * ["address"] = 1, * ["quantity"] = 1, * ["value"] = 0xFF00 * } * } *

type ReturnValue

type ReturnValue struct {
	Pos         int
	Type        string
	Description string
}

type RulexLibDoc

type RulexLibDoc struct {
	Name        string
	Version     string
	ReleaseTime string
	Funcs       []Fun
}

func (*RulexLibDoc) AddFunc

func (doc *RulexLibDoc) AddFunc(f Fun)

func (*RulexLibDoc) BuildDoc

func (doc *RulexLibDoc) BuildDoc()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL