同步代码
This commit is contained in:
15
api/close.go
Normal file
15
api/close.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/aceld/zinx/ziface"
|
||||
"github.com/aceld/zinx/znet"
|
||||
)
|
||||
|
||||
// CloseApi 关闭连接API
|
||||
type CloseApi struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
func (*CloseApi) Handle(request ziface.IRequest) {
|
||||
request.GetConnection().Stop()
|
||||
}
|
||||
44
api/heart_beat.go
Normal file
44
api/heart_beat.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"omc/omc"
|
||||
|
||||
"github.com/aceld/zinx/ziface"
|
||||
"github.com/aceld/zinx/zlog"
|
||||
"github.com/aceld/zinx/znet"
|
||||
)
|
||||
|
||||
// HeartBeatApi 心跳请求
|
||||
type HeartBeatApi struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
func (*HeartBeatApi) Handle(request ziface.IRequest) {
|
||||
// 解包
|
||||
msgBody := omc.MsgBody{
|
||||
RawData: request.GetData(),
|
||||
Msg: make(map[string]string, 0),
|
||||
}
|
||||
if err := msgBody.Decode(); err != nil {
|
||||
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
|
||||
request.GetConnection().SendMsg(omc.AckHeartBeat, omc.ErrorMsg("ackHeartBeat", "", "inlaid message body"))
|
||||
return
|
||||
}
|
||||
|
||||
reqId, ok := msgBody.Msg["reqId"]
|
||||
if !ok {
|
||||
zlog.Ins().ErrorF("missing parameter of message body")
|
||||
request.GetConnection().SendMsg(omc.AckHeartBeat, omc.ErrorMsg("ackHeartBeat", "", "missing parameter of message body"))
|
||||
return
|
||||
}
|
||||
|
||||
//ack
|
||||
ackBody := omc.MsgBody{
|
||||
MsgName: "ackHeartBeat",
|
||||
Msg: make(map[string]string, 0),
|
||||
}
|
||||
ackBody.Msg["reqId"] = reqId
|
||||
ackBody.Keys = append(ackBody.Keys, "reqId")
|
||||
ackBody.Pack()
|
||||
request.GetConnection().SendMsg(omc.AckHeartBeat, ackBody.RawData)
|
||||
}
|
||||
161
api/login.go
Normal file
161
api/login.go
Normal file
@@ -0,0 +1,161 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"github.com/aceld/zinx/ziface"
|
||||
"github.com/aceld/zinx/zlog"
|
||||
"github.com/aceld/zinx/znet"
|
||||
"github.com/google/uuid"
|
||||
"omc/core"
|
||||
"omc/omc"
|
||||
"omc/service"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LoginApi 登录API
|
||||
type LoginApi struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
// Handle Login reqLoginAlarm;user=yiy;key=qw#$@;type=msg
|
||||
func (*LoginApi) Handle(request ziface.IRequest) {
|
||||
// 登录消息处理
|
||||
msgBody := omc.MsgBody{
|
||||
RawData: request.GetData(),
|
||||
Msg: make(map[string]string, 0),
|
||||
}
|
||||
if err := msgBody.Decode(); err != nil {
|
||||
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "inlaid message body"))
|
||||
return
|
||||
}
|
||||
|
||||
user, userOK := msgBody.Msg["user"]
|
||||
pw, pwOK := msgBody.Msg["key"]
|
||||
tp, tpOK := msgBody.Msg["type"]
|
||||
if !userOK || !pwOK || !tpOK {
|
||||
zlog.Ins().ErrorF("missing parameter of message body")
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "missing parameter of message body"))
|
||||
return
|
||||
}
|
||||
m := core.GetManager(request.GetConnection().GetName())
|
||||
if m == nil {
|
||||
zlog.Ins().ErrorF("server internal error")
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "server internal error"))
|
||||
return
|
||||
}
|
||||
uID, err := request.GetConnection().GetProperty("UID")
|
||||
if err != nil {
|
||||
zlog.Ins().ErrorF("GetProperty UID error %s", err)
|
||||
request.GetConnection().Stop()
|
||||
return
|
||||
}
|
||||
|
||||
//登录信息check
|
||||
if err := service.UserLogin(user, pw); err != nil {
|
||||
zlog.Ins().ErrorF("LoginFail %s", err)
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "Incorrect username and password"))
|
||||
isClose, _ := m.LoginFail(uID.(string)) //登录错误超过3次,断开连接
|
||||
if isClose {
|
||||
request.GetConnection().Stop()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//manager 更新
|
||||
if err := m.LoginSuccess(uID.(string), user, tp); err != nil {
|
||||
zlog.Ins().ErrorF("manager:%s", err)
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", err.Error()))
|
||||
return
|
||||
}
|
||||
zlog.Ins().InfoF("user login loginSuccess,username:%s, type:%s, channel:%s", user, tp, request.GetConnection().GetName())
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.SuccessMsg("ackLoginAlarm", "", ""))
|
||||
}
|
||||
|
||||
// CMCALoginSeq 登录API
|
||||
|
||||
type CMCALoginSeq struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
//reqCMCALoginSeq
|
||||
|
||||
func (*CMCALoginSeq) Handle(request ziface.IRequest) {
|
||||
uid := uuid.New()
|
||||
seqNo := hex.EncodeToString(uid[0:])
|
||||
seqNo = strings.ToUpper(seqNo)
|
||||
//发送文件同步信息
|
||||
ackBody := omc.MsgBody{
|
||||
MsgName: "ackCMCALoginSeq",
|
||||
Msg: make(map[string]string, 0),
|
||||
}
|
||||
ackBody.Msg["seqNo"] = seqNo
|
||||
ackBody.Pack()
|
||||
|
||||
request.GetConnection().SendMsg(omc.AckCMCALoginSeq, ackBody.RawData)
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
type CMCALoginAlarm struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
//reqCMCALoginSeq
|
||||
//reqCMCALoginAlarm;user=yiy;key=12313121213123;cert=AAAAAAAAAA;type=msg
|
||||
|
||||
func (*CMCALoginAlarm) Handle(request ziface.IRequest) {
|
||||
// 登录消息处理
|
||||
msgBody := omc.MsgBody{
|
||||
RawData: request.GetData(),
|
||||
Msg: make(map[string]string, 0),
|
||||
}
|
||||
if err := msgBody.Decode(); err != nil {
|
||||
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "inlaid message body"))
|
||||
return
|
||||
}
|
||||
|
||||
user, userOK := msgBody.Msg["user"]
|
||||
pw, pwOK := msgBody.Msg["key"]
|
||||
tp, tpOK := msgBody.Msg["type"]
|
||||
if !userOK || !pwOK || !tpOK {
|
||||
zlog.Ins().ErrorF("missing parameter of message body")
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "missing parameter of message body"))
|
||||
return
|
||||
}
|
||||
m := core.GetManager(request.GetConnection().GetName())
|
||||
if m == nil {
|
||||
zlog.Ins().ErrorF("server internal error")
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "server internal error"))
|
||||
return
|
||||
}
|
||||
uID, err := request.GetConnection().GetProperty("UID")
|
||||
if err != nil {
|
||||
zlog.Ins().ErrorF("GetProperty UID error %s", err)
|
||||
request.GetConnection().Stop()
|
||||
return
|
||||
}
|
||||
|
||||
//登录信息check
|
||||
if err := service.UserLogin(user, pw); err != nil {
|
||||
zlog.Ins().ErrorF("LoginFail %s", err)
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", "Incorrect username and password"))
|
||||
isClose, _ := m.LoginFail(uID.(string)) //登录错误超过3次,断开连接
|
||||
if isClose {
|
||||
request.GetConnection().Stop()
|
||||
return
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//manager 更新
|
||||
if err := m.LoginSuccess(uID.(string), user, tp); err != nil {
|
||||
zlog.Ins().ErrorF("manager:%s", err)
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.ErrorMsg("ackLoginAlarm", "", err.Error()))
|
||||
return
|
||||
}
|
||||
zlog.Ins().InfoF("user login loginSuccess,username:%s, type:%s, channel:%s", user, tp, request.GetConnection().GetName())
|
||||
request.GetConnection().SendMsg(omc.AckLoginAlarm, omc.SuccessMsg("ackLoginAlarm", "", ""))
|
||||
}
|
||||
61
api/req_sync_alarm.go
Normal file
61
api/req_sync_alarm.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/aceld/zinx/ziface"
|
||||
"github.com/aceld/zinx/zlog"
|
||||
"github.com/aceld/zinx/znet"
|
||||
"omc/core"
|
||||
"omc/omc"
|
||||
"omc/service"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// SyncAlarmApi 消息方式同步告警请求
|
||||
type SyncAlarmApi struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
func (*SyncAlarmApi) Handle(request ziface.IRequest) {
|
||||
// 消息处理
|
||||
checker := []string{"reqId", "alarmSeq"}
|
||||
msg, err := core.APIDecode(request, checker)
|
||||
if err != nil {
|
||||
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmMsg, omc.ErrorMsg("ackSyncAlarmMsg", "", err.Error()))
|
||||
return
|
||||
}
|
||||
//管理模块
|
||||
m := core.GetManager(request.GetConnection().GetName())
|
||||
if m == nil {
|
||||
zlog.Ins().ErrorF("server internal error")
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.ErrorMsg("ackSyncAlarmFile", msg.Msg["reqId"], "server internal error"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户是否登录
|
||||
u := m.GetUserByPID(msg.UID)
|
||||
if !u.LoginState || u.AlarmType != omc.MSG {
|
||||
zlog.Ins().ErrorF("no permissions ")
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmMsg, omc.ErrorMsg("ackSyncAlarmMsg", msg.Msg["reqId"], "no permissions"))
|
||||
return
|
||||
}
|
||||
|
||||
alarmSeq, err := strconv.Atoi(msg.Msg["alarmSeq"])
|
||||
if err != nil || alarmSeq < 1 {
|
||||
zlog.Ins().ErrorF("invalid parameter of message body")
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmMsg, omc.ErrorMsg("ackSyncAlarmMsg", msg.Msg["reqId"], "invalid parameter of message body"))
|
||||
return
|
||||
}
|
||||
|
||||
//check alarmSeq 是否存在
|
||||
neBind, _ := core.ConvertBindFlag(m.BindFlag)
|
||||
alarms, _ := service.GetRealTimeAlarm(neBind.NeType, neBind.NeId, int32(alarmSeq))
|
||||
if len(alarms) == 0 {
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmMsg, omc.ErrorMsg("ackSyncAlarmMsg", msg.Msg["reqId"], "alarm seq does not exist"))
|
||||
return
|
||||
}
|
||||
//更新实时上报的alarm seq
|
||||
m.UpdateAlarmSeq(int32(alarmSeq))
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmMsg, omc.SuccessMsg("ackSyncAlarmMsg", msg.Msg["reqId"], ""))
|
||||
|
||||
}
|
||||
91
api/req_sync_alarm_file.go
Normal file
91
api/req_sync_alarm_file.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/aceld/zinx/ziface"
|
||||
"github.com/aceld/zinx/zlog"
|
||||
"github.com/aceld/zinx/znet"
|
||||
"omc/conf"
|
||||
"omc/core"
|
||||
"omc/lib"
|
||||
"omc/omc"
|
||||
"omc/service"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// SyncAlarmFileApi 文件方式同步告警请求
|
||||
type SyncAlarmFileApi struct {
|
||||
znet.BaseRouter
|
||||
}
|
||||
|
||||
// Handle
|
||||
//reqSyncAlarmFile;reqId=33;startTime=2014-11-27 10:00:00;endTime=2014-11-27 10:30:00; syncSource =0
|
||||
func (*SyncAlarmFileApi) Handle(request ziface.IRequest) {
|
||||
// 消息处理
|
||||
checker := []string{"reqId", "syncSource"}
|
||||
msg, err := core.APIDecode(request, checker)
|
||||
if err != nil {
|
||||
zlog.Ins().ErrorF("inlaid message body %s", err.Error())
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.ErrorMsg("ackSyncAlarmFile", "", err.Error()))
|
||||
return
|
||||
}
|
||||
//管理模块
|
||||
m := core.GetManager(request.GetConnection().GetName())
|
||||
if m == nil {
|
||||
zlog.Ins().ErrorF("server internal error")
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.ErrorMsg("ackSyncAlarmFile", msg.Msg["reqId"], "server internal error"))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查用户是否登录
|
||||
u := m.GetUserByPID(msg.UID)
|
||||
if !u.LoginState || u.AlarmType != omc.FILE {
|
||||
zlog.Ins().ErrorF("no permissions ")
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.ErrorMsg("ackSyncAlarmFile", msg.Msg["reqId"], "no permissions"))
|
||||
return
|
||||
}
|
||||
//查询需要上报的告警信息
|
||||
start := ""
|
||||
end := ""
|
||||
syncSource := ""
|
||||
alarmSeq := 0
|
||||
fmt.Println("msg.Msg:", msg.Msg)
|
||||
//map[alarmSeq:1 reqId:35 syncSource:1]
|
||||
// map[endTime:2023-07-15 23:59:59 reqId:34 startTime:2023-01-08 16:07:00 syncSource:0]
|
||||
if v, ok := msg.Msg["startTime"]; ok {
|
||||
start = v
|
||||
}
|
||||
if v, ok := msg.Msg["endTime"]; ok {
|
||||
end = v
|
||||
}
|
||||
if v, ok := msg.Msg["syncSource"]; ok {
|
||||
syncSource = v
|
||||
}
|
||||
if v, ok := msg.Msg["alarmSeq"]; ok {
|
||||
if seq, err := strconv.Atoi(v); err == nil {
|
||||
alarmSeq = seq
|
||||
}
|
||||
}
|
||||
neBind, _ := core.ConvertBindFlag(m.BindFlag)
|
||||
alarms, err := service.GetAlarm(neBind.NeType, neBind.NeId, start, end, syncSource, alarmSeq)
|
||||
if err != nil || len(alarms) == 0 {
|
||||
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.ErrorMsg("ackSyncAlarmFile", msg.Msg["reqId"], "not find record"))
|
||||
return
|
||||
}
|
||||
//ack
|
||||
request.GetConnection().SendMsg(omc.AckSyncAlarmFile, omc.SuccessMsg("ackSyncAlarmFile", msg.Msg["reqId"], ""))
|
||||
|
||||
//打包结果文件
|
||||
//打包生成文件
|
||||
var meta lib.FileMeta
|
||||
meta.DirRoot = conf.OmcConf.FTPRoot
|
||||
meta.Province = m.Province
|
||||
meta.DeviceCode = m.DeviceCode
|
||||
meta.Index = "001"
|
||||
meta.Time = time.Now().Format("20060102150405")
|
||||
meta.Compress = false
|
||||
go service.GenFile(request, &meta, alarms)
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user