public ActionResult <AddValueResponse> Post(AddValue request) { AddValueResponse ret = new AddValueResponse(); GXUpdateArgs arg; if (request.Items != null) { DateTime now = DateTime.Now; foreach (GXValue it in request.Items) { if (it.Read == DateTime.MinValue) { it.Read = now; } GXAttribute value = new GXAttribute(); value.Id = it.AttributeId; value.Value = it.Value; value.Read = it.Read; arg = GXUpdateArgs.Update <GXAttribute>(value, q => new { q.Value, q.Read }); host.Connection.Update(arg); } host.Connection.Insert(GXInsertArgs.InsertRange(request.Items)); host.SetChange(TargetType.Value, now); } return(ret); }
public ActionResult <AttributeTemplateDeleteResponse> Post(AttributeTemplateDelete request) { if (request.Ids != null) { foreach (UInt64 id in request.Ids) { if (id == 0) { return(BadRequest("Attribute template ID is invalid.")); } } GXSelectArgs arg = GXSelectArgs.Select <GXAttributeTemplate>(a => a.Id, q => request.Ids.Contains(q.Id)); List <GXAttributeTemplate> list = host.Connection.Select <GXAttributeTemplate>(arg); if (list.Count != 0) { DateTime now = DateTime.Now; foreach (GXAttributeTemplate it in list) { it.Removed = now; host.Connection.Update(GXUpdateArgs.Update(it, q => q.Removed)); } host.SetChange(TargetType.AttributeTemplate, DateTime.Now); } } else { return(BadRequest("Attribute template ID is invalid.")); } return(new AttributeTemplateDeleteResponse()); }
public ActionResult <DeleteScheduleResponse> Post(DeleteSchedule request) { //Remove mapping. if (request.AttributeIds != null && request.ScheduleIds != null) { for (int pos = 0; pos != request.AttributeIds.Length; ++pos) { GXDeleteArgs arg = GXDeleteArgs.Delete <GXScheduleToAttribute>(q => request.ScheduleIds[pos] == q.ScheduleId); arg.Where.And <GXScheduleToAttribute>(q => request.AttributeIds[pos] == q.AttributeId); host.Connection.Delete(arg); } host.SetChange(TargetType.Schedule, DateTime.Now); } else if (request.ScheduleIds != null) { GXSelectArgs arg = GXSelectArgs.Select <GXSchedule>(a => a.Id, q => request.ScheduleIds.Contains(q.Id)); List <GXSchedule> list = host.Connection.Select <GXSchedule>(arg); DateTime now = DateTime.Now; foreach (GXSchedule it in list) { it.Removed = now; host.Connection.Update(GXUpdateArgs.Update(it, q => q.Removed)); } host.SetChange(TargetType.Schedule, now); } return(new DeleteScheduleResponse()); }
public ActionResult <AddReaderResponse> Post(AddReader request) { GXSelectArgs arg = GXSelectArgs.SelectAll <GXReaderInfo>(); arg.Where.And <GXReaderInfo>(q => q.Guid == request.Reader.Guid); GXReaderInfo i = host.Connection.SingleOrDefault <GXReaderInfo>(arg); if (i == null) { request.Reader.Detected = request.Reader.Generation = DateTime.Now; host.Connection.Insert(GXInsertArgs.Insert(request.Reader)); } else if (i.Name != request.Reader.Name && !string.IsNullOrEmpty(request.Reader.Name)) { //Update reader name. i.Detected = DateTime.Now; host.Connection.Update(GXUpdateArgs.Update(i, u => new { u.Detected, u.Name })); } else { i.Detected = DateTime.Now; host.Connection.Update(GXUpdateArgs.Update(i, u => u.Detected)); } host.SetChange(TargetType.Readers, DateTime.Now); return(new AddReaderResponse()); }
public ActionResult <UpdateScheduleExecutionTimeResponse> Post(UpdateScheduleExecutionTime request) { GXSchedule s = host.Connection.SelectById <GXSchedule>(request.Id); s.ExecutionTime = request.Time; host.Connection.Update(GXUpdateArgs.Update(s, c => c.ExecutionTime)); host.SetChange(TargetType.Schedule, DateTime.Now); return(new UpdateScheduleExecutionTimeResponse()); }
public void UpdateTest() { TestClass t = new TestClass(); t.Id = 2; t.Time = DateTime.SpecifyKind(new DateTime(2014, 1, 2), DateTimeKind.Utc); GXUpdateArgs args = GXUpdateArgs.Update(t, x => new { x.Id, x.Guid, x.Time }); Assert.AreEqual("UPDATE TestClass SET `ID` = 2, `Guid` = '00000000000000000000000000000000', `Time` = '2014-01-02 00:00:00' WHERE `ID` = 2", args.ToString()); }
public ActionResult <GetNextTaskResponse> Post(GetNextTask request) { GetNextTaskResponse ret = new GetNextTaskResponse(); lock (host) { GXSelectArgs arg = GXSelectArgs.Select <GXTask>(c => c.Id, q => q.Start == DateTime.MinValue); arg.Joins.AddInnerJoin <GXTask, GXObject>(a => a.Object, b => b.Id); arg.Joins.AddInnerJoin <GXObject, GXDevice>(a => a.DeviceId, b => b.Id); //Don't get meters that are mapped to other readers. arg.Joins.AddLeftJoin <GXDevice, GXDeviceToReader>(a => a.Id, b => b.DeviceId); if (request.DeviceId != 0) { arg.Where.And <GXDevice>(q => q.Id == request.DeviceId); } if (!request.Listener) { arg.Where.And <GXDevice>(q => q.Dynamic == false); } arg.OrderBy.Add <GXTask>(q => q.Id); GXSelectArgs onProgress = GXSelectArgs.Select <GXObject>(c => c.DeviceId, q => q.Removed == DateTime.MinValue); onProgress.Joins.AddInnerJoin <GXTask, GXObject>(a => a.Object, b => b.Id); onProgress.Where.And <GXTask>(q => q.Start != DateTime.MinValue && q.End == DateTime.MinValue); arg.Where.And <GXObject>(q => !GXSql.Exists <GXObject, GXDevice>(a => a.DeviceId, b => b.Id, onProgress)); GXTask task = host.Connection.SingleOrDefault <GXTask>(arg); if (task != null) { //Get task device ID and creation time. arg = GXSelectArgs.SelectAll <GXTask>(q => q.Id == task.Id); arg.Columns.Add <GXObject>(); arg.Joins.AddInnerJoin <GXTask, GXObject>(a => a.Object, b => b.Id); arg.Joins.AddInnerJoin <GXObject, GXDevice>(a => a.DeviceId, b => b.Id); task = host.Connection.SingleOrDefault <GXTask>(arg); //Get all tasks that are created at the same time for same meter. arg = GXSelectArgs.SelectAll <GXTask>(q => q.Generation == task.Generation); arg.Where.And <GXDevice>(q => q.Id == task.Object.DeviceId); arg.Where.And <GXObject>(q => q.Removed == DateTime.MinValue); arg.Columns.Add <GXObject>(); arg.Columns.Add <GXDevice>(); arg.Columns.Add <GXAttribute>(); arg.Joins.AddInnerJoin <GXTask, GXObject>(a => a.Object, b => b.Id); arg.Joins.AddInnerJoin <GXObject, GXDevice>(a => a.DeviceId, b => b.Id); arg.Joins.AddInnerJoin <GXObject, GXAttribute>(a => a.Id, b => b.ObjectId); ret.Tasks = host.Connection.Select <GXTask>(arg).ToArray(); DateTime now = DateTime.Now; foreach (GXTask it in ret.Tasks) { it.Start = now; host.Connection.Update(GXUpdateArgs.Update(it, q => q.Start)); } host.SetChange(TargetType.Tasks, DateTime.Now); } } return(ret); }
public void CultureUpdateTest() { GXLanguage item = new GXLanguage() { Id = "fi", EnglishName = "Finland", }; GXUpdateArgs args = GXUpdateArgs.Update(item); args.Exclude <GXLanguage>(e => e.EnglishName); Assert.AreEqual("Mikko", args.ToString()); }
public void UpdateUserTest() { GXUser user = new GXUser(); user.Id = "1"; user.UserName = "******"; GXUpdateArgs arg2 = GXUpdateArgs.Update(user); arg2.Exclude <GXUser>(q => new { q.PasswordHash, q.SecurityStamp, q.CreationTime }); Assert.AreEqual("UPDATE [GXUser] SET [UserName] = 'Gurux', [NormalizedUserName] = NULL, [Email] = NULL, [NormalizedEmail] = NULL, [EmailConfirmed] = 0, [ConcurrencyStamp] = NULL, [PhoneNumber] = NULL, [PhoneNumberConfirmed] = 0, [TwoFactorEnabled] = 0, [LockoutEnd] = '00010101 00:00:00 +00:00', [LockoutEnabled] = 0, [AccessFailedCount] = 0, [Updated] = '00010101 00:00:00', [Detected] = '00010101 00:00:00', [Removed] = '00010101 00:00:00', [DateOfBirth] = NULL WHERE [ID] = '1'", arg2.ToString()); }
public void EpochTimeFormatTest() { TestClass t = new TestClass(); t.Time = DateTime.SpecifyKind(new DateTime(2014, 1, 2), DateTimeKind.Utc); t.Id = 1; GXUpdateArgs args = GXUpdateArgs.Update(t); args.Settings.UseEpochTimeFormat = true; args.Add <TestClass>(t, x => x.Time); Assert.AreEqual("UPDATE TestClass SET `Time` = 1388620800 WHERE `ID` = 1", args.ToString()); }
public void UpdateTest() { string format = "yyyy-MM-dd HH:mm:ss"; DateTime dt = DateTime.ParseExact("2014-01-02 00:00:00", format, CultureInfo.CurrentCulture); TestClass t = new TestClass(); t.Id = 2; t.Time = DateTime.SpecifyKind(new DateTime(2014, 1, 2), DateTimeKind.Utc); GXUpdateArgs args = GXUpdateArgs.Update(t, x => new { x.Id, x.Guid, x.Time }); Assert.AreEqual("UPDATE TestClass SET `ID` = 2, `Guid` = '00000000000000000000000000000000', `Time` = '" + dt.ToString(format) + "' WHERE `ID` = 2", args.ToString()); }
public void ExcludeUpdateTest() { string format = "yyyy-MM-dd HH:mm:ss"; DateTime dt = DateTime.ParseExact("2014-01-02 00:00:00", format, CultureInfo.CurrentCulture); TestClass t = new TestClass(); t.Id = 2; t.Time = DateTime.SpecifyKind(new DateTime(2014, 1, 2), DateTimeKind.Utc); GXUpdateArgs args = GXUpdateArgs.Update(t, x => new { x.Id, x.Guid, x.Time }); args.Exclude <TestClass>(x => new { x.Text, x.Text2, x.Text3, x.Text4, x.BooleanTest, x.IntTest, x.DoubleTest, x.FloatTest, x.Span, x.Object, x.Status }); Assert.AreEqual("UPDATE TestClass SET `ID` = 2, `Guid` = '00000000000000000000000000000000', `Time` = '" + dt.ToString(format) + "' WHERE `ID` = 2", args.ToString()); }
public void RemoveUsersTest() { RemoveUser req = new RemoveUser(); req.Ids = new string[] { Guid.Empty.ToString(), Guid.Empty.ToString() }; //Set removed time for all the removed users. GXUser u = new GXUser() { Removed = DateTime.Now }; GXUpdateArgs update = GXUpdateArgs.Update(u, q => q.Removed); update.Where.And <GXUser>(q => req.Ids.Contains(q.Id)); Assert.AreEqual("", update.ToString()); }
public ActionResult <UpdateScheduleResponse> Post(UpdateSchedule request) { if (request.Schedules == null || request.Schedules.Count == 0) { return(BadRequest("Schedules array is empty.")); } UpdateScheduleResponse ret = new UpdateScheduleResponse(); List <UInt64> list = new List <UInt64>(); List <GXScheduleToAttribute> list2 = new List <GXScheduleToAttribute>(); foreach (var it in request.Schedules) { List <GXObject> tmp = it.Objects; it.Objects = null; if (it.Id == 0) { host.Connection.Insert(GXInsertArgs.Insert(it)); list.Add(it.Id); if (tmp != null) { foreach (GXObject o in tmp) { if (o.Attributes != null) { foreach (GXAttribute a in o.Attributes) { list2.Add(new GXScheduleToAttribute() { ScheduleId = it.Id, AttributeId = a.Id }); } } } host.Connection.Insert(GXInsertArgs.InsertRange(list2)); } } else { host.Connection.Update(GXUpdateArgs.Update(it)); } } ret.ScheduleIds = list.ToArray(); host.SetChange(TargetType.Schedule, DateTime.Now); return(ret); }
public ActionResult <DeviceDeleteResponse> Post(DeviceDelete request) { if (request.Ids != null) { GXSelectArgs arg = GXSelectArgs.Select <GXDevice>(a => a.Id, q => request.Ids.Contains(q.Id)); List <GXDevice> list = host.Connection.Select <GXDevice>(arg); if (list.Count != 0) { DateTime now = DateTime.Now; foreach (GXDevice it in list) { it.Removed = now; host.Connection.Update(GXUpdateArgs.Update(it, q => q.Removed)); } host.SetChange(TargetType.Device, now); } } return(new DeviceDeleteResponse()); }
public ActionResult <TaskReadyResponse> Post(TaskReady request) { if (request.Tasks != null) { foreach (GXTask it in request.Tasks) { it.End = DateTime.Now; if (it.TaskType == TaskType.Read) { host.Connection.Update(GXUpdateArgs.Update(it, q => new { q.End, q.Result, q.Data })); } else { host.Connection.Update(GXUpdateArgs.Update(it, q => new { q.End, q.Result })); } } host.Connection.Update(GXUpdateArgs.UpdateRange(request.Tasks, q => new { q.End, q.Result })); host.SetChange(TargetType.Tasks, DateTime.Now); } return(new TaskReadyResponse()); }
public ActionResult <UpdateDeviceResponse> Post(UpdateDevice request) { if (request.Device.TemplateId == 0) { return(BadRequest("Device template ID is unknown.")); } bool newDevice = request.Device.Id == 0; if (request.Device.DeviceSystemTitle != null) { request.Device.DeviceSystemTitle = request.Device.DeviceSystemTitle.Replace(" ", ""); } if (request.Device.AuthenticationKey != null) { request.Device.AuthenticationKey = request.Device.AuthenticationKey.Replace(" ", ""); } if (request.Device.BlockCipherKey != null) { request.Device.BlockCipherKey = request.Device.BlockCipherKey.Replace(" ", ""); } if (newDevice) { DateTime now = DateTime.Now; request.Device.Generation = now; //Add new DC. List <GXObject> tmp = request.Device.Objects; request.Device.Objects = null; request.Device.Generation = now; host.Connection.Insert(GXInsertArgs.Insert(request.Device)); //Add default objects. GXSelectArgs arg = GXSelectArgs.SelectAll <GXObjectTemplate>(q => q.DeviceTemplateId == request.Device.TemplateId && q.Removed == DateTime.MinValue); arg.Columns.Add <GXAttributeTemplate>(); arg.Joins.AddLeftJoin <GXObjectTemplate, GXAttributeTemplate>(o => o.Id, a => a.ObjectTemplateId); arg.Where.And <GXAttributeTemplate>(q => q.Removed == DateTime.MinValue); List <GXObjectTemplate> l = host.Connection.Select <GXObjectTemplate>(arg); foreach (GXObjectTemplate it in l) { GXObject obj = new GXObject() { TemplateId = it.Id, Generation = now, DeviceId = request.Device.Id, ObjectType = it.ObjectType, Name = it.Name, LogicalName = it.LogicalName, ShortName = it.ShortName, }; host.Connection.Insert(GXInsertArgs.Insert(obj)); foreach (GXAttributeTemplate ait in it.Attributes) { GXAttribute a = new GXAttribute(); a.ObjectId = obj.Id; a.Index = ait.Index; a.TemplateId = ait.Id; a.AccessLevel = ait.AccessLevel; a.DataType = ait.DataType; a.UIDataType = ait.UIDataType; a.Generation = now; a.ExpirationTime = ait.ExpirationTime; obj.Attributes.Add(a); } ; host.Connection.Insert(GXInsertArgs.InsertRange(obj.Attributes)); } host.SetChange(TargetType.ObjectTemplate, DateTime.Now); } else { request.Device.Updated = DateTime.Now; host.Connection.Update(GXUpdateArgs.Update(request.Device)); host.SetChange(TargetType.DeviceTemplate, DateTime.Now); } return(new UpdateDeviceResponse() { DeviceId = request.Device.Id }); }
public ActionResult <UpdateDeviceTemplateResponse> Post(UpdateDeviceTemplate request) { GXDeviceTemplate dev = request.Device; if (dev == null) { return(BadRequest("Device is null.")); } bool newDevice = dev.Id == 0; if (dev.DeviceSystemTitle != null) { dev.DeviceSystemTitle = dev.DeviceSystemTitle.Replace(" ", ""); } if (dev.AuthenticationKey != null) { dev.AuthenticationKey = dev.AuthenticationKey.Replace(" ", ""); } if (dev.BlockCipherKey != null) { dev.BlockCipherKey = dev.BlockCipherKey.Replace(" ", ""); } dev.Generation = DateTime.Now; if (newDevice) { //Add new DC. List <GXObjectTemplate> tmp = dev.Objects; dev.Objects = null; DateTime now = DateTime.Now; dev.Generation = now; host.Connection.Insert(GXInsertArgs.Insert(dev)); if (tmp != null) { //Add default objects. foreach (GXObjectTemplate it in tmp) { List <GXAttributeTemplate> tmp2 = it.Attributes; it.Attributes = null; it.DeviceTemplateId = dev.Id; it.Generation = now; host.Connection.Insert(GXInsertArgs.Insert(it)); if (it.Attributes != tmp2) { foreach (GXAttributeTemplate a in tmp2) { a.Generation = now; a.ObjectTemplateId = it.Id; } host.Connection.Insert(GXInsertArgs.InsertRange(tmp2)); } } } host.SetChange(TargetType.Object, DateTime.Now); } else { host.Connection.Update(GXUpdateArgs.Update(dev)); host.SetChange(TargetType.DeviceTemplate, DateTime.Now); } return(new UpdateDeviceTemplateResponse() { DeviceId = dev.Id }); }