Monitor.go 1.29 KB
package main

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"time"
)

var lastState = 0

func main() {

	for {
		time.Sleep(30 * time.Second)
		httpState, body, error := tools.GET("http://leyoutu.st-i.com.cn/AllScenic")
		if httpState == 200 && error == nil {

			oBody := tools.ResponseSeccess{}
			json.Unmarshal([]byte(body), &oBody)

			rlen := len(oBody.Result.([]interface{}))
			if oBody.ErrCode != 0 || rlen == 0 {
				sms(1)
			} else {
				sms(2)
			}
		} else {

			sms(1)
		}

	}

}

func sms(state int) {

	stateString := ""
	if state == 1{
		stateString = "Fail"
	}
	if state == 2{
		stateString = "Success"
	}
	println("state:",stateString)


	if lastState == 0 {
		lastState = state
	}

	if lastState != state{

		lastState = state

		client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", "LTAI4FdQeNMQXRU6u5J3EFQc", "PwvyF5rRNBWLDya41WrCpvENevYZGi")
		request := dysmsapi.CreateSendSmsRequest()
		request.Scheme = "https"
		request.PhoneNumbers = "18616619599"
		request.SignName = "乐游图"
		request.TemplateCode = "SMS_182595013"
		request.TemplateParam = "{\"code\":\"" + stateString +"\"}"
		response, err := client.SendSms(request)
		if err != nil {
			println(err.Error())
		}
		println("SMS:", response.Code)
	}
}