2 Star 0 Fork 0

EdgexFoundry/core-metadata-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mongo_deviceservice.go 4.56 KB
一键复制 编辑 原始数据 按行查看 历史
Masataka Mizukoshi 提交于 2017-12-12 22:15 . add mgo package
/*******************************************************************************
* Copyright 2017 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*
* @microservice: core-metadata-go service
* @author: Spencer Bull & Ryan Comer, Dell
* @version: 0.5.0
*******************************************************************************/
package main
import (
"github.com/edgexfoundry/core-domain-go/models"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// Internal version of the device service struct
// Use this to handle DBRef
type MongoDeviceService struct {
models.DeviceService
}
// Custom marshaling into mongo
func (mds MongoDeviceService) GetBSON() (interface{}, error) {
return struct {
models.DescribedObject `bson:",inline"`
Id bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name"` // time in milliseconds that the device last provided any feedback or responded to any request
LastConnected int64 `bson:"lastConnected"` // time in milliseconds that the device last reported data to the core
LastReported int64 `bson:"lastReported"` // operational state - either enabled or disabled
OperatingState models.OperatingState `bson:"operatingState"` // operational state - ether enabled or disableddc
Labels []string `bson:"labels"` // tags or other labels applied to the device service for search or other identification needs
Addressable mgo.DBRef `bson:"addressable"` // address (MQTT topic, HTTP address, serial bus, etc.) for reaching the service
AdminState models.AdminState `bson:"adminState"` // Device Service Admin State
}{
DescribedObject: mds.Service.DescribedObject,
Id: mds.Service.Id,
Name: mds.Service.Name,
AdminState: mds.AdminState,
OperatingState: mds.Service.OperatingState,
Addressable: mgo.DBRef{Collection: ADDCOL, Id: mds.Service.Addressable.Id},
LastConnected: mds.Service.LastConnected,
LastReported: mds.Service.LastReported,
Labels: mds.Service.Labels,
}, nil
}
// Custom unmarshaling out of mongo
func (mds *MongoDeviceService) SetBSON(raw bson.Raw) error {
decoded := new(struct {
models.DescribedObject `bson:",inline"`
Id bson.ObjectId `bson:"_id,omitempty"`
Name string `bson:"name"` // time in milliseconds that the device last provided any feedback or responded to any request
LastConnected int64 `bson:"lastConnected"` // time in milliseconds that the device last reported data to the core
LastReported int64 `bson:"lastReported"` // operational state - either enabled or disabled
OperatingState models.OperatingState `bson:"operatingState"` // operational state - ether enabled or disableddc
Labels []string `bson:"labels"` // tags or other labels applied to the device service for search or other identification needs
Addressable mgo.DBRef `bson:"addressable"` // address (MQTT topic, HTTP address, serial bus, etc.) for reaching the service
AdminState models.AdminState `bson:"adminState"` // Device Service Admin State
})
bsonErr := raw.Unmarshal(decoded)
if bsonErr != nil {
return bsonErr
}
// Copy over the non-DBRef fields
mds.Service.DescribedObject = decoded.DescribedObject
mds.Service.Id = decoded.Id
mds.Service.Name = decoded.Name
mds.AdminState = decoded.AdminState
mds.Service.OperatingState = decoded.OperatingState
mds.Service.LastConnected = decoded.LastConnected
mds.Service.LastReported = decoded.LastReported
mds.Service.Labels = decoded.Labels
// De-reference the DBRef fields
ds := DS.dataStore()
defer ds.s.Close()
addCol := ds.s.DB(DB).C(ADDCOL)
var a models.Addressable
err := addCol.Find(bson.M{"_id": decoded.Addressable.Id}).One(&a)
if err != nil {
return err
}
mds.Service.Addressable = a
return nil
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/EdgexFoundry/core-metadata-go.git
git@gitee.com:EdgexFoundry/core-metadata-go.git
EdgexFoundry
core-metadata-go
core-metadata-go
master

搜索帮助