123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- package NatsServer
- import (
- "ColdVerify_local/conf"
- "ColdVerify_local/lib"
- "ColdVerify_local/logs"
- "ColdVerify_local/models/Account"
- "ColdVerify_local/models/Device"
- "ColdVerify_local/models/Task"
- "errors"
- "github.com/nats-io/nats.go"
- "github.com/vmihailenco/msgpack/v5"
- "time"
- )
- func Update_Task(v Task.Task) error {
- logs.Debug("Nats =>", lib.FuncName(), v)
- b, err := msgpack.Marshal(&v)
- if err != nil {
- return err
- }
- msg, err := lib.Nats.Request("ColdVerify_Server_Update_Task", b, 3*time.Second)
- if err != nil {
- return err
- }
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data Task.Task `xml:"Data"`
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- return err
- }
- if t_R.Code != 200 {
- return errors.New(t_R.Msg)
- }
- return nil
- }
- func Read_Task(T_task_id string) (task Task.Task, err error) {
- logs.Println("Nats =>", lib.FuncName(), T_task_id)
- if lib.Nats == nil || !lib.Nats.IsConnected() {
- lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
- if err != nil {
- logs.Println("无法重新连接到 NATS:", err)
- }
- logs.Println("成功重新连接到 NATS...")
- }
- msg, err := lib.Nats.Request("ColdVerify_Server_Read_Task", []byte(T_task_id), 3*time.Second)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return task, err
- }
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data Task.Task `xml:"Data"`
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return task, err
- }
- if t_R.Code != 200 {
- err = errors.New(t_R.Msg)
- logs.Error(lib.FuncName(), err.Error())
- return task, err
- }
- return t_R.Data, nil
- }
- func Read_User(T_uuid string) (user Account.User, err error) {
- logs.Println("Nats =>", lib.FuncName(), T_uuid)
- msg, err := lib.Nats.Request("ColdVerify_Server_Read_User", []byte(T_uuid), 3*time.Second)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return user, err
- }
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data Account.User `xml:"Data"`
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return user, err
- }
- if t_R.Code != 200 {
- err = errors.New(t_R.Msg)
- logs.Error(lib.FuncName(), err.Error())
- return user, err
- }
- return t_R.Data, nil
- }
- func Device_Class_List(T_task_id string) (list []Device.DeviceClassList, err error) {
- logs.Println("Nats =>", lib.FuncName(), T_task_id)
- if !lib.Nats.IsConnected() {
- lib.Nats, err = nats.Connect("nats://" + conf.NatsServer_Url)
- if err != nil {
- logs.Println("无法重新连接到 NATS:", err)
- }
- logs.Println("成功重新连接到 NATS...")
- }
- msg, err := lib.Nats.Request("ColdVerify_Server_Device_Class_List", []byte(T_task_id), 3*time.Second)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return list, err
- }
- type T_R struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- Data []Device.DeviceClassList `xml:"Data"`
- }
- var t_R T_R
- err = msgpack.Unmarshal(msg.Data, &t_R)
- if err != nil {
- logs.Error(lib.FuncName(), err.Error())
- return list, err
- }
- if t_R.Code != 200 {
- err = errors.New(t_R.Msg)
- logs.Error(lib.FuncName(), err.Error())
- return list, err
- }
- return t_R.Data, nil
- }
- func Add_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
- logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
- type T_R struct {
- T_task_id string `xml:"T_task_id"` // 任务主键id
- T_sn string `xml:"T_sn"`
- T_id string `xml:"T_id"`
- }
- t_R := T_R{
- T_task_id: T_task_id,
- T_sn: T_sn,
- T_id: T_id,
- }
- b, err := msgpack.Marshal(&t_R)
- if err != nil {
- return err
- }
- msg, err := lib.Nats.Request("ColdVerify_Server_Add_DeviceClassList", b, 3*time.Second)
- if err != nil {
- return err
- }
- type T_Res struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- }
- var t_Res T_Res
- err = msgpack.Unmarshal(msg.Data, &t_Res)
- if err != nil {
- return err
- }
- if t_Res.Code != 200 {
- return errors.New(t_Res.Msg)
- }
- return nil
- }
- func Edit_DeviceClassList(T_task_id, T_sn, T_id string) (err error) {
- logs.Debug("Nats =>", lib.FuncName(), T_task_id, T_sn, T_id)
- type T_R struct {
- T_task_id string `xml:"T_task_id"` // 任务主键id
- T_sn string `xml:"T_sn"`
- T_id string `xml:"T_id"`
- }
- t_R := T_R{
- T_task_id: T_task_id,
- T_sn: T_sn,
- T_id: T_id,
- }
- b, err := msgpack.Marshal(&t_R)
- if err != nil {
- return err
- }
- msg, err := lib.Nats.Request("ColdVerify_Server_Edit_DeviceClassList", b, 3*time.Second)
- if err != nil {
- return err
- }
- type T_Res struct {
- Code int16 `xml:"Code"`
- Msg string `xml:"Msg"`
- }
- var t_Res T_Res
- err = msgpack.Unmarshal(msg.Data, &t_Res)
- if err != nil {
- return err
- }
- if t_Res.Code != 200 {
- return errors.New(t_Res.Msg)
- }
- return nil
- }
|