package controllers import ( "bufio" "encoding/base64" "encoding/json" "fmt" "io/ioutil" "net/http" "os" "reflect" "strconv" "strings" "time" "dashoo.cn/business/permission" "dashoo.cn/utils" ) type Series struct { Name string `json:"name"` Columns []string `json:"columns"` Points [][]interface{} `json:"points"` } // 返回Bootstrap格式的提示信息 func Alert(message string) (alert string) { // Bootstrap提示信息HTML模板 template := `
%s
` return fmt.Sprintf(template, message) // 提示信息HTML代码 } // 如果当前分类被选中则返回` selected`字符串 func IsSelected(categoryId1 int, categoryId2 int) bool { if categoryId1 == categoryId2 { return true } else { return false } } // 判断选择状态 func MenuSelectCss(urlstr, selcturl string) string { if urlstr == selcturl { return "active teal" } else { return "" } } //打印相关 type PrintColType struct { JsonType string FieldIndex int FieldType string FieldSize int FieldName string Required bool } type PrintData struct { Data interface{} Cols []PrintColType } type PrintInfo struct { Alldata []PrintData `json:"alldata"` Vars []string `json:"vars"` } func AutoColType(source interface{}) (cols []PrintColType) { sObjT := reflect.TypeOf(source).Elem() for i := 0; i < sObjT.NumField(); i++ { fieldT := sObjT.Field(i) var fieldName, jsontype, datatype string = fieldT.Name, fieldT.Type.Kind().String(), "string" var fildsize = 2000 switch fieldT.Type.Kind() { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: fildsize = 0 datatype = "integer" case reflect.Float32, reflect.Float64: datatype = "float" fildsize = 0 } cols = append(cols, PrintColType{jsontype, i, datatype, fildsize, fieldName, false}) } return } //base64 解密 const ( base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ) var coder = base64.NewEncoding(base64Table) func Base64Decode(src string) string { co, _ := coder.DecodeString(src) return string(co) } // 判断选择状态 func Actionselect(actionsitem, actionsitemname int) string { if actionsitem == actionsitemname { return "selected" } else { return "" } } // 判断单选框选择状态 func Radioselect(actionsitem, actionsitemname int) string { if actionsitem == actionsitemname { return "checked" } else { return "" } } // 保留两位小数 func Get2point(v float64) string { return strconv.FormatFloat(v, 'f', 2, 64) } func Timeisnull(t time.Time) bool { var to time.Time return t.Equal(to) } func IsAuthorized(uid int, permissionItemCode string) bool { svc := permission.GetPermissionService(utils.DBE) return svc.IsAuthorized(utils.ToStr(uid), permissionItemCode) } //网页导出成文件 func PageUrltoFile(url, upath, filename string) string { response, _ := http.Get(url) defer response.Body.Close() body, _ := ioutil.ReadAll(response.Body) var spit string if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符 spit = "\\" } else { spit = "/" } var path = upath path = strings.Replace(path, "/", spit, -1) dir, _ := os.Getwd() //当前目录 path = dir + path os.MkdirAll(path, os.ModePerm) file, _ := os.Create(path + filename) defer file.Close() w := bufio.NewWriter(file) w.WriteString(string(body)) w.Flush() return upath + filename } func AddNum(i1, i2 int) int { return i1 + i2 } func GetChannelInfo(code, u, p string, startandend ...int64) (ctype string, cols []string, arrs [][]interface{}) { var data []Series strUrl := utils.Cfg.MustValue("server", "dataurl") + fmt.Sprintf("/datapoints/channels/%v?u=%v&p=%v", code, u, p) if len(startandend) > 0 { strUrl = utils.Cfg.MustValue("server", "dataurl") + fmt.Sprintf("/datapoints/channels/%v?u=%v&p=%v&start=%v&end=%v", code, u, p, startandend[0], startandend[1]) } //strUrl := "http://114.215.122.32:9086/v1/datapoints/channels/ceszh01?u=sandbox&p=sandbox&start=1416799160&end=1516899260" json.Unmarshal(Apiget(strUrl), &data) if len(data) > 0 { indexlt := utils.SliceIndexOf(data[0].Columns, "lat") indexlg := utils.SliceIndexOf(data[0].Columns, "lng") indexw := utils.SliceIndexOf(data[0].Columns, "w") indexh := utils.SliceIndexOf(data[0].Columns, "h") indexp := utils.SliceIndexOf(data[0].Columns, "p") indexbd := utils.SliceIndexOf(data[0].Columns, "bd") indextime := utils.SliceIndexOf(data[0].Columns, "time") if indexlt > -1 && indexlg > -1 { ctype = "gps" for i, j := 0, len(data[0].Points); i < j; i++ { if indextime > -1 { arrs = append(arrs, []interface{}{data[0].Points[j-1-i][indextime], data[0].Points[j-1-i][indexlt], data[0].Points[j-1-i][indexlg]}) } else { arrs = append(arrs, []interface{}{data[0].Points[j-1-i][indexlt], data[0].Points[j-1-i][indexlg]}) } } cols = []string{"time", "lt", "lg"} } else if indexw > -1 && indexh > -1 && indexp > -1 && indexbd > -1 { ctype = "photo" for i, j := 0, len(data[0].Points); i < j; i++ { if indextime > -1 { arrs = append(arrs, []interface{}{data[0].Points[j-1-i][indextime], data[0].Points[j-1-i][indexw], data[0].Points[j-1-i][indexh], data[0].Points[j-1-i][indexp], data[0].Points[j-1-i][indexbd]}) } else { arrs = append(arrs, []interface{}{data[0].Points[j-1-i][indexw], data[0].Points[j-1-i][indexh], data[0].Points[j-1-i][indexp], data[0].Points[j-1-i][indexbd]}) } } cols = []string{"time", "w", "h", "p", "bd"} } else { ctype = "value" cols, arrs = ChannelInfoValue(data[0].Columns, data[0].Points) } return } return } func ChannelInfoValue(clos []string, points [][]interface{}) (closnew []string, pointsnew [][]interface{}) { colsindexstr := make([]string, 0) for k, v := range clos { //去除一些非数字的特殊字段 if v == "sequence_number" || strings.Index(v, "zbackup") == 0 || v == "bbmac" || v == "cjtime" || v == "cstime" { colsindexstr = append(colsindexstr, utils.ToStr(k)) } else { closnew = append(closnew, v) } } for i, j := 0, len(points); i < j; i++ { ps := make([]interface{}, 0) for k, v := range points[j-1-i] { if !utils.SliceContains(colsindexstr, utils.ToStr(k)) { ps = append(ps, v) } } pointsnew = append(pointsnew, ps) } return } func Apiget(str string) (body []byte) { str = strings.Replace(str, " ", "%20", -1) response, _ := http.Get(str) if response != nil { defer response.Body.Close() body, _ = ioutil.ReadAll(response.Body) } //json.Unmarshal(body, &devices) return } //121.42.183.126 10.251.48.16 //"ccreader", "CC@1511162efvbf2d", "coldcloud", "114.215.122.32:9086" // "ccqa", "ccqa@1qaz2wsx", "coldcloudqa", "121.42.183.126:9086"10.251.48.16:9086 func GetInfluxDBService(qadb string) (string, string, string, string) { if qadb == "test" { return "ccqa", "ccqa@1qaz2wsx", "coldcloudqa", "47.92.212.59:9086" } else { return "coldcloud", "cc@1qaz2wsx", "coldcloud", "47.92.212.59:9086" } }