gateway.go 3.7 KB
Newer Older
E
eoLinker API Management 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
package gateway

import (
	"net/http"
	"strconv"

	"github.com/eolinker/goku/console/controller"
	"github.com/eolinker/goku/console/module/gateway"
	monitor_read "github.com/eolinker/goku/server/monitor/monitor-read"
)

func GetGatewayConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationGatewayConfig, controller.OperationREAD)
	if e != nil {
		return
	}

	result, err := gateway.GetGatewayConfig()
	if err != nil {

		controller.WriteError(httpResponse,
			"320000",
			"gateway",
			"[ERROR]The gateway config does not exist",
			err)
		return
	}
	controller.WriteResultInfo(httpResponse, "gateway", "gatewayConfig", result)

	return
}

func EditGatewayBaseConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationGatewayConfig, controller.OperationEDIT)
	if e != nil {
		return
	}

	successCode := httpRequest.PostFormValue("successCode")
	nodeUpdatePeriod := httpRequest.PostFormValue("nodeUpdatePeriod")
	monitorUpdatePeriod := httpRequest.PostFormValue("monitorUpdatePeriod")
	monitorTimeout := httpRequest.PostFormValue("monitorTimeout")

	nodePeriod, err := strconv.Atoi(nodeUpdatePeriod)
	if err != nil {

		controller.WriteError(httpResponse,
			"320001",
			"gateway",
			"[ERROR]Illegal nodeUpdatePeriod!",
			err)
		return
	}
	monitorPeriod, err := strconv.Atoi(monitorUpdatePeriod)
	if err != nil && monitorUpdatePeriod != "" {

		controller.WriteError(httpResponse,
			"320002",
			"gateway",
			"[ERROR]Illegal monitorUpdatePeriod!",
			err)
		return
	}
	if monitorUpdatePeriod == "" {
		monitorPeriod = 30
	}
	timeout, err := strconv.Atoi(monitorTimeout)
	if (err != nil && monitorTimeout != "") || (timeout < 1 && timeout > 30) {

		controller.WriteError(httpResponse,
			"320011",
			"gateway",
			"[ERROR]Illegal monitorTimeout!",
			err)
		return
	}
	if monitorTimeout == "" {
		timeout = 5
	}
	flag, result, err := gateway.EditGatewayBaseConfig(successCode, nodePeriod, monitorPeriod, timeout)
	if !flag {
		monitor_read.SetPeriod(monitorPeriod)

		controller.WriteError(httpResponse,
			"320000",
			"gateway",
			result,
			err)
		return
	}

	controller.WriteResultInfo(httpResponse, "gateway", "", nil)

	return
}

func EditGatewayAlarmConfig(httpResponse http.ResponseWriter, httpRequest *http.Request) {
	_, e := controller.CheckLogin(httpResponse, httpRequest, controller.OperationGatewayConfig, controller.OperationEDIT)
	if e != nil {
		return
	}

	alertStatus := httpRequest.PostFormValue("alertStatus")
	apiAlertInfo := httpRequest.PostFormValue("apiAlertInfo")
	sender := httpRequest.PostFormValue("sender")
	senderPassword := httpRequest.PostFormValue("senderPassword")
	smtpAddress := httpRequest.PostFormValue("smtpAddress")
	smtpPort := httpRequest.PostFormValue("smtpPort")
	smtpProtocol := httpRequest.PostFormValue("smtpProtocol")

	aStatus, err := strconv.Atoi(alertStatus)
	if err != nil {

		controller.WriteError(httpResponse,
			"320003",
			"gateway",
			"[ERROR]Illegal alertStatus!",
			err)
		return
	}
	port, err := strconv.Atoi(smtpPort)
	if err != nil {

		controller.WriteError(httpResponse,
			"320005",
			"gateway",
			"[ERROR]Illegal smtpPort!",
			err)
		return
	}
	proto, err := strconv.Atoi(smtpProtocol)
	if err != nil {
		controller.WriteError(httpResponse,
			"320006",
			"gateway",
			"[ERROR]Illegal smtpProtocol!",
			err)
		return
	}
	flag, result, err := gateway.EditGatewayAlarmConfig(apiAlertInfo, sender, senderPassword, smtpAddress, aStatus, port, proto)
	if !flag {
		controller.WriteError(httpResponse,
			"320000",
			"gateway",
			result,
			err)
		return
	}

	controller.WriteResultInfo(httpResponse, "gateway", "", nil)

	return
}