主程序保留

This commit is contained in:
TsMask
2023-08-25 10:52:29 +08:00
parent 187ada0f10
commit 10b5a32c4a

View File

@@ -8,13 +8,10 @@ import (
"fmt" "fmt"
"io" "io"
"net" "net"
"omc/handle/model" "testing"
"time" "time"
"github.com/aceld/zinx/zconf" "github.com/aceld/zinx/zconf"
"github.com/aceld/zinx/zlog"
"gorm.io/driver/mysql"
"gorm.io/gorm"
) )
type Message struct { type Message struct {
@@ -31,7 +28,7 @@ type TcpClient struct {
isOnline chan bool isOnline chan bool
} }
func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) { func (s *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
fmt.Println("unpack:", hex.EncodeToString(headdata)) fmt.Println("unpack:", hex.EncodeToString(headdata))
headBuf := bytes.NewReader(headdata) headBuf := bytes.NewReader(headdata)
@@ -66,7 +63,7 @@ func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
return head, nil return head, nil
} }
func (this *TcpClient) Pack(msgID uint32, dataBytes []byte) (out []byte, err error) { func (s *TcpClient) Pack(msgID uint32, dataBytes []byte) (out []byte, err error) {
outbuff := bytes.NewBuffer([]byte{}) outbuff := bytes.NewBuffer([]byte{})
// Write the oxffff // Write the oxffff
@@ -97,10 +94,10 @@ func (this *TcpClient) Pack(msgID uint32, dataBytes []byte) (out []byte, err err
return return
} }
func (this *TcpClient) SendMsg(msgID uint32, data []byte) { func (s *TcpClient) SendMsg(msgID uint32, data []byte) {
sendData, err := this.Pack(msgID, data) sendData, err := s.Pack(msgID, data)
if err == nil { if err == nil {
_, err := this.conn.Write(sendData) _, err := s.conn.Write(sendData)
fmt.Println("send msg:", hex.EncodeToString(sendData), " err:", err) fmt.Println("send msg:", hex.EncodeToString(sendData), " err:", err)
} else { } else {
fmt.Println(err) fmt.Println(err)
@@ -108,17 +105,17 @@ func (this *TcpClient) SendMsg(msgID uint32, data []byte) {
return return
} }
func (this *TcpClient) Receive() { func (s *TcpClient) Receive() {
for { for {
//读取服务端发来的数据 ==》 SyncPID //读取服务端发来的数据 ==》 SyncPID
//1.读取8字节 //1.读取8字节
////第一次读取,读取数据头 ////第一次读取,读取数据头
headData := make([]byte, 9) headData := make([]byte, 9)
if _, err := io.ReadFull(this.conn, headData); err != nil { if _, err := io.ReadFull(s.conn, headData); err != nil {
fmt.Println(err) fmt.Println(err)
return return
} }
pkgHead, err := this.Unpack(headData) pkgHead, err := s.Unpack(headData)
if err != nil { if err != nil {
fmt.Println("Unpack", err) fmt.Println("Unpack", err)
return return
@@ -126,7 +123,7 @@ func (this *TcpClient) Receive() {
//data //data
if pkgHead.LenOfBody > 0 { if pkgHead.LenOfBody > 0 {
pkgHead.Value = make([]byte, pkgHead.LenOfBody) pkgHead.Value = make([]byte, pkgHead.LenOfBody)
if _, err := io.ReadFull(this.conn, pkgHead.Value); err != nil { if _, err := io.ReadFull(s.conn, pkgHead.Value); err != nil {
return return
} }
} }
@@ -143,10 +140,10 @@ func (this *TcpClient) Receive() {
} }
} }
func (this *TcpClient) Start() { func (s *TcpClient) Start() {
//登录 //登录
data := "reqLoginAlarm;user=audit;key=omc@password;type=ftp" // data := "reqLoginAlarm;user=audit;key=omc@password;type=ftp"
this.SendMsg(0x01, []byte(data)) // s.SendMsg(1, []byte(data))
//发送同步告警信息 //发送同步告警信息
//data = "reqSyncAlarmMsg;reqId=33;alarmSeq=1" //data = "reqSyncAlarmMsg;reqId=33;alarmSeq=1"
@@ -155,40 +152,12 @@ func (this *TcpClient) Start() {
//发送文件同步告警 //发送文件同步告警
//data = "reqSyncAlarmFile;reqId=35;alarmSeq=2000;syncSource=1" //data = "reqSyncAlarmFile;reqId=35;alarmSeq=2000;syncSource=1"
//data = "reqSyncAlarmFile;reqId=33;startTime=2023-01-08 00:00:00;syncSource=0" //data = "reqSyncAlarmFile;reqId=33;startTime=2023-01-08 00:00:00;syncSource=0"
data = "reqSyncAlarmFile;reqId=34;startTime=2023-01-08 16:07:00;endTime=2023-07-19 23:59:59;syncSource=1" // data = "reqSyncAlarmFile;reqId=34;startTime=2023-01-08 16:07:00;endTime=2023-07-19 23:59:59;syncSource=1"
this.SendMsg(11, []byte(data))
go this.Receive()
} // 心跳
data := "reqHeartBeat;reqId=33"
func DataMock() { s.SendMsg(8, []byte(data))
conf := "root:1000omc@kp!@tcp(192.168.4.130:33066)/omc_db?charset=utf8mb4&parseTime=True&loc=Local" go s.Receive()
d, err := gorm.Open(mysql.Open(conf), &gorm.Config{})
if err != nil {
zlog.Ins().ErrorF("open mysql %s error, ", conf, err)
panic(err)
}
var alarms []model.Alarm
if err := d.Model(&model.Alarm{}).Where("ne_type = ? and ne_id = ? and alarm_seq >= ?", "SMF", "SZ_01", 0).Order("alarm_seq asc").Limit(5).Find(&alarms).Error; err != nil {
zlog.Ins().ErrorF("open mysql %s error, ", conf, err)
return
}
var alarm model.Alarm
if err := d.Model(&model.Alarm{}).Where("ne_type = ? and ne_id = ?", "SMF", "SZ_01").Order("alarm_seq desc").First(&alarm).Error; err != nil {
zlog.Ins().ErrorF("db error %s", err)
return
}
index := alarm.AlarmSeq + 1
for {
for _, v := range alarms {
v.AlarmSeq = index
v.Id = 0
d.Create(&v)
index++
}
time.Sleep(10 * time.Microsecond)
}
} }
@@ -206,14 +175,14 @@ func NewTcpClient(ip string, port int) *TcpClient {
isOnline: make(chan bool), isOnline: make(chan bool),
} }
fmt.Println(fmt.Sprintf("conn: %+v. Connected to server...", conn)) fmt.Printf("conn: %+v. Connected to server...", conn)
return client return client
} }
func main() { func TestFile(t *testing.T) {
client := NewTcpClient("192.168.4.130", 31232) client := NewTcpClient("127.0.0.1", 31232)
client.Start() client.Start()
//DataMock() //防止进程退出,等待中断信号
select {} select {}
} }