Przeglądaj źródła

add system notiry

LinkLeong 2 lat temu
rodzic
commit
4776b76b16
4 zmienionych plików z 40 dodań i 6 usunięć
  1. 18 0
      common/notify.go
  2. 2 0
      route/route.go
  3. 6 0
      route/v1/notiry.go
  4. 14 6
      service/notify.go

+ 18 - 0
common/notify.go

@@ -18,6 +18,7 @@ const (
 
 type NotifyService interface {
 	SendNotify(path string, message map[string]interface{}) error
+	SendSystemNotify(message map[string]interface{}) error
 }
 type notifyService struct {
 	address string
@@ -41,7 +42,24 @@ func (n *notifyService) SendNotify(path string, message map[string]interface{})
 	return nil
 
 }
+func (n *notifyService) SendSystemNotify(message map[string]interface{}) error {
 
+	url := strings.TrimSuffix(n.address, "/") + "/" + APICasaOSNotify
+	body, err := json.Marshal(message)
+	if err != nil {
+		return err
+	}
+	response, err := http.Post(url, "application/json", bytes.NewBuffer(body))
+	if err != nil {
+		return err
+	}
+
+	if response.StatusCode != http.StatusCreated {
+		return errors.New("failed to send notify (status code: " + fmt.Sprint(response.StatusCode) + ")")
+	}
+	return nil
+
+}
 func NewNotifyService(runtimePath string) (NotifyService, error) {
 	casaosAddressFile := filepath.Join(runtimePath, CasaOSURLFilename)
 

+ 2 - 0
route/route.go

@@ -244,6 +244,8 @@ func InitRouter() *gin.Engine {
 		v1NotifyGroup.Use()
 		{
 			v1NotifyGroup.POST("/:path", v1.PostNotifyMssage)
+			//merge to system
+			v1NotifyGroup.POST("", v1.PostSystemNotyfiy)
 		}
 	}
 	return r

+ 6 - 0
route/v1/notiry.go

@@ -14,3 +14,9 @@ func PostNotifyMssage(c *gin.Context) {
 	service.MyService.Notify().SendNotify(path, message)
 	c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
 }
+func PostSystemNotyfiy(c *gin.Context) {
+	message := make(map[string]interface{})
+	c.ShouldBind(&message)
+	service.MyService.Notify().SettingSystemTempData(message)
+	c.JSON(common_err.SUCCESS, model.Result{Success: common_err.SUCCESS, Message: common_err.GetMsg(common_err.SUCCESS)})
+}

+ 14 - 6
service/notify.go

@@ -38,14 +38,21 @@ type NotifyServer interface {
 	SendAllHardwareStatusBySocket(disk model2.Summary, list []model2.DriveUSB, mem map[string]interface{}, cpu map[string]interface{}, netList []model2.IOCountersStat)
 	SendStorageBySocket(message notify.StorageMessage)
 	SendNotify(path string, message map[string]interface{})
+	SettingSystemTempData(message map[string]interface{})
 }
 
 type notifyServer struct {
-	db *gorm.DB
+	db            *gorm.DB
+	SystemTempMap map[string]interface{}
 }
 
-func (i *notifyServer) SendNotify(path string, message map[string]interface{}) {
+func (i *notifyServer) SettingSystemTempData(message map[string]interface{}) {
+	for k, v := range message {
+		i.SystemTempMap[k] = v
+	}
+}
 
+func (i *notifyServer) SendNotify(path string, message map[string]interface{}) {
 	msg := gosf.Message{}
 	msg.Body = message
 	msg.Success = true
@@ -77,9 +84,6 @@ func (i *notifyServer) SendStorageBySocket(message notify.StorageMessage) {
 func (i *notifyServer) SendAllHardwareStatusBySocket(disk model2.Summary, list []model2.DriveUSB, mem map[string]interface{}, cpu map[string]interface{}, netList []model2.IOCountersStat) {
 
 	body := make(map[string]interface{})
-	body["sys_disk"] = disk
-
-	body["sys_usb"] = list
 
 	body["sys_mem"] = mem
 
@@ -87,6 +91,10 @@ func (i *notifyServer) SendAllHardwareStatusBySocket(disk model2.Summary, list [
 
 	body["sys_net"] = netList
 
+	for k, v := range i.SystemTempMap {
+		body[k] = v
+	}
+
 	msg := gosf.Message{}
 	msg.Body = body
 	msg.Success = true
@@ -470,5 +478,5 @@ func SendMeg() {
 // }
 
 func NewNotifyService(db *gorm.DB) NotifyServer {
-	return &notifyServer{db: db}
+	return &notifyServer{db: db, SystemTempMap: make(map[string]interface{})}
 }