123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- 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
- }
|