package conf

import (
	"gopkg.in/yaml.v2"
	"os"
)

var DbConfig *EnvConfig

type EnvConfig struct {
	Finr                   int    `yaml:"finr"`
	Mongdbname             string `yaml:"mongdbname"`
	Mongdbip               string `yaml:"mongdbip"`
	Mongdbport             int    `yaml:"mongdbport"`
	Masterdbserver         string `yaml:"masterdbserver"`
	Masterdbdrivername     string `yaml:"masterdbdrivername"`
	Masterdbuser           string `yaml:"masterdbuser"`
	Masterdbpassword       string `yaml:"masterdbpassword"`
	Masterdbname           string `yaml:"masterdbname"`
	Masterdbport           int    `yaml:"masterdbport"`
	TemplatePath           string `yaml:"templatepath"`
	ReadTaskInterval       int    `yaml:"readtaskinterval"`
	PrinterType            string `yaml:"printertype"`
	Inbox                  string `yaml:"inbox"`
	Outbox                 string `yaml:"outbox"`
	AndonTouchOff          string `yaml:"andontouchoff"`
	PortalGetTimeUrl       string `yaml:"portalgettimeurl"`
	PortalPostFileUrl      string `yaml:"portalpostfileurl"`
	PortalCachePostFileUrl string `yaml:"portalcachepostfileurl"`
	PortalHost             string `yaml:"portalhost"`
	Proxy           string `yaml:"proxy"`
	PLCWriteBackend string `yaml:"plcwritebackend"`
	PLCWriteStringBackend string `yaml:"plcwritestringbackend"`
	PLCReadBackend string `yaml:"plcreadbackend"`
	PLCStep1DB      string `yaml:"plcstep1db"`
	PLCStep2DB             string `yaml:"plcstep2db"`
	PLCStep3DB             string `yaml:"plcstep3db"`
	// 南沙工厂座椅下线的PLC地址位
	OfflineDBSign string `yaml:"offlinedbsign"`
	OfflineDBSave string `yaml:"offlinedbdata"`
	OfflineDBSaveLen int `yaml:"offlinedbdatalen"`
	OfflinePLCType string `yaml:"offlineplctype"`
	OfflinePLCAddress string `yaml:"offlineplcaddress"`
	OfflinePLCRetry int `yaml:"offlineplcretry"`
	// 南沙 顺引
	CallOffFolder string `yaml:"callofffolder"`
	OutCallOffFolder string `yaml:"outcallofffolder"`
	ErrorCallOffFolder string `yaml:"errorcallofffolder"`
	CallOffPlcAddress  string `yaml:"calloffplcaddress"`
	CallOffPlcType     string `yaml:"calloffplctype"`
	CallOffPlcReadSign string `yaml:"calloffplcreadsign"`
	CallOffPlcWrite string `yaml:"calloffplcwrite"`
	CallOffPlcWriteLen int `yaml:"calloffplcwritelen"`
	CallOffPlcWriteSign string `yaml:"calloffplcwritesign"`
	// 南沙 座椅返修
	RepairPlcAddress  string `yaml:"repairplcaddress"`
	RepairPlcType     string `yaml:"repairplctype"`
	RepairPlcReadSign string `yaml:"repairplcreadsign"`
	RepairPlcWrite string `yaml:"repairplcwrite"`
	RepairPlcWriteLen int `yaml:"repairplcwritelen"`
	// 南沙 座椅数据回写
	BackFlushPlcAddress  string `yaml:"backflushaddress"`
	BackFlushPlcType     string `yaml:"backflushplctype"`
	BackFlushPlcReadSign string `yaml:"backflushreadsign"`
	BackFlushPlcWriteSign string `yaml:"backflushwritesign"`
	BackFlushPlcReadData string `yaml:"backflushreaddata"`
	BackFlushPlcReadLen int `yaml:"backflushreadlen"`
	// 讴歌 座椅下线打包 plc放行
	PermitThroughPlcType string `yaml:"permitthroughplctype"`
	PermitThroughPlcAddress string `yaml:"permitthroughplcaddress"`
	PermitThroughPlcDB   string `yaml:"permitthroughdb"`
}

//read yaml config
//注：path为yaml或yml文件的路径
func ReadYamlConfig(path string) error {
	DbConfig = &EnvConfig{}
	f, err := os.Open(path)

	if err != nil {
		return err
	} else {
		yaml.NewDecoder(f).Decode(DbConfig)
	}
	defer f.Close()

	return nil
}
