2 Star 0 Fork 0

EdgexFoundry/core-metadata-go

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dbOps.go 15.97 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
/*******************************************************************************
* 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 (
"time"
enums "github.com/edgexfoundry/core-domain-go/enums"
models "github.com/edgexfoundry/core-domain-go/models"
mgo "gopkg.in/mgo.v2"
bson "gopkg.in/mgo.v2/bson"
)
type DataStore struct {
s *mgo.Session
}
func (ds DataStore) dataStore() *DataStore {
return &DataStore{ds.s.Copy()}
}
// Connect to the database
func dbConnect() bool {
if DATABASE == enums.MONGODB {
mongoDBDialInfo := &mgo.DialInfo{
Addrs: []string{DOCKERMONGO},
Timeout: time.Duration(configuration.MongoDBConnectTimeout) * time.Millisecond,
Database: MONGODATABASE,
Username: DBUSER,
Password: DBPASS,
}
s, err := mgo.DialWithInfo(mongoDBDialInfo)
if err != nil {
return false
}
// Set timeout based on configuration
s.SetSocketTimeout(time.Duration(configuration.MongoDBConnectTimeout) * time.Millisecond)
DS.s = s
return true
}
return false
}
/* ----------------------- Schedule Event ------------------------------*/
func getAllScheduleEvents(se *[]models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoGetAllScheduleEvents(se)
}
return nil
}
func addScheduleEvent(se *models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoAddScheduleEvent(se)
}
return nil
}
func getScheduleEventByName(se *models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventByName(se, n)
}
return nil
}
func updateScheduleEvent(se models.ScheduleEvent) error {
if DATABASE == enums.MONGODB {
return mgoUpdateScheduleEvent(se)
}
return nil
}
func getScheduleEventById(se *models.ScheduleEvent, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventById(se, id)
}
return nil
}
func getScheduleEventsByScheduleName(se *[]models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByScheduleName(se, n)
}
return nil
}
func getScheduleEventsByAddressableId(se *[]models.ScheduleEvent, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByAddressableId(se, id)
}
return nil
}
func getScheduleEventsByServiceName(se *[]models.ScheduleEvent, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleEventsByServiceName(se, n)
}
return nil
}
/* -------------------------- Schedule ---------------------------------*/
func getAllSchedules(s *[]models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoGetAllSchedules(s)
}
return nil
}
func addSchedule(s *models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoAddSchedule(s)
}
return nil
}
func getScheduleByName(s *models.Schedule, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleByName(s, n)
}
return nil
}
func updateSchedule(s models.Schedule) error {
if DATABASE == enums.MONGODB {
return mgoUpdateSchedule(s)
}
return nil
}
func getScheduleById(s *models.Schedule, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetScheduleById(s, id)
}
return nil
}
/* ------------------------Device Report -------------------------------*/
func getAllDeviceReports(dr *[]models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDeviceReports(dr)
}
return nil
}
func getDeviceReportByDeviceName(dr *[]models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportByDeviceName(dr, n)
}
return nil
}
func getDeviceReportByName(dr *models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportByName(dr, n)
}
return nil
}
func getDeviceReportById(dr *models.DeviceReport, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportById(dr, id)
}
return nil
}
func addDeviceReport(dr *models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoAddDeviceReport(dr)
}
return nil
}
func updateDeviceReport(dr *models.DeviceReport) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceReport(dr)
}
return nil
}
func getDeviceReportsByScheduleEventName(dr *[]models.DeviceReport, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceReportsByScheduleEventName(dr, n)
}
return nil
}
// ------------------------------------- DEVICE --------------------------------------------
func UpdateDevice(d models.Device) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDevice(d)
}
return nil
}
func getDeviceById(d *models.Device, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceById(d, id)
}
return nil
}
func getDeviceByName(d *models.Device, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceByName(d, n)
}
return nil
}
func getAllDevices(d *[]models.Device) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDevices(d)
}
return nil
}
func getDevicesByProfileId(d *[]models.Device, pid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByProfileId(d, pid)
}
return nil
}
func getDevicesByProfileName(d *[]models.Device, pn string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByProfileName(d, pn)
}
return nil
}
func getDevicesByServiceId(d *[]models.Device, sid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByServiceId(d, sid)
}
return nil
}
func getDevicesByServiceName(d *[]models.Device, sn string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByServiceName(d, sn)
}
return nil
}
func getDevicesByAddressableId(d *[]models.Device, aid string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByAddressableId(d, aid)
}
return nil
}
func getDevicesByAddressableName(d *[]models.Device, an string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesByAddressableName(d, an)
}
return nil
}
func getDevicesWithLabel(d *[]models.Device, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDevicesWithLabel(d, l)
}
return nil
}
func addDevice(d *models.Device) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDevice(d)
}
return nil
}
func updateDeviceProfile(dp *models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceProfile(dp)
}
return nil
}
func addDeviceProfile(d *models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDeviceProfile(d)
}
return nil
}
func getAllDeviceProfiles(d *[]models.DeviceProfile) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfiles(d, bson.M{})
}
return nil
}
func getDeviceProfileById(d *models.DeviceProfile, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfileById(d, id)
}
return nil
}
func deleteDeviceProfileById(dpid string) error {
if err := deleteById(DPCOL, dpid); err != nil {
return err
}
return nil
}
func deleteDeviceProfileByName(n string) error {
var dp models.DeviceProfile
getDeviceProfileByName(&dp, n)
// Delete all of the commands for the device profile
for i := 0; i < len(dp.Commands); i++ {
// TODO Figure out how to store MONGO ID
if err := deleteById(COMCOL, dp.Commands[i].Id.Hex()); err != nil {
return err
}
}
// Delete the device profile
if err := deleteByName(DPCOL, n); err != nil {
return err
}
return nil
}
//func getDeviceProfilesByCommandName(d *[]models.DeviceProfile, cn string) error {
// if DATABASE == enums.MONGODB {
// return mgoGetDeviceProfilesByCommandName(d, cn)
// }
// return nil
//}
func getDeviceProfilesByModel(dp *[]models.DeviceProfile, m string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByModel(dp, m)
}
return nil
}
func getDeviceProfilesWithLabel(dp *[]models.DeviceProfile, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesWithLabel(dp, l)
}
return nil
}
func getDeviceProfilesByManufacturerModel(dp *[]models.DeviceProfile, man string, mod string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByManufacturerModel(dp, man, mod)
}
return nil
}
func getDeviceProfilesByManufacturer(dp *[]models.DeviceProfile, man string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesByManufacturer(dp, man)
}
return nil
}
func getDeviceProfileByName(dp *models.DeviceProfile, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfileByName(dp, n)
}
return nil
}
func updateAddressable(ra *models.Addressable, r *models.Addressable) error {
if DATABASE == enums.MONGODB {
return mgoUpdateAddressable(ra, r)
}
return nil
}
func addAddressable(a *models.Addressable) error {
if DATABASE == enums.MONGODB {
return mgoAddNewAddressable(a)
}
return nil
}
func getAddressableById(a *models.Addressable, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressableById(a, id)
}
return nil
}
func getAddressableByName(a *models.Addressable, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressableByName(a, n)
}
return nil
}
func getAddressablesByTopic(a *[]models.Addressable, t string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByTopic(a, t)
}
return nil
}
func getAddressablesByPort(a *[]models.Addressable, p int) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByPort(a, p)
}
return nil
}
func getAddressablesByPublisher(a *[]models.Addressable, p string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByPublisher(a, p)
}
return nil
}
func getAddressablesByAddress(a *[]models.Addressable, add string) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressablesByAddress(a, add)
}
return nil
}
func getAddressable(d *models.Addressable, q bson.M) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressable(d, q)
}
return nil
}
func getAddressables(d *[]models.Addressable, q bson.M) error {
if DATABASE == enums.MONGODB {
return mgoGetAddressables(d, q)
}
return nil
}
func isAddressableAssociatedToDevice(a models.Addressable) (bool, error) {
if DATABASE == enums.MONGODB {
return mgoIsAddressableAssociatedToDevice(a)
}
return false, nil
}
func isAddressableAssociatedToDeviceService(a models.Addressable) (bool, error) {
if DATABASE == enums.MONGODB {
return mgoIsAddressableAssociatedToDeviceService(a)
}
return false, nil
}
// ------------------------ DEVICE SERVICE -----------------------
func updateDeviceService(ds models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoUpdateDeviceService(ds)
}
return nil
}
func getDeviceServicesByAddressableName(d *[]models.DeviceService, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesByAddressableName(d, n)
}
return nil
}
func getDeviceServicesByAddressableId(d *[]models.DeviceService, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesByAddressableId(d, id)
}
return nil
}
func getDeviceServicesWithLabel(d *[]models.DeviceService, l []string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServicesWithLabel(d, l)
}
return nil
}
func getDeviceServiceById(d *models.DeviceService, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServiceById(d, id)
}
return nil
}
func getDeviceServiceByName(d *models.DeviceService, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceServiceByName(d, n)
}
return nil
}
func getAllDeviceServices(d *[]models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoGetAllDeviceServices(d)
}
return nil
}
func addDeviceService(ds *models.DeviceService) error {
if DATABASE == enums.MONGODB {
return mgoAddNewDeviceService(ds)
}
return nil
}
/* -----------------------Provision Watcher ----------------------*/
func getProvisionWatcherById(pw *models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherById(pw, id)
}
return nil
}
func getAllProvisionWatchers(pw *[]models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoGetAllProvisionWatchers(pw)
}
return nil
}
func getProvisionWatcherByName(pw *models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherByName(pw, n)
}
return nil
}
func getProvisionWatcherByProfileId(pw *[]models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatcherByProfileId(pw, id)
}
return nil
}
func getProvisionWatchersByProfileName(pw *[]models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByProfileName(pw, n)
}
return nil
}
func getProvisionWatchersByServiceId(pw *[]models.ProvisionWatcher, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByServiceId(pw, id)
}
return nil
}
func getProvisionWatchersByServiceName(pw *[]models.ProvisionWatcher, n string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByServiceName(pw, n)
}
return nil
}
func getProvisionWatchersByIdentifier(pw *[]models.ProvisionWatcher, k string, v string) error {
if DATABASE == enums.MONGODB {
return mgoGetProvisionWatchersByIdentifier(pw, k, v)
}
return nil
}
func addProvisionWatcher(pw *models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoAddProvisionWatcher(pw)
}
return nil
}
func updateProvisionWatcher(pw models.ProvisionWatcher) error {
if DATABASE == enums.MONGODB {
return mgoUpdateProvisionWatcher(pw)
}
return nil
}
/* -----------------------COMMAND ----------------------*/
func getCommandById(c *models.Command, id string) error {
if DATABASE == enums.MONGODB {
err := mgoGetCommandById(c, id)
if err == mgo.ErrNotFound {
return ErrNotFound
} else {
return err
}
}
return nil
}
func getCommandByName(d *[]models.Command, id string) error {
if DATABASE == enums.MONGODB {
return mgoGetCommandByName(d, id)
}
return nil
}
func addCommand(c *models.Command) error {
if DATABASE == enums.MONGODB {
return mgoAddCommand(c)
}
return nil
}
func getAllCommands(d *[]models.Command) error {
if DATABASE == enums.MONGODB {
return mgoGetAllCommands(d)
}
return nil
}
func updateCommand(c *models.Command, r *models.Command) error {
if DATABASE == enums.MONGODB {
return mgoUpdateCommand(c, r)
}
return nil
}
func deleteByName(c string, n string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteByName(c, n)
}
return nil
}
func deleteCommandById(id string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteCommandById(id)
}
return nil
}
// Get the device profiles that are using the command
func getDeviceProfilesUsingCommand(dp *[]models.DeviceProfile, c models.Command) error {
if DATABASE == enums.MONGODB {
return mgoGetDeviceProfilesUsingCommand(dp, c)
}
return nil
}
func deleteById(c string, did string) error {
if DATABASE == enums.MONGODB {
return mgoDeleteById(c, did)
}
return nil
}
func setByName(c string, n string, pv2 string, p2 string) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByName(c, n, pv2, p2)
}
return nil
}
func setByNameInt(c string, n string, pv2 string, p2 int64) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByNameInt(c, n, pv2, p2)
}
return nil
}
func setById(c string, did string, pv2 string, p2 string) error {
if DATABASE == enums.MONGODB {
return mgoUpdateById(c, did, pv2, p2)
}
return nil
}
func setByIdInt(c string, did string, pv2 string, p2 int64) error {
if DATABASE == enums.MONGODB {
return mgoUpdateByIdInt(c, did, pv2, p2)
}
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

搜索帮助