Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 1 | // Copyright 2018 Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 18 | importer "./proto" |
| 19 | "bufio" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 20 | "bytes" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 21 | "crypto/tls" |
| 22 | "fmt" |
| 23 | "github.com/Shopify/sarama" |
| 24 | log "github.com/Sirupsen/logrus" |
| 25 | "golang.org/x/net/context" |
| 26 | "google.golang.org/grpc" |
| 27 | "google.golang.org/grpc/status" |
| 28 | "net" |
| 29 | "net/http" |
| 30 | "os" |
| 31 | "os/exec" |
| 32 | "os/signal" |
| 33 | "strconv" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 34 | "strings" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 37 | var REDFISH_ROOT = "/redfish/v1" |
| 38 | var CONTENT_TYPE = "application/json" |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 39 | var EVENTS_MAP = map[string]string{ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 40 | "add": "ResourceAdded", |
| 41 | "rm": "ResourceRemoved", |
| 42 | "alert": "Alert", |
| 43 | "update": "Update"} |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 44 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 45 | var default_address string = "localhost:31085" |
| 46 | var default_port string = "8888" |
| 47 | var default_vendor string = "edgecore" |
| 48 | var default_freq uint64 = 180 |
| 49 | var attach_device_ip string = "" |
| 50 | var importerTopic = "importer" |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 51 | var DataConsumer sarama.Consumer |
| 52 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 53 | var cc importer.DeviceManagementClient |
| 54 | var ctx context.Context |
| 55 | var conn *grpc.ClientConn |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 56 | |
| 57 | /*///////////////////////////////////////////////////////////////////////*/ |
| 58 | // Allows user to register the device for data collection and frequency. |
| 59 | // |
| 60 | // |
| 61 | /*///////////////////////////////////////////////////////////////////////*/ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 62 | func Attach(aip string, avendor string, afreq uint32) error { |
| 63 | fmt.Println("Received Attach\n") |
| 64 | var default_protocol string = "https" |
| 65 | deviceinfo := new(importer.DeviceInfo) |
| 66 | deviceinfo.IpAddress = aip |
| 67 | deviceinfo.Vendor = avendor |
| 68 | deviceinfo.Frequency = afreq |
| 69 | deviceinfo.Protocol = default_protocol |
| 70 | _, err := cc.SendDeviceInfo(ctx, deviceinfo) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 71 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 72 | return err |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /*///////////////////////////////////////////////////////////////////////*/ |
| 76 | // Allows user to change the frequency of data collection |
| 77 | // |
| 78 | // |
| 79 | /*///////////////////////////////////////////////////////////////////////*/ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 80 | func UpdateFreq(ip_address string, wd uint32) error { |
| 81 | fmt.Println("Received Period\n") |
| 82 | freqinfo := new(importer.FreqInfo) |
| 83 | freqinfo.Frequency = wd |
| 84 | freqinfo.IpAddress = ip_address |
| 85 | _, err := cc.SetFrequency(ctx, freqinfo) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 86 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 87 | return err |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | /*///////////////////////////////////////////////////////////////////////*/ |
| 91 | // Allows user to unsubscribe events |
| 92 | // |
| 93 | // |
| 94 | /*///////////////////////////////////////////////////////////////////////*/ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 95 | func Subscribe(ip_address string, Giveneventlist []string) error { |
| 96 | fmt.Println("Received Subscribe\n") |
| 97 | giveneventlist := new(importer.GivenEventList) |
| 98 | giveneventlist.Events = Giveneventlist |
| 99 | giveneventlist.EventIpAddress = ip_address |
| 100 | _, err := cc.SubsrcribeGivenEvents(ctx, giveneventlist) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 101 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 102 | return err |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /*///////////////////////////////////////////////////////////////////////*/ |
| 106 | // Allows user to unsubscribe events |
| 107 | // |
| 108 | // |
| 109 | /*///////////////////////////////////////////////////////////////////////*/ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 110 | func UnSubscribe(ip_address string, Giveneventlist []string) error { |
| 111 | fmt.Println("Received UnSubscribe\n") |
| 112 | giveneventlist := new(importer.GivenEventList) |
| 113 | giveneventlist.Events = Giveneventlist |
| 114 | giveneventlist.EventIpAddress = ip_address |
| 115 | _, err := cc.UnSubsrcribeGivenEvents(ctx, giveneventlist) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 116 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 117 | return err |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /*///////////////////////////////////////////////////////////////////////*/ |
| 121 | // Allows user to get the events supported by device |
| 122 | // |
| 123 | // |
| 124 | /*///////////////////////////////////////////////////////////////////////*/ |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 125 | func GetEventSupportList(vendor string) (error, []string) { |
| 126 | fmt.Println("Received GetEventSupportList\n") |
| 127 | vendorinfo := new(importer.VendorInfo) |
| 128 | vendorinfo.Vendor = vendor |
| 129 | var ret_msg *importer.EventList |
| 130 | ret_msg, err := cc.GetEventList(ctx, vendorinfo) |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame^] | 131 | if err != nil { |
| 132 | return err, nil |
| 133 | } else { |
| 134 | return err, ret_msg.Events |
| 135 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | /*///////////////////////////////////////////////////////////////////////*/ |
| 139 | // Allows user to get the current events subscribed by device |
| 140 | // |
| 141 | // |
| 142 | /*///////////////////////////////////////////////////////////////////////*/ |
| 143 | func GetEventCurrentDeviceList(ip_address string) (error, []string) { |
| 144 | fmt.Println("Received GetEventCurrentDeviceList\n") |
| 145 | currentdeviceinfo := new(importer.Device) |
| 146 | currentdeviceinfo.IpAddress = ip_address |
| 147 | var ret_msg *importer.EventList |
| 148 | ret_msg, err := cc.GetCurrentEventList(ctx, currentdeviceinfo) |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame^] | 149 | if err != nil { |
| 150 | return err, nil |
| 151 | } else { |
| 152 | return err, ret_msg.Events |
| 153 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /*///////////////////////////////////////////////////////////////////////*/ |
| 157 | // Allows user to get the current events subscribed by device |
| 158 | // |
| 159 | // |
| 160 | /*///////////////////////////////////////////////////////////////////////*/ |
| 161 | func ClearCurrentDeviceEventList(ip_address string) error { |
| 162 | fmt.Println("Received ClearCurrentDeviceEventList\n") |
| 163 | currentdeviceinfo := new(importer.Device) |
| 164 | currentdeviceinfo.IpAddress = ip_address |
| 165 | _, err := cc.ClearCurrentEventList(ctx, currentdeviceinfo) |
| 166 | |
| 167 | return err |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 168 | } |
| 169 | |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame^] | 170 | /*///////////////////////////////////////////////////////////////////////*/ |
| 171 | // Allows user to get the current devices that are monitored |
| 172 | // |
| 173 | // |
| 174 | /*///////////////////////////////////////////////////////////////////////*/ |
| 175 | func GetCurrentDevices() (error, []string) { |
| 176 | fmt.Println("Testing GetCurrentDevices\n") |
| 177 | empty := new(importer.Empty) |
| 178 | var ret_msg *importer.DeviceList |
| 179 | ret_msg, err := cc.GetCurrentDevices(ctx, empty) |
| 180 | if err != nil { |
| 181 | return err, nil |
| 182 | } else { |
| 183 | return err, ret_msg.Ip |
| 184 | } |
| 185 | } |
| 186 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 187 | func init() { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 188 | Formatter := new(log.TextFormatter) |
| 189 | Formatter.TimestampFormat = "02-01-2006 15:04:05" |
| 190 | Formatter.FullTimestamp = true |
| 191 | log.SetFormatter(Formatter) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | func topicListener(topic *string, master sarama.Consumer) { |
| 195 | log.Info("Starting topicListener for ", *topic) |
| 196 | consumer, err := master.ConsumePartition(*topic, 0, sarama.OffsetOldest) |
| 197 | if err != nil { |
| 198 | log.Error("topicListener panic, topic=[%s]: %s", *topic, err.Error()) |
| 199 | os.Exit(1) |
| 200 | } |
| 201 | signals := make(chan os.Signal, 1) |
| 202 | signal.Notify(signals, os.Interrupt) |
| 203 | doneCh := make(chan struct{}) |
| 204 | go func() { |
| 205 | for { |
| 206 | select { |
| 207 | case err := <-consumer.Errors(): |
| 208 | log.Error("Consumer error: %s", err.Err) |
| 209 | case msg := <-consumer.Messages(): |
| 210 | log.Info("Got message on topic=[%s]: %s", *topic, string(msg.Value)) |
| 211 | case <-signals: |
| 212 | log.Warn("Interrupt is detected") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 213 | os.Exit(1) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | }() |
| 217 | <-doneCh |
| 218 | } |
| 219 | |
| 220 | func kafkainit() { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 221 | cmd := exec.Command("/bin/sh", "kafka_ip.sh") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 222 | var kafkaIP string |
| 223 | var out bytes.Buffer |
| 224 | cmd.Stdout = &out |
| 225 | err := cmd.Run() |
| 226 | if err != nil { |
| 227 | log.Info(err) |
| 228 | os.Exit(1) |
| 229 | } |
| 230 | |
| 231 | kafkaIP = out.String() |
| 232 | kafkaIP = strings.TrimSuffix(kafkaIP, "\n") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 233 | kafkaIP = kafkaIP + ":9092" |
| 234 | fmt.Println("IP address of kafka-cord-0:", kafkaIP) |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 235 | config := sarama.NewConfig() |
| 236 | config.Consumer.Return.Errors = true |
| 237 | master, err := sarama.NewConsumer([]string{kafkaIP}, config) |
| 238 | if err != nil { |
| 239 | panic(err) |
| 240 | } |
| 241 | DataConsumer = master |
| 242 | |
| 243 | go topicListener(&importerTopic, master) |
| 244 | } |
| 245 | func main() { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 246 | http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} |
| 247 | fmt.Println("Launching server...") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 248 | log.Info("kafkaInit starting") |
| 249 | kafkainit() |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 250 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 251 | ln, err := net.Listen("tcp", ":9999") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 252 | if err != nil { |
| 253 | fmt.Println("could not listen") |
| 254 | log.Fatal("did not listen: %v", err) |
| 255 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 256 | defer ln.Close() |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 257 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 258 | connS, err := ln.Accept() |
| 259 | if err != nil { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 260 | fmt.Println("Accept error") |
| 261 | log.Fatal("Accept error: %v", err) |
| 262 | } else { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 263 | |
| 264 | conn, err = grpc.Dial(default_address, grpc.WithInsecure()) |
| 265 | if err != nil { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 266 | fmt.Println("could not connect") |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 267 | log.Fatal("did not connect: %v", err) |
| 268 | } |
| 269 | defer conn.Close() |
| 270 | |
| 271 | cc = importer.NewDeviceManagementClient(conn) |
| 272 | ctx = context.Background() |
| 273 | |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 274 | loop := true |
| 275 | |
| 276 | for loop == true { |
| 277 | cmd, _ := bufio.NewReader(connS).ReadString('\n') |
| 278 | |
| 279 | cmd = strings.TrimSuffix(cmd, "\n") |
| 280 | s := strings.Split(cmd, ":") |
| 281 | newmessage := "cmd error!!" |
| 282 | cmd = s[0] |
| 283 | |
| 284 | switch string(cmd) { |
| 285 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 286 | case "attach": |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 287 | cmd_size := len(s) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 288 | var err error |
| 289 | var uafreq uint64 |
| 290 | if cmd_size == 5 { |
| 291 | aip := s[1] |
| 292 | aport := s[2] |
| 293 | avendor := s[3] |
| 294 | afreq := s[4] |
| 295 | uafreq, err = strconv.ParseUint(afreq, 10, 64) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 296 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 297 | if err != nil { |
| 298 | fmt.Print("ParseUint error!!\n") |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 301 | attach_device_ip = aip + ":" + aport |
| 302 | |
| 303 | err = Attach(attach_device_ip, avendor, uint32(uafreq)) |
| 304 | if err != nil { |
| 305 | errStatus, _ := status.FromError(err) |
| 306 | fmt.Println(errStatus.Message()) |
| 307 | fmt.Println(errStatus.Code()) |
| 308 | fmt.Print("attach error!!\n") |
| 309 | newmessage = errStatus.Message() |
| 310 | |
| 311 | } else { |
| 312 | fmt.Print("attatch IP:\n", attach_device_ip) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 313 | newmessage = attach_device_ip |
| 314 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 315 | } else { |
| 316 | fmt.Print("Need IP addres,port,vendor,freqs !!\n") |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 317 | newmessage = "Need IP address !!" |
| 318 | |
| 319 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 320 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 321 | case "period": |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 322 | cmd_size := len(s) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 323 | fmt.Print("cmd_size period %d", cmd_size) |
| 324 | if cmd_size == 4 { |
| 325 | fip := s[1] |
| 326 | fport := s[2] |
| 327 | pv := s[3] |
| 328 | fmt.Print("pv:", pv) |
| 329 | u, err := strconv.ParseUint(pv, 10, 64) |
| 330 | |
| 331 | if err != nil { |
| 332 | fmt.Print("ParseUint error!!\n") |
| 333 | } else { |
| 334 | wd := uint32(u) |
| 335 | ip_address := fip + ":" + fport |
| 336 | err = UpdateFreq(ip_address, wd) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 337 | |
| 338 | if err != nil { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 339 | errStatus, _ := status.FromError(err) |
| 340 | fmt.Println(errStatus.Message()) |
| 341 | fmt.Println(errStatus.Code()) |
| 342 | newmessage = errStatus.Message() |
| 343 | fmt.Print("period error!!\n") |
| 344 | } else { |
| 345 | newmessage = strings.ToUpper(cmd) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 346 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 347 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 348 | } else { |
| 349 | fmt.Print("Need period value !!\n") |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 350 | newmessage = "Need period value !!" |
| 351 | } |
| 352 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 353 | case "sub", "unsub": |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 354 | cmd_size := len(s) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 355 | fmt.Print("cmd is :", cmd, cmd_size) |
| 356 | if cmd_size > 6 || cmd_size < 0 { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 357 | fmt.Print("error event !!") |
| 358 | newmessage = "error event !!" |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 359 | } else { |
| 360 | ip := s[1] |
| 361 | port := s[2] |
| 362 | ip_address := ip + ":" + port |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 363 | var events_list []string |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 364 | for i := 3; i < cmd_size; i++ { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 365 | if value, ok := EVENTS_MAP[s[i]]; ok { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 366 | events_list = append(events_list, value) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 367 | } else { |
| 368 | fmt.Println("key not found") |
| 369 | } |
| 370 | } |
| 371 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 372 | if string(cmd) == "sub" { |
| 373 | err = Subscribe(ip_address, events_list) |
| 374 | if err != nil { |
| 375 | errStatus, _ := status.FromError(err) |
| 376 | fmt.Println(errStatus.Message()) |
| 377 | fmt.Println(errStatus.Code()) |
| 378 | newmessage = errStatus.Message() |
| 379 | fmt.Print("sub error!!") |
| 380 | } else { |
| 381 | newmessage = strings.ToUpper(cmd) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 382 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 383 | } else { |
| 384 | err = UnSubscribe(ip_address, events_list) |
| 385 | if err != nil { |
| 386 | errStatus, _ := status.FromError(err) |
| 387 | fmt.Println(errStatus.Message()) |
| 388 | fmt.Println(errStatus.Code()) |
| 389 | newmessage = errStatus.Message() |
| 390 | fmt.Print("unsub error!!") |
| 391 | } else { |
| 392 | newmessage = strings.ToUpper(cmd) |
| 393 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 396 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 397 | case "showeventlist": |
| 398 | cmd_size := len(s) |
| 399 | fmt.Print("cmd is :", cmd, cmd_size) |
| 400 | if cmd_size > 3 || cmd_size < 0 { |
| 401 | fmt.Print("error event !!") |
| 402 | newmessage = "error event !!" |
| 403 | } else { |
| 404 | vendor := s[1] |
| 405 | err, supportlist := GetEventSupportList(vendor) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 406 | |
| 407 | if err != nil { |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 408 | errStatus, _ := status.FromError(err) |
| 409 | fmt.Println(errStatus.Message()) |
| 410 | fmt.Println(errStatus.Code()) |
| 411 | newmessage = errStatus.Message() |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 412 | fmt.Print("showeventlist error!!") |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 413 | } else { |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 414 | fmt.Print("showeventlist ", supportlist) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 415 | newmessage = strings.Join(supportlist[:], ",") |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 416 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 419 | case "showdeviceeventlist": |
| 420 | cmd_size := len(s) |
| 421 | fmt.Print("cmd is :", cmd, cmd_size) |
| 422 | if cmd_size > 4 || cmd_size < 0 { |
| 423 | fmt.Print("error event !!") |
| 424 | newmessage = "error event !!" |
| 425 | } else { |
| 426 | eip := s[1] |
| 427 | eport := s[2] |
| 428 | ip_address := eip + ":" + eport |
| 429 | err, currentlist := GetEventCurrentDeviceList(ip_address) |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 430 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 431 | if err != nil { |
| 432 | errStatus, _ := status.FromError(err) |
| 433 | fmt.Println(errStatus.Message()) |
| 434 | fmt.Println(errStatus.Code()) |
| 435 | newmessage = errStatus.Message() |
| 436 | fmt.Print("showdeviceeventlist error!!") |
| 437 | } else { |
| 438 | fmt.Print("showeventlist ", currentlist) |
| 439 | newmessage = strings.Join(currentlist[:], ",") |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | case "cleardeviceeventlist": |
| 444 | cmd_size := len(s) |
| 445 | fmt.Print("cmd is :", cmd, cmd_size) |
| 446 | if cmd_size > 4 || cmd_size < 0 { |
| 447 | fmt.Print("error event !!") |
| 448 | newmessage = "error event !!" |
| 449 | } else { |
| 450 | clip := s[1] |
| 451 | clport := s[2] |
| 452 | ip_address := clip + ":" + clport |
| 453 | err = ClearCurrentDeviceEventList(ip_address) |
| 454 | if err != nil { |
| 455 | errStatus, _ := status.FromError(err) |
| 456 | fmt.Println(errStatus.Message()) |
| 457 | fmt.Println(errStatus.Code()) |
| 458 | newmessage = errStatus.Message() |
| 459 | fmt.Print("cleardeviceeventlist error!!") |
| 460 | } else { |
| 461 | newmessage = strings.ToUpper(cmd) |
| 462 | } |
| 463 | } |
| 464 | |
dileepbk | 86ef010 | 2019-11-13 00:08:33 +0000 | [diff] [blame^] | 465 | case "showdevices": |
| 466 | cmd_size := len(s) |
| 467 | fmt.Print("cmd is :", cmd, cmd_size) |
| 468 | if cmd_size > 2 || cmd_size < 0 { |
| 469 | fmt.Print("error event !!") |
| 470 | newmessage = "error event !!" |
| 471 | } else { |
| 472 | err, currentlist := GetCurrentDevices() |
| 473 | |
| 474 | if err != nil { |
| 475 | errStatus, _ := status.FromError(err) |
| 476 | fmt.Println(errStatus.Message()) |
| 477 | fmt.Println(errStatus.Code()) |
| 478 | newmessage = errStatus.Message() |
| 479 | fmt.Print("showdevices error!!") |
| 480 | } else { |
| 481 | fmt.Print("showdevices ", currentlist) |
| 482 | newmessage = strings.Join(currentlist[:], ", ") |
| 483 | } |
| 484 | } |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 485 | case "QUIT": |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 486 | loop = false |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 487 | newmessage = "QUIT" |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 488 | |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 489 | default: |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 490 | } |
| 491 | // send string back to client |
| 492 | connS.Write([]byte(newmessage + "\n")) |
Dinesh Belwalkar | b5db83f | 2019-10-24 17:27:58 +0000 | [diff] [blame] | 493 | } |
nickhuang | 6b31f8f | 2019-09-26 02:02:14 +0000 | [diff] [blame] | 494 | } |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 495 | } |