package main

import (
	"context"
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/os/gproc"
	"os"
	"sparrow-sdk/config"
	"sparrow-sdk/protocal"
	gatewayV2 "sparrow-sdk/v2"
	"yx-4g-gateway/schema"
	"yx-4g-gateway/server"
)

func main() {
	ctx := context.Background()
	err := glog.SetLevelStr(g.Cfg().GetString("Server.RunMode"))
	if err != nil {
		panic(err)
	}
	gw := gatewayV2.NewGateway(&config.Config{
		SparrowServer: g.Cfg().GetString("Sparrow.Server"),
		ProductKey:    g.Cfg().GetString("Sparrow.ProductKey"),
		Protocol:      "mqtt",
		DeviceCode:    g.Cfg().GetString("Sparrow.DeviceCode"),
		Version:       "1.0.0",
		Debug:         g.Cfg().GetBool("Sparrow.Debug"),
	})
	if _, err = gw.Register(); err != nil {
		panic(err)
	}
	if _, err = gw.Authentication(); err != nil {
		panic(err)
	} // 通用指令回调
	gw.SetReportCommandCallback(func(deviceCode, subId string) error {
		return nil
	})
	go gw.Connect()

	srv := server.NewServer(
		ctx,
		g.Cfg().GetString("Server.Addr"),
		g.Cfg().GetInt("Server.Port"),
		gw,
	)
	go func() {
		if err := srv.Start(); err != nil {
			panic(err)
		}
	}()

	closeReportChan := make(chan struct{})
	go func() {
		for {
			select {
			case <-closeReportChan:
				return
			}
		}
	}()

	if err = gw.RegisterCommand("setPower", func(msg protocal.CloudSend) error {
		var params schema.SetPowerReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}
		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if params.Power == 1 {
				if err = client.PowerOn(); err != nil {
					glog.Errorf("执行命令出错:%s", err.Error())
					return err
				}
			} else {
				if err = client.PowerOff(); err != nil {
					glog.Errorf("执行命令出错:%s", err.Error())
					return err
				}
			}

		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setMode", func(msg protocal.CloudSend) error {
		var params schema.SetModeReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}
		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if err = client.SetMode(params.Mode); err != nil {
				glog.Errorf("执行命令出错:%s", err.Error())
				return err
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setTemp", func(msg protocal.CloudSend) error {
		var params schema.SetTempReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}

		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if err = client.SetTemp(params.Temp); err != nil {
				glog.Errorf("执行命令出错:%s", err.Error())
				return err
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setFanLevel", func(msg protocal.CloudSend) error {
		var params schema.FanSpeedReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}
		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if err = client.SetFanSpeed(params.Speed); err != nil {
				glog.Errorf("执行命令出错:%s", err.Error())
				return err
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setNewFan", func(msg protocal.CloudSend) error {
		var params schema.SetNewFanReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}

		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if err = client.SetNewFan(params.Mode); err != nil {
				glog.Errorf("执行命令出错:%s", err.Error())
				return err
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setFanValue", func(msg protocal.CloudSend) error {
		var params schema.FanValveReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}

		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if params.FanValve == "03" {
				if err = client.SetFanGateThreeLevel(params.Value); err != nil {
					glog.Errorf("执行命令出错:%s", err.Error())
					return err
				}
			}
			if params.FanValve == "04" {
				if err = client.SetFanGateFourLevel(params.Value); err != nil {
					glog.Errorf("执行命令出错:%s", err.Error())
					return err
				}
			}
			if params.FanValve == "05" {
				if err = client.SetFanGateFiveLevel(params.Value); err != nil {
					glog.Errorf("执行命令出错:%s", err.Error())
					return err
				}
			}

		}
		return nil
	}); err != nil {
		panic(err)
	}

	if err = gw.RegisterCommand("setFan", func(msg protocal.CloudSend) error {
		var params schema.SetValvePowerReq
		j := gjson.New(msg.Data.Params)
		err = j.Struct(&params)
		if err != nil {
			glog.Errorf("错误的指令参数%s", err.Error())
			return err
		}
		glog.Debugf("指令:%s, 子设备Id:%s, 参数:%v", msg.Data.Cmd, msg.SubDeviceId, params)
		client := srv.GetClient(msg.SubDeviceId)
		if client != nil {
			if err = client.SetValvePower(&params); err != nil {
				glog.Errorf("执行命令出错:%s", err.Error())
				return err
			}
		}
		return nil
	}); err != nil {
		panic(err)
	}

	gproc.AddSigHandlerShutdown(func(sig os.Signal) {
		gw.Close()
		srv.Stop()
	})
	gproc.Listen()
}
