87 lines
2.5 KiB
C#
87 lines
2.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Manager.Interfaces.Models;
|
|
using ManagerService.Data;
|
|
using Microsoft.Extensions.Configuration;
|
|
using MongoDB.Driver;
|
|
|
|
namespace Manager.Services
|
|
{
|
|
public class DeviceDatabaseService
|
|
{
|
|
private readonly IMongoCollection<Device> _Devices;
|
|
|
|
public DeviceDatabaseService(IConfiguration config)
|
|
{
|
|
var client = new MongoClient(config.GetConnectionString("TabletDb"));
|
|
var database = client.GetDatabase("TabletDb");
|
|
_Devices = database.GetCollection<Device>("Devices");
|
|
}
|
|
|
|
public List<Device> GetAll(string instanceId)
|
|
{
|
|
return _Devices.Find(d => d.InstanceId == instanceId).ToList();
|
|
}
|
|
|
|
public List<Device> GetAllConnected()
|
|
{
|
|
return _Devices.Find(d => d.Connected).ToList();
|
|
}
|
|
|
|
public List<Device> GetAllWithConfig(string configId)
|
|
{
|
|
return _Devices.Find(d => d.ConfigurationId == configId).ToList();
|
|
}
|
|
|
|
public Device GetById(string id)
|
|
{
|
|
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public Device GetByIdentifier(string identifier)
|
|
{
|
|
return _Devices.Find<Device>(d => d.Identifier == identifier).FirstOrDefault();
|
|
}
|
|
|
|
public bool IsExistIdentifier(string identifier)
|
|
{
|
|
return _Devices.Find<Device>(d => d.Identifier == identifier).FirstOrDefault() != null ? true : false;
|
|
}
|
|
|
|
public bool IsExistIpWLAN(string ip)
|
|
{
|
|
return _Devices.Find<Device>(d => d.IpAddressWLAN == ip).FirstOrDefault() != null ? true : false;
|
|
}
|
|
|
|
public bool IsExistIpETH(string ip)
|
|
{
|
|
return _Devices.Find<Device>(d => d.IpAddressETH == ip).FirstOrDefault() != null ? true : false;
|
|
}
|
|
|
|
public bool IsExist(string id)
|
|
{
|
|
return _Devices.Find<Device>(d => d.Id == id).FirstOrDefault() != null ? true : false;
|
|
}
|
|
|
|
public Device Create(Device device)
|
|
{
|
|
_Devices.InsertOne(device);
|
|
return device;
|
|
}
|
|
|
|
public Device Update(string id, Device deviceIn)
|
|
{
|
|
_Devices.ReplaceOne(d => d.Id == id, deviceIn);
|
|
return deviceIn;
|
|
}
|
|
|
|
public void Remove(string id)
|
|
{
|
|
_Devices.DeleteOne(d => d.Id == id);
|
|
}
|
|
|
|
}
|
|
}
|