|
| 1 | +// Author Raido Pahtma |
| 2 | +// License MIT |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import "fmt" |
| 7 | +import "os" |
| 8 | +import "log" |
| 9 | +import "time" |
| 10 | +import "encoding/hex" |
| 11 | + |
| 12 | +import "github.com/jessevdk/go-flags" |
| 13 | +import "github.com/proactivity-lab/go-loggers" |
| 14 | +import "github.com/proactivity-lab/go-sfconnection" |
| 15 | +import "github.com/proactivity-lab/go-devparam" |
| 16 | + |
| 17 | +const ApplicationVersionMajor = 0 |
| 18 | +const ApplicationVersionMinor = 1 |
| 19 | +const ApplicationVersionPatch = 0 |
| 20 | + |
| 21 | +var ApplicationBuildDate string |
| 22 | +var ApplicationBuildDistro string |
| 23 | + |
| 24 | +type HexString []byte |
| 25 | + |
| 26 | +func (self *HexString) UnmarshalFlag(value string) error { |
| 27 | + data, err := hex.DecodeString(value) |
| 28 | + *self = data |
| 29 | + return err |
| 30 | +} |
| 31 | + |
| 32 | +func (self HexString) MarshalFlag() (string, error) { |
| 33 | + return hex.EncodeToString(self), nil |
| 34 | +} |
| 35 | + |
| 36 | +func main() { |
| 37 | + |
| 38 | + var opts struct { |
| 39 | + Positional struct { |
| 40 | + ConnectionString string `description:"Connectionstring sf@HOST:PORT"` |
| 41 | + } `positional-args:"yes"` |
| 42 | + |
| 43 | + Group sfconnection.AMGroup `short:"g" long:"group" default:"22" description:"Packet AM Group (hex)"` |
| 44 | + |
| 45 | + Parameter string `short:"p" long:"parameter" description:"Name of the parameter"` |
| 46 | + Value HexString `short:"v" long:"value" description:"Value to set"` |
| 47 | + |
| 48 | + Debug []bool `short:"D" long:"debug" description:"Debug mode, print raw packets"` |
| 49 | + ShowVersion func() `short:"V" long:"version" description:"Show application version"` |
| 50 | + } |
| 51 | + |
| 52 | + opts.ShowVersion = func() { |
| 53 | + if ApplicationBuildDate == "" { |
| 54 | + ApplicationBuildDate = "YYYY-mm-dd_HH:MM:SS" |
| 55 | + } |
| 56 | + if ApplicationBuildDistro == "" { |
| 57 | + ApplicationBuildDistro = "unknown" |
| 58 | + } |
| 59 | + fmt.Printf("deviceparameter %d.%d.%d (%s %s)\n", ApplicationVersionMajor, ApplicationVersionMinor, ApplicationVersionPatch, ApplicationBuildDate, ApplicationBuildDistro) |
| 60 | + os.Exit(0) |
| 61 | + } |
| 62 | + |
| 63 | + _, err := flags.Parse(&opts) |
| 64 | + if err != nil { |
| 65 | + fmt.Printf("Argument parser error: %s\n", err) |
| 66 | + os.Exit(1) |
| 67 | + } |
| 68 | + |
| 69 | + host, port, err := sfconnection.ParseSfConnectionString(opts.Positional.ConnectionString) |
| 70 | + if err != nil { |
| 71 | + fmt.Printf("ERROR: %s\n", err) |
| 72 | + os.Exit(1) |
| 73 | + } |
| 74 | + |
| 75 | + sfc := sfconnection.NewSfConnection() |
| 76 | + dpm := deviceparameters.NewDeviceParameterManager(sfc) |
| 77 | + |
| 78 | + logger := logsetup(len(opts.Debug)) |
| 79 | + if len(opts.Debug) > 0 { |
| 80 | + sfc.SetLoggers(logger) |
| 81 | + } |
| 82 | + dpm.SetLoggers(logger) |
| 83 | + |
| 84 | + // Connect to the host |
| 85 | + err = sfc.Connect(host, port) |
| 86 | + if err != nil { |
| 87 | + logger.Error.Printf("Unable to connect to %s:%d\n", host, port) |
| 88 | + os.Exit(1) |
| 89 | + } |
| 90 | + logger.Info.Printf("Connected to %s:%d\n", host, port) |
| 91 | + |
| 92 | + if len(opts.Parameter) > 0 { |
| 93 | + if len(opts.Value) > 0 { |
| 94 | + logger.Info.Printf("Set %s to %X\n", opts.Parameter, opts.Value) |
| 95 | + err := dpm.SetValue(opts.Parameter, opts.Value) |
| 96 | + if err == nil { |
| 97 | + logger.Info.Printf("Done\n") |
| 98 | + } else { |
| 99 | + logger.Info.Printf("Failed: %s\n", err) |
| 100 | + } |
| 101 | + } else { |
| 102 | + logger.Info.Printf("Get %s\n", opts.Parameter) |
| 103 | + val, err := dpm.GetValue(opts.Parameter) |
| 104 | + if err == nil { |
| 105 | + logger.Info.Printf("%s = %X\n", opts.Parameter, val) |
| 106 | + } else { |
| 107 | + logger.Info.Printf("Failed: %s\n", err) |
| 108 | + } |
| 109 | + } |
| 110 | + } else { |
| 111 | + logger.Info.Printf("Get parameter list:\n") |
| 112 | + pchan, err := dpm.GetList() |
| 113 | + if err == nil { |
| 114 | + param := <-pchan |
| 115 | + for ; param != nil; param = <-pchan { |
| 116 | + if param.Error == nil { |
| 117 | + logger.Info.Printf("%d: %s %s\n", param.Seqnum, param.Name, param) |
| 118 | + } else { |
| 119 | + logger.Info.Printf("%d: %s\n", param.Seqnum, param.Error) |
| 120 | + } |
| 121 | + } |
| 122 | + } else { |
| 123 | + logger.Info.Printf("Failed: %s\n", err) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + dpm.Close() |
| 128 | + sfc.Disconnect() |
| 129 | + time.Sleep(100 * time.Millisecond) |
| 130 | +} |
| 131 | + |
| 132 | +func logsetup(debuglevel int) *loggers.DIWEloggers { |
| 133 | + logger := loggers.New() |
| 134 | + logformat := log.Ldate | log.Ltime | log.Lmicroseconds |
| 135 | + |
| 136 | + if debuglevel > 1 { |
| 137 | + logformat = logformat | log.Lshortfile |
| 138 | + } |
| 139 | + |
| 140 | + if debuglevel > 0 { |
| 141 | + logger.SetDebugLogger(log.New(os.Stdout, "DEBUG: ", logformat)) |
| 142 | + logger.SetInfoLogger(log.New(os.Stdout, "INFO: ", logformat)) |
| 143 | + } else { |
| 144 | + logger.SetInfoLogger(log.New(os.Stdout, "", logformat)) |
| 145 | + } |
| 146 | + logger.SetWarningLogger(log.New(os.Stdout, "WARN: ", logformat)) |
| 147 | + logger.SetErrorLogger(log.New(os.Stdout, "ERROR: ", logformat)) |
| 148 | + return logger |
| 149 | +} |
0 commit comments