package websocket import ( "bytes" "encoding/json" "os" "path/filepath" "primesys_command_producer/src/logger" . "primesys_command_producer/src/types" "primesys_command_producer/src/utils" ) // handleMessage validates the incoming command request and pushes it in kafka func (c *Client) handleMessage(message []byte, userId string) { pendingCount := 0 // create a map for command allPendingcommands := make(map[string][]PendingCommandJson) // fetch all existing command if exists existingCommands, err := handlePendingData(c.FilePath) if err != nil { logger.Error("Error while data reading %v", err) } if len(existingCommands) > 0 { for _, cmd := range existingCommands { allPendingcommands[cmd.UserId] = append( allPendingcommands[cmd.UserId], cmd, ) } } commands, exists := allPendingcommands[userId] if exists { pendingCount = commands[len(commands)-1].Count + 1 } var packet DivisionCommandRequest if err := json.Unmarshal(message, &packet); err != nil { logger.Error("userId=%s invalid json: %v", c.UserID, err) c.reply(CommandResponse{Status: false, Message: "Invalid json"}) return } if packet.DivisionID == "" || packet.Command == nil { c.reply(CommandResponse{UserId: packet.DivisionID, Status: false, Message: "UserId and commands are required"}) return } count := 0 pendingCommandBucket := []CommandRequestPacket{} serverCount := 0 if pendingCount > 0 { for _, cmd := range packet.Command { pendingBuck := CommandRequestPacket{ Imei: cmd.DeviceImei, Command: cmd.Command, UserID: userId, TimeStamp: utils.IndianEpoch(), } pendingCommandBucket = append(pendingCommandBucket, pendingBuck) } err := writePendingCommand(existingCommands, pendingCommandBucket, c.UserID, pendingCount, c.FilePath) if err != nil { logger.Error("Error in writing commands : %v", err) } } else { for _, cmd := range packet.Command { if count >= 3 { pendingBuck := CommandRequestPacket{ Imei: cmd.DeviceImei, Command: cmd.Command, UserID: userId, TimeStamp: utils.IndianEpoch(), } pendingCommandBucket = append(pendingCommandBucket, pendingBuck) } else { buck := CommandRequestPacket{ Imei: cmd.DeviceImei, Command: cmd.Command, UserID: userId, TimeStamp: utils.IndianEpoch(), ServerFlag: getServerFlagCount(c.ServerFlag, serverCount), } } count = count + 1 serverCount = serverCount + 1 } if len(pendingCommandBucket) > 0 { err := writePendingCommand(existingCommands, pendingCommandBucket, c.UserID, pendingCount, c.FilePath) if err != nil { logger.Error("Error in writing commands : %v", err) } } } } func handlePendingData(filePath string) (command []PendingCommandJson, err error) { data, err := os.ReadFile(filePath) if err != nil { logger.Error("Error reading file: %v", err) return nil, err } if len(bytes.TrimSpace(data)) == 0 { logger.Error("File is empty") return []PendingCommandJson{}, nil } var pendingCommand []PendingCommandJson if err := json.Unmarshal(data, &pendingCommand); err != nil { logger.Error("Error parsing pending JSON: %v; content: %q", err, string(data)) return nil, err } logger.Info("Pending commands count: %d", len(pendingCommand)) return pendingCommand, nil } // it write all the pending commands in pending.json file func writePendingCommand(existingCommands []PendingCommandJson, commandBuckets []CommandRequestPacket, userId string, count int, filePath string) error { updateCommands := existingCommands for _, cmd := range commandBuckets { pending := PendingCommandJson{ UserId: userId, Count: count, Command: cmd, } count = count + 1 updateCommands = append(updateCommands, pending) } data, err := json.MarshalIndent(updateCommands, "", " ") if err != nil { logger.Error("Error in Marshaling pending commands: %v", err) return err } if err := os.MkdirAll(filepath.Dir(filePath), 0755); err != nil { logger.Error("Error creating pending directory: %v", err) return err } if err := os.WriteFile(filePath, data, 0644); err != nil { logger.Error("Error in writing commands %v", err) return err } logger.Info("Successfully saved %d pending commands", len(updateCommands)) return nil }