deviceController.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "rtzh_elec_temperature/enum"
  6. "rtzh_elec_temperature/logger"
  7. "rtzh_elec_temperature/models/bo"
  8. "rtzh_elec_temperature/rtelec_app_public_lib/service"
  9. "rtzh_elec_temperature/rtelec_app_public_lib/utils"
  10. "rtzh_elec_temperature/tools"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "git.rtzhtech.cn/iss/public-lib/model"
  15. "github.com/spf13/cast"
  16. )
  17. //设备管理相关服务
  18. type DeviceController struct {
  19. BaseController
  20. }
  21. // 查询设备类型定义
  22. // @Summary 查询设备类型数据列表
  23. // @Description 查询设备类型数据列表。仅返回类型ID(id)和名称(type_name)
  24. // @Tags api
  25. // @Accept x-www-form-urlencoded
  26. // @Produce json
  27. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  28. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  29. // @Failure 500 status 服务器|后台发生错误
  30. // @router /type/list [get]
  31. func (c *DeviceController) GetDeviceTypeList() {
  32. var DeviceInfo = new(service.DeviceService)
  33. DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
  34. list, err := DeviceInfo.SearchDeviceTypeList()
  35. if err != nil {
  36. c.Data["json"] = c.ApiError(err.Error())
  37. } else {
  38. c.Data["json"] = c.ApiOK(list)
  39. }
  40. c.ServeJSON()
  41. }
  42. // 查询设备模型信息
  43. // @Summary 查询设备模型信息列表
  44. // @Description 查询设备模型信息列表。
  45. // @Tags api
  46. // @Accept x-www-form-urlencoded
  47. // @Produce json
  48. // @Param deviceid query string false "查询条件:设备ID。可以为空,不指定设备ID时将获取所有的模型列表信息"
  49. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  50. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  51. // @Failure 500 status 服务器|后台发生错误
  52. // @router /model/list [get]
  53. func (c *DeviceController) GetDeviceModelList() {
  54. deviceid, _ := c.GetInt32("deviceid")
  55. if deviceid == 0 {
  56. //返回全部模型信息
  57. modelobj := new(service.ModelService)
  58. modelobj.UserInfo = c.GetCurrentUserInfo_rt()
  59. list := modelobj.GetModelListObj()
  60. c.Data["json"] = c.ApiOK(list)
  61. c.ServeJSON()
  62. return
  63. }
  64. var DeviceInfo = new(service.DeviceService)
  65. DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
  66. list := DeviceInfo.GetComboxListById(deviceid)
  67. if list == nil {
  68. c.Data["json"] = c.ApiError("未查询到设备的模型信息")
  69. } else {
  70. modSvr := new(service.ModelService)
  71. obj := modSvr.GetModelInfo(tools.IsEmpty(list.Modelid))
  72. c.Data["json"] = c.ApiOK([]interface{}{obj})
  73. }
  74. c.ServeJSON()
  75. }
  76. // 查询设备数据记录
  77. // @Summary 查询设备化记录列表
  78. // @Description 查询设备记录列表
  79. // @Tags api
  80. // @Accept x-www-form-urlencoded
  81. // @Produce json
  82. // @Param devicename query string false "查询条件:设备名称"
  83. // @Param regionid query string false "查询条件:区域ID"
  84. // @Param typeid query string false "查询条件:设备类型ID"
  85. // @Param modelid query string false "查询条件:物模型ID"
  86. // @Param pageindex query int false "当前页码"
  87. // @Param pagesize query int false "每页记录数"
  88. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  89. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  90. // @Failure 500 status 服务器|后台发生错误
  91. // @router /list [get]
  92. func (c *DeviceController) GetDeviceList() {
  93. var DeviceInfo = new(service.DeviceService)
  94. DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
  95. param := map[string]interface{}{}
  96. param["DeviceName"] = c.GetString("devicename")
  97. param["Regionid"] = c.GetString("regionid")
  98. param["Typeid"] = c.GetString("typeid")
  99. param["Modelid"] = c.GetString("modelid")
  100. param["PageIndex"], _ = c.GetInt("pageindex", 1)
  101. param["PageSize"], _ = c.GetInt("pagesize", 20)
  102. list, num, err := DeviceInfo.SearchDeviceList(param)
  103. if err != nil {
  104. c.Data["json"] = c.ApiError(err.Error())
  105. } else {
  106. c.Data["json"] = c.ApiOK(map[string]interface{}{"total": num, "list": list})
  107. }
  108. c.ServeJSON()
  109. }
  110. //设备详细记录信息
  111. // @Summary 获取指定设备的详情
  112. // @Description 获取指定设备的详情,包含设备信息、测点信息、控制点信息
  113. // @Tags api
  114. // @Accept x-www-form-urlencoded
  115. // @Produce json
  116. // @Param deviceid query string true "设备ID"
  117. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  118. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  119. // @Failure 500 status 服务器|后台发生错误
  120. // @router /detail [get]
  121. func (c *DeviceController) GetDeviceDetail() {
  122. var DeviceInfo = new(service.DeviceService)
  123. DeviceInfo.DeviceId, _ = c.GetInt("deviceid")
  124. if DeviceInfo.DeviceId == 0 {
  125. c.Data["json"] = c.ApiError("设备Id参数(deviceid)不允许为空或0!")
  126. } else {
  127. list, err := DeviceInfo.DeviceDetail()
  128. if err != nil {
  129. c.Data["json"] = c.ApiError(err.Error())
  130. } else {
  131. c.Data["json"] = c.ApiOK(list)
  132. }
  133. }
  134. c.ServeJSON()
  135. }
  136. //创建设备
  137. // @Summary 创建新设备
  138. // @Description 创建新设备。需要提交设备基本信息、测点信息以及控制点信息,其中测点信息的控制点信息均为序列化成字符串的JSON对象
  139. // @Tags api
  140. // @Accept x-www-form-urlencoded
  141. // @Produce json
  142. // @Param deviceid query int false "设备ID。编辑设备时必传,否则认为是新建设备"
  143. // @Param stationid query int true "变电站ID。"
  144. // @Param modelid query int true "设备采用的数据模型ID。"
  145. // @Param typeid query int true "设备类型ID。"
  146. // @Param regionid query int false "设备所在区域ID。"
  147. // @Param devicename query string true "设备名称。"
  148. // @Param serial query string false "设备Modbus协议串口号。设备采用串口连接时,必须指定,长度不超过30个字符。"
  149. // @Param slaveid query int false "设备Modbus协议从设备号。设备采用串口连接时,必须指定,仅支持0-255之间的整数值"
  150. // @Param baudrate query int false "设备Modbus协议的波特率。设备采用串口连接时,必须指定,支持4800,9600,14400,19200,32000,38400,56000,57600,152000,194000其中之一,默认为9600。"
  151. // @Param stopbit query int false "设备Modbus协议的停止位。设备采用串口连接时,必须指定,仅支持1或2,默认为1。"
  152. // @Param check_bit query int false "设备Modbus协议的校验位。设备采用串口连接时,必须指定,仅支持N、O、E其中之一,默认为N。"
  153. // @Param data_bit query string false "设备Modbus协议的数据位。设备采用串口连接时,必须指定,仅支持5、6、7、8其中之一,默认为8。"
  154. // @Param ip query string false "设备IP协议地址。设备采用网口连接时,必须指定,长度不超过17个字符,格式为xxx.xxx.xxx.xxx。"
  155. // @Param port query int false "设备IP协议端口。设备采用网口连接时,必须指定,仅支持80-65535之间的整数值。"
  156. // @Param mpinfo_para query JSONString false "测点信息,采用序列化成字符串的JSON对象"
  157. // @Param control_para query JSONString false "测点信息,采用序列化成字符串的JSON对象"
  158. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  159. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  160. // @Failure 500 status 服务器|后台发生错误
  161. // @router /edit [post]
  162. func (c *DeviceController) EditDeviceInfo() {
  163. logObj := new(bo.SystemLog)
  164. user := c.GetCurrentUserInfo_rt()
  165. logObj.UserInfo = map[string]interface{}{"name": user.Usrname, "ip": user.Ip}
  166. logObj.Audittype = enum.AuditType_device
  167. logObj.Eventtype = enum.OptEventType_Bus
  168. logObj.Eventlevel = enum.OptEventLevel_Hight
  169. var deviceField model.DevDevinfo
  170. deviceField.Deviceid, _ = c.GetInt32("deviceid")
  171. if deviceField.Deviceid == 0 {
  172. logObj.Logtype = enum.LogType_Insert
  173. } else {
  174. devObj := new(service.DeviceService)
  175. devObj.DeviceId = int(deviceField.Deviceid)
  176. logObj.Logtype = enum.LogType_Update
  177. deviceOldInfo, err := devObj.DeviceDetail()
  178. if err != nil {
  179. logger.Logger.Error(err)
  180. c.Data["json"] = c.ApiError(err.Error())
  181. c.ServeJSON()
  182. return
  183. }
  184. devoldinfo := deviceOldInfo["device"]
  185. if devoldinfo == nil {
  186. c.Data["json"] = c.ApiError("未找到该设备信息")
  187. c.ServeJSON()
  188. return
  189. }
  190. deviceField = devoldinfo.(model.DevDevinfo)
  191. }
  192. pv, _ := c.GetInt32("stationid")
  193. if deviceField.Deviceid == 0 || pv != 0 {
  194. //新增或者前端传了该参数值时,采用该参数值
  195. deviceField.Stationid = pv
  196. }
  197. ModelId, _ := c.GetInt32("modelid")
  198. if deviceField.Deviceid == 0 || ModelId != 0 {
  199. //新增或者前端传了该参数值时,采用该参数值
  200. deviceField.Modelid = ModelId
  201. }
  202. checkResult := true //校验结果
  203. logdesc := ""
  204. pv, _ = c.GetInt32("regionid")
  205. if deviceField.Deviceid == 0 || pv != 0 {
  206. //新增或者前端传了该参数值时,采用该参数值
  207. deviceField.RegionId = pv
  208. }
  209. pv, _ = c.GetInt32("typeid")
  210. if deviceField.Deviceid == 0 || pv != 0 {
  211. //新增或者前端传了该参数值时,采用该参数值
  212. deviceField.DevTypeId = pv //设备类型ID
  213. if deviceField.DevTypeId == 0 {
  214. checkResult = false
  215. logdesc = "设备类型不能为空"
  216. }
  217. }
  218. stringpv := c.GetString("devicename")
  219. if deviceField.Deviceid == 0 || stringpv != "" {
  220. //新增或者前端传了该参数值时,采用该参数值
  221. deviceField.Devicename = stringpv
  222. if deviceField.Devicename == "" {
  223. checkResult = false
  224. logdesc = "设备名称不能为空"
  225. }
  226. }
  227. stringpv = c.GetString("serial")
  228. if deviceField.Deviceid == 0 || stringpv != "" {
  229. //新增或者前端传了该参数值时,采用该参数值
  230. deviceField.Serial = stringpv
  231. }
  232. pv, _ = c.GetInt32("slaveid", -1)
  233. if deviceField.Deviceid == 0 || pv != -1 {
  234. //新增或者前端传了该参数值时,采用该参数值
  235. deviceField.Slaveid = pv
  236. }
  237. pv, _ = c.GetInt32("baudrate", -1)
  238. if deviceField.Deviceid == 0 || pv != -1 {
  239. //新增或者前端传了该参数值时,采用该参数值
  240. deviceField.Baudrate = pv
  241. }
  242. pv, _ = c.GetInt32("stopbit", -1)
  243. if deviceField.Deviceid == 0 || pv != -1 {
  244. //新增或者前端传了该参数值时,采用该参数值
  245. deviceField.Stopbit = pv
  246. }
  247. stringpv = c.GetString("check_bit", "")
  248. if deviceField.Deviceid == 0 || stringpv != "" {
  249. //新增或者前端传了该参数值时,采用该参数值
  250. deviceField.Checkbit = tools.IsEmpty(stringpv, "N")
  251. }
  252. pv, _ = c.GetInt32("data_bit", -1)
  253. if deviceField.Deviceid == 0 || pv != -1 {
  254. //新增或者前端传了该参数值时,采用该参数值
  255. deviceField.Databit = pv
  256. }
  257. if deviceField.Serial != "" && deviceField.Slaveid == -1 {
  258. logdesc = "从设备号不能为空"
  259. checkResult = false
  260. }
  261. stringpv = c.GetString("ip", "")
  262. if deviceField.Deviceid == 0 || stringpv != "" {
  263. //新增或者前端传了该参数值时,采用该参数值
  264. deviceField.IP = stringpv
  265. if deviceField.IP == "" && deviceField.Serial == "" {
  266. logdesc = "设备串口号或IP地址不能同时为空"
  267. checkResult = false
  268. }
  269. }
  270. pv, _ = c.GetInt32("port", -1)
  271. if deviceField.Deviceid == 0 || pv != -1 {
  272. //新增或者前端传了该参数值时,采用该参数值
  273. deviceField.Port = pv
  274. if deviceField.IP != "" && deviceField.Port == -1 {
  275. logdesc = "网口协议端口不能为空!"
  276. checkResult = false
  277. }
  278. }
  279. if deviceField.Deviceid == 0 {
  280. deviceField.Eid = c.GetString("eid")
  281. deviceField.Disable, _ = c.GetInt32("disable")
  282. }
  283. stringpv = c.GetString("topic", "")
  284. if deviceField.Deviceid == 0 || stringpv != "" {
  285. //新增或者前端传了该参数值时,采用该参数值
  286. deviceField.Mqtttopic = stringpv
  287. }
  288. if !checkResult {
  289. logObj.Description = logdesc
  290. logObj.Fail2()
  291. c.Data["json"] = c.ApiError(logdesc)
  292. c.ServeJSON()
  293. return
  294. }
  295. var AppId = cast.ToInt32(service.RtelecManageApp().RegAppID)
  296. deviceField.Appid = AppId
  297. var err error
  298. //测点信息
  299. mpMgr := new(service.MpinfoService)
  300. MpField := []model.DevMpinfo{}
  301. var MpParameter = c.GetString("mpinfo_para")
  302. if MpParameter != "" {
  303. //设备数据中包含测点数据时
  304. MpField, err = mpMgr.GetMpField(MpParameter, AppId, ModelId)
  305. if err != nil {
  306. c.Data["json"] = c.ApiError(err.Error())
  307. c.ServeJSON()
  308. return
  309. }
  310. }
  311. //控制点信息
  312. controlData := []map[string]interface{}{}
  313. var control_parameter = c.GetString("control_para")
  314. if control_parameter != "" {
  315. //设备数据中包含控制点数据时
  316. controlData, err = mpMgr.GetControlField(control_parameter, AppId, ModelId, cast.ToInt64(deviceField.Deviceid))
  317. if err != nil {
  318. c.Data["json"] = c.ApiError(err.Error())
  319. c.ServeJSON()
  320. return
  321. }
  322. }
  323. var messageId = tools.GetUid()
  324. msgobj := new(utils.MsgStateManage)
  325. msgobj.SetMessageStateValue(messageId, utils.MsgState{Success: false, State: false, Message: ""})
  326. deviceObj := new(service.DeviceService)
  327. deviceObj.UserInfo = c.GetCurrentUserInfo_rt()
  328. editError := deviceObj.CreateDevice(messageId, deviceField, MpField, controlData)
  329. if editError != nil {
  330. c.Data["json"] = c.ApiError(editError.Error())
  331. } else {
  332. Result := msgobj.HanderMesage(messageId, 5)
  333. if Result.Success {
  334. logdesc = fmt.Sprintf("创建设备[%s]成功!", deviceField.Devicename)
  335. logObj.Description = logdesc
  336. logObj.Success2()
  337. c.Data["json"] = c.ApiOK(logdesc)
  338. new(service.LogService).SaveLog(logdesc)
  339. } else {
  340. logdesc = fmt.Sprintf("创建设备[%s]失败,错误:%s,操作数据:%+v", deviceField.Devicename, Result.Message, deviceField)
  341. logObj.Description = logdesc
  342. logObj.Fail2()
  343. c.Data["json"] = c.ApiError(Result.Message)
  344. new(service.LogService).SaveLog(logdesc)
  345. }
  346. }
  347. c.ServeJSON()
  348. }
  349. // 保存设备测点信息
  350. // @Summary 保存设备测点信息
  351. // @Description 保存设备测点信息。
  352. // @Tags api
  353. // @Accept x-www-form-urlencoded
  354. // @Produce json
  355. // @Param deviceid query int true "测点所属的设备ID。"
  356. // @Param modelid query int true "测点关联的模型ID"
  357. // @Param mpid query int false "测点ID。未传值时表示新增操作"
  358. // @Param mpname query string true "测点名称。建议20个字符以内"
  359. // @Param zonename query string false "测点所在区域名称"
  360. // @Param positionname query string false "测点所在位置名称"
  361. // @Param unit query string false "测点单位。指该测点采集数据的计量单位。"
  362. // @Param attrname query string true "测点关联的模型属性名称"
  363. // @Param phase query string false "测点监测对象的相位。仅为空值、A、B、C、N其中之一。"
  364. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  365. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  366. // @Failure 500 status 服务器|后台发生错误
  367. // @router /mp/save [post]
  368. func (c *DeviceController) SaveMpInfo() {
  369. var appId = cast.ToInt32(service.RtelecManageApp().RegAppID)
  370. logObj := new(bo.SystemLog)
  371. user := c.GetCurrentUserInfo_rt()
  372. logObj.UserInfo = map[string]interface{}{"name": user.Usrname, "ip": user.Ip}
  373. logObj.Audittype = enum.AuditType_device
  374. logObj.Eventtype = enum.OptEventType_Bus
  375. logObj.Eventlevel = enum.OptEventLevel_Hight
  376. deviceid, _ := c.GetInt32("deviceid")
  377. if deviceid == 0 {
  378. c.Data["json"] = c.ApiError("设备编号不能为空!")
  379. c.ServeJSON()
  380. return
  381. }
  382. mpId, _ := c.GetInt64("mpid") //测点id。未提交时为新增操作
  383. deviceObj := new(service.DeviceService)
  384. deviceObj.UserInfo = user
  385. mp_Field := []model.DevMpinfo{}
  386. mp_Field = append(mp_Field, model.DevMpinfo{})
  387. isAdd := true
  388. if mpId > 0 {
  389. isAdd = false
  390. } else {
  391. mp_Field[0].Online = 1 //新增测点时,默认为离线
  392. mp_Field[0].Disable = 1 //新增时默认为启用
  393. }
  394. mp_Field[0].Mpid = mpId
  395. mp_Field[0].Appid = appId
  396. mp_Field[0].Attrname = strings.Trim(c.GetString("attrname"), " ")
  397. if mp_Field[0].Attrname == "" {
  398. c.Data["json"] = c.ApiError("关联的模型属性名称不能为空!")
  399. c.ServeJSON()
  400. return
  401. }
  402. mp_Field[0].Modelid, _ = c.GetInt32("modelid")
  403. mp_Field[0].Deviceid = deviceid
  404. mp_Field[0].Mpname = c.GetString("mpname")
  405. if mp_Field[0].Mpname == "" {
  406. c.Data["json"] = c.ApiError("测点名称不能为空!")
  407. c.ServeJSON()
  408. return
  409. }
  410. mp_Field[0].Positionname = c.GetString("positionname")
  411. mp_Field[0].Zonename = c.GetString("zonename")
  412. mp_Field[0].Unit = c.GetString("unit")
  413. mp_Field[0].Phase = c.GetString("phase")
  414. //logger.Logger.Debug(fmt.Sprintf("================编辑测点:%+v", mp_Field[0]))
  415. err := deviceObj.EditMpInfo(mp_Field, deviceid, isAdd)
  416. if err != nil {
  417. c.Data["json"] = c.ApiError(err.Error())
  418. } else {
  419. c.Data["json"] = c.ApiOK("")
  420. }
  421. c.ServeJSON()
  422. }
  423. //获取区域名称接口。[该接口已废弃,建议使用/api/region/list]
  424. // @router /area [get]
  425. func (c *DeviceController) GetAreaName() {
  426. deviceObject := new(service.DeviceService)
  427. deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
  428. list, err := deviceObject.GetAreaName()
  429. if err != nil {
  430. c.Data["json"] = c.ApiError(err.Error())
  431. } else {
  432. c.Data["json"] = c.ApiOK(list)
  433. }
  434. c.ServeJSON()
  435. }
  436. //获取设备测点数据
  437. // @Summary 获取设备测点信息列表
  438. // @Description 获取设备测点信息列表。仅返回设备ID、测点ID以及测点名称。
  439. // @Param deviceid query int true "测点所属的设备ID。"
  440. // @Param pageindex query int false "当前页码"
  441. // @Param pagesize query int false "每页记录数"
  442. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  443. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  444. // @Failure 500 status 服务器|后台发生错误
  445. // @router /mp/list [get]
  446. func (c *DeviceController) GetDeviceMpid() {
  447. deviceObject := new(service.DeviceService)
  448. deviceid, _ := c.GetInt32("deviceid")
  449. if deviceid == 0 {
  450. c.Data["json"] = c.ApiError("设备编号不能为空")
  451. } else {
  452. pageindex, _ := c.GetInt("pageindex")
  453. pagesize, _ := c.GetInt("pagesize")
  454. list, num, err := deviceObject.DeviceMpList(deviceid, pageindex, pagesize)
  455. if err != nil {
  456. c.Data["json"] = c.ApiError(err.Error())
  457. } else {
  458. c.Data["json"] = c.ApiOK(map[string]interface{}{"list": list, "total": num})
  459. }
  460. }
  461. c.ServeJSON()
  462. }
  463. // 获取设备测点的某一个测点信息
  464. // @Summary 获取设备一个测点的详细信息
  465. // @Description 获取设备一个测点的详细信息
  466. // @Param mpid query int true "设备测点的id"
  467. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  468. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  469. // @Failure 500 status 服务器|后台发生错误
  470. // @router /mp/mpInfoDetail [get]
  471. func (c *DeviceController) GetMpInfoDetail() {
  472. mpid, err := c.GetInt64("mpid")
  473. if err != nil {
  474. logger.Logger.Error(err)
  475. c.Data["json"] = c.ApiError("测点id获取出现错误!类型转化错误!")
  476. c.ServeJSON()
  477. return
  478. } else if mpid == 0 {
  479. logger.Logger.Error(mpid, "获取id出现错误!数值为0")
  480. c.Data["json"] = c.ApiError("测点id获取出现错误!类型转化错误!")
  481. c.ServeJSON()
  482. return
  483. }
  484. deviceObject := new(service.MpinfoService)
  485. mpinfo, err := deviceObject.GetMpinfo(mpid)
  486. if err == nil {
  487. c.Data["json"] = c.ApiOK(mpinfo)
  488. } else {
  489. logger.Logger.Error(err)
  490. c.Data["json"] = c.ApiError("测点id获取详情出现错误!")
  491. }
  492. c.ServeJSON()
  493. }
  494. //获取设备动作信息
  495. // @router /controlid [post,get]
  496. func (c *DeviceController) GetDeviceControlID() {
  497. deviceObject := new(service.DeviceService)
  498. list, err := deviceObject.GetDeviceControlId()
  499. if err != nil {
  500. c.Data["json"] = c.ApiError(err.Error())
  501. } else {
  502. c.Data["json"] = c.ApiOK(list)
  503. }
  504. c.ServeJSON()
  505. }
  506. // 获取设备树
  507. // @Summary 获取设备列表并以树形数据结构返回
  508. // @Description 获取设备列表并以树形数据结构返回。数据格式为: [{id:[节点ID],label:[节点名称],type:[节点类型(root|region|device)],devcount:[挂载设备数量],children:[嵌套子节点]}]。
  509. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  510. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  511. // @Failure 500 status 服务器|后台发生错误
  512. // @router /tree [get]
  513. func (c *DeviceController) GetDeviceTree() {
  514. usr := c.GetCurrentUserInfo_rt()
  515. deviceObject := new(service.DeviceService)
  516. regService := new(service.RegionService)
  517. deviceObject.UserInfo = usr
  518. regService.UserInfo = usr
  519. var err error
  520. //获取变电站名称
  521. stationinfo, err := regService.GetStationInfo()
  522. if err != nil {
  523. c.Data["json"] = c.ApiError(err.Error())
  524. c.ServeJSON()
  525. return
  526. }
  527. stationname := tools.IsEmpty(stationinfo["name"])
  528. //获取区域列表
  529. regionlist, err := regService.List(nil)
  530. if err != nil {
  531. c.Data["json"] = c.ApiError(err.Error())
  532. c.ServeJSON()
  533. return
  534. }
  535. tree := []interface{}{}
  536. //生成根节点
  537. node := map[string]interface{}{"id": tools.IsEmpty(stationinfo["stationid"]),
  538. "label": stationname,
  539. "type": "root",
  540. "children": []interface{}{},
  541. }
  542. regionNodes := []interface{}{}
  543. for _, row := range regionlist {
  544. regnode := map[string]interface{}{
  545. "id": tools.IsEmpty(row["id"]),
  546. "label": tools.IsEmpty(row["region_name"]),
  547. "type": "region",
  548. "devcount": 0,
  549. "children": []interface{}{},
  550. }
  551. regionNodes = append(regionNodes, regnode)
  552. }
  553. devlist, err := deviceObject.GetAllDevice()
  554. if err != nil {
  555. c.Data["json"] = c.ApiError(err.Error())
  556. } else {
  557. for _, regitem := range regionNodes {
  558. regitem2 := regitem.(map[string]interface{})
  559. regid := tools.IsEmpty(regitem2["id"])
  560. for _, devitem := range devlist {
  561. devregid := tools.IsEmpty(devitem["region_id"])
  562. if regid == devregid {
  563. devnode := map[string]interface{}{
  564. "id": tools.IsEmpty(devitem["deviceid"]),
  565. "label": tools.IsEmpty(devitem["devicename"]),
  566. "type": "device",
  567. //"children": []interface{}{},
  568. }
  569. regitem2["devcount"] = cast.ToInt(regitem2["devcount"]) + 1
  570. regitem2["children"] = append(regitem2["children"].([]interface{}), devnode)
  571. }
  572. }
  573. }
  574. node["children"] = append(node["children"].([]interface{}), regionNodes)
  575. tree = append(tree, node)
  576. c.Data["json"] = c.ApiOK(tree)
  577. }
  578. c.ServeJSON()
  579. }
  580. // 设备列表
  581. // @Summary 获取所有设备详情信息列表
  582. // @Description 获取所有设备详情信息列表。
  583. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  584. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  585. // @Failure 500 status 服务器|后台发生错误
  586. // @router /all [get]
  587. func (c *DeviceController) GetAllList() {
  588. usr := c.GetCurrentUserInfo_rt()
  589. deviceObject := new(service.DeviceService)
  590. deviceObject.UserInfo = usr
  591. list, err := deviceObject.GetAllDevice()
  592. if err != nil {
  593. c.Data["json"] = c.ApiError(err.Error())
  594. } else {
  595. c.Data["json"] = c.ApiOK(list)
  596. }
  597. c.ServeJSON()
  598. }
  599. //设备列表,包含模型
  600. // @Summary 获取设备简单信息列表
  601. // @Description 获取设备简单信息列表。仅返回设备ID和设备名称。
  602. // @Param deviceid query int true "测点所属的设备ID。"
  603. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  604. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  605. // @Failure 500 status 服务器|后台发生错误
  606. // @router /comboxlist [get]
  607. func (c *DeviceController) GetComboxList() {
  608. deviceObject := new(service.DeviceService)
  609. list, err := deviceObject.GetComboxList()
  610. var result []map[string]interface{}
  611. if err != nil {
  612. c.Data["json"] = c.ApiError(err.Error())
  613. } else {
  614. if len(list) > 0 {
  615. modelInfo := new(service.ModelService).GetModelListObj()
  616. for _, row := range list {
  617. var devicename = row.Name
  618. for _, row2 := range modelInfo {
  619. record := row2.(map[string]interface{})
  620. recordid := tools.IsEmpty(string(record["id"].(json.Number)))
  621. if recordid != "" {
  622. modelid, _ := strconv.Atoi(recordid)
  623. if row.Modelid == modelid {
  624. devicename += "(" + record["model_name"].(string) + ")"
  625. break
  626. }
  627. }
  628. }
  629. result = append(result, map[string]interface{}{"deviceid": row.Deviceid, "devicename": devicename})
  630. }
  631. }
  632. c.Data["json"] = c.ApiOK(result)
  633. }
  634. c.ServeJSON()
  635. }
  636. // 删除设备
  637. // @Summary 删除设备
  638. // @Description 删除设备。同时删除设备关联数据,包括关联的测点、控制点以及历史数据等
  639. // @Tags api
  640. // @Accept x-www-form-urlencoded
  641. // @Produce json
  642. // @Param deviceid query int true "测点所属的设备ID。"
  643. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  644. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  645. // @Failure 500 status 服务器|后台发生错误
  646. // @router /delete [post]
  647. func (c *DeviceController) DeleteDeviceInfo() {
  648. Id, _ := c.GetInt("deviceid", 0)
  649. if Id == 0 {
  650. c.Data["json"] = c.ApiError("设备ID参数(deviceid)不允许为空!")
  651. } else {
  652. deviceObject := new(service.DeviceService)
  653. deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
  654. deviceObject.DeviceId = Id
  655. list, _ := deviceObject.DeviceDetail()
  656. if list == nil {
  657. c.Data["json"] = c.ApiError(fmt.Sprintf("设备(device_id:%d)不存在!", Id))
  658. c.ServeJSON()
  659. return
  660. }
  661. if list == nil || list["device"] == nil {
  662. c.Data["json"] = c.ApiError(fmt.Sprintf("设备(device_id:%d)不存在!", Id))
  663. c.ServeJSON()
  664. return
  665. }
  666. deviceInfo := list["device"].(model.DevDevinfo)
  667. err := deviceObject.DeleteDeviceByMqtt(Id)
  668. if err != nil {
  669. c.Data["json"] = c.ApiError(err.Error())
  670. } else {
  671. c.Data["json"] = c.ApiOK("")
  672. new(service.LogService).SaveLog(fmt.Sprintf("删除设备成功,设备名称:%s", deviceInfo.Devicename))
  673. }
  674. }
  675. c.ServeJSON()
  676. }
  677. // 删除设备的指定测点
  678. // @Summary 删除设备的指定测点
  679. // @Description 删除设备的指定测点。
  680. // @Tags api
  681. // @Accept x-www-form-urlencoded
  682. // @Produce json
  683. // @Param deviceid query int true "测点所属的设备ID。"
  684. // @Param mpid query int false "测点ID。未传值时表示删除该设备所有测点"
  685. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  686. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  687. // @Failure 500 status 服务器|后台发生错误
  688. // @router /mp/delete [post]
  689. func (c *DeviceController) DeleteDeviceMpInfo() {
  690. Id, _ := c.GetInt32("deviceid", 0)
  691. mpId, _ := c.GetInt64("mpid", 0)
  692. if Id == 0 || mpId == 0 {
  693. c.Data["json"] = c.ApiError("设备ID参数(deviceid)或者测点ID(mpid)不允许为空!")
  694. } else {
  695. deviceObject := new(service.DeviceService)
  696. deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
  697. err := deviceObject.DeleteMapByDeviceId(Id, mpId)
  698. if err != nil {
  699. c.Data["json"] = c.ApiError(err.Error())
  700. } else {
  701. c.Data["json"] = c.ApiOK("")
  702. }
  703. }
  704. c.ServeJSON()
  705. }
  706. //历史数据查询
  707. // @Summary 查询设备历史数据列表
  708. // @Description 查询设备历史数据列表。
  709. // @Tags api
  710. // @Accept x-www-form-urlencoded
  711. // @Produce json
  712. // @Param deviceid query int true "查询条件:设备ID"
  713. // @Param attrname query int false "查询条件:模型属性名称"
  714. // @Param starttime query int false "查询条件:开始日期"
  715. // @Param endtime query int false "查询条件:结束日期"
  716. // @Param mpid query int false "查询条件:测点ID"
  717. // @Param pageindex query int false "当前页码"
  718. // @Param pagesize query int false "每页记录数"
  719. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  720. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  721. // @Failure 500 status 服务器|后台发生错误
  722. // @router /history [get]
  723. func (c *DeviceController) SearchHistory() {
  724. var hisotryObject = new(service.HistoryService)
  725. hisotryObject.StartDate = c.GetString("starttime")
  726. hisotryObject.EndDate = c.GetString("endtime")
  727. s1 := int64(0)
  728. s2 := int64(0)
  729. if hisotryObject.StartDate != "" {
  730. s1 = tools.DateString2Unix(hisotryObject.StartDate)
  731. }
  732. if hisotryObject.EndDate != "" {
  733. s2 = tools.DateString2Unix(hisotryObject.EndDate)
  734. } else {
  735. s2 = time.Now().Unix()
  736. }
  737. if s1 > 0 {
  738. if s2-s1 > (7 * 24 * 60 * 60) {
  739. c.Data["json"] = c.ApiError("查询日期周期不能大于7天")
  740. c.ServeJSON()
  741. return
  742. }
  743. }
  744. hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
  745. if hisotryObject.Model.Deviceid == 0 {
  746. c.Data["json"] = c.ApiError("设备编号不能为空")
  747. c.ServeJSON()
  748. return
  749. }
  750. hisotryObject.Model.Attrname = c.GetString("attrname")
  751. hisotryObject.PageIndex, _ = c.GetInt("pageindex", 1)
  752. hisotryObject.PageSize, _ = c.GetInt("pagesize", 20)
  753. ids := c.GetString("attrnames")
  754. if ids != "" {
  755. hisotryObject.MpAttrnames = strings.Split(ids, ",")
  756. }
  757. names := c.GetString("names")
  758. if names != "" {
  759. hisotryObject.Mpnames = strings.Split(names, ",")
  760. }
  761. list, cnt, err := hisotryObject.SearchHistory()
  762. if err != nil {
  763. c.Data["json"] = c.ApiError(err.Error())
  764. } else {
  765. c.Data["json"] = c.ApiPageOK(list, cnt)
  766. }
  767. c.ServeJSON()
  768. }
  769. //历史数据EchartsLine数据获取
  770. // @Summary 历史数据EchartsLine数据获取。按Echarts Line要求格式返回结果。
  771. // @Description 历史数据EchartsLine数据获取。按Echarts Line要求格式返回结果。
  772. // @Tags api
  773. // @Accept x-www-form-urlencoded
  774. // @Produce json
  775. // @Param deviceid query int true "查询条件:设备ID"
  776. // @Param attrnames query string false "查询条件:模型属性名,多个属性名使用逗号分隔,如Tem1,Tem2,...。attrnames与names不能同时为空"
  777. // @Param names query string false "查询条件:测点名称,多个测点名使用逗号分隔,如触头1,触头2,...。attrnames与names不能同时为空"
  778. // @Param starttime query int false "查询条件:开始日期"
  779. // @Param endtime query int false "查询条件:结束日期"
  780. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  781. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  782. // @Failure 500 status 服务器|后台发生错误
  783. // @router /echarts_history [get]
  784. func (c *DeviceController) GetEchartsLineHistoryData() {
  785. var hisotryObject = new(service.HistoryService)
  786. hisotryObject.StartDate = c.GetString("starttime")
  787. hisotryObject.EndDate = c.GetString("endtime")
  788. s1 := int64(0)
  789. s2 := int64(0)
  790. if hisotryObject.StartDate != "" {
  791. s1 = tools.DateString2Unix(hisotryObject.StartDate)
  792. }
  793. if hisotryObject.EndDate != "" {
  794. s2 = tools.DateString2Unix(hisotryObject.EndDate)
  795. } else {
  796. s2 = time.Now().Unix()
  797. }
  798. if s1 > 0 {
  799. if s2-s1 > (7 * 24 * 60 * 60) {
  800. c.Data["json"] = c.ApiError("查询日期周期不能大于7天")
  801. c.ServeJSON()
  802. return
  803. }
  804. }
  805. hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
  806. if hisotryObject.Model.Deviceid == 0 {
  807. c.Data["json"] = c.ApiError("设备编号不能为空")
  808. c.ServeJSON()
  809. return
  810. }
  811. hisotryObject.Model.Attrname = c.GetString("attrname")
  812. ids := c.GetString("attrnames")
  813. if ids != "" {
  814. hisotryObject.MpAttrnames = strings.Split(ids, ",")
  815. }
  816. names := c.GetString("names")
  817. if names != "" {
  818. hisotryObject.Mpnames = strings.Split(names, ",")
  819. }
  820. if ids == "" && names == "" {
  821. c.Data["json"] = c.ApiError("模型属性名和测点名不能同时为空")
  822. c.ServeJSON()
  823. return
  824. }
  825. list, err := hisotryObject.GetHistoryDataEchartsLine()
  826. if err != nil {
  827. c.Data["json"] = c.ApiError(err.Error())
  828. } else {
  829. c.Data["json"] = c.ApiOK(list)
  830. }
  831. c.ServeJSON()
  832. }
  833. // 获取设备最新采集数据
  834. // @Summary 获取设备最新采集数据
  835. // @Description 获取设备最新采集数据。以上数据后台会通过主题/rtelec/runtime/device/data进行发布。前端可订阅该主题。
  836. // @Tags api
  837. // @Accept x-www-form-urlencoded
  838. // @Produce json
  839. // @Param deviceid query int false "查询条件:设备ID。为空时查询所有设备的最新采集数据"
  840. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  841. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  842. // @Failure 500 status 服务器|后台发生错误
  843. // @router /lastdata [get]
  844. func (c *DeviceController) GetLastData() {
  845. var hisotryObject = new(service.HistoryService)
  846. hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
  847. list, err := hisotryObject.GetLastData()
  848. if err != nil {
  849. c.Data["json"] = c.ApiError(err.Error())
  850. } else {
  851. c.Data["json"] = c.ApiOK(list)
  852. }
  853. c.ServeJSON()
  854. }
  855. //获取设备实时(最新)数据
  856. // @Summary 获取设备最新采集数据,以Table展示(分为表头和表体数据)格式返回。以同一模型设备为一组。
  857. // @Description 获取设备最新采集数据,以Table展示(分为表头和表体数据)格式返回。以同一模型设备为一组。
  858. // @Tags api
  859. // @Accept x-www-form-urlencoded
  860. // @Produce json
  861. // @Param deviceid query int false "查询条件:设备ID。为空时查询所有设备的最新采集数据"
  862. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  863. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  864. // @Failure 500 status 服务器|后台发生错误
  865. // @router /lastdata/table [get]
  866. func (c *DeviceController) GetDeviceRuntimeData() {
  867. deviceid, _ := c.GetInt32("deviceid")
  868. var hisotryObject = new(service.HistoryService)
  869. hisotryObject.Model.Deviceid = deviceid
  870. list, err := hisotryObject.GetRuntimeData()
  871. if err != nil {
  872. c.Data["json"] = c.ApiError(err.Error())
  873. } else {
  874. c.Data["json"] = c.ApiOK(list)
  875. }
  876. c.ServeJSON()
  877. }
  878. //获取设备测点分组列表
  879. // @Summary 获取设备分组列表
  880. // @Description 获取设备分组列表
  881. // @Tags api
  882. // @Accept x-www-form-urlencoded
  883. // @Produce json
  884. // @Param deviceid query int true "查询条件:设备ID"
  885. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  886. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  887. // @Failure 500 status 服务器|后台发生错误
  888. // @router /mpgroup/list [get]
  889. func (c *DeviceController) GetMpGroupList() {
  890. var mgrObject = new(service.MpinfoService)
  891. deviceid, _ := c.GetInt32("deviceid")
  892. list, err := mgrObject.GroupList(deviceid)
  893. if err != nil {
  894. c.Data["json"] = c.ApiError(err.Error())
  895. } else {
  896. c.Data["json"] = c.ApiOK(list)
  897. }
  898. c.ServeJSON()
  899. }
  900. //获取设备测点分组的测点列表
  901. // @Summary 获取设备测点分组的测点列表
  902. // @Description 获取设备测点分组的测点列表
  903. // @Tags api
  904. // @Accept x-www-form-urlencoded
  905. // @Produce json
  906. // @Param deviceid query int true "查询条件:设备ID"
  907. // @Param groupid query int true "查询条件:分组ID"
  908. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  909. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  910. // @Failure 500 status 服务器|后台发生错误
  911. // @router /mpgroup/mplist [get]
  912. func (c *DeviceController) GetMpGroupMpList() {
  913. var mgrObject = new(service.MpinfoService)
  914. deviceid, _ := c.GetInt32("deviceid")
  915. groupid, _ := c.GetInt64("groupid")
  916. list, err := mgrObject.GroupMpList(deviceid, groupid)
  917. if err != nil {
  918. c.Data["json"] = c.ApiError(err.Error())
  919. } else {
  920. c.Data["json"] = c.ApiOK(list)
  921. }
  922. c.ServeJSON()
  923. }
  924. //保存设备测点分组信息
  925. // @Summary 保存设备测点分组信息
  926. // @Description 保存设备测点分组信息
  927. // @Tags api
  928. // @Accept x-www-form-urlencoded
  929. // @Produce json
  930. // @Param deviceid query int true "设备ID"
  931. // @Param groupid query int false "分组ID。为空或未传时表示新建分组;否则为编辑分组"
  932. // @Param groupname query int true "分组名称。"
  933. // @Param mpids query string true "测点ID列表。多个测点ID使用逗号分隔,测点数量不能超过3个。"
  934. // @Success 200 {object} ApiOK|ApiError 服务访问成功
  935. // @Failure 401 status 认证未通过,一般是未指定token或token已失效
  936. // @Failure 500 status 服务器|后台发生错误
  937. // @router /mpgroup/save [post]
  938. func (c *DeviceController) SaveMpGroup() {
  939. var mgrObject = new(service.MpinfoService)
  940. mgrObject.UserInfo = c.GetCurrentUserInfo_rt()
  941. deviceid, _ := c.GetInt32("deviceid")
  942. groupid, _ := c.GetInt64("groupid")
  943. groupname := c.GetString("groupname")
  944. mps := c.GetString("mpids")
  945. logger.Logger.Debug(fmt.Sprintf("保存设备测点分组信息:deviceid:%d groupid:%d groupname:%s mpids:%+v", deviceid, groupid, groupname, mps))
  946. err := mgrObject.SaveGroup(deviceid, groupid, groupname, strings.Split(mps, ","))
  947. if err != nil {
  948. c.Data["json"] = c.ApiError(err.Error())
  949. } else {
  950. c.Data["json"] = c.ApiOK("")
  951. }
  952. c.ServeJSON()
  953. }