github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/engine/function/sendMessage.go (about)

     1  package funcs
     2  
     3  import (
     4  	//	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"time"
     8  
     9  	//	dapr "github.com/dapr/go-sdk/client"
    10  	"github.com/mdaxf/iac/com"
    11  )
    12  
    13  type SendMessageFuncs struct {
    14  }
    15  
    16  // Execute executes the SendMessageFuncs function.
    17  // It sets the start time, defers the calculation of elapsed time and logging of performance,
    18  // and recovers from any panics that occur during execution.
    19  // It retrieves the inputs, sets the topic and data, and marshals the data into JSON format.
    20  // If the topic is empty, it logs an error and returns.
    21  // It then invokes the SignalRClient or IACMessageBusClient to send the message.
    22  // Finally, it sets the outputs of the function.
    23  func (cf *SendMessageFuncs) Execute(f *Funcs) {
    24  	startTime := time.Now()
    25  	defer func() {
    26  		elapsed := time.Since(startTime)
    27  		f.iLog.PerformanceWithDuration("engine.funcs.SendEmessage.Execute", elapsed)
    28  	}()
    29  	defer func() {
    30  		if err := recover(); err != nil {
    31  			f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.SendEmessage.Execute with error: %s", err))
    32  			f.CancelExecution(fmt.Sprintf("There is error to engine.funcs.SendEmessage.Execute with error: %s", err))
    33  			f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.SendEmessage.Execute with error: %s", err)
    34  			return
    35  		}
    36  	}()
    37  
    38  	f.iLog.Debug(fmt.Sprintf("SendMessageFuncs Execute: %v", f))
    39  
    40  	namelist, valuelist, _ := f.SetInputs()
    41  	f.iLog.Debug(fmt.Sprintf("SendMessageFuncs Execute: %v, %v", namelist, valuelist))
    42  
    43  	Topic := ""
    44  	data := make(map[string]interface{})
    45  	for i, name := range namelist {
    46  		if name == "Topic" {
    47  			Topic = valuelist[i]
    48  			continue
    49  		}
    50  		data[name] = valuelist[i]
    51  	}
    52  
    53  	if Topic == "" {
    54  		f.iLog.Error(fmt.Sprintf("SendMessageFuncs validate wrong: %v", "Topic is empty"))
    55  		return
    56  	}
    57  
    58  	jsonData, err := json.Marshal(data)
    59  	if err != nil {
    60  		f.iLog.Error(fmt.Sprintf("Error:%v", err))
    61  		return
    62  	}
    63  	// Convert JSON byte array to string
    64  	jsonString := string(jsonData)
    65  
    66  	f.iLog.Debug(fmt.Sprintf("SendMessageFuncs Execute: topic, %s, message: %v", Topic, jsonString))
    67  	//iacmb.IACMB.Channel.Write(jsonString)
    68  
    69  	//c.Invoke("send", "Test", "this is a message from the GO client", "")
    70  	if f.SignalRClient != nil {
    71  		f.SignalRClient.Invoke("send", Topic, jsonString, "")
    72  	} else {
    73  		com.IACMessageBusClient.Invoke("send", Topic, jsonString, "")
    74  		<-time.After(time.Microsecond * 100)
    75  	}
    76  	/*client, err := dapr.NewClient()
    77  
    78  	if err != nil {
    79  		f.iLog.Error(fmt.Sprintf("Error creating Dapr client for client '%s': %v\n", "clientID", err))
    80  		return
    81  	}
    82  
    83  	defer client.Close()
    84  
    85  	// Publish the message to the client's topic
    86  	err = client.PublishEvent(context.Background(), "IACF-DAPR-BackGround-Function-clientID", Topic, jsonData)
    87  
    88  	if err != nil {
    89  		f.iLog.Error(fmt.Sprintf("Error publishing message to client '%s': %v\n", "IACF-DAPR-clientID", err))
    90  		return
    91  	}
    92  	*/
    93  	outputs := make(map[string][]interface{})
    94  	f.SetOutputs(f.convertMap(outputs))
    95  }
    96  
    97  // Validate validates the SendMessageFuncs function.
    98  // It checks if the namelist and valuelist are empty,
    99  // and if the "Topic" name is present in the namelist.
   100  // Returns true if the validation passes, otherwise returns false with an error.
   101  // It also logs the performance of the function.
   102  func (cf *SendMessageFuncs) Validate(f *Funcs) (bool, error) {
   103  	startTime := time.Now()
   104  	defer func() {
   105  		elapsed := time.Since(startTime)
   106  		f.iLog.PerformanceWithDuration("engine.funcs.SendEmessage.Validate", elapsed)
   107  	}()
   108  	/*	defer func() {
   109  			if err := recover(); err != nil {
   110  				f.iLog.Error(fmt.Sprintf("There is error to engine.funcs.SendEmessage.Validate with error: %s", err))
   111  				f.ErrorMessage = fmt.Sprintf("There is error to engine.funcs.SendEmessage.Validate with error: %s", err)
   112  				return
   113  			}
   114  		}()
   115  	*/
   116  	f.iLog.Debug(fmt.Sprintf("SendMessageFuncs validate: %v", f))
   117  	namelist, valuelist, _ := f.SetInputs()
   118  
   119  	if len(namelist) == 0 {
   120  		return false, fmt.Errorf("SendMessageFuncs validate: %v", "namelist is empty")
   121  	}
   122  
   123  	if len(valuelist) == 0 {
   124  		return false, fmt.Errorf("SendMessageFuncs validate: %v", "valuelist is empty")
   125  	}
   126  	found := false
   127  	for _, name := range namelist {
   128  		if name == "" {
   129  			return false, fmt.Errorf("SendMessageFuncs validate: %v", "name is empty")
   130  		}
   131  
   132  		if name == "Topic" {
   133  			found = true
   134  		}
   135  	}
   136  	if !found {
   137  		return false, fmt.Errorf("SendMessageFuncs validate: %v", "Topic is not found")
   138  	}
   139  
   140  	return true, nil
   141  }