123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "rtzh_elec_temperature/enum"
- "rtzh_elec_temperature/logger"
- "rtzh_elec_temperature/models/bo"
- "rtzh_elec_temperature/rtelec_app_public_lib/service"
- "rtzh_elec_temperature/rtelec_app_public_lib/utils"
- "rtzh_elec_temperature/tools"
- "strconv"
- "strings"
- "time"
- "git.rtzhtech.cn/iss/public-lib/model"
- "github.com/spf13/cast"
- )
- //设备管理相关服务
- type DeviceController struct {
- BaseController
- }
- // 查询设备类型定义
- // @Summary 查询设备类型数据列表
- // @Description 查询设备类型数据列表。仅返回类型ID(id)和名称(type_name)
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /type/list [get]
- func (c *DeviceController) GetDeviceTypeList() {
- var DeviceInfo = new(service.DeviceService)
- DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
- list, err := DeviceInfo.SearchDeviceTypeList()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- // 查询设备模型信息
- // @Summary 查询设备模型信息列表
- // @Description 查询设备模型信息列表。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query string false "查询条件:设备ID。可以为空,不指定设备ID时将获取所有的模型列表信息"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /model/list [get]
- func (c *DeviceController) GetDeviceModelList() {
- deviceid, _ := c.GetInt32("deviceid")
- if deviceid == 0 {
- //返回全部模型信息
- modelobj := new(service.ModelService)
- modelobj.UserInfo = c.GetCurrentUserInfo_rt()
- list := modelobj.GetModelListObj()
- c.Data["json"] = c.ApiOK(list)
- c.ServeJSON()
- return
- }
- var DeviceInfo = new(service.DeviceService)
- DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
- list := DeviceInfo.GetComboxListById(deviceid)
- if list == nil {
- c.Data["json"] = c.ApiError("未查询到设备的模型信息")
- } else {
- modSvr := new(service.ModelService)
- obj := modSvr.GetModelInfo(tools.IsEmpty(list.Modelid))
- c.Data["json"] = c.ApiOK([]interface{}{obj})
- }
- c.ServeJSON()
- }
- // 查询设备数据记录
- // @Summary 查询设备化记录列表
- // @Description 查询设备记录列表
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param devicename query string false "查询条件:设备名称"
- // @Param regionid query string false "查询条件:区域ID"
- // @Param typeid query string false "查询条件:设备类型ID"
- // @Param modelid query string false "查询条件:物模型ID"
- // @Param pageindex query int false "当前页码"
- // @Param pagesize query int false "每页记录数"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /list [get]
- func (c *DeviceController) GetDeviceList() {
- var DeviceInfo = new(service.DeviceService)
- DeviceInfo.UserInfo = c.GetCurrentUserInfo_rt()
- param := map[string]interface{}{}
- param["DeviceName"] = c.GetString("devicename")
- param["Regionid"] = c.GetString("regionid")
- param["Typeid"] = c.GetString("typeid")
- param["Modelid"] = c.GetString("modelid")
- param["PageIndex"], _ = c.GetInt("pageindex", 1)
- param["PageSize"], _ = c.GetInt("pagesize", 20)
- list, num, err := DeviceInfo.SearchDeviceList(param)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(map[string]interface{}{"total": num, "list": list})
- }
- c.ServeJSON()
- }
- //设备详细记录信息
- // @Summary 获取指定设备的详情
- // @Description 获取指定设备的详情,包含设备信息、测点信息、控制点信息
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query string true "设备ID"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /detail [get]
- func (c *DeviceController) GetDeviceDetail() {
- var DeviceInfo = new(service.DeviceService)
- DeviceInfo.DeviceId, _ = c.GetInt("deviceid")
- if DeviceInfo.DeviceId == 0 {
- c.Data["json"] = c.ApiError("设备Id参数(deviceid)不允许为空或0!")
- } else {
- list, err := DeviceInfo.DeviceDetail()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- }
- c.ServeJSON()
- }
- //创建设备
- // @Summary 创建新设备
- // @Description 创建新设备。需要提交设备基本信息、测点信息以及控制点信息,其中测点信息的控制点信息均为序列化成字符串的JSON对象
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int false "设备ID。编辑设备时必传,否则认为是新建设备"
- // @Param stationid query int true "变电站ID。"
- // @Param modelid query int true "设备采用的数据模型ID。"
- // @Param typeid query int true "设备类型ID。"
- // @Param regionid query int false "设备所在区域ID。"
- // @Param devicename query string true "设备名称。"
- // @Param serial query string false "设备Modbus协议串口号。设备采用串口连接时,必须指定,长度不超过30个字符。"
- // @Param slaveid query int false "设备Modbus协议从设备号。设备采用串口连接时,必须指定,仅支持0-255之间的整数值"
- // @Param baudrate query int false "设备Modbus协议的波特率。设备采用串口连接时,必须指定,支持4800,9600,14400,19200,32000,38400,56000,57600,152000,194000其中之一,默认为9600。"
- // @Param stopbit query int false "设备Modbus协议的停止位。设备采用串口连接时,必须指定,仅支持1或2,默认为1。"
- // @Param check_bit query int false "设备Modbus协议的校验位。设备采用串口连接时,必须指定,仅支持N、O、E其中之一,默认为N。"
- // @Param data_bit query string false "设备Modbus协议的数据位。设备采用串口连接时,必须指定,仅支持5、6、7、8其中之一,默认为8。"
- // @Param ip query string false "设备IP协议地址。设备采用网口连接时,必须指定,长度不超过17个字符,格式为xxx.xxx.xxx.xxx。"
- // @Param port query int false "设备IP协议端口。设备采用网口连接时,必须指定,仅支持80-65535之间的整数值。"
- // @Param mpinfo_para query JSONString false "测点信息,采用序列化成字符串的JSON对象"
- // @Param control_para query JSONString false "测点信息,采用序列化成字符串的JSON对象"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /edit [post]
- func (c *DeviceController) EditDeviceInfo() {
- logObj := new(bo.SystemLog)
- user := c.GetCurrentUserInfo_rt()
- logObj.UserInfo = map[string]interface{}{"name": user.Usrname, "ip": user.Ip}
- logObj.Audittype = enum.AuditType_device
- logObj.Eventtype = enum.OptEventType_Bus
- logObj.Eventlevel = enum.OptEventLevel_Hight
- var deviceField model.DevDevinfo
- deviceField.Deviceid, _ = c.GetInt32("deviceid")
- if deviceField.Deviceid == 0 {
- logObj.Logtype = enum.LogType_Insert
- } else {
- devObj := new(service.DeviceService)
- devObj.DeviceId = int(deviceField.Deviceid)
- logObj.Logtype = enum.LogType_Update
- deviceOldInfo, err := devObj.DeviceDetail()
- if err != nil {
- logger.Logger.Error(err)
- c.Data["json"] = c.ApiError(err.Error())
- c.ServeJSON()
- return
- }
- devoldinfo := deviceOldInfo["device"]
- if devoldinfo == nil {
- c.Data["json"] = c.ApiError("未找到该设备信息")
- c.ServeJSON()
- return
- }
- deviceField = devoldinfo.(model.DevDevinfo)
- }
- pv, _ := c.GetInt32("stationid")
- if deviceField.Deviceid == 0 || pv != 0 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Stationid = pv
- }
- ModelId, _ := c.GetInt32("modelid")
- if deviceField.Deviceid == 0 || ModelId != 0 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Modelid = ModelId
- }
- checkResult := true //校验结果
- logdesc := ""
- pv, _ = c.GetInt32("regionid")
- if deviceField.Deviceid == 0 || pv != 0 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.RegionId = pv
- }
- pv, _ = c.GetInt32("typeid")
- if deviceField.Deviceid == 0 || pv != 0 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.DevTypeId = pv //设备类型ID
- if deviceField.DevTypeId == 0 {
- checkResult = false
- logdesc = "设备类型不能为空"
- }
- }
- stringpv := c.GetString("devicename")
- if deviceField.Deviceid == 0 || stringpv != "" {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Devicename = stringpv
- if deviceField.Devicename == "" {
- checkResult = false
- logdesc = "设备名称不能为空"
- }
- }
- stringpv = c.GetString("serial")
- if deviceField.Deviceid == 0 || stringpv != "" {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Serial = stringpv
- }
- pv, _ = c.GetInt32("slaveid", -1)
- if deviceField.Deviceid == 0 || pv != -1 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Slaveid = pv
- }
- pv, _ = c.GetInt32("baudrate", -1)
- if deviceField.Deviceid == 0 || pv != -1 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Baudrate = pv
- }
- pv, _ = c.GetInt32("stopbit", -1)
- if deviceField.Deviceid == 0 || pv != -1 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Stopbit = pv
- }
- stringpv = c.GetString("check_bit", "")
- if deviceField.Deviceid == 0 || stringpv != "" {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Checkbit = tools.IsEmpty(stringpv, "N")
- }
- pv, _ = c.GetInt32("data_bit", -1)
- if deviceField.Deviceid == 0 || pv != -1 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Databit = pv
- }
- if deviceField.Serial != "" && deviceField.Slaveid == -1 {
- logdesc = "从设备号不能为空"
- checkResult = false
- }
- stringpv = c.GetString("ip", "")
- if deviceField.Deviceid == 0 || stringpv != "" {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.IP = stringpv
- if deviceField.IP == "" && deviceField.Serial == "" {
- logdesc = "设备串口号或IP地址不能同时为空"
- checkResult = false
- }
- }
- pv, _ = c.GetInt32("port", -1)
- if deviceField.Deviceid == 0 || pv != -1 {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Port = pv
- if deviceField.IP != "" && deviceField.Port == -1 {
- logdesc = "网口协议端口不能为空!"
- checkResult = false
- }
- }
- if deviceField.Deviceid == 0 {
- deviceField.Eid = c.GetString("eid")
- deviceField.Disable, _ = c.GetInt32("disable")
- }
- stringpv = c.GetString("topic", "")
- if deviceField.Deviceid == 0 || stringpv != "" {
- //新增或者前端传了该参数值时,采用该参数值
- deviceField.Mqtttopic = stringpv
- }
- if !checkResult {
- logObj.Description = logdesc
- logObj.Fail2()
- c.Data["json"] = c.ApiError(logdesc)
- c.ServeJSON()
- return
- }
- var AppId = cast.ToInt32(service.RtelecManageApp().RegAppID)
- deviceField.Appid = AppId
- var err error
- //测点信息
- mpMgr := new(service.MpinfoService)
- MpField := []model.DevMpinfo{}
- var MpParameter = c.GetString("mpinfo_para")
- if MpParameter != "" {
- //设备数据中包含测点数据时
- MpField, err = mpMgr.GetMpField(MpParameter, AppId, ModelId)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- c.ServeJSON()
- return
- }
- }
- //控制点信息
- controlData := []map[string]interface{}{}
- var control_parameter = c.GetString("control_para")
- if control_parameter != "" {
- //设备数据中包含控制点数据时
- controlData, err = mpMgr.GetControlField(control_parameter, AppId, ModelId, cast.ToInt64(deviceField.Deviceid))
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- c.ServeJSON()
- return
- }
- }
- var messageId = tools.GetUid()
- msgobj := new(utils.MsgStateManage)
- msgobj.SetMessageStateValue(messageId, utils.MsgState{Success: false, State: false, Message: ""})
- deviceObj := new(service.DeviceService)
- deviceObj.UserInfo = c.GetCurrentUserInfo_rt()
- editError := deviceObj.CreateDevice(messageId, deviceField, MpField, controlData)
- if editError != nil {
- c.Data["json"] = c.ApiError(editError.Error())
- } else {
- Result := msgobj.HanderMesage(messageId, 5)
- if Result.Success {
- logdesc = fmt.Sprintf("创建设备[%s]成功!", deviceField.Devicename)
- logObj.Description = logdesc
- logObj.Success2()
- c.Data["json"] = c.ApiOK(logdesc)
- new(service.LogService).SaveLog(logdesc)
- } else {
- logdesc = fmt.Sprintf("创建设备[%s]失败,错误:%s,操作数据:%+v", deviceField.Devicename, Result.Message, deviceField)
- logObj.Description = logdesc
- logObj.Fail2()
- c.Data["json"] = c.ApiError(Result.Message)
- new(service.LogService).SaveLog(logdesc)
- }
- }
- c.ServeJSON()
- }
- // 保存设备测点信息
- // @Summary 保存设备测点信息
- // @Description 保存设备测点信息。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "测点所属的设备ID。"
- // @Param modelid query int true "测点关联的模型ID"
- // @Param mpid query int false "测点ID。未传值时表示新增操作"
- // @Param mpname query string true "测点名称。建议20个字符以内"
- // @Param zonename query string false "测点所在区域名称"
- // @Param positionname query string false "测点所在位置名称"
- // @Param unit query string false "测点单位。指该测点采集数据的计量单位。"
- // @Param attrname query string true "测点关联的模型属性名称"
- // @Param phase query string false "测点监测对象的相位。仅为空值、A、B、C、N其中之一。"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mp/save [post]
- func (c *DeviceController) SaveMpInfo() {
- var appId = cast.ToInt32(service.RtelecManageApp().RegAppID)
- logObj := new(bo.SystemLog)
- user := c.GetCurrentUserInfo_rt()
- logObj.UserInfo = map[string]interface{}{"name": user.Usrname, "ip": user.Ip}
- logObj.Audittype = enum.AuditType_device
- logObj.Eventtype = enum.OptEventType_Bus
- logObj.Eventlevel = enum.OptEventLevel_Hight
- deviceid, _ := c.GetInt32("deviceid")
- if deviceid == 0 {
- c.Data["json"] = c.ApiError("设备编号不能为空!")
- c.ServeJSON()
- return
- }
- mpId, _ := c.GetInt64("mpid") //测点id。未提交时为新增操作
- deviceObj := new(service.DeviceService)
- deviceObj.UserInfo = user
- mp_Field := []model.DevMpinfo{}
- mp_Field = append(mp_Field, model.DevMpinfo{})
- isAdd := true
- if mpId > 0 {
- isAdd = false
- } else {
- mp_Field[0].Online = 1 //新增测点时,默认为离线
- mp_Field[0].Disable = 1 //新增时默认为启用
- }
- mp_Field[0].Mpid = mpId
- mp_Field[0].Appid = appId
- mp_Field[0].Attrname = strings.Trim(c.GetString("attrname"), " ")
- if mp_Field[0].Attrname == "" {
- c.Data["json"] = c.ApiError("关联的模型属性名称不能为空!")
- c.ServeJSON()
- return
- }
- mp_Field[0].Modelid, _ = c.GetInt32("modelid")
- mp_Field[0].Deviceid = deviceid
- mp_Field[0].Mpname = c.GetString("mpname")
- if mp_Field[0].Mpname == "" {
- c.Data["json"] = c.ApiError("测点名称不能为空!")
- c.ServeJSON()
- return
- }
- mp_Field[0].Positionname = c.GetString("positionname")
- mp_Field[0].Zonename = c.GetString("zonename")
- mp_Field[0].Unit = c.GetString("unit")
- mp_Field[0].Phase = c.GetString("phase")
- //logger.Logger.Debug(fmt.Sprintf("================编辑测点:%+v", mp_Field[0]))
- err := deviceObj.EditMpInfo(mp_Field, deviceid, isAdd)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK("")
- }
- c.ServeJSON()
- }
- //获取区域名称接口。[该接口已废弃,建议使用/api/region/list]
- // @router /area [get]
- func (c *DeviceController) GetAreaName() {
- deviceObject := new(service.DeviceService)
- deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
- list, err := deviceObject.GetAreaName()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //获取设备测点数据
- // @Summary 获取设备测点信息列表
- // @Description 获取设备测点信息列表。仅返回设备ID、测点ID以及测点名称。
- // @Param deviceid query int true "测点所属的设备ID。"
- // @Param pageindex query int false "当前页码"
- // @Param pagesize query int false "每页记录数"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mp/list [get]
- func (c *DeviceController) GetDeviceMpid() {
- deviceObject := new(service.DeviceService)
- deviceid, _ := c.GetInt32("deviceid")
- if deviceid == 0 {
- c.Data["json"] = c.ApiError("设备编号不能为空")
- } else {
- pageindex, _ := c.GetInt("pageindex")
- pagesize, _ := c.GetInt("pagesize")
- list, num, err := deviceObject.DeviceMpList(deviceid, pageindex, pagesize)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(map[string]interface{}{"list": list, "total": num})
- }
- }
- c.ServeJSON()
- }
- // 获取设备测点的某一个测点信息
- // @Summary 获取设备一个测点的详细信息
- // @Description 获取设备一个测点的详细信息
- // @Param mpid query int true "设备测点的id"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mp/mpInfoDetail [get]
- func (c *DeviceController) GetMpInfoDetail() {
- mpid, err := c.GetInt64("mpid")
- if err != nil {
- logger.Logger.Error(err)
- c.Data["json"] = c.ApiError("测点id获取出现错误!类型转化错误!")
- c.ServeJSON()
- return
- } else if mpid == 0 {
- logger.Logger.Error(mpid, "获取id出现错误!数值为0")
- c.Data["json"] = c.ApiError("测点id获取出现错误!类型转化错误!")
- c.ServeJSON()
- return
- }
- deviceObject := new(service.MpinfoService)
- mpinfo, err := deviceObject.GetMpinfo(mpid)
- if err == nil {
- c.Data["json"] = c.ApiOK(mpinfo)
- } else {
- logger.Logger.Error(err)
- c.Data["json"] = c.ApiError("测点id获取详情出现错误!")
- }
- c.ServeJSON()
- }
- //获取设备动作信息
- // @router /controlid [post,get]
- func (c *DeviceController) GetDeviceControlID() {
- deviceObject := new(service.DeviceService)
- list, err := deviceObject.GetDeviceControlId()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- // 获取设备树
- // @Summary 获取设备列表并以树形数据结构返回
- // @Description 获取设备列表并以树形数据结构返回。数据格式为: [{id:[节点ID],label:[节点名称],type:[节点类型(root|region|device)],devcount:[挂载设备数量],children:[嵌套子节点]}]。
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /tree [get]
- func (c *DeviceController) GetDeviceTree() {
- usr := c.GetCurrentUserInfo_rt()
- deviceObject := new(service.DeviceService)
- regService := new(service.RegionService)
- deviceObject.UserInfo = usr
- regService.UserInfo = usr
- var err error
- //获取变电站名称
- stationinfo, err := regService.GetStationInfo()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- c.ServeJSON()
- return
- }
- stationname := tools.IsEmpty(stationinfo["name"])
- //获取区域列表
- regionlist, err := regService.List(nil)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- c.ServeJSON()
- return
- }
- tree := []interface{}{}
- //生成根节点
- node := map[string]interface{}{"id": tools.IsEmpty(stationinfo["stationid"]),
- "label": stationname,
- "type": "root",
- "children": []interface{}{},
- }
- regionNodes := []interface{}{}
- for _, row := range regionlist {
- regnode := map[string]interface{}{
- "id": tools.IsEmpty(row["id"]),
- "label": tools.IsEmpty(row["region_name"]),
- "type": "region",
- "devcount": 0,
- "children": []interface{}{},
- }
- regionNodes = append(regionNodes, regnode)
- }
- devlist, err := deviceObject.GetAllDevice()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- for _, regitem := range regionNodes {
- regitem2 := regitem.(map[string]interface{})
- regid := tools.IsEmpty(regitem2["id"])
- for _, devitem := range devlist {
- devregid := tools.IsEmpty(devitem["region_id"])
- if regid == devregid {
- devnode := map[string]interface{}{
- "id": tools.IsEmpty(devitem["deviceid"]),
- "label": tools.IsEmpty(devitem["devicename"]),
- "type": "device",
- //"children": []interface{}{},
- }
- regitem2["devcount"] = cast.ToInt(regitem2["devcount"]) + 1
- regitem2["children"] = append(regitem2["children"].([]interface{}), devnode)
- }
- }
- }
- node["children"] = append(node["children"].([]interface{}), regionNodes)
- tree = append(tree, node)
- c.Data["json"] = c.ApiOK(tree)
- }
- c.ServeJSON()
- }
- // 设备列表
- // @Summary 获取所有设备详情信息列表
- // @Description 获取所有设备详情信息列表。
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /all [get]
- func (c *DeviceController) GetAllList() {
- usr := c.GetCurrentUserInfo_rt()
- deviceObject := new(service.DeviceService)
- deviceObject.UserInfo = usr
- list, err := deviceObject.GetAllDevice()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //设备列表,包含模型
- // @Summary 获取设备简单信息列表
- // @Description 获取设备简单信息列表。仅返回设备ID和设备名称。
- // @Param deviceid query int true "测点所属的设备ID。"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /comboxlist [get]
- func (c *DeviceController) GetComboxList() {
- deviceObject := new(service.DeviceService)
- list, err := deviceObject.GetComboxList()
- var result []map[string]interface{}
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- if len(list) > 0 {
- modelInfo := new(service.ModelService).GetModelListObj()
- for _, row := range list {
- var devicename = row.Name
- for _, row2 := range modelInfo {
- record := row2.(map[string]interface{})
- recordid := tools.IsEmpty(string(record["id"].(json.Number)))
- if recordid != "" {
- modelid, _ := strconv.Atoi(recordid)
- if row.Modelid == modelid {
- devicename += "(" + record["model_name"].(string) + ")"
- break
- }
- }
- }
- result = append(result, map[string]interface{}{"deviceid": row.Deviceid, "devicename": devicename})
- }
- }
- c.Data["json"] = c.ApiOK(result)
- }
- c.ServeJSON()
- }
- // 删除设备
- // @Summary 删除设备
- // @Description 删除设备。同时删除设备关联数据,包括关联的测点、控制点以及历史数据等
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "测点所属的设备ID。"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /delete [post]
- func (c *DeviceController) DeleteDeviceInfo() {
- Id, _ := c.GetInt("deviceid", 0)
- if Id == 0 {
- c.Data["json"] = c.ApiError("设备ID参数(deviceid)不允许为空!")
- } else {
- deviceObject := new(service.DeviceService)
- deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
- deviceObject.DeviceId = Id
- list, _ := deviceObject.DeviceDetail()
- if list == nil {
- c.Data["json"] = c.ApiError(fmt.Sprintf("设备(device_id:%d)不存在!", Id))
- c.ServeJSON()
- return
- }
- if list == nil || list["device"] == nil {
- c.Data["json"] = c.ApiError(fmt.Sprintf("设备(device_id:%d)不存在!", Id))
- c.ServeJSON()
- return
- }
- deviceInfo := list["device"].(model.DevDevinfo)
- err := deviceObject.DeleteDeviceByMqtt(Id)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK("")
- new(service.LogService).SaveLog(fmt.Sprintf("删除设备成功,设备名称:%s", deviceInfo.Devicename))
- }
- }
- c.ServeJSON()
- }
- // 删除设备的指定测点
- // @Summary 删除设备的指定测点
- // @Description 删除设备的指定测点。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "测点所属的设备ID。"
- // @Param mpid query int false "测点ID。未传值时表示删除该设备所有测点"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mp/delete [post]
- func (c *DeviceController) DeleteDeviceMpInfo() {
- Id, _ := c.GetInt32("deviceid", 0)
- mpId, _ := c.GetInt64("mpid", 0)
- if Id == 0 || mpId == 0 {
- c.Data["json"] = c.ApiError("设备ID参数(deviceid)或者测点ID(mpid)不允许为空!")
- } else {
- deviceObject := new(service.DeviceService)
- deviceObject.UserInfo = c.GetCurrentUserInfo_rt()
- err := deviceObject.DeleteMapByDeviceId(Id, mpId)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK("")
- }
- }
- c.ServeJSON()
- }
- //历史数据查询
- // @Summary 查询设备历史数据列表
- // @Description 查询设备历史数据列表。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "查询条件:设备ID"
- // @Param attrname query int false "查询条件:模型属性名称"
- // @Param starttime query int false "查询条件:开始日期"
- // @Param endtime query int false "查询条件:结束日期"
- // @Param mpid query int false "查询条件:测点ID"
- // @Param pageindex query int false "当前页码"
- // @Param pagesize query int false "每页记录数"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /history [get]
- func (c *DeviceController) SearchHistory() {
- var hisotryObject = new(service.HistoryService)
- hisotryObject.StartDate = c.GetString("starttime")
- hisotryObject.EndDate = c.GetString("endtime")
- s1 := int64(0)
- s2 := int64(0)
- if hisotryObject.StartDate != "" {
- s1 = tools.DateString2Unix(hisotryObject.StartDate)
- }
- if hisotryObject.EndDate != "" {
- s2 = tools.DateString2Unix(hisotryObject.EndDate)
- } else {
- s2 = time.Now().Unix()
- }
- if s1 > 0 {
- if s2-s1 > (7 * 24 * 60 * 60) {
- c.Data["json"] = c.ApiError("查询日期周期不能大于7天")
- c.ServeJSON()
- return
- }
- }
- hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
- if hisotryObject.Model.Deviceid == 0 {
- c.Data["json"] = c.ApiError("设备编号不能为空")
- c.ServeJSON()
- return
- }
- hisotryObject.Model.Attrname = c.GetString("attrname")
- hisotryObject.PageIndex, _ = c.GetInt("pageindex", 1)
- hisotryObject.PageSize, _ = c.GetInt("pagesize", 20)
- ids := c.GetString("attrnames")
- if ids != "" {
- hisotryObject.MpAttrnames = strings.Split(ids, ",")
- }
- names := c.GetString("names")
- if names != "" {
- hisotryObject.Mpnames = strings.Split(names, ",")
- }
- list, cnt, err := hisotryObject.SearchHistory()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiPageOK(list, cnt)
- }
- c.ServeJSON()
- }
- //历史数据EchartsLine数据获取
- // @Summary 历史数据EchartsLine数据获取。按Echarts Line要求格式返回结果。
- // @Description 历史数据EchartsLine数据获取。按Echarts Line要求格式返回结果。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "查询条件:设备ID"
- // @Param attrnames query string false "查询条件:模型属性名,多个属性名使用逗号分隔,如Tem1,Tem2,...。attrnames与names不能同时为空"
- // @Param names query string false "查询条件:测点名称,多个测点名使用逗号分隔,如触头1,触头2,...。attrnames与names不能同时为空"
- // @Param starttime query int false "查询条件:开始日期"
- // @Param endtime query int false "查询条件:结束日期"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /echarts_history [get]
- func (c *DeviceController) GetEchartsLineHistoryData() {
- var hisotryObject = new(service.HistoryService)
- hisotryObject.StartDate = c.GetString("starttime")
- hisotryObject.EndDate = c.GetString("endtime")
- s1 := int64(0)
- s2 := int64(0)
- if hisotryObject.StartDate != "" {
- s1 = tools.DateString2Unix(hisotryObject.StartDate)
- }
- if hisotryObject.EndDate != "" {
- s2 = tools.DateString2Unix(hisotryObject.EndDate)
- } else {
- s2 = time.Now().Unix()
- }
- if s1 > 0 {
- if s2-s1 > (7 * 24 * 60 * 60) {
- c.Data["json"] = c.ApiError("查询日期周期不能大于7天")
- c.ServeJSON()
- return
- }
- }
- hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
- if hisotryObject.Model.Deviceid == 0 {
- c.Data["json"] = c.ApiError("设备编号不能为空")
- c.ServeJSON()
- return
- }
- hisotryObject.Model.Attrname = c.GetString("attrname")
- ids := c.GetString("attrnames")
- if ids != "" {
- hisotryObject.MpAttrnames = strings.Split(ids, ",")
- }
- names := c.GetString("names")
- if names != "" {
- hisotryObject.Mpnames = strings.Split(names, ",")
- }
- if ids == "" && names == "" {
- c.Data["json"] = c.ApiError("模型属性名和测点名不能同时为空")
- c.ServeJSON()
- return
- }
- list, err := hisotryObject.GetHistoryDataEchartsLine()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- // 获取设备最新采集数据
- // @Summary 获取设备最新采集数据
- // @Description 获取设备最新采集数据。以上数据后台会通过主题/rtelec/runtime/device/data进行发布。前端可订阅该主题。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int false "查询条件:设备ID。为空时查询所有设备的最新采集数据"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /lastdata [get]
- func (c *DeviceController) GetLastData() {
- var hisotryObject = new(service.HistoryService)
- hisotryObject.Model.Deviceid, _ = c.GetInt32("deviceid")
- list, err := hisotryObject.GetLastData()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //获取设备实时(最新)数据
- // @Summary 获取设备最新采集数据,以Table展示(分为表头和表体数据)格式返回。以同一模型设备为一组。
- // @Description 获取设备最新采集数据,以Table展示(分为表头和表体数据)格式返回。以同一模型设备为一组。
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int false "查询条件:设备ID。为空时查询所有设备的最新采集数据"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /lastdata/table [get]
- func (c *DeviceController) GetDeviceRuntimeData() {
- deviceid, _ := c.GetInt32("deviceid")
- var hisotryObject = new(service.HistoryService)
- hisotryObject.Model.Deviceid = deviceid
- list, err := hisotryObject.GetRuntimeData()
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //获取设备测点分组列表
- // @Summary 获取设备分组列表
- // @Description 获取设备分组列表
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "查询条件:设备ID"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mpgroup/list [get]
- func (c *DeviceController) GetMpGroupList() {
- var mgrObject = new(service.MpinfoService)
- deviceid, _ := c.GetInt32("deviceid")
- list, err := mgrObject.GroupList(deviceid)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //获取设备测点分组的测点列表
- // @Summary 获取设备测点分组的测点列表
- // @Description 获取设备测点分组的测点列表
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "查询条件:设备ID"
- // @Param groupid query int true "查询条件:分组ID"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mpgroup/mplist [get]
- func (c *DeviceController) GetMpGroupMpList() {
- var mgrObject = new(service.MpinfoService)
- deviceid, _ := c.GetInt32("deviceid")
- groupid, _ := c.GetInt64("groupid")
- list, err := mgrObject.GroupMpList(deviceid, groupid)
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK(list)
- }
- c.ServeJSON()
- }
- //保存设备测点分组信息
- // @Summary 保存设备测点分组信息
- // @Description 保存设备测点分组信息
- // @Tags api
- // @Accept x-www-form-urlencoded
- // @Produce json
- // @Param deviceid query int true "设备ID"
- // @Param groupid query int false "分组ID。为空或未传时表示新建分组;否则为编辑分组"
- // @Param groupname query int true "分组名称。"
- // @Param mpids query string true "测点ID列表。多个测点ID使用逗号分隔,测点数量不能超过3个。"
- // @Success 200 {object} ApiOK|ApiError 服务访问成功
- // @Failure 401 status 认证未通过,一般是未指定token或token已失效
- // @Failure 500 status 服务器|后台发生错误
- // @router /mpgroup/save [post]
- func (c *DeviceController) SaveMpGroup() {
- var mgrObject = new(service.MpinfoService)
- mgrObject.UserInfo = c.GetCurrentUserInfo_rt()
- deviceid, _ := c.GetInt32("deviceid")
- groupid, _ := c.GetInt64("groupid")
- groupname := c.GetString("groupname")
- mps := c.GetString("mpids")
- logger.Logger.Debug(fmt.Sprintf("保存设备测点分组信息:deviceid:%d groupid:%d groupname:%s mpids:%+v", deviceid, groupid, groupname, mps))
- err := mgrObject.SaveGroup(deviceid, groupid, groupname, strings.Split(mps, ","))
- if err != nil {
- c.Data["json"] = c.ApiError(err.Error())
- } else {
- c.Data["json"] = c.ApiOK("")
- }
- c.ServeJSON()
- }
|