CommWeb.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. package controllers
  2. import (
  3. "bufio"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "os"
  10. "reflect"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "dashoo.cn/business/permission"
  15. "dashoo.cn/utils"
  16. )
  17. type Series struct {
  18. Name string `json:"name"`
  19. Columns []string `json:"columns"`
  20. Points [][]interface{} `json:"points"`
  21. }
  22. // 返回Bootstrap格式的提示信息
  23. func Alert(message string) (alert string) {
  24. // Bootstrap提示信息HTML模板
  25. template := `<div class="alert alert-info">
  26. <button type="button" class="close" data-dismiss="alert">&times;</button>
  27. %s
  28. </div>
  29. `
  30. return fmt.Sprintf(template, message) // 提示信息HTML代码
  31. }
  32. // 如果当前分类被选中则返回` selected`字符串
  33. func IsSelected(categoryId1 int, categoryId2 int) bool {
  34. if categoryId1 == categoryId2 {
  35. return true
  36. } else {
  37. return false
  38. }
  39. }
  40. // 判断选择状态
  41. func MenuSelectCss(urlstr, selcturl string) string {
  42. if urlstr == selcturl {
  43. return "active teal"
  44. } else {
  45. return ""
  46. }
  47. }
  48. //打印相关
  49. type PrintColType struct {
  50. JsonType string
  51. FieldIndex int
  52. FieldType string
  53. FieldSize int
  54. FieldName string
  55. Required bool
  56. }
  57. type PrintData struct {
  58. Data interface{}
  59. Cols []PrintColType
  60. }
  61. type PrintInfo struct {
  62. Alldata []PrintData `json:"alldata"`
  63. Vars []string `json:"vars"`
  64. }
  65. func AutoColType(source interface{}) (cols []PrintColType) {
  66. sObjT := reflect.TypeOf(source).Elem()
  67. for i := 0; i < sObjT.NumField(); i++ {
  68. fieldT := sObjT.Field(i)
  69. var fieldName, jsontype, datatype string = fieldT.Name, fieldT.Type.Kind().String(), "string"
  70. var fildsize = 2000
  71. switch fieldT.Type.Kind() {
  72. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  73. fildsize = 0
  74. datatype = "integer"
  75. case reflect.Float32, reflect.Float64:
  76. datatype = "float"
  77. fildsize = 0
  78. }
  79. cols = append(cols, PrintColType{jsontype, i, datatype, fildsize, fieldName, false})
  80. }
  81. return
  82. }
  83. //base64 解密
  84. const (
  85. base64Table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  86. )
  87. var coder = base64.NewEncoding(base64Table)
  88. func Base64Decode(src string) string {
  89. co, _ := coder.DecodeString(src)
  90. return string(co)
  91. }
  92. // 判断选择状态
  93. func Actionselect(actionsitem, actionsitemname int) string {
  94. if actionsitem == actionsitemname {
  95. return "selected"
  96. } else {
  97. return ""
  98. }
  99. }
  100. // 判断单选框选择状态
  101. func Radioselect(actionsitem, actionsitemname int) string {
  102. if actionsitem == actionsitemname {
  103. return "checked"
  104. } else {
  105. return ""
  106. }
  107. }
  108. // 保留两位小数
  109. func Get2point(v float64) string {
  110. return strconv.FormatFloat(v, 'f', 2, 64)
  111. }
  112. func Timeisnull(t time.Time) bool {
  113. var to time.Time
  114. return t.Equal(to)
  115. }
  116. func IsAuthorized(uid int, permissionItemCode string) bool {
  117. svc := permission.GetPermissionService(utils.DBE)
  118. return svc.IsAuthorized(utils.ToStr(uid), permissionItemCode)
  119. }
  120. //网页导出成文件
  121. func PageUrltoFile(url, upath, filename string) string {
  122. response, _ := http.Get(url)
  123. defer response.Body.Close()
  124. body, _ := ioutil.ReadAll(response.Body)
  125. var spit string
  126. if os.IsPathSeparator('\\') { //前边的判断是否是系统的分隔符
  127. spit = "\\"
  128. } else {
  129. spit = "/"
  130. }
  131. var path = upath
  132. path = strings.Replace(path, "/", spit, -1)
  133. dir, _ := os.Getwd() //当前目录
  134. path = dir + path
  135. os.MkdirAll(path, os.ModePerm)
  136. file, _ := os.Create(path + filename)
  137. defer file.Close()
  138. w := bufio.NewWriter(file)
  139. w.WriteString(string(body))
  140. w.Flush()
  141. return upath + filename
  142. }
  143. func AddNum(i1, i2 int) int {
  144. return i1 + i2
  145. }
  146. func GetChannelInfo(code, u, p string, startandend ...int64) (ctype string, cols []string, arrs [][]interface{}) {
  147. var data []Series
  148. strUrl := utils.Cfg.MustValue("server", "dataurl") + fmt.Sprintf("/datapoints/channels/%v?u=%v&p=%v", code, u, p)
  149. if len(startandend) > 0 {
  150. 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])
  151. }
  152. //strUrl := "http://114.215.122.32:9086/v1/datapoints/channels/ceszh01?u=sandbox&p=sandbox&start=1416799160&end=1516899260"
  153. json.Unmarshal(Apiget(strUrl), &data)
  154. if len(data) > 0 {
  155. indexlt := utils.SliceIndexOf(data[0].Columns, "lat")
  156. indexlg := utils.SliceIndexOf(data[0].Columns, "lng")
  157. indexw := utils.SliceIndexOf(data[0].Columns, "w")
  158. indexh := utils.SliceIndexOf(data[0].Columns, "h")
  159. indexp := utils.SliceIndexOf(data[0].Columns, "p")
  160. indexbd := utils.SliceIndexOf(data[0].Columns, "bd")
  161. indextime := utils.SliceIndexOf(data[0].Columns, "time")
  162. if indexlt > -1 && indexlg > -1 {
  163. ctype = "gps"
  164. for i, j := 0, len(data[0].Points); i < j; i++ {
  165. if indextime > -1 {
  166. 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]})
  167. } else {
  168. arrs = append(arrs, []interface{}{data[0].Points[j-1-i][indexlt], data[0].Points[j-1-i][indexlg]})
  169. }
  170. }
  171. cols = []string{"time", "lt", "lg"}
  172. } else if indexw > -1 && indexh > -1 && indexp > -1 && indexbd > -1 {
  173. ctype = "photo"
  174. for i, j := 0, len(data[0].Points); i < j; i++ {
  175. if indextime > -1 {
  176. 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]})
  177. } else {
  178. 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]})
  179. }
  180. }
  181. cols = []string{"time", "w", "h", "p", "bd"}
  182. } else {
  183. ctype = "value"
  184. cols, arrs = ChannelInfoValue(data[0].Columns, data[0].Points)
  185. }
  186. return
  187. }
  188. return
  189. }
  190. func ChannelInfoValue(clos []string, points [][]interface{}) (closnew []string, pointsnew [][]interface{}) {
  191. colsindexstr := make([]string, 0)
  192. for k, v := range clos {
  193. //去除一些非数字的特殊字段
  194. if v == "sequence_number" || strings.Index(v, "zbackup") == 0 || v == "bbmac" || v == "cjtime" || v == "cstime" {
  195. colsindexstr = append(colsindexstr, utils.ToStr(k))
  196. } else {
  197. closnew = append(closnew, v)
  198. }
  199. }
  200. for i, j := 0, len(points); i < j; i++ {
  201. ps := make([]interface{}, 0)
  202. for k, v := range points[j-1-i] {
  203. if !utils.SliceContains(colsindexstr, utils.ToStr(k)) {
  204. ps = append(ps, v)
  205. }
  206. }
  207. pointsnew = append(pointsnew, ps)
  208. }
  209. return
  210. }
  211. func Apiget(str string) (body []byte) {
  212. str = strings.Replace(str, " ", "%20", -1)
  213. response, _ := http.Get(str)
  214. if response != nil {
  215. defer response.Body.Close()
  216. body, _ = ioutil.ReadAll(response.Body)
  217. }
  218. //json.Unmarshal(body, &devices)
  219. return
  220. }
  221. //121.42.183.126 10.251.48.16 //"ccreader", "CC@1511162efvbf2d", "coldcloud", "114.215.122.32:9086"
  222. // "ccqa", "ccqa@1qaz2wsx", "coldcloudqa", "121.42.183.126:9086"10.251.48.16:9086
  223. func GetInfluxDBService(qadb string) (string, string, string, string) {
  224. if qadb == "test" {
  225. return "ccqa", "ccqa@1qaz2wsx", "coldcloudqa", "47.92.212.59:9086"
  226. } else {
  227. return "coldcloud", "cc@1qaz2wsx", "coldcloud", "47.92.212.59:9086"
  228. }
  229. }