package controllers
|
|
import (
|
"basic.com/valib/bhomeclient.git"
|
"basic.com/valib/logger.git"
|
"encoding/json"
|
"github.com/satori/go.uuid"
|
"nanomsg.org/go-mangos"
|
"nanomsg.org/go-mangos/protocol/req"
|
"nanomsg.org/go-mangos/transport/ipc"
|
"nanomsg.org/go-mangos/transport/tcp"
|
"strconv"
|
"time"
|
"vamicro/system-service/models"
|
)
|
|
//设备授权
|
type DevAuthController struct {
|
|
}
|
|
//获取设备授权配置
|
func (dac *DevAuthController) AuthConfig(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var config models.AuthConfig
|
i, _ := config.Select()
|
if i > 0 {
|
return &bhomeclient.Reply{ Success: true, Data: config }
|
}
|
return &bhomeclient.Reply{ Success: true, Data: models.AuthConfig{}}
|
}
|
|
//修改设备授权配置
|
func (dac *DevAuthController) SaveAuthConfig(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
//一旦修改,如果在集群内,则集群内所有设备的授权配置都将修改
|
var reqBody models.AuthConfig
|
if err := c.BindJSON(&reqBody);err != nil {
|
return &bhomeclient.Reply{ Msg: "参数有误"+err.Error()}
|
}
|
var config models.AuthConfig
|
i, _ := config.Select()
|
if i == 0{
|
tmp := models.AuthConfig{
|
AuthType: reqBody.AuthType,
|
Password: reqBody.Password,
|
}
|
if tmp.Insert() {
|
return &bhomeclient.Reply{ Success: true, Msg: "保存成功"}
|
} else {
|
return &bhomeclient.Reply{ Msg: "保存失败"}
|
}
|
} else {
|
config.AuthType = reqBody.AuthType
|
config.Password = reqBody.Password
|
if config.Update() {
|
return &bhomeclient.Reply{ Success: true, Msg: "保存成功"}
|
} else {
|
return &bhomeclient.Reply{ Msg: "保存失败"}
|
}
|
}
|
}
|
|
//设备管理发送的申请请求
|
func (dac *DevAuthController) Apply(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
type applyArg struct {
|
Key string `json:"key"`
|
FromDevId string `json:"fromDevId"`
|
FromIp string `json:"fromIp"`
|
}
|
var reqBody applyArg
|
if err := c.BindJSON(&reqBody); err != nil || reqBody.Key == "" || reqBody.FromIp == "" || reqBody.FromDevId == "" {
|
return &bhomeclient.Reply{ Msg: "参数有误"}
|
}
|
//1.验证请求的key是否匹配
|
var config models.AuthConfig
|
ic, _ := config.Select()
|
if ic > 0 {
|
if config.AuthType == models.AuthType_Key && reqBody.Key != config.Password {
|
return &bhomeclient.Reply{ Msg: "申请密钥不匹配"}
|
}
|
}
|
|
//2.写入authDevice
|
var da models.AuthDevice
|
i, _ := da.FindByDevId(reqBody.FromDevId)
|
if i == 0 { //未申请过
|
tmp := models.AuthDevice{
|
Id: uuid.NewV4().String(),
|
DevId: reqBody.FromDevId,
|
DevIp: reqBody.FromIp,
|
ApplyKey: reqBody.Key,
|
CreateTime: time.Now().Format("2006-01-02 15:04:05"),
|
}
|
if tmp.Insert() {
|
return &bhomeclient.Reply{ Success:true, Msg: "添加成功,待审核"}
|
} else {
|
return &bhomeclient.Reply{ Msg: "添加失败"}
|
}
|
} else { //已申请过
|
if da.Status == models.AuthStatus_Agreed {
|
return &bhomeclient.Reply{ Success: true, Msg: "已通过,无需重复申请"}
|
} else if da.Status == models.AuthStatus_AuthCanceled {
|
return &bhomeclient.Reply{ Msg: "已取消授权"}
|
} else {
|
return &bhomeclient.Reply{ Success:true, Msg:"请等候审核"}
|
}
|
}
|
}
|
|
//显示申请信息
|
func (dac *DevAuthController) ApplyShow(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var ad models.AuthDevice
|
list := ad.FindByStatus(models.AuthStatus_Waiting)
|
if list == nil {
|
list = make([]models.AuthDevice, 0)
|
}
|
return &bhomeclient.Reply{ Success: true, Data: list}
|
}
|
|
//通过或拒绝
|
func (dac *DevAuthController) Approve(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
type approveArg struct {
|
Id string `json:"id"`
|
DevId string `json:"devId"`
|
DevIp string `json:"devIp"`
|
Status int `json:"status"`
|
}
|
var reqBody approveArg
|
c.BindJSON(&reqBody)
|
if reqBody.Status == models.AuthStatus_Agreed || reqBody.Status == models.AuthStatus_Rejected { //通过或拒绝
|
//1.通知对方,已通过授权或已拒绝(serf1运行在master节点上,设备管理服务可能运行在集群内任一一台服务器上)
|
//根据管理设备的id获取ip(通过设备间bus通信反馈)?
|
arg, _ := json.Marshal(reqBody)
|
remoteReply,err := approve(arg, reqBody.DevIp)
|
if err != nil {
|
return &bhomeclient.Reply{ Msg: err.Error()}
|
}
|
if remoteReply.Success {
|
//2.修改本地记录的状态
|
//if len(resp) > 0 {
|
var ad models.AuthDevice
|
if ad.UpdateStatus(reqBody.Id, reqBody.Status) {
|
return &bhomeclient.Reply{ Success: true, Msg: "操作成功"}
|
} else {
|
return &bhomeclient.Reply{ Msg: "操作失败"}
|
}
|
//} else {
|
// return &bhomeclient.Reply{Msg:"设备管理响应失败"}
|
//}
|
} else {
|
return remoteReply
|
}
|
|
} else {
|
return &bhomeclient.Reply{ Msg: "参数有误"}
|
}
|
}
|
|
func approve(arg []byte, devIp string) (*bhomeclient.Reply, error) {
|
var sock mangos.Socket
|
var err error
|
var msg []byte
|
|
if sock,err = req.NewSocket();err !=nil {
|
logger.Debug("comp can't new req socket:%s",err.Error())
|
return nil, err
|
}
|
sock.AddTransport(ipc.NewTransport())
|
sock.AddTransport(tcp.NewTransport())
|
if err = sock.Dial(devIp+":"+strconv.Itoa(4012));err !=nil {
|
logger.Debug("comp can't dial on req socket:%s",err.Error())
|
return nil, err
|
}
|
sock.SetOption(mangos.OptionMaxRecvSize, 1024*1024*100)
|
sock.SetOption(mangos.OptionRecvDeadline, time.Second*5)
|
if err = sock.Send(arg);err !=nil {
|
logger.Debug("comp can't send message on push socket:%s",err.Error())
|
return nil, err
|
}
|
if msg,err = sock.Recv();err !=nil {
|
logger.Debug("comp sock.Recv receive err:%s",err.Error())
|
return nil, err
|
}
|
sock.Close()
|
var ret bhomeclient.Reply
|
retErr := json.Unmarshal(msg, &ret)
|
if retErr != nil {
|
return nil, retErr
|
}
|
return &ret, nil
|
}
|
|
//获取已授权的设备列表
|
func (dac *DevAuthController) AuthedList(h *bhomeclient.WrapperHandler, c *bhomeclient.Request) *bhomeclient.Reply {
|
var ad models.AuthDevice
|
list := ad.FindByStatus(models.AuthStatus_Agreed)
|
if list == nil {
|
list = make([]models.AuthDevice, 0)
|
}
|
return &bhomeclient.Reply{ Success: true, Data: list}
|
}
|