主程序保留
This commit is contained in:
@@ -8,13 +8,10 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"omc/handle/model"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/aceld/zinx/zconf"
|
||||
"github.com/aceld/zinx/zlog"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
@@ -31,7 +28,7 @@ type TcpClient struct {
|
||||
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))
|
||||
headBuf := bytes.NewReader(headdata)
|
||||
|
||||
@@ -66,7 +63,7 @@ func (this *TcpClient) Unpack(headdata []byte) (head *Message, err error) {
|
||||
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{})
|
||||
|
||||
// Write the oxffff
|
||||
@@ -97,10 +94,10 @@ func (this *TcpClient) Pack(msgID uint32, dataBytes []byte) (out []byte, err err
|
||||
return
|
||||
}
|
||||
|
||||
func (this *TcpClient) SendMsg(msgID uint32, data []byte) {
|
||||
sendData, err := this.Pack(msgID, data)
|
||||
func (s *TcpClient) SendMsg(msgID uint32, data []byte) {
|
||||
sendData, err := s.Pack(msgID, data)
|
||||
if err == nil {
|
||||
_, err := this.conn.Write(sendData)
|
||||
_, err := s.conn.Write(sendData)
|
||||
fmt.Println("send msg:", hex.EncodeToString(sendData), " err:", err)
|
||||
} else {
|
||||
fmt.Println(err)
|
||||
@@ -108,17 +105,17 @@ func (this *TcpClient) SendMsg(msgID uint32, data []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func (this *TcpClient) Receive() {
|
||||
func (s *TcpClient) Receive() {
|
||||
for {
|
||||
//读取服务端发来的数据 ==》 SyncPID
|
||||
//1.读取8字节
|
||||
////第一次读取,读取数据头
|
||||
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)
|
||||
return
|
||||
}
|
||||
pkgHead, err := this.Unpack(headData)
|
||||
pkgHead, err := s.Unpack(headData)
|
||||
if err != nil {
|
||||
fmt.Println("Unpack", err)
|
||||
return
|
||||
@@ -126,7 +123,7 @@ func (this *TcpClient) Receive() {
|
||||
//data
|
||||
if pkgHead.LenOfBody > 0 {
|
||||
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
|
||||
}
|
||||
}
|
||||
@@ -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"
|
||||
this.SendMsg(0x01, []byte(data))
|
||||
// data := "reqLoginAlarm;user=audit;key=omc@password;type=ftp"
|
||||
// s.SendMsg(1, []byte(data))
|
||||
|
||||
//发送同步告警信息
|
||||
//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=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"
|
||||
this.SendMsg(11, []byte(data))
|
||||
go this.Receive()
|
||||
// data = "reqSyncAlarmFile;reqId=34;startTime=2023-01-08 16:07:00;endTime=2023-07-19 23:59:59;syncSource=1"
|
||||
|
||||
}
|
||||
|
||||
func DataMock() {
|
||||
conf := "root:1000omc@kp!@tcp(192.168.4.130:33066)/omc_db?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
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)
|
||||
}
|
||||
// 心跳
|
||||
data := "reqHeartBeat;reqId=33"
|
||||
s.SendMsg(8, []byte(data))
|
||||
go s.Receive()
|
||||
|
||||
}
|
||||
|
||||
@@ -206,14 +175,14 @@ func NewTcpClient(ip string, port int) *TcpClient {
|
||||
isOnline: make(chan bool),
|
||||
}
|
||||
|
||||
fmt.Println(fmt.Sprintf("conn: %+v. Connected to server...", conn))
|
||||
fmt.Printf("conn: %+v. Connected to server...", conn)
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
func main() {
|
||||
client := NewTcpClient("192.168.4.130", 31232)
|
||||
func TestFile(t *testing.T) {
|
||||
client := NewTcpClient("127.0.0.1", 31232)
|
||||
client.Start()
|
||||
//DataMock()
|
||||
//防止进程退出,等待中断信号
|
||||
select {}
|
||||
}
|
||||
Reference in New Issue
Block a user