package gate_command import ( "encoding/json" "fmt" "git.getensh.com/common/gopkgs/database" "github.com/tidwall/gjson" "property-device/errors" dbmodel "property-device/model" "property-device/utils/gate_utils" "time" ) type YufanHttpv1Commander struct { ByIp bool command bool GateInfo *dbmodel.TGate } func (p *YufanHttpv1Commander) Open() error { if p.ByIp { return p.OpenByIp() } task := gate_utils.DeviceOpenTask{ TaskNo: "", InterfaceName: gate_utils.DeviceOpenTaskName, Result: true, OType: 1, } bytes, _ := json.Marshal(task) now := time.Now() gcmd := &dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.GateInfo.ID, Param: string(bytes), Desc: gate_utils.CommandCodeMap[gate_utils.OpenCommand], Status: gate_utils.CommandStatusWait, ResultStatus: 0, ResultStatusDesc: "", Code: gate_utils.OpenCommand, } if err := gcmd.Insert(database.DB()); err != nil { return errors.DataBaseError } p.command = true return nil } func (p *YufanHttpv1Commander) Reboot() error { if p.ByIp { return p.RebootByIp() } task := gate_utils.DeviceRebootTask{ TaskNo: "", InterfaceName: gate_utils.DeviceRebootTaskName, Result: true, } bytes, _ := json.Marshal(task) now := time.Now() gcmd := &dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.GateInfo.ID, Param: string(bytes), Desc: gate_utils.CommandCodeMap[gate_utils.RebootCommand], Status: gate_utils.CommandStatusWait, ResultStatus: 0, ResultStatusDesc: "", Code: gate_utils.RebootCommand, } if err := gcmd.Insert(database.DB()); err != nil { return errors.DataBaseError } p.command = true return nil } func (p *YufanHttpv1Commander) OpenByIp() error { now := time.Now() body := map[string]interface{}{ "pass": p.GateInfo.Password, "type": 1, } bytes, _ := json.Marshal(body) h := gate_utils.HttpRequestWithHead{ Method: "POST", Body: bytes, TimeOut: 20 * time.Second, Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, Url: fmt.Sprintf("http://%s:%d/device/openDoorControl", p.GateInfo.Ip, p.GateInfo.Port), } resultStatus := 1 msg := "" bytes, err := h.Request() if err != nil { resultStatus = 2 msg = err.Error() } else { code := gjson.GetBytes(bytes, "code").String() if code != "LAN_SUS-0" { msg = fmt.Sprintf("%s-%s", code, gjson.GetBytes(bytes, "msg").String()) } } tgc := &dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.GateInfo.ID, //Param: req.CmdParams, Desc: gate_utils.CommandCodeMap[gate_utils.OpenCommand], Status: gate_utils.CommandStatusOver, ResultStatus: int32(resultStatus), ResultStatusDesc: msg, Code: gate_utils.OpenCommand, } tgc.Insert(database.DB()) return nil } func (p *YufanHttpv1Commander) RebootByIp() error { now := time.Now() body := map[string]interface{}{ "pass": p.GateInfo.Password, } bytes, _ := json.Marshal(body) h := gate_utils.HttpRequestWithHead{ Method: "POST", Body: bytes, TimeOut: 20 * time.Second, Head: map[string]string{"Content-Type": "application/x-www-form-urlencoded"}, Url: fmt.Sprintf("http://%s:%d/restartDevice", p.GateInfo.Ip, p.GateInfo.Port), } resultStatus := 1 msg := "" bytes, err := h.Request() if err != nil { resultStatus = 2 msg = err.Error() } else { code := gjson.GetBytes(bytes, "code").String() if code != "LAN_SUS-0" { msg = fmt.Sprintf("%s-%s", code, gjson.GetBytes(bytes, "msg").String()) } } tgc := &dbmodel.TGateCommand{ CreatedAt: now, UpdatedAt: now, DeviceId: p.GateInfo.ID, //Param: req.CmdParams, Desc: gate_utils.CommandCodeMap[gate_utils.RebootCommand], Status: gate_utils.CommandStatusOver, ResultStatus: int32(resultStatus), ResultStatusDesc: msg, Code: gate_utils.RebootCommand, } tgc.Insert(database.DB()) return nil } func (p *YufanHttpv1Commander) Command() bool { return p.command }