| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714 |
- package system
- import (
- "encoding/json"
- "strings"
- "time"
- "dashoo.cn/backend/api/business/actions"
- "dashoo.cn/backend/api/business/authcode"
- "dashoo.cn/backend/api/business/device"
- "dashoo.cn/backend/api/business/trigger"
- "dashoo.cn/backend/api/business/triggerhistory"
- "dashoo.cn/backend/api/business/userchannels"
- . "dashoo.cn/backend/api/controllers"
- "dashoo.cn/labsop"
- "dashoo.cn/models"
- "dashoo.cn/utils"
- )
- // 动作接口说明
- type ChannelsController struct {
- BaseController
- }
- type MyResponse struct {
- Results []MyResult `json:"results,omitempty"`
- Err string `json:"err,omitempty"`
- }
- type MyResult struct {
- Series []models.Row `json:"series"`
- Err string `json:"err,omitempty"`
- }
- type ChannelLast struct {
- Temp string
- Hum string
- Vol string
- Rssi string
- Time string
- O2 string
- Co2 string
- Power string
- Supply string
- Signal string
- DState int //设备状态 1在线,2离线
- LiquidLevel string //液位
- }
- type ChannelModel struct {
- Title string `json:"title"`
- Serial string `json:"serial"`
- TagCode string `json:"tagCode"`
- DeviceState string `json:"devicestate"`
- About string `json:"about"`
- }
- type AdjuModel struct {
- CalibrationValue string `json:"temp"`
- CalibrationHumidity string `json:"hum"`
- CalibrationCO2 string `json:"co2"`
- CalibrationO2 string `json:"o2"`
- }
- type Triggerwaringhistory struct {
- Items []triggerhistory.Trigger_History `json:"items"` //数据列表
- }
- type ChannelDataModel struct {
- Value [][]float64
- GroupValue []float64
- }
- type SortDataModel struct {
- ChannelIds string
- }
- // @Title 设备列表
- // @Description 设备列表
- // @Success 200 {object} business.device.DeviceChannels
- // @router /list [get]
- func (this *ChannelsController) List() {
- page := this.GetPageInfoForm()
- svc := device.GetDeviceService(utils.DBE)
- svcuc := userchannels.GetUserChannelService(utils.DBE)
- channelid := svcuc.GetChannelids(utils.ToStr(this.User.Id))
- Uid := utils.ToStr(this.User.Id)
- where := " (a.CreateUserId=" + utils.ToStr(this.User.Id) + " or a.Id in (" + strings.Join(channelid, ",") + ")) and a.DataItem in (" + ChannelItem_Sensor + ") "
- keyword := this.GetString("keyword")
- if keyword != "" {
- where = where + " and (a.Title like '%" + keyword + "%' or a.TagCode like '%" + keyword + "%' or a.Serial like '%" + keyword + "%')"
- }
- cstate := this.GetString("cstate")
- if cstate != "" {
- if cstate == "1" {
- where = where + " and a.ChannelState in (1,0)"
- } else {
- where = where + " and a.ChannelState=" + cstate
- }
- }
- total, devices := svc.GetPagingEntitiesWithOrderSearch(page.CurrentPage, page.Size, "c.sortcode, a.CreateOn desc", where, Uid)
- var datainfo DataInfo
- datainfo.Items = devices
- datainfo.CurrentItemCount = total
- this.Data["json"] = &datainfo
- this.ServeJSON()
- }
- // @Title 设备列表
- // @Description 设备列表
- // @Success 200 {object} business.device.DeviceChannels
- // @router /bblist [get]
- func (this *ChannelsController) BBList() {
- var list []device.Device
- svc := device.GetDeviceService(utils.DBE)
- svc.GetEntities(&list, " CreateUserId="+utils.ToStr(this.User.Id)+" and DataItem="+Device_Box+" order by CreateOn desc")
- this.Data["json"] = &list
- this.ServeJSON()
- }
- // @Title 采集数据
- // @Description 采集数据,修改为由接口取数
- // @Param serial path string true "设备SN"
- // @Success 200 {object} coldcloud.DatapointColdCloud
- // @Failure 403 :serial 为空
- // @router /datavalue/:serial [get]
- func (this *ChannelsController) DataValue() {
- serial := this.Ctx.Input.Param(":serial")
- svcauthcode := authcode.GetAuthCodeService(utils.DBE)
- var aclist []authcode.AuthCode
- whereac := " AccCode='" + this.User.AccCode + "'"
- aclist = svcauthcode.GetList(" Id desc limit 1 ", whereac)
- var errinfo ErrorInfo
- if len(aclist) == 0 {
- errinfo.Message = "请需要先设置授权码"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- keystr := aclist[0].AuthCode
- //调接口获取sensors
- var sresponse MyResponse
- if keystr != "" {
- channel_id := "c" + serial
- strurl := utils.Cfg.MustValue("server", "dataapiurl") + "/channels/" + channel_id + "/datapoints"
- json.Unmarshal(ApiKeyRequest(strurl, "GET", keystr, nil), &sresponse)
- }
- // lastdata, err := GetChannelLast(serial)
- var data ChannelLast
- if len(sresponse.Results) > 0 && len(sresponse.Results[0].Series) > 0 &&
- len(sresponse.Results[0].Series[0].Values) > 0 {
- cols, arrs := sresponse.Results[0].Series[0].Columns, sresponse.Results[0].Series[0].Values
- vl := len(arrs)
- if vl == 1 {
- indextemp := utils.SliceIndexOf(cols, "temperature")
- timeindex := utils.SliceIndexOf(cols, "time")
- if indextemp > -1 {
- valuef, _ := arrs[0][indextemp].(float64)
- data.Temp = utils.ToStr(utils.FloatPoint(valuef, 2))
- }
- if timeindex > -1 {
- valuef, _ := arrs[0][timeindex].(float64)
- data.Time = time.Unix(int64(valuef), 0).Format("2006-01-02 15:04:05")
- }
- }
- // data.Temp = utils.ToStr(lastdata.Temperature)
- // data.Hum = utils.ToStr(lastdata.Humidity)
- // data.Vol = utils.ToStr(lastdata.Voltage)
- // data.Rssi = utils.ToStr(lastdata.RSSI)
- // data.O2 = utils.ToStr(lastdata.O2)
- // data.Co2 = utils.ToStr(lastdata.Co2)
- // data.Power = utils.ToStr(lastdata.ElectricalPower)
- // data.Supply = utils.ToStr(lastdata.ElectricalSupply)
- // data.Signal = utils.ToStr(lastdata.SimSignal)
- // data.LiquidLevel = utils.ToStr(lastdata.LiquidLevel)
- // data.Time = lastdata.Time.Format("2006-01-02 15:04:05")
- // if (time.Now().Unix() - 3600*int64(5)) < lasttimeint {
- // data.DState = 1
- // } else {
- // data.DState = 2
- // }
- }
- this.Data["json"] = &data
- this.ServeJSON()
- }
- // @Title 验证采集设备序列号
- // @Description 采集数据
- // @Param code path string true "设备SN"
- // @Success 200 {object} ErrorInfo
- // @Failure 403 :code 为空
- // @router /validcode/:code [get]
- func (this *ChannelsController) ValidCode() {
- code := this.Ctx.Input.Param(":code")
- var errinfo ErrorInfo
- var entity device.Channels
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/serial?serial=" + code
- json.Unmarshal(Apiget(strUrl), &entity)
- if entity.Code != "" && DeviceItemContainint(ChannelItem_Sensor, entity.DataItem) {
- errinfo.Message = "验证通过!"
- errinfo.Code = 0
- } else {
- errinfo.Message = "传感器序列号不存在!"
- errinfo.Code = -1
- }
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- // @Title 创建传感器
- // @Description 创建传感器
- // @Param body body business.device.DeviceChannels "传感器信息"
- // @Success 200 {object} controllers.Request
- // @router / [post]
- func (this *ChannelsController) AddChannel() {
- var model ChannelModel
- var jsonblob = this.Ctx.Input.RequestBody
- json.Unmarshal(jsonblob, &model)
- var errinfo ErrorInfo
- var channel device.Channels
- u, p := this.GetuAndp()
- channel.About = model.About
- channel.DeviceState, _ = utils.StrTo(model.DeviceState).Int()
- channel.Serial = model.Serial
- channel.TagCode = model.TagCode
- channel.Title = model.Title
- svc := device.GetDeviceService(utils.DBE)
- var entity device.Channels
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/serial?serial=" + channel.Serial
- json.Unmarshal(Apiget(strUrl), &entity)
- var devices device.Device
- //取缓存数据,如果是半小时内的数据就自动获取bbmac
- lastdata, err := GetChannelLast(channel.Serial)
- if lasttimeint := lastdata.Time.Unix(); err == nil && time.Now().Unix()-lasttimeint <= 30*60 {
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/serial?serial=" + lastdata.BBMac
- json.Unmarshal(Apiget(strUrl), &devices)
- //判断如果box未开通记录到数据库
- if devices.Id == 0 {
- notopen := device.Device_NotOpen{Serial: lastdata.BBMac, CSerial: channel.Serial}
- svc.InsertEntity(¬open)
- }
- }
- if !DeviceItemContainint(Device_Box, devices.DataItem) {
- devices = device.Device{}
- }
- if svc.VerifyChannel(entity.Code) {
- errinfo.Message = "传感器已存在!"
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- } else {
- api_wdid := devices.Id
- if !svc.VerifyDevice(devices.Id) && api_wdid > 0 {
- devices.Wdid = devices.Id
- devices.Id = 0
- devices.Serial = devices.Serial
- devices.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
- devices.CreateBy = this.User.Realname
- devices.Dtype = 1
- svc.InsertEntity(&devices)
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/devices/soursedevice/" + utils.ToStr(api_wdid) + "?u=" + u + "&p=" + p + "&sourse=coldchain&account=" + this.GetAccode() + "&accountname=" + this.User.Realname
- Apipost(strUrl, "PUT", nil)
- }
- if channel.Title == "" {
- channel.Title = entity.Title
- }
- if channel.Tags == "" {
- channel.Tags = entity.Tags
- }
- if channel.About == "" {
- channel.About = entity.About
- }
- cdid := svc.GetDIdBySerial(devices.Serial)
- if cdid == 0 {
- channel.DId = devices.Id
- } else {
- channel.DId = cdid
- }
- channel.Code = entity.Code
- channel.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
- channel.CreateBy = this.User.Realname
- //2015-4-21增加设备类型(区分普通传感器和blackbox) zh
- channel.DataItem = entity.DataItem
- _, err := svc.InsertEntity(&channel)
- if err == nil {
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/channels/" + entity.Code + "?u=" + u + "&p=" + p
- Apipost(strUrl, "PUT", channel)
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/channels/sourse/" + entity.Code + "?u=" + u + "&p=" + p + "&sourse=coldchain&account=" + this.GetAccode() + "&accountname=" + this.User.Realname
- Apipost(strUrl, "PUT", channel)
- //判断是否需要反写传感器对应关系到waterdrop
- if api_wdid != entity.DId && api_wdid > 0 {
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/channels/updatedevice/" + entity.Code + "?u=" + u + "&p=" + p + "&did=" + utils.ToStr(api_wdid)
- Apipost(strUrl, "PUT", nil)
- }
- //去查有没有blackbox被绑定
- var channeldevice1 device.Channels
- has := svc.GetEntity(&channeldevice1, " Did= "+utils.ToStr(svc.GetBlackBoxIdByCode(channel.Code))+" and DataItem=1 and Serial= '"+devices.Serial+"' ")
- if !has {
- var channeldevice device.Channels
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/serial?serial=" + devices.Serial //查询
- json.Unmarshal(Apiget(strUrl), &channeldevice)
- if channeldevice.Code != "" && DeviceItemContainint(ChannelItem_Box, channeldevice.DataItem) {
- channeldevice.DId = channel.DId
- var channeldevicedb device.Channels
- channeldevicedb.Title = channeldevice.Title
- channeldevicedb.Code = channeldevice.Code
- channeldevicedb.Serial = channeldevice.Serial
- channeldevicedb.Tags = channeldevice.Tags
- channeldevicedb.DId = channeldevice.DId
- channeldevicedb.DataItem = channeldevice.DataItem
- channeldevicedb.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
- channeldevicedb.CreateBy = this.User.Realname
- svc.InsertEntity(&channeldevicedb) //添加到冷云
- //绑定
- strUrl = utils.Cfg.MustValue("server", "apiurl") + "/channels/sourse/" + channeldevice.Code + "?u=" + u + "&p=" + p + "&sourse=coldchain&account=" + this.GetAccode() + "&accountname=" + this.User.Realname
- Apipost(strUrl, "PUT", nil)
- }
- }
- errinfo.Message = "保存成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- } else {
- errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- }
- }
- // @Title 编辑传感器
- // @Description 编辑传感器
- // @Param code path string true "需要修改的传感器编号"
- // @Param body body business.device.DeviceChannels "传感器信息"
- // @Success 200 {object} ErrorInfo
- // @router /:code [put]
- func (this *ChannelsController) EditChannel() {
- code := this.Ctx.Input.Param(":code")
- var errinfo ErrorInfo
- if code == "" {
- errinfo.Message = "操作失败!请求信息不完整"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- code = "c" + code
- var model ChannelModel
- var jsonblob = this.Ctx.Input.RequestBody
- json.Unmarshal(jsonblob, &model)
- var channel device.Channels
- u, p := this.GetuAndp()
- channel.About = model.About
- channel.DeviceState, _ = utils.StrTo(model.DeviceState).Int()
- channel.Serial = model.Serial
- channel.TagCode = model.TagCode
- channel.Title = model.Title
- var channelempty device.Channels
- svc := device.GetDeviceService(utils.DBE)
- channel.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
- channel.ModifiedBy = this.User.Realname
- var cols []string = []string{"Title", "Tags", "TagCode", "DeviceState", "About", "VisitLevel", "ModifiedUserId", "ModifiedBy"}
- err := svc.UpdateEntityAndBackupByCols(svc.GetIdByCode(code), &channel, &channelempty, cols, utils.ToStr(this.User.Id), this.User.Realname)
- if err == nil {
- // code := "c" + this.GetString("Serial")
- // enabled := this.GetString("alertor")
- // //修改报警设置状态
- // if code != "" && enabled != "" {
- // var status Status
- // u, p := this.GetuAndp()
- // strUrl := utils.Cfg.MustValue("server", "apiurl") + "/triggers/enabled?code=" + code + "&enabled=" + enabled + "&u=" + u + "&p=" + p
- // json.Unmarshal(Apipost(strUrl, "PUT", nil), &status)
- // //修改时报警状态的日志
- // var entity AllalarmLog_Log
- // title := channel.Title
- // svc1 := device.GetDeviceService(utils.DBE)
- // entity.Code = strings.Replace(code, "c", "", 1)
- // entity.Title = title
- // entity.Enabled, _ = utils.StrTo(enabled).Int()
- // svc1.InsertUpdateLog(0, &entity, utils.ToStr(this.User.Id), this.User.Realname, AllalarmLogTName)
- // }
- //修改缓存中设备状态
- cachevalue, _ := labsop.GetBoxCacheValue(channel.Serial)
- cachevalue.DeviceState = channel.DeviceState
- labsop.UpdateBoxChcheValue(channel.Serial, cachevalue)
- // //更新地理信息到device
- // if channel.DId > 0 {
- // var devicemodel device.Device
- // devicemodel.Id = channel.DId
- // devicemodel.Local = channel.Local
- // devicemodel.Latitude = channel.Latitude
- // devicemodel.Longitude = channel.Longitude
- // var devicecols []string = []string{"Longitude", "Latitude", "Local"}
- // svc.UpdateEntityByIdCols(devicemodel.Id, &devicemodel, devicecols)
- // }
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/" + code + "?u=" + u + "&p=" + p
- Apipost(strUrl, "PUT", channel)
- errinfo.Message = "保存成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "保存失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title 删除传感器
- // @Description 删除传感器
- // @Param code path string true "需要删除的传感器编号"
- // @Success 200 {object} ErrorInfo
- // @Failure 403 :code 为空
- // @router /:code [delete]
- func (this *ChannelsController) Delete() {
- code := this.Ctx.Input.Param(":code")
- var errinfo ErrorInfo
- if code == "" {
- errinfo.Message = "操作失败!请求信息不完整"
- errinfo.Code = -3
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- code = "c" + code
- var triggerlist []trigger.Trigger
- u, p := this.GetuAndp()
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/triggers/?cid=" + code + "&u=" + u + "&p=" + p
- json.Unmarshal(Apiget(strUrl), &triggerlist)
- if len(triggerlist) > 0 {
- errinfo.Message = "操作失败!请先删除该设备的报警设置!"
- errinfo.Code = -4
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- var entity device.Channels
- var devices device.Device
- var devicesempty device.Device
- var entityempty device.Channels
- svc := device.GetDeviceService(utils.DBE)
- cid := svc.GetIdByCode(code)
- has := svc.GetEntityById(cid, &entity)
- if has {
- //冷云数据校准值记录到缓存,清除
- cachevalue, _ := labsop.GetBoxCacheValue(entity.Serial)
- cachevalue.CalibrationValue = 0
- cachevalue.CalibrationHumidity = 0
- cachevalue.CalibrationCO2 = 0
- cachevalue.CalibrationO2 = 0
- cachevalue.DeviceState = 0
- labsop.UpdateBoxChcheValue(entity.Serial, cachevalue)
- err := svc.DeleteEntityAndBackup(cid, &entity, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
- if err == nil {
- //删除权限
- svcchannels := userchannels.GetUserChannelService(utils.DBE)
- svcchannels.ClearBBChannel(cid)
- //取消waterdrop绑定状态
- strUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + code + "?u=" + u + "&p=" + p
- Apipost(strUrl, "PUT", nil)
- //删除设备
- cnum := svc.GetChannelsCount(" DataItem in (" + ChannelItem_Sensor + ") and DId=" + utils.ToStr(entity.DId))
- if cnum <= 0 {
- //取消waterdrop绑定状态
- wdid := svc.GetWDidByDid(entity.DId)
- strUrldevice := utils.Cfg.MustValue("server", "apiurl") + "/devices/unbingding/" + utils.ToStr(wdid) + "?u=" + u + "&p=" + p
- Apipost(strUrldevice, "PUT", nil)
- svc.DeleteEntityAndBackup(entity.DId, &devices, &devicesempty, utils.ToStr(this.User.Id), this.User.Username)
- var channeldevice device.Channels
- haschannel := svc.GetEntity(&channeldevice, " DataItem=1 and DId="+utils.ToStr(entity.DId))
- if haschannel {
- svc.DeleteEntityAndBackup(channeldevice.Id, &channeldevice, &entityempty, utils.ToStr(this.User.Id), this.User.Username)
- cstrUrl := utils.Cfg.MustValue("server", "apiurl") + "/channels/unbingding/" + channeldevice.Code + "?u=" + u + "&p=" + p
- Apipost(cstrUrl, "PUT", nil)
- }
- }
- errinfo.Message = "删除成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- } else {
- errinfo.Message = "删除失败!"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title 验证报警器权限
- // @Description 验证报警器权限
- // @Success 200 {object} ErrorInfo
- // @router /validalert [get]
- func (this *ChannelsController) ValidAlert() {
- wdid := this.GetString("ids")
- serial := this.GetString("serial")
- wdids := strings.Split(wdid, ",")
- var errinfo ErrorInfo
- errinfo.Message = "验证通过!"
- errinfo.Code = 0
- svc := actions.GetActionsService(utils.DBE)
- for _, v := range wdids {
- action := svc.GetByWdid(v)
- if action.AItem == 4 {
- code := "c" + serial
- selectstr := ""
- lastdata, err := GetChannelLast("calert" + action.SPara4)
- if err == nil && lastdata.Time.Unix() > 0 {
- selectstr = lastdata.RequestData
- }
- if !utils.SliceContains(strings.Split(selectstr, ","), code) {
- errinfo.Message = "报警器(" + action.AName + ")未关联此设备"
- errinfo.Code = -1
- break
- }
- }
- }
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- // @Title 获取校准数据
- // @Description 获取校准数据
- // @Param serial path string true "设备SN"
- // @Success 200 {object} AdjuModel
- // @Failure 403 :serial 为空
- // @router /adjudata/:serial [get]
- func (this *ChannelsController) GetAdjuData() {
- svc := device.GetDeviceService(utils.DBE)
- var entity device.Channels
- svc.GetEntity(&entity, "Serial='"+this.Ctx.Input.Param(":serial")+"'")
- this.Data["json"] = &entity
- this.ServeJSON()
- }
- // @Title 校准数据
- // @Description 校准数据
- // @Param serial path string true "设备SN"
- // @Success 200 {object} ErrorInfo
- // @Failure 403 :serial 为空
- // @router /adjudata/:serial [put]
- func (this *ChannelsController) SaveAdjuData() {
- serial := this.Ctx.Input.Param(":serial")
- var errinfo ErrorInfo
- if serial == "" {
- errinfo.Message = "操作失败!请求信息不完整"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- code := "c" + serial
- var model AdjuModel
- var jsonblob = this.Ctx.Input.RequestBody
- json.Unmarshal(jsonblob, &model)
- var channel device.Channels
- var channelempty device.Channels
- svc := device.GetDeviceService(utils.DBE)
- channel.CalibrationValue, _ = utils.StrTo(model.CalibrationValue).Float32()
- channel.CalibrationHumidity, _ = utils.StrTo(model.CalibrationHumidity).Float32()
- channel.CalibrationCO2, _ = utils.StrTo(model.CalibrationCO2).Float32()
- channel.CalibrationO2, _ = utils.StrTo(model.CalibrationO2).Float32()
- channel.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
- channel.ModifiedBy = this.User.Realname
- var cols []string = []string{"CalibrationValue", "CalibrationHumidity", "CalibrationCO2", "CalibrationO2", "ModifiedUserId", "ModifiedBy"}
- err := svc.UpdateEntityAndBackupByCols(svc.GetIdByCode(code), &channel, &channelempty, cols, utils.ToStr(this.User.Id), this.User.Realname)
- if err == nil {
- //冷云数据校准值记录到缓存
- cachevalue, _ := labsop.GetBoxCacheValue(serial)
- cachevalue.CalibrationValue = channel.CalibrationValue
- cachevalue.CalibrationHumidity = channel.CalibrationHumidity
- cachevalue.CalibrationCO2 = channel.CalibrationCO2
- cachevalue.CalibrationO2 = channel.CalibrationO2
- labsop.UpdateBoxChcheValue(serial, cachevalue)
- errinfo.Message = "校准成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "校准失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title 历史采集数据
- // @Description 历史采集数据
- // @Param serial path string true "设备SN"
- // @Success 200 {object} coldcloud.DatapointColdCloud
- // @Failure 403 :serial 为空
- // @router /historydatavalue/:serial [get]
- func (this *ChannelsController) HistoryDataValue() {
- dataitem := this.GetString("_dataitem")
- serial := this.Ctx.Input.Param(":serial")
- cid := "c" + serial
- startint, _ := this.GetInt64("_start")
- endint, _ := this.GetInt64("_end")
- // client := labsop.GetLabSopClient(this.GetupdbAndHost())
- svcauthcode := authcode.GetAuthCodeService(utils.DBE)
- var aclist []authcode.AuthCode
- whereac := " AccCode='" + this.User.AccCode + "'"
- aclist = svcauthcode.GetList(" Id desc limit 1 ", whereac)
- if len(aclist) == 0 {
- this.Data["json"] = ChannelDataModel{}
- this.ServeJSON()
- return
- }
- valuedata, datagroup := this.ChannelManageview(aclist[0].AuthCode, cid, dataitem, startint/1000-1, endint/1000+1)
- this.Data["json"] = ChannelDataModel{valuedata, datagroup}
- this.ServeJSON()
- }
- func (this *ChannelsController) ChannelManageview(authcode, code, dataitem string, start, end int64) ([][]float64, []float64) {
- //调接口获取sensors
- var sresponse MyResponse
- channel_id := code
- strurl := utils.Cfg.MustValue("server", "dataapiurl") + "/channels/" + channel_id + "/datapoints"
- strurl += "?start=" + utils.ToStr(start) + "&end=" + utils.ToStr(end)
- json.Unmarshal(ApiKeyRequest(strurl, "GET", authcode, nil), &sresponse)
- var arrvs [][]float64
- if len(sresponse.Results) > 0 && len(sresponse.Results[0].Series) > 0 &&
- len(sresponse.Results[0].Series[0].Values) > 0 {
- cols, arrs := sresponse.Results[0].Series[0].Columns, sresponse.Results[0].Series[0].Values
- vl := len(arrs)
- indextemp := utils.SliceIndexOf(cols, "temperature")
- timeindex := utils.SliceIndexOf(cols, "time")
- voltageindex := utils.SliceIndexOf(cols, "voltage")
- if indextemp > -1 && timeindex > -1 && voltageindex > -1 {
- //历史数据
- for x, y := 0, vl; x < y; x++ {
- valuetempf, _ := arrs[x][indextemp].(float64)
- valuetimef, _ := arrs[x][timeindex].(float64)
- valuevolf, _ := arrs[x][voltageindex].(float64)
- arr := []float64{valuetimef * 1000, valuetempf, valuevolf}
- arrvs = append(arrvs, arr)
- }
- }
- }
- var arrgroup []float64
- return arrvs, arrgroup
- }
- // @Title 设备排序
- // @Description 设备排序
- // @Success 200 {object} ErrorInfo
- // @router /sortsave [put]
- func (this *ChannelsController) SaveSort() {
- var model SortDataModel
- var jsonblob = this.Ctx.Input.RequestBody
- json.Unmarshal(jsonblob, &model)
- channelId := strings.Split(model.ChannelIds, ",")
- var channelsSort device.ChannelsSort
- svc := device.GetDeviceService(utils.DBE)
- channelsSort.UserId, _ = utils.StrTo(this.User.Id).Int()
- svc.DeleteEntity(&channelsSort)
- channelsSort.CreateUserId = channelsSort.UserId
- channelsSort.CreateBy = this.User.Realname
- channelsSort.ModifiedUserId = channelsSort.UserId
- channelsSort.ModifiedBy = this.User.Realname
- for i, j := 0, len(channelId)-1; i < j; i++ {
- channelsSort.ChannelId, _ = utils.StrTo(channelId[i]).Int()
- channelsSort.SortCode = i
- svc.InsertEntity(&channelsSort)
- }
- var errinfo ErrorInfo
- errinfo.Message = "保存成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
|