forked from IoTSharp/IoTSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
247 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using System; | ||
using IoTSharp.Contracts; | ||
using System; | ||
|
||
namespace IoTSharp.Data | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using IoTSharp.Contracts; | ||
using IoTSharp.Data; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace IoTSharp.Data.Extensions | ||
{ | ||
public static class AlarmExtension | ||
{ | ||
|
||
public static async Task<ApiResult<Alarm>> OccurredAlarm(this ApplicationDbContext _context, CreateAlarmDto cad) | ||
{ | ||
Guid OriginatorId = Guid.Empty; | ||
OriginatorType originatorType = cad.OriginatorType; | ||
if (cad.OriginatorType == OriginatorType.Device || cad.OriginatorType == OriginatorType.Gateway || cad.OriginatorType == OriginatorType.Unknow) | ||
{ | ||
var dev = _context.Device.Include(d=>d.Tenant).Include(d=>d.Customer).FirstOrDefault(d => d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName); | ||
if (dev != null) | ||
{ | ||
if (dev.DeviceType == DeviceType.Gateway) | ||
{ | ||
if (dev.Id.ToString() != cad.OriginatorName && dev.Name != cad.OriginatorName) | ||
{ | ||
var subdev = from g in _context.Device.Include(d => d.Tenant).Include(d => d.Customer).Include(g => g.Owner) where g.Owner == dev && g.Name == cad.OriginatorName select g; | ||
var orig = await subdev.FirstOrDefaultAsync(); | ||
OriginatorId = orig.Id; | ||
originatorType = OriginatorType.Device; | ||
} | ||
else | ||
{ | ||
originatorType = OriginatorType.Gateway; | ||
OriginatorId = dev.Id; | ||
} | ||
} | ||
else if (dev.DeviceType == DeviceType.Device) | ||
{ | ||
originatorType = OriginatorType.Device; | ||
OriginatorId = dev.Id; | ||
} | ||
return await _context.OccurredAlarm(cad, _alarm => | ||
{ | ||
_alarm.OriginatorType = originatorType; | ||
_alarm.OriginatorId = OriginatorId; | ||
_alarm.Tenant = dev.Tenant; | ||
_alarm.Customer = dev.Customer; | ||
}); | ||
} | ||
else return new ApiResult<Alarm>(ApiCode.NotFoundDevice, "Originator name not a device!",null); | ||
} | ||
else if (cad.OriginatorType == OriginatorType.Asset) | ||
{ | ||
var ass = _context.Assets.Include(a => a.Tenant).Include(a => a.Customer).FirstOrDefault(d => d.Id.ToString() == cad.OriginatorName || d.Name == cad.OriginatorName); | ||
if (ass != null) | ||
{ | ||
originatorType = OriginatorType.Asset; | ||
OriginatorId = ass.Id; | ||
return await _context.OccurredAlarm(cad, _alarm => | ||
{ | ||
_alarm.OriginatorType = originatorType; | ||
_alarm.OriginatorId = OriginatorId; | ||
_alarm.Tenant = ass.Tenant; | ||
_alarm.Customer = ass.Customer; | ||
}); | ||
} | ||
else return new ApiResult<Alarm>(ApiCode.NotFoundDevice, "Originator name not a asset",null); | ||
} | ||
else return new ApiResult<Alarm>(ApiCode.NotFoundDevice, "Originator name not a asset",null); | ||
} | ||
|
||
|
||
|
||
public static async Task<ApiResult<Alarm>> OccurredAlarm(this ApplicationDbContext _context, CreateAlarmDto dto, Action<Alarm> action) | ||
{ | ||
var result = new ApiResult<Alarm>(ApiCode.InValidData,"",null); | ||
try | ||
{ | ||
var alarm = new Alarm | ||
{ | ||
Id = Guid.NewGuid(), | ||
AckDateTime = DateTime.Now, | ||
AlarmDetail = dto.AlarmDetail, | ||
AlarmStatus = AlarmStatus.Active_UnAck, | ||
AlarmType = dto.AlarmType, | ||
ClearDateTime = new DateTime(1970, 1, 1), | ||
EndDateTime = new DateTime(1970, 1, 1), | ||
Propagate = true, | ||
Serverity = dto.Serverity, | ||
StartDateTime = DateTime.Now, | ||
}; | ||
action?.Invoke(alarm); | ||
var isone = from a in _context.Alarms where a.OriginatorId == alarm.OriginatorId && a.AlarmType == alarm.AlarmType && (a.AlarmStatus == AlarmStatus.Cleared_UnAck|| a.AlarmStatus == AlarmStatus.Active_UnAck) select a; | ||
if (isone.Any()) | ||
{ | ||
var old = isone.First(); | ||
old.AlarmDetail = alarm.AlarmDetail; | ||
if ( old.Serverity != dto.Serverity) | ||
{ | ||
if (old.Serverity== ServerityLevel.Indeterminate && dto.Serverity!= ServerityLevel.Indeterminate) | ||
{ | ||
old.StartDateTime = DateTime.Now; | ||
alarm.Propagate = true; | ||
} | ||
else if (old.Serverity != ServerityLevel.Indeterminate && dto.Serverity == ServerityLevel.Indeterminate) | ||
{ | ||
old.EndDateTime = DateTime.Now; | ||
if (old.ClearDateTime.Year == 1970) | ||
{ | ||
old.ClearDateTime = DateTime.Now; | ||
} | ||
alarm.Propagate = true; | ||
} | ||
else | ||
{ | ||
alarm.Propagate = false; | ||
} | ||
old.Serverity = dto.Serverity; | ||
} | ||
} | ||
else | ||
{ | ||
|
||
_context.Alarms.Add(alarm); | ||
} | ||
int ret = await _context.SaveChangesAsync(); | ||
result = new ApiResult<Alarm>(ret > 0 ? ApiCode.Success : ApiCode.NothingToDo, ret > 0 ? "OK" : "No data", alarm); | ||
} | ||
catch (Exception ex) | ||
{ | ||
result = new ApiResult<Alarm>(ApiCode.Exception, ex.Message,null); | ||
} | ||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using IoTSharp.Contracts; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace IoTSharp.Data.Extensions | ||
{ | ||
public static class DeviceExtension | ||
{ | ||
|
||
/// <summary> | ||
/// When creating a device, all the things that need to be done here are done | ||
/// </summary> | ||
/// <param name="_context"></param> | ||
/// <param name="device"></param> | ||
public static void AfterCreateDevice(this ApplicationDbContext _context, Device device) | ||
{ | ||
if (device.Customer == null || device.Tenant == null || string.IsNullOrEmpty(device.Name)) | ||
{ | ||
throw new Exception($"Customer({device.Customer?.Id}) or Tenant({device.Tenant?.Id}) or Name({device.Name}) is null or empty!"); | ||
} | ||
else | ||
{ | ||
_context.DeviceIdentities.Add(new DeviceIdentity() | ||
{ | ||
Device = device, | ||
IdentityType = IdentityType.AccessToken, | ||
IdentityId = Guid.NewGuid().ToString().Replace("-", "") | ||
}); | ||
Dictionary<string, object> pairs = new Dictionary<string, object>(); | ||
pairs.Add("CreateDateTime", DateTime.Now); | ||
_context.PreparingData<AttributeLatest>(pairs, device.Id, DataSide.ServerSide); | ||
} | ||
} | ||
public static void AfterCreateDevice(this ApplicationDbContext _context, Device device,string username,string password) | ||
{ | ||
if (device.Customer == null || device.Tenant == null || string.IsNullOrEmpty(device.Name)) | ||
{ | ||
throw new Exception($"Customer({device.Customer?.Id}) or Tenant({device.Tenant?.Id}) or Name({device.Name}) is null or empty!"); | ||
} | ||
else | ||
{ | ||
_context.DeviceIdentities.Add(new DeviceIdentity() | ||
{ | ||
Device = device, | ||
IdentityType = IdentityType.DevicePassword, | ||
IdentityId = username, | ||
IdentityValue = password | ||
}) ; | ||
Dictionary<string, object> pairs = new Dictionary<string, object>(); | ||
pairs.Add("CreateDateTime", DateTime.Now); | ||
_context.PreparingData<AttributeLatest>(pairs, device.Id, DataSide.ServerSide); | ||
} | ||
} | ||
public static async Task<DeviceRule[]> GerDeviceRulesList(this ApplicationDbContext _dbContext, Guid devid, MountType mountType) | ||
{ | ||
DeviceRule[] lst = null; | ||
var r = from dr in _dbContext.DeviceRules.Include(d => d.Device).Include(d => d.FlowRule) where dr.Device.Id == devid && dr.FlowRule.MountType == mountType select dr ; | ||
if (r.Any()) | ||
{ | ||
lst = await r.ToArrayAsync(); | ||
} | ||
return lst; | ||
} | ||
public static async Task<Guid> GerDeviceRpcRulesList(this ApplicationDbContext _dbContext, Guid devid, MountType mountType,string method) | ||
{ | ||
var rules = await GerDeviceRulesList(_dbContext, devid, mountType); | ||
var g = (rules.FirstOrDefault(r => r.FlowRule.Name == method)?.FlowRule.RuleId); | ||
return g.GetValueOrDefault(); | ||
} | ||
public static async Task<Guid[]> GerDeviceRulesIdList(this ApplicationDbContext _dbContext, Guid devid, MountType mountType) | ||
{ | ||
var rules =await GerDeviceRulesList(_dbContext, devid, mountType); | ||
return rules?.Select(xc => xc.FlowRule.RuleId).ToArray(); | ||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters