package config import ( "bytes" "encoding/json" "fmt" "io/ioutil" "os" ) type Config struct { NsqServer string `json:"nsq_server"` // nsq TCP服务端地址 PubPLCDataTopic string `json:"pub_plc_data_topic"` // 发布plc数据的topic WritePLCDataTopic string `json:"write_plc_data_topic"` // 接收plc配置数据的topic SubDeviceTopic string `json:"sub_device_topic"` // 接收设备变更通知的topic DeviceListWebApi string `json:"device_List_webapi"` // 获取设备列表的接口, aps 提供, http接口 PostPLCDataWebApi string `json:"post_plc_data_webapi"` // 上传给aps plc数据的接口地址. aps 提供, http接口 } const configPath = "config.json" var Options Config func DefaultConfig() { Options.NsqServer = "fai365.com:4150" Options.PubPLCDataTopic = "aps.factory.plc.livedata" Options.WritePLCDataTopic = "aps.factory.plc.write" Options.SubDeviceTopic = "" Options.DeviceListWebApi = "aps.factory.plc.device" Options.PostPLCDataWebApi = "" } func Load() { if !pathExists(configPath) { DefaultConfig() SaveConfig() } else { file, _ := os.Open(configPath) defer file.Close() fd := json.NewDecoder(file) fd.Decode(&Options) } } func SaveConfig() error { b, err := json.Marshal(Options) if err == nil { var out bytes.Buffer err = json.Indent(&out, b, "", " ") if err == nil { err = ioutil.WriteFile(configPath, out.Bytes(), 0644) } } return err } func pathExists(path string) bool { _, err := os.Stat(path) if err == nil { return true } fmt.Printf("%v", err.Error()) return false }