Skip to content

Commit

Permalink
dev: fix and re-design some module
Browse files Browse the repository at this point in the history
  • Loading branch information
wwhai committed Jul 27, 2022
1 parent c591688 commit 639ed1f
Show file tree
Hide file tree
Showing 23 changed files with 328 additions and 207 deletions.
20 changes: 20 additions & 0 deletions common/binary_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ func SetABitOnByte(b *byte, position uint8, value bool) (byte, error) {

}

/*
*
* 字符串转字节
*
*/
func BitStringToBytes(s string) ([]byte, error) {
if len(s)%8 != 0 {
return nil, errors.New("length must be integer multiple of 8")
}
b := make([]byte, (len(s)+(8-1))/8)
for i := 0; i < len(s); i++ {
c := s[i]
if c < '0' || c > '1' {
return nil, errors.New("value out of range")
}
b[i>>3] |= (c - '0') << uint(7-i&7)
}
return b, nil
}

/*
*
* 字节上某个位转逻辑值
Expand Down
2 changes: 1 addition & 1 deletion driver/modbus_rtu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (d *modBusRtuDriver) Write(data []byte) (int, error) {
if err := json.Unmarshal(data, &dataMap); err != nil {
return 0, err
}
for _, r := range d.Registers {
for _, r := range dataMap {
if r.Function == common.WRITE_SINGLE_COIL {
_, err := d.client.WriteSingleCoil(r.Address, binary.BigEndian.Uint16([]byte(r.Value)[0:2]))
if err != nil {
Expand Down
51 changes: 20 additions & 31 deletions driver/yk8_relay_controller_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package driver
//
import (
"encoding/json"
"errors"

"github.com/i4de/rulex/common"
"github.com/i4de/rulex/typex"
Expand Down Expand Up @@ -107,43 +106,33 @@ func (yk8 *YK8RelayControllerDriver) Read(data []byte) (int, error) {
copy(data, bytes)
return len(bytes), nil
}
func i2bool(v byte) bool {
if v == 0 {
return false
}
return true
}

//
// 写入数据必须是有8个布尔值的字节数组: [1,1,1,1,1,1,1,1]
// 写入数据
//
func (yk8 *YK8RelayControllerDriver) Write(data []byte) (int, error) {
if len(data) != 8 {
return 0, errors.New("操作继电器组最少8个布尔值")
dataMap := []common.RegisterRW{}
if err := json.Unmarshal(data, &dataMap); err != nil {
return 0, err
}
for _, v := range data {
if v > 1 {
return 0, errors.New("必须是逻辑值")
for _, r := range dataMap {
yk8.handler.SlaveId = r.SlaverId
bytes, err0 := common.BitStringToBytes(r.Value)
if err0 != nil {
return 0, err0
}
_, err1 := yk8.client.WriteMultipleCoils(0, 1, bytes)
if err1 != nil {
return 0, err1
}
}

Sw1 := common.ByteToBool(data[0])
Sw2 := common.ByteToBool(data[1])
Sw3 := common.ByteToBool(data[2])
Sw4 := common.ByteToBool(data[3])
Sw5 := common.ByteToBool(data[4])
Sw6 := common.ByteToBool(data[5])
Sw7 := common.ByteToBool(data[6])
Sw8 := common.ByteToBool(data[7])
var value byte
common.SetABitOnByte(&value, 0, Sw1)
common.SetABitOnByte(&value, 1, Sw2)
common.SetABitOnByte(&value, 2, Sw3)
common.SetABitOnByte(&value, 3, Sw4)
common.SetABitOnByte(&value, 4, Sw5)
common.SetABitOnByte(&value, 5, Sw6)
common.SetABitOnByte(&value, 6, Sw7)
common.SetABitOnByte(&value, 7, Sw8)

_, err := yk8.client.WriteMultipleCoils(0, 1, []byte{value})
if err != nil {
return 0, err
}
return 0, err
return 0, nil
}

//---------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions engine/load_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,19 @@ func (e *RuleEngine) RemoveDevice(uuid string) {
// 加载设备
//
func (e *RuleEngine) LoadDevice(deviceInfo *typex.Device) error {
if deviceInfo.Type == "TSS200V02" {
if deviceInfo.Type == typex.TSS200V02 {
return startDevices(device.NewTS200Sensor(e), deviceInfo, e)
}
if deviceInfo.Type == "YK8RELAY" {
if deviceInfo.Type == typex.YK08_RELAY {
return startDevices(device.NewYK8Controller(e), deviceInfo, e)
}
if deviceInfo.Type == "RTU485_THER" {
if deviceInfo.Type == typex.RTU485_THER {
return startDevices(device.NewRtu485Ther(e), deviceInfo, e)
}
if deviceInfo.Type == "S1200PLC" {
if deviceInfo.Type == typex.S1200PLC {
return startDevices(device.NewS1200plc(e), deviceInfo, e)
}
if deviceInfo.Type == "GENERIC_MODBUS" {
if deviceInfo.Type == typex.GENERIC_MODBUS {
return startDevices(device.NewGenericModbusDevice(e), deviceInfo, e)
}
return fmt.Errorf("unsupported Device type:%s", deviceInfo.Type)
Expand Down
8 changes: 4 additions & 4 deletions rulexlib/device_lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
* 写: rulexlib:WriteDevice(ID, []byte{}) -> data, err
*
*/
var __readBuffer []byte = []byte{}
var deviceReadBuffer []byte = []byte{}

func ReadDevice(rx typex.RuleX) func(*lua.LState) int {
return func(l *lua.LState) int {
devUUID := l.ToString(2)
Device := rx.GetDevice(devUUID)
if Device != nil {
n, err := Device.Device.OnRead(__readBuffer)
n, err := Device.Device.OnRead(deviceReadBuffer)
if err != nil {
glogger.GLogger.Error(err)
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(lua.LString(__readBuffer[:n]))
l.Push(lua.LString(deviceReadBuffer[:n]))
l.Push(lua.LNil)
return 2
}
Expand Down Expand Up @@ -61,6 +61,6 @@ func WriteDevice(rx typex.RuleX) func(*lua.LState) int {
}
l.Push(lua.LNil)
l.Push(lua.LString("device not exists:" + devUUID))
return 0
return 2
}
}
22 changes: 22 additions & 0 deletions rulexlib/size_t.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package rulexlib

/*
*
* 这里规定一些LUA和golang的类型映射
* local t = {
* ["type"] = 5,
* ["params"] = {
* ["address"] = 1,
* ["quantity"] = 1,
* ["value"] = 0xFF00
* }
* }
*
*/
type ModbusW struct {
SlaverId byte // 从机ID
Function int // 功能码
Address uint16 // 地址
Quantity uint16 // 读写数量
Value []byte // 值
}
69 changes: 69 additions & 0 deletions rulexlib/source_lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package rulexlib

import (
"github.com/i4de/rulex/glogger"
"github.com/i4de/rulex/typex"

lua "github.com/yuin/gopher-lua"
)

//
// rulexlib:WriteInStream('INEND', rulexlib:T2J(t))
//
var sourceReadBuffer []byte = []byte{}

/*
*
* 向资源写入数据
*
*/
func WriteSource(rx typex.RuleX) func(l *lua.LState) int {
return func(l *lua.LState) int {
uuid := l.ToString(2)
data := l.ToString(3)

if value, ok := rx.AllInEnd().Load(uuid); ok {
n, err := value.(*typex.InEnd).Source.DownStream([]byte(data))
if err != nil {
glogger.GLogger.Error(err)
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(lua.LNumber(n))
l.Push(lua.LNil)
return 2
}
l.Push(lua.LNil)
l.Push(lua.LString("source not exists:" + uuid))
return 2
}
}

/*
*
* 从资源里面读数据出来
*
*/
func ReadSource(rx typex.RuleX) func(*lua.LState) int {

return func(l *lua.LState) int {
uuid := l.ToString(2)
InEnd := rx.GetInEnd(uuid)
if InEnd != nil {
n, err := InEnd.Source.UpStream(sourceReadBuffer)
if err != nil {
glogger.GLogger.Error(err)
l.Push(lua.LNil)
l.Push(lua.LString(err.Error()))
return 2
}
l.Push(lua.LString(sourceReadBuffer[:n]))
l.Push(lua.LNil)
return 2
}
l.Push(lua.LNil)
l.Push(lua.LString("source not exists:" + uuid))
return 2
}
}
8 changes: 6 additions & 2 deletions source/coap_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ func (*coAPInEndSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*coAPInEndSource) DownStream([]byte) {}
func (*coAPInEndSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*coAPInEndSource) UpStream() {}
func (*coAPInEndSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/cs104_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ func (cs *cs104Source) Stop() {
//
// 来自外面的数据
//
func (*cs104Source) DownStream([]byte) {}
func (*cs104Source) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*cs104Source) UpStream() {}
func (*cs104Source) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/grpc_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,13 @@ func (*grpcInEndSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*grpcInEndSource) DownStream([]byte) {}
func (*grpcInEndSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*grpcInEndSource) UpStream() {}
func (*grpcInEndSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/http_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ func (*httpInEndSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*httpInEndSource) DownStream([]byte) {}
func (*httpInEndSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*httpInEndSource) UpStream() {}
func (*httpInEndSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/modbus_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,13 @@ func (*modbusMasterSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*modbusMasterSource) DownStream([]byte) {}
func (*modbusMasterSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*modbusMasterSource) UpStream() {}
func (*modbusMasterSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/mqtt_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,13 @@ func (*mqttInEndSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*mqttInEndSource) DownStream([]byte) {}
func (*mqttInEndSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*mqttInEndSource) UpStream() {}
func (*mqttInEndSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/natsio_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ func (*natsSource) Topology() []typex.TopologyPoint {
//
// 来自外面的数据
//
func (*natsSource) DownStream([]byte) {}
func (*natsSource) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*natsSource) UpStream() {}
func (*natsSource) UpStream([]byte) (int, error) {
return 0, nil
}
8 changes: 6 additions & 2 deletions source/siemens_s7_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,13 @@ func (s7 *siemensS7Source) Stop() {
//
// 来自外面的数据
//
func (*siemensS7Source) DownStream([]byte) {}
func (*siemensS7Source) DownStream([]byte) (int, error) {
return 0, nil
}

//
// 上行数据
//
func (*siemensS7Source) UpStream() {}
func (*siemensS7Source) UpStream([]byte) (int, error) {
return 0, nil
}
Loading

0 comments on commit 639ed1f

Please sign in to comment.