| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- package main
- import (
- "io/ioutil"
- "os"
- "regexp"
- "strconv"
- "strings"
- "lc/common/protocol"
- "lc/common/util"
- )
- func HandleFile(topic string, obj *protocol.Pack_SeqFileObject) error {
- switch topic {
- case protocol.TP_GW_SET_APP:
- var app protocol.AppConfig
- if err := json.UnmarshalFromString(obj.Data.Content, &app); err != nil {
- return err
- }
- return SaveFile(obj.Data.File, obj.Data.Content, 0)
- case protocol.TP_GW_SET_SERIAL:
- var sc protocol.SerialConfig
- if err := json.UnmarshalFromString(obj.Data.Content, &sc); err != nil {
- return err
- }
- return SaveFile(obj.Data.File, obj.Data.Content, 0)
- case protocol.TP_GW_SET_RTU:
- var rtu protocol.MapDevConfig
- if err := json.UnmarshalFromString(obj.Data.Content, &rtu); err != nil {
- return err
- }
- return SaveFile(obj.Data.File, obj.Data.Content, 1)
- case protocol.TP_GW_SET_MODEL:
- var iot protocol.IotModel
- if err := json.UnmarshalFromString(obj.Data.Content, &iot); err != nil {
- return err
- }
- return SaveFile(obj.Data.File, obj.Data.Content, 2)
- }
- return nil
- }
- func SaveFile(fname, content string, flag uint8) error {
- var SavePath string
- switch flag {
- case 0:
- SavePath = util.GetPath(0)
- case 1:
- SavePath = util.GetPath(1)
- case 2:
- SavePath = util.GetPath(2)
- }
- //存成临时文件
- if err := ioutil.WriteFile(util.GetPath(4)+fname, []byte(content), os.ModePerm); err != nil {
- return err
- }
- //备份原文件
- if err := os.Rename(SavePath+fname, SavePath+fname+".bak"); err != nil {
- return err
- }
- //拷贝新文件
- if err := os.Rename(util.GetPath(4)+fname, SavePath+fname); err != nil {
- //拷贝失败,则还原文件
- if err := os.Rename(SavePath+fname+".bak", SavePath+fname); err != nil {
- return err
- }
- return err
- }
- return nil
- }
- func ReadFileContent(topic string, o *protocol.Pack_SeqFileObject) {
- switch topic {
- case protocol.TP_GW_APP:
- if buf, err := ioutil.ReadFile(util.GetPath(0) + "app.json"); err == nil {
- o.Data.File = "app.json"
- o.Data.Content = string(buf)
- }
- case protocol.TP_GW_SERIAL:
- if buf, err := ioutil.ReadFile(util.GetPath(0) + "serial.json"); err == nil {
- o.Data.File = "serial.json"
- o.Data.Content = string(buf)
- }
- }
- }
- func ReadMutilFileContent(topic string, id uint, o *protocol.Pack_MutilFileObject) {
- var FileDir string
- if topic == protocol.TP_GW_RTU {
- FileDir = util.GetPath(1)
- } else if topic == protocol.TP_GW_MODEL {
- FileDir = util.GetPath(2)
- } else if topic == protocol.TP_GW_LOG {
- FileDir = util.GetPath(3)
- } else if topic == protocol.TP_GW_APP || topic == protocol.TP_GW_SERIAL {
- FileDir = util.GetPath(0)
- }
- //RTU或模型文件
- if topic == protocol.TP_GW_RTU || topic == protocol.TP_GW_MODEL {
- if id > 0 {
- fname := strconv.Itoa(int(id)) + ".json"
- AddFileObject(FileDir, fname, o)
- } else {
- rd, _ := ioutil.ReadDir(FileDir)
- for _, fi := range rd {
- if !fi.IsDir() {
- ok, _ := regexp.MatchString(`[1-9].*json`, fi.Name())
- if ok {
- AddFileObject(FileDir, fi.Name(), o)
- }
- }
- }
- }
- } else if topic == protocol.TP_GW_LOG { //日志
- rd, _ := ioutil.ReadDir(FileDir)
- for _, fi := range rd {
- if !fi.IsDir() {
- ok := strings.HasSuffix(fi.Name(), ".log")
- if ok {
- AddFileObject(FileDir, fi.Name(), o)
- }
- }
- }
- } else if topic == protocol.TP_GW_APP {
- AddFileObject(FileDir, "app.json", o)
- } else if topic == protocol.TP_GW_SERIAL {
- AddFileObject(FileDir, "serial.json", o)
- }
- }
- func AddFileObject(fpath, fname string, o *protocol.Pack_MutilFileObject) {
- if buf, err := ioutil.ReadFile(fpath + fname); err == nil {
- var fo protocol.FileObject
- fo.File = fname
- fo.Content = string(buf)
- o.Data.Files = append(o.Data.Files, fo)
- }
- }
|