提交 4a9dd81d 编写于 作者: J Jason

添加了project和template,未完成

上级 858e4159
package device
type device struct {
}
package device
import (
"iot-master/model"
"iot-master/types"
)
type project struct {
model.Project
//TODO 支持多个链接
link types.Link
devices types.Device
}
......@@ -4,7 +4,9 @@ import "time"
type Device struct {
Id int64 `json:"id"`
ProjectId int64 `json:"project_id"`
ProjectId int64 `json:"project_id"`
Created time.Time `json:"created" storm:"created"`
Slave uint8 `json:"slave"` //从站号
Created time.Time `json:"created" xorm:"created"`
}
......@@ -3,19 +3,17 @@ package model
import "time"
type Element struct {
Id int64 `json:"id"`
UUID string `json:"uuid" storm:"unique"` //唯一码,自动生成
Name string `json:"name"`
Description string `json:"description"`
Origin string `json:"origin"` //来源
Id int64 `json:"id"`
Origin string `json:"origin"` //平台UUID,自动生成
Name string `json:"name"`
Manufacturer string `json:"manufacturer"` //厂商
Model string `json:"model"` //型号
Version string `json:"version"` //版本
Variables []ElementVariable `json:"variables" xorm:"json"` //变量
Created time.Time `json:"created" storm:"created"`
Created time.Time `json:"created" xorm:"created"`
}
//Modbus
......
......@@ -5,9 +5,9 @@ import "time"
type Plugin struct {
Id int64 `json:"id"`
Name string `json:"name"`
Key string `json:"key" storm:"unique"`
Key string `json:"key" xorm:"unique"`
Secret string `json:"secret"`
Disabled bool `json:"disabled"`
Created time.Time `json:"created" storm:"created"`
Updated time.Time `json:"updated" storm:"updated"`
Created time.Time `json:"created" xorm:"created"`
Updated time.Time `json:"updated" xorm:"updated"`
}
......@@ -6,48 +6,17 @@ import (
type Project struct {
Id int64 `json:"id"`
UUID string `json:"uuid" storm:"unique"` //唯一码,自动生成
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Disabled bool `json:"disabled"`
LinkId int `json:"link_id"`
Protocol string `json:"protocol"`
Manifest TemplateManifest `json:"manifest" xorm:"json"`
Variables []ProjectVariable `json:"variables"`
Validators []ProjectValidator `json:"validators"`
Functions []ProjectFunction `json:"functions"`
Strategies []ProjectStrategy `json:"strategies"`
//模板项目
IsTemplate bool `json:"is_template"`
TemplateId int64 `json:"template_id"`
Created time.Time `json:"created" xorm:"created"`
}
type ProjectVariable struct {
Element string `json:"element"` //uuid
Variable `storm:"inline"`
//TODO 添加采样周期
}
type ProjectValidator struct {
Alert string `json:"alert"`
Expression string `json:"expression"` //表达式,检测变量名
}
type ProjectFunction struct {
ID int `json:"id"`
Name string `json:"name"` //项目功能脚本唯一,供外部调用
Description string `json:"description"`
Script string `json:"script"` //javascript
Operators map[string]interface{} `json:"operators"`
}
type ProjectStrategy struct {
Name string `json:"name"`
Cron string `json:"cron"`
Expression string `json:"expression"` //触发条件 表达式,检测变量名
Script string `json:"script"` //javascript
Operators map[string]interface{} `json:"operators"`
}
package model
import (
"time"
)
type Template struct {
Id int64 `json:"id"`
Origin string `json:"origin"` //平台UUID,自动生成
Name string `json:"name"`
Description string `json:"description"`
Version string `json:"version"`
Manifest TemplateManifest `json:"manifest" xorm:"json"`
Created time.Time `json:"created" xorm:"created"`
}
type TemplateManifest struct {
//协议
Protocol string `json:"protocol"`
ProtocolOptions string `json:"protocol_options"`
//变量等
Variables []TemplateVariable `json:"variables"`
Validators []TemplateValidator `json:"validators"`
Functions []TemplateFunction `json:"functions"`
Strategies []TemplateStrategy `json:"strategies"`
}
type TemplateVariable struct {
Element string `json:"element"` //uuid
Slave uint8 `json:"slave"` //从站号
Variable
//TODO 添加采样周期
}
type TemplateValidator struct {
Alert string `json:"alert"`
Expression string `json:"expression"` //表达式,检测变量名
}
type TemplateFunction struct {
Name string `json:"name"` //项目功能脚本唯一,供外部调用
Description string `json:"description"`
Script string `json:"script"` //javascript
Operators map[string]interface{} `json:"operators"`
}
type TemplateStrategy struct {
Name string `json:"name"`
Cron string `json:"cron"`
Expression string `json:"expression"` //触发条件 表达式,检测变量名
Script string `json:"script"` //javascript
Operators map[string]interface{} `json:"operators"`
}
......@@ -9,21 +9,22 @@ type Tunnel struct {
Addr string `json:"addr"`
Timeout int `json:"timeout"`
//注册包
RegisterEnable bool `json:"register_enable"`
RegisterRegex string `json:"register_regex"`
RegisterMin int `json:"register_min"`
RegisterMax int `json:"register_max"`
//心跳包
HeartBeatEnable bool `json:"heart_beat_enable"`
HeartBeatInterval int `json:"heart_beat_interval"`
HeartBeatContent string `json:"heart_beat_content"`
HeartBeatIsHex bool `json:"heart_beat_is_hex"`
Disabled bool `json:"disabled"`
Active bool `json:"active"`
//模板ID,根据模板ID自动创建项目
TemplateId int `json:"template_id"`
TemplateId int `json:"template_id"` //模板ID
Disabled bool `json:"disabled"`
Created time.Time `json:"created" xorm:"created"`
Updated time.Time `json:"updated" xorm:"updated"`
......
package protocol
import (
"iot-master/base"
)
type Adapter interface {
Attach(link base.Link)
OnData(data []byte)
Read(slave uint8, code uint8, offset uint16, size uint16) ([]byte, error)
Write(slave uint8, code uint8, offset uint16, buf []byte) error
}
......@@ -3,9 +3,9 @@ package modbus
import (
"errors"
"fmt"
"iot-master/base"
"iot-master/protocol"
"iot-master/protocol/helper"
"iot-master/types"
)
func init() {
......@@ -27,7 +27,7 @@ type response struct {
}
type RTU struct {
link base.Link
link types.Link
resp chan response
}
......@@ -37,62 +37,58 @@ func NewModbusRtu(opts string) (protocol.Adapter, error) {
}, nil
}
func (m *RTU) OnData(buf []byte) {
func (m *RTU) Attach(link base.Link) {
m.link = link
link.Listen(func(buf []byte) {
//解析数据
l := len(buf)
crc := helper.ParseUint16(buf[l-2:])
//解析数据
l := len(buf)
crc := helper.ParseUint16(buf[l-2:])
if crc != CRC16(buf[:l-2]) {
//检验错误
m.resp <- response{err: errors.New("校验错误")}
return
}
if crc != CRC16(buf[:l-2]) {
//检验错误
m.resp <- response{err: errors.New("校验错误")}
return
//解析错误码
if buf[1]&0x80 > 0 {
m.resp <- response{err: fmt.Errorf("错误码:%d", buf[2])}
return
}
//解析数据
length := 4
count := int(helper.ParseUint16(buf[1:]))
switch buf[1] {
case FuncCodeReadDiscreteInputs,
FuncCodeReadCoils:
length += 1 + count/8
if count%8 != 0 {
length++
}
//解析错误码
if buf[1]&0x80 > 0 {
m.resp <- response{err: fmt.Errorf("错误码:%d", buf[2])}
if l < length {
//长度不够
m.resp <- response{err: errors.New("长度不够")}
return
}
//解析数据
length := 4
count := int(helper.ParseUint16(buf[1:]))
switch buf[1] {
case FuncCodeReadDiscreteInputs,
FuncCodeReadCoils:
length += 1 + count/8
if count%8 != 0 {
length++
}
if l < length {
//长度不够
m.resp <- response{err: errors.New("长度不够")}
return
}
b := buf[2 : l-2]
//数组解压
bb := helper.ExpandBool(b, count)
m.resp <- response{buf: bb}
case FuncCodeReadInputRegisters,
FuncCodeReadHoldingRegisters,
FuncCodeReadWriteMultipleRegisters:
length += 1 + count*2
if l < length {
//长度不够
m.resp <- response{err: errors.New("长度不够")}
return
}
b := buf[2 : l-2]
m.resp <- response{buf: b}
default:
m.resp <- response{}
b := buf[2 : l-2]
//数组解压
bb := helper.ExpandBool(b, count)
m.resp <- response{buf: bb}
case FuncCodeReadInputRegisters,
FuncCodeReadHoldingRegisters,
FuncCodeReadWriteMultipleRegisters:
length += 1 + count*2
if l < length {
//长度不够
m.resp <- response{err: errors.New("长度不够")}
return
}
})
b := buf[2 : l-2]
m.resp <- response{buf: b}
default:
m.resp <- response{}
}
}
func (m *RTU) Read(slave uint8, code uint8, offset uint16, size uint16) ([]byte, error) {
......
......@@ -6,25 +6,21 @@ import (
"errors"
"fmt"
"git.zgwit.com/iot/beeq/packet"
"iot-master/base"
"iot-master/dbus"
"iot-master/model"
"iot-master/protocol"
"iot-master/types"
"log"
"net"
"sync"
"time"
)
type Link interface {
Write(buf []byte) error
Close() error
}
type link struct {
model.Link
//指向通道
tunnel Tunnel
tunnel types.Tunnel
//设备连接
conn net.Conn
......@@ -32,15 +28,25 @@ type link struct {
//发送缓存
cache [][]byte
peer base.OnDataFunc
listener base.OnDataFunc
peer types.OnDataFunc
listener types.LinkListener
lastTime time.Time
//协议
adapter protocol.Adapter
//项目
project types.Project
//设备,以从站号为KEY
devices sync.Map //<slave, Device>
}
func (l *link) onData(buf []byte) {
//过滤心跳
c := l.tunnel.GetTunnel()
c := l.tunnel.GetModel()
if c.HeartBeatEnable && time.Now().Sub(l.lastTime) > time.Second*time.Duration(c.HeartBeatInterval) {
var b []byte
if c.HeartBeatIsHex {
......@@ -60,7 +66,7 @@ func (l *link) onData(buf []byte) {
//计数
//ln := len(buf)
//l.Rx += ln
//l.tunnel.GetTunnel().Rx += ln
//l.tunnel.GetModel().Rx += ln
l.lastTime = time.Now()
......@@ -72,7 +78,7 @@ func (l *link) onData(buf []byte) {
//响应数据
if l.listener != nil {
l.listener(buf)
l.listener.OnData(buf)
}
//发送至MQTT
......@@ -98,7 +104,7 @@ func (l *link) Write(buf []byte) error {
//ln := len(buf)
//l.Tx += ln
//l.tunnel.GetTunnel().Tx += ln
//l.tunnel.GetModel().Tx += ln
l.lastTime = time.Now()
......@@ -135,7 +141,7 @@ func (l *link) Close() error {
return err
}
func (l *link) Attach(listener base.OnDataFunc) error {
func (l *link) Attach(listener types.OnDataFunc) error {
//check peer
l.peer = listener
return nil
......@@ -148,16 +154,16 @@ func (l *link) Detach() error {
return nil
}
func (l *link) Listen(listener base.OnDataFunc) {
func (l *link) Listen(listener types.LinkListener) {
l.listener = listener
}
func newLink(t Tunnel, conn net.Conn) *link {
c := t.GetTunnel()
func newLink(t types.Tunnel, conn net.Conn) *link {
c := t.GetModel()
return &link{
Link: model.Link{
TunnelId: c.Id,
Active: true,
Active: true,
},
tunnel: t,
conn: conn,
......@@ -165,15 +171,15 @@ func newLink(t Tunnel, conn net.Conn) *link {
}
}
func newPacketLink(ch Tunnel, conn net.PacketConn, addr net.Addr) *link {
c := ch.GetTunnel()
func newPacketLink(ch types.Tunnel, conn net.PacketConn, addr net.Addr) *link {
c := ch.GetModel()
return &link{
Link: model.Link{
TunnelId: c.Id,
Active: true,
Active: true,
},
tunnel: ch,
conn: base.NewPackConn(conn, addr),
conn: NewPackConn(conn, addr),
cache: make([][]byte, 0),
}
}
package base
package tunnel
import (
"errors"
......
......@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"iot-master/model"
"iot-master/types"
"regexp"
"sync"
)
......@@ -11,21 +12,13 @@ import (
var tunnels sync.Map
//通道
type Tunnel interface {
Open() error
Close() error
GetTunnel() *model.Tunnel
GetLink(id int64) (Link, error)
}
type tunnel struct {
model.Tunnel
//model.ProjectAdapter
}
func (t *tunnel) GetTunnel() *model.Tunnel {
func (t *tunnel) GetModel() *model.Tunnel {
return &t.Tunnel
}
......@@ -51,7 +44,7 @@ func (t *tunnel) checkRegister(buf []byte) (string, error) {
return serial, nil
}
func NewTunnel(t *model.Tunnel) (Tunnel, error) {
func NewTunnel(t *model.Tunnel) (types.Tunnel, error) {
switch t.Type {
case "tcp-server":
return &TcpServer{
......@@ -86,7 +79,7 @@ func NewTunnel(t *model.Tunnel) (Tunnel, error) {
}
func StartTunnel(c *model.Tunnel) (Tunnel, error) {
func StartTunnel(c *model.Tunnel) (types.Tunnel, error) {
//log.Println("Start core", c)
tunnel, err := NewTunnel(c)
if err != nil {
......@@ -106,18 +99,18 @@ func DeleteTunnel(id int64) error {
return errors.New("通道不存在")
}
tunnels.Delete(id)
return v.(Tunnel).Close()
return v.(types.Tunnel).Close()
}
func GetTunnel(id int64) (Tunnel, error) {
func GetTunnel(id int64) (types.Tunnel, error) {
v, ok := tunnels.Load(id)
if !ok {
return nil, errors.New("通道不存在")
}
return v.(Tunnel), nil
return v.(types.Tunnel), nil
}
func GetLink(tunnelId, linkId int64) (Link, error) {
func GetLink(tunnelId, linkId int64) (types.Link, error) {
t, err := GetTunnel(tunnelId)
if err != nil {
return nil, err
......
......@@ -3,6 +3,7 @@ package tunnel
import (
"iot-master/db"
"iot-master/model"
"iot-master/types"
"log"
"net"
)
......@@ -39,7 +40,7 @@ func (c *TcpUdpClient) Close() error {
return nil
}
func (c *TcpUdpClient) GetLink(id int64)(Link, error) {
func (c *TcpUdpClient) GetLink(id int64)(types.Link, error) {
return c.link, nil
}
......
......@@ -5,7 +5,8 @@ import (
"fmt"
"iot-master/db"
"iot-master/model"
"log"
"iot-master/types"
"log"
"net"
"sync"
"time"
......@@ -48,7 +49,7 @@ func (s *TcpServer) Close() error {
return nil
}
func (s *TcpServer) GetLink(id int64)(Link, error) {
func (s *TcpServer) GetLink(id int64)(types.Link, error) {
return s.getLink(id)
}
......
......@@ -5,6 +5,7 @@ import (
"fmt"
"iot-master/db"
"iot-master/model"
"iot-master/types"
"log"
"net"
"sync"
......@@ -45,7 +46,7 @@ func (c *PacketServer) Close() error {
return nil
}
func (c *PacketServer) GetLink(id int64)(Link, error) {
func (c *PacketServer) GetLink(id int64)(types.Link, error) {
return c.getLink(id)
}
......
package types
type Device interface {
}
package base
package types
//连接
type Link interface {
......@@ -8,8 +8,12 @@ type Link interface {
Attach(peer OnDataFunc) error
Detach() error
Listen(listener OnDataFunc)
Listen(listener LinkListener)
//UnListen()
}
type LinkListener interface {
OnData([]byte)
}
type OnDataFunc func([]byte)
package types
type Project interface {
}
package types
import "iot-master/model"
//通道
type Tunnel interface {
Open() error
Close() error
GetModel() *model.Tunnel
GetLink(id int64) (Link, error)
}
package api
import (
"iot-master/model"
"github.com/google/uuid"
"iot-master/model"
)
func elementBeforeCreate(data interface{}) error {
element := data.(*model.Element)
element.UUID = uuid.New().String()
if element.Origin == "" {
element.Origin = uuid.New().String()
}
return nil
}
......
package api
import (
"iot-master/model"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func projectBeforeCreate(data interface{}) error {
project := data.(*model.Project)
project.UUID = uuid.New().String()
return nil
}
......
package api
import (
"github.com/google/uuid"
"iot-master/model"
)
func templateBeforeCreate(data interface{}) error {
template := data.(*model.Template)
if template.Origin == "" {
template.Origin = uuid.New().String()
}
return nil
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册