902 lines
37 KiB
C#
902 lines
37 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using Xylem.Common.Cryptology.Security;
|
|
using Xylem.Common.Logic.ProductionOrderCore.OrderData;
|
|
using Xylem.Common.Logic.ServiceCore;
|
|
using Xylem.ServiceFwUpdate.Common.FwUpdateDb;
|
|
using Xylem.ServiceFwUpdate.Common.FwUpdateSafe;
|
|
using Xylem.ServiceFwUpdate.Common.FwUpdateSafe.MeterRecovery;
|
|
|
|
namespace Xylem.Common.Service.MeterProcessState.Controllers
|
|
{
|
|
public class FwUpdateController : ApiController
|
|
{
|
|
public static class GlobalConfig
|
|
{
|
|
public static Lazy<String> connectionString = new Lazy<String>(()
|
|
=>
|
|
System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString
|
|
);
|
|
}
|
|
//
|
|
[Route("GetUserList"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetUserList(String LogInName = "", Int32? Id = null)
|
|
{
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.OK, getUserList(LogInName, Id));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
|
|
}
|
|
private System.Collections.Generic.List<UserInformation> getUserList(String LogInName = "", Int32? Id = null)
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" select UserId, FullName,LogInName, Domain,HardwareId,PasswordHash,RegisterDate,ValidDate,AccountActive from FWUpdateUser ");
|
|
if (Id.HasValue)
|
|
{
|
|
sb.Append($" where UserId={Id.Value} ");
|
|
|
|
}
|
|
else if (!string.IsNullOrEmpty(LogInName))
|
|
{
|
|
sb.Append($" where LogInName like '%{LogInName}%' ");
|
|
}
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
ret.Add(
|
|
new UserInformation((Int32)row["UserId"])
|
|
{
|
|
FullName = row["FullName"] == DBNull.Value ? "" : (String)row["FullName"],
|
|
LogInName = (String)row["LogInName"],
|
|
Domain = (String)row["Domain"],
|
|
HardwareId = row["HardwareId"] == DBNull.Value ? "" : (String)row["HardwareId"],
|
|
PasswordHash = row["PasswordHash"] == DBNull.Value ? "" : (String)row["PasswordHash"],
|
|
RegisterDate = (DateTimeOffset)row["RegisterDate"],
|
|
ValidDate = (DateTimeOffset)row["ValidDate"],
|
|
AccountActive = (Boolean)row["AccountActive"],
|
|
});
|
|
|
|
}
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
[Route("GetAllBuilderOperator"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetAllBuilderOperator()
|
|
{
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" select UserId, FullName,LogInName, Domain,HardwareId,AccountActive from FWUpdateBuilderUser ");
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
ret.Add(
|
|
new UserInformation((Int32)row["UserId"])
|
|
{
|
|
FullName = row["FullName"] == DBNull.Value ? "" : (String)row["FullName"],
|
|
LogInName = (String)row["LogInName"],
|
|
Domain = (String)row["Domain"],
|
|
HardwareId = row["HardwareId"] == DBNull.Value ? "" : (String)row["HardwareId"],
|
|
AccountActive = (Boolean)row["AccountActive"],
|
|
});
|
|
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.OK, ret);
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[Route("RefreshUsersHardwareId"), HttpGet]
|
|
public async Task<HttpResponseMessage> RefreshUsersHardwareId(Int32 id, String HardwareId)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" Update FWUpdateUser set HardwareId = '{HardwareId}' where UserId = {id} ");
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, true);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
[Route("RefreshUsersPasswordHash"), HttpGet]
|
|
public async Task<HttpResponseMessage> RefreshUsersPasswordHash(Int32 id, String PasswordHash)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" Update FWUpdateUser set PasswordHash = '{PasswordHash}' where UserId = {id} ");
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, true);
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
|
|
|
|
|
|
});
|
|
}
|
|
|
|
[Route("EditUserValidity"), HttpGet]
|
|
public async Task<HttpResponseMessage> EditUserValidity(Int32 id, DateTimeOffset? ValidDate = null, Boolean? AccountActive = null)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
if (ValidDate.HasValue || AccountActive.HasValue)
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" Update FWUpdateUser set ");
|
|
|
|
if (ValidDate.HasValue)
|
|
{
|
|
sb.AppendLine($" ValidDate = '{ValidDate.Value.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss +00:00")}' ");
|
|
|
|
}
|
|
if (AccountActive.HasValue)
|
|
{
|
|
if (ValidDate.HasValue)
|
|
{
|
|
sb.AppendLine($" , ");
|
|
}
|
|
if (AccountActive.Value)
|
|
{
|
|
sb.AppendLine($" AccountActive = 1 ");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine($" AccountActive = 0 ");
|
|
}
|
|
}
|
|
|
|
sb.AppendLine($" where UserId = {id} ");
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, true);
|
|
}
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, "no change");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
[Route("GetFileContent"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetFileContent(Int32 FileId)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" SELECT TOP (1) [FileContent] FROM [Auftrag].[dbo].[File] where FileId = {FileId}");
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, dbResult.Rows[0][0]);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
[Route("GetFWUpdateSafeContainers"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetFWUpdateSafeContainers(Int32 UserId)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<FwUpdateSafeDb>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" select * from [dbo].[FWUpdateSafeContainer] ");
|
|
sb.Append($" where FWUpdateSafeContainer_UserId ={UserId} ");
|
|
//FWUpdateSafeContainer_ValidDate >= CURRENT_TIMESTAMP and
|
|
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
ret.Add(
|
|
new FwUpdateSafeDb()
|
|
{
|
|
ContainerId = (Int32)row["FWUpdateSafeContainer_ID"],
|
|
Name = (String)row["FWUpdateSafeContainer_Name"],
|
|
UserId = (Int32)row["FWUpdateSafeContainer_UserId"],
|
|
FilePartId = (Int32)row["FWUpdateSafeContainer_FileId"],
|
|
ValidDate = (DateTime)row["FWUpdateSafeContainer_ValidDate"]
|
|
});
|
|
|
|
}
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.OK, ret);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
[Route("AddPcbIdsToSafe"), HttpPost]
|
|
public async Task<HttpResponseMessage> AddPcbIdsToSafe(Int32 ContainerDbId, [FromBody] List<Int32> PcbIds)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
StringBuilder sb = null;
|
|
|
|
|
|
foreach (var item in PcbIds)
|
|
{
|
|
if (sb == null)
|
|
{
|
|
sb = new StringBuilder();
|
|
sb.AppendLine($" insert into FWUpdateSafeContainerPcbIds ");
|
|
}
|
|
else
|
|
{
|
|
sb.AppendLine($" UNION ");
|
|
}
|
|
sb.AppendLine($" SELECT {ContainerDbId}, {item}");
|
|
}
|
|
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, true);
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
[Route("GetPcbIdsFromSafe"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetPcbIdsFromSafe(Int32 ContainerDbId)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<Int32>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" select [FWUpdateSafeContainer_PcbId] from [dbo].[FWUpdateSafeContainerPcbIds] ");
|
|
sb.Append($" where [FWUpdateSafeContainer_ID] = {ContainerDbId} ");
|
|
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
ret.Add((Int32)row["FWUpdateSafeContainer_PcbId"]);
|
|
|
|
}
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.OK, ret);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="PUserId"></param>
|
|
/// <param name="PFileName"></param>
|
|
/// <param name="PValidDate"></param>
|
|
/// <param name="PBuilderOperatorName"></param>
|
|
/// <returns></returns>
|
|
[Route("PostFWUpdateSafeOdd"), HttpPost]
|
|
|
|
public async Task<HttpResponseMessage> PostFWUpdateSafeOdd(Int32 PUserId, String PFileName, DateTime PValidDate, String PBuilderOperatorName = "none")
|
|
{
|
|
if (PFileName.Length > 150)
|
|
{
|
|
throw new ApplicationException("File name is to long");
|
|
}
|
|
var inpStrem = HttpContext.Current.Request.GetBufferlessInputStream(true);
|
|
var inpHeaders = Request.Content.Headers;
|
|
return await Task.Run(async () =>
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var parameters = new List<System.Data.SqlClient.SqlParameter>();
|
|
var FileName = new System.Data.SqlClient.SqlParameter("@FileName", SqlDbType.NVarChar, 150);
|
|
var UserId = new System.Data.SqlClient.SqlParameter("@UserId", SqlDbType.Int);
|
|
var ValidDate = new System.Data.SqlClient.SqlParameter("@ValidDate", SqlDbType.Date);
|
|
|
|
|
|
FileName.Value = PFileName;
|
|
UserId.Value = PUserId;
|
|
ValidDate.Value = PValidDate;
|
|
|
|
|
|
parameters.Add(ValidDate);
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" Declare @FileID as int ");
|
|
sb.AppendLine($" Declare @FileName as NVarChar(150) ");
|
|
sb.AppendLine($" Declare @UserId as int ");
|
|
sb.AppendLine($" set @FileName = '{PFileName}' ");
|
|
sb.AppendLine($" set @UserId = {PUserId} ");
|
|
|
|
|
|
//sb.AppendLine($" INSERT INTO [dbo].[File] ([FileContent], [FileName]) VALUES (@FileContent, @FileName) ;");
|
|
//sb.AppendLine($" SELECT @FileID = @@IDENTITY; ");
|
|
sb.AppendLine($" INSERT INTO [dbo].[FWUpdateSafeContainer] ([FWUpdateSafeContainer_Name],[FWUpdateSafeContainer_UserId],[FWUpdateSafeContainer_FileId],[FWUpdateSafeContainer_ValidDate])");
|
|
sb.AppendLine($" VALUES(@FileName,@UserId,0,@ValidDate); ");
|
|
sb.AppendLine($" select @@IDENTITY as ContainerID, @FileID as FilePartId ");
|
|
|
|
|
|
|
|
var retDT = dataacces.ExecuteQuery(sb.ToString(), parameters);
|
|
var newID = Convert.ToInt32(retDT.Rows[0][0].ToString());
|
|
|
|
var fileuploadPath = $"C:\\\\Temp\\{newID}\\";
|
|
Directory.CreateDirectory(fileuploadPath);
|
|
var provider = new MultipartFormDataStreamProvider(fileuploadPath);
|
|
var content = new StreamContent(inpStrem);
|
|
foreach (var header in inpHeaders)
|
|
{
|
|
content.Headers.TryAddWithoutValidation(header.Key, header.Value);
|
|
}
|
|
|
|
await content.ReadAsMultipartAsync(provider);
|
|
|
|
String uploadingFileName = provider.FileData.Select(x => x.LocalFileName).FirstOrDefault();
|
|
String originalFileName = string.Concat(fileuploadPath, "\\" + (provider.Contents[0].Headers.ContentDisposition.FileName).Trim(new Char[] { '"' }));
|
|
|
|
if (File.Exists(originalFileName))
|
|
{
|
|
File.Delete(originalFileName);
|
|
}
|
|
|
|
File.Move(uploadingFileName, originalFileName);
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, newID);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
[Route("GetFWUpdateSafeContainerContent"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetFWUpdateSafeContainerContent(Int32 SafeContainerId)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<FwUpdateSafeDb>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine($" select * from [dbo].[FWUpdateSafeContainer] ");
|
|
sb.Append($" where FWUpdateSafeContainer_ValidDate >= CURRENT_TIMESTAMP and FWUpdateSafeContainer_ID ={SafeContainerId} ");
|
|
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
var content = new Byte[] { };
|
|
var filePath = $"C:\\\\Temp\\{(Int32)row["FWUpdateSafeContainer_ID"]}\\{(String)row["FWUpdateSafeContainer_Name"]}";
|
|
if (File.Exists(filePath))
|
|
{
|
|
content = File.ReadAllBytes(filePath);
|
|
}
|
|
else
|
|
{
|
|
content = ASCIIEncoding.ASCII.GetBytes("File Not Found");
|
|
}
|
|
ret.Add(
|
|
new FwUpdateSafeDb()
|
|
{
|
|
ContainerId = (Int32)row["FWUpdateSafeContainer_ID"],
|
|
Name = (String)row["FWUpdateSafeContainer_Name"],
|
|
UserId = (Int32)row["FWUpdateSafeContainer_UserId"],
|
|
FilePartId = (Int32)row["FWUpdateSafeContainer_FileId"],
|
|
ValidDate = (DateTime)row["FWUpdateSafeContainer_ValidDate"],
|
|
Content = content
|
|
});
|
|
|
|
}
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.OK, ret);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
[Route("PostFWUpdateSafe"), HttpPost]
|
|
public async Task<HttpResponseMessage> PostFWUpdateSafe([FromBody] FwUpdateSafeDb fwc)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
|
|
|
|
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var parameters = new List<System.Data.SqlClient.SqlParameter>();
|
|
var FileContent = new System.Data.SqlClient.SqlParameter("FileContent", SqlDbType.VarBinary);
|
|
// var FileName = new System.Data.SqlClient.SqlParameter("FileName", SqlDbType.NVarChar, 50);
|
|
var UserId = new System.Data.SqlClient.SqlParameter("UserId", SqlDbType.Int);
|
|
var ValidDate = new System.Data.SqlClient.SqlParameter("ValidDate", SqlDbType.Date);
|
|
|
|
|
|
// FileName.Value = fwc.Name;
|
|
FileContent.Value = fwc.Content;
|
|
UserId.Value = fwc.UserId;
|
|
ValidDate.Value = fwc.ValidDate.Date;
|
|
|
|
//parameters.Add(FileName);
|
|
parameters.Add(UserId);
|
|
parameters.Add(FileContent);
|
|
parameters.Add(ValidDate);
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" Declare @FileID as int ");
|
|
|
|
|
|
sb.AppendLine($" INSERT INTO [dbo].[File] ([FileContent], [FileName]) VALUES (@FileContent, 't') ;");
|
|
sb.AppendLine($" SELECT @FileID = @@IDENTITY; ");
|
|
sb.AppendLine($" INSERT INTO [dbo].[FWUpdateSafeContainer] ([FWUpdateSafeContainer_Name],[FWUpdateSafeContainer_UserId],[FWUpdateSafeContainer_FileId],[FWUpdateSafeContainer_ValidDate])");
|
|
sb.AppendLine($" VALUES('t',@UserId,@FileID,@ValidDate); ");
|
|
sb.AppendLine($" select @@IDENTITY as ContainerID, @FileID as FilePartId ");
|
|
|
|
|
|
|
|
var retDT = dataacces.ExecuteQuery(sb.ToString(), parameters);
|
|
var newID = Convert.ToInt32(retDT.Rows[0][0].ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, newID);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual Byte[] GetFileBytes(HttpPostedFile uploadedFile)
|
|
{
|
|
var bytes = new Byte[uploadedFile.ContentLength];
|
|
uploadedFile.InputStream.Read(bytes, 0, uploadedFile.ContentLength);
|
|
return bytes;
|
|
}
|
|
|
|
[Route("PostFWUpdateReportFile"), HttpPost]
|
|
public async Task<HttpResponseMessage> PostFWUpdateReportFiles([FromBody] FwUpdateReportDb FileContent)
|
|
{
|
|
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var parameters = new List<System.Data.SqlClient.SqlParameter>();
|
|
var DbFileContent = new System.Data.SqlClient.SqlParameter("@FileContent", SqlDbType.VarBinary);
|
|
var DbFileName = new System.Data.SqlClient.SqlParameter("@FileName", SqlDbType.NVarChar,150 );
|
|
|
|
|
|
|
|
DbFileName.Value = FileContent.FileName;
|
|
DbFileContent.Value = UTF32Encoding.UTF32.GetBytes(JsonConvert.SerializeObject(FileContent));
|
|
|
|
|
|
|
|
parameters.Add(DbFileName);
|
|
|
|
parameters.Add(DbFileContent);
|
|
|
|
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" Declare @FileID as int ");
|
|
|
|
|
|
sb.AppendLine($" INSERT INTO [dbo].[File] ([FileContent], [FileName]) VALUES (@FileContent, @FileName) ;");
|
|
sb.AppendLine($" SELECT @FileID = @@IDENTITY; ");
|
|
sb.AppendLine($" INSERT INTO [dbo].[FWUpdateReports] ([FWUpdateReport_Name],[FWUpdateReport_UserId], [FWUpdateReport_FileId],[FWUpdateReport_Date],[FWUpdateReport_PcbId], [FWUpdateReport_OrderNr], [FWUpdateReport_OrderPos]) ");
|
|
sb.AppendLine($" VALUES(@FileName,{FileContent.UserId},@FileID,CURRENT_TIMESTAMP, {FileContent.PcbId}, {FileContent.OrderNr}, {FileContent.OrderPos} ); ");
|
|
sb.AppendLine($" select @@IDENTITY as FWUpdateReportsId, @FileID as FilePartId ");
|
|
|
|
|
|
|
|
var retDT = dataacces.ExecuteQuery(sb.ToString(), parameters);
|
|
var newID = Convert.ToInt32(retDT.Rows[0][0].ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, newID);
|
|
|
|
|
|
}
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, false);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
[Route("GetFWUpdateReportFile"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetFWUpdateReportFile(Int32? PcbId = null, Int32? UserId = null, Int32? OrderNr = null, Boolean WithContent = true)
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<FwUpdateReportDb>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
sb.AppendLine($" select FWUpdateReport_ID, FWUpdateReport_Name, FWUpdateReport_UserId, FWUpdateReport_FileId, FWUpdateReport_Date, FWUpdateReport_PcbId, FWUpdateReport_OrderNr ");
|
|
if (WithContent)
|
|
{
|
|
sb.Append(", FileContent ");
|
|
}
|
|
sb.AppendLine($" from [dbo].[FWUpdateReports] r ");
|
|
if (WithContent)
|
|
{
|
|
sb.AppendLine($" inner join [dbo].[File] f on r.FWUpdateReport_FileId = f.FileId ");
|
|
}
|
|
sb.AppendLine($" where FWUpdateReport_ID > 0 ");
|
|
|
|
if (PcbId.HasValue)
|
|
{
|
|
sb.AppendLine($" and FWUpdateReport_PcbId = {PcbId.Value}");
|
|
}
|
|
if (UserId.HasValue)
|
|
{
|
|
sb.AppendLine($" and FWUpdateReport_UserId = {UserId.Value}");
|
|
}
|
|
if (OrderNr.HasValue)
|
|
{
|
|
sb.AppendLine($" and FWUpdateReport_OrderNr = {OrderNr.Value}");
|
|
}
|
|
|
|
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
foreach (DataRow row in dbResult.Rows)
|
|
{
|
|
var addOBJ = JsonConvert.DeserializeObject<FwUpdateReportDb>(UTF32Encoding.UTF32.GetString((Byte[])row["FileContent"]));
|
|
addOBJ.FileId = (Int32)row["FWUpdateReport_ID"];
|
|
addOBJ.Date = (DateTime)row["FWUpdateReport_Date"];
|
|
ret.Add(addOBJ);
|
|
//help to download all file to local store
|
|
//File.WriteAllText($"E:\\bat\\report\\{addOBJ.PcbId}_{addOBJ.FileId}.txt", addOBJ.Content);
|
|
}
|
|
}
|
|
return Request.CreateResponse(HttpStatusCode.OK, ret);
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
[Route("GetRecoveryFile"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetRecoveryFile(String pcbId, Int32? radioFrq, String region, String flexnetVersion)
|
|
{
|
|
|
|
var file = new RecoverySettings { PcbId = pcbId, RadioFrequencyMhz = radioFrq, Region = region, Release = flexnetVersion,
|
|
RecoveryRegisters = new List<RecoveryRegisterItem>
|
|
{
|
|
new RecoveryRegisterItem { RegisterIdent = "GENESISFLOW_SealDisplay", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0x00 },
|
|
IsCritical = true, ReadBackValue = new Byte[] { 0x00, 0x00, 0x00, 0x00 } }
|
|
}
|
|
};
|
|
|
|
|
|
var csdRet = OrderProgrammingParameters.GetProgrammingParameters(pcbId, GlobalConfig.connectionString.Value);
|
|
|
|
|
|
foreach (var item in csdRet.Where(w => w.Source == Logic.ProductionOrderCore.ProgrammingSource.Vako))
|
|
{
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem()
|
|
{
|
|
RegisterIdent = item.RegisterName,
|
|
WriteValue = item.RegisterValue,
|
|
ReadBackValue = null,
|
|
IsCritical = false,
|
|
Access = RegisterRecoveryAccess.SetAlways,
|
|
});
|
|
}
|
|
|
|
foreach (var item in csdRet.Where(w => w.Source == Logic.ProductionOrderCore.ProgrammingSource.Csd))
|
|
{
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem()
|
|
{
|
|
RegisterIdent = item.RegisterName,
|
|
WriteValue = item.RegisterValue,
|
|
ReadBackValue = null,
|
|
IsCritical = false,
|
|
Access = RegisterRecoveryAccess.SetAlways,
|
|
});
|
|
}
|
|
|
|
|
|
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem() { RegisterIdent = "GENESISFLOW_SealDisplay", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0x01 }, IsCritical = true });
|
|
|
|
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem() { RegisterIdent = "SENSUSRADIO_SystemState", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0x01 }, IsCritical = true, ReadBackValue = new Byte[] { 0x00, 0x00, 0x00, 0x0c } });
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem() { RegisterIdent = "SENSUSRADIO_MainAlarmMask", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0x00 } });
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem() { RegisterIdent = "SENSUSRADIO_ExtendedAlarmMask", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0x00 } });
|
|
|
|
file.RecoveryRegisters.Add(new RecoveryRegisterItem() { RegisterIdent = "SENSUSRADIO_SystemState", WriteValue = new Byte[] { 0x00, 0x00, 0x00, 0xFF }, IsCritical = true, ReadBackValue = new Byte[] { 0x00, 0x00, 0x00, 0x0c } });
|
|
|
|
|
|
|
|
var blackList = new List<String>()
|
|
{
|
|
"CUSTOMER_RebootCount",
|
|
"GENESISFLOW_ForwardArrow",
|
|
"SENSUSRADIO_FrequencyOffset",
|
|
"SENSUSRADIO_FrequencyIndicator",
|
|
"PERIODICLOG_AverageFlowPeriod",
|
|
"PERIODICLOG_PeriodicLogLifeTimeCounter",
|
|
"SENSUSRADIO_MetroRadioLifeTimeCounter"
|
|
|
|
};
|
|
|
|
|
|
var Critical = new List<String>()
|
|
{
|
|
"GENESISFLOW_SealDisplay",
|
|
"GENESISFLOW_MeterSize",
|
|
"GENESISFLOW_DisplayUnits",
|
|
"METROLOGYASST_FlowUnits",
|
|
"METROLOGYASST_FlowPoint",
|
|
"METROLOGYASST_PressurePresent",
|
|
"GENESISFLOW_DisplayPow10",
|
|
"POWERMON_WarnFromClamp",
|
|
"SYSTEM_UpgradePermissions",
|
|
"POWERMON_BatteryQuantity",
|
|
"GENESISFLOW_LedMode",
|
|
"GENESISFLOW_LowFlowMaxPeriod",
|
|
"GENESISFLOW_LowFlowThreshold",
|
|
"GENESISFLOW_MaxValidAmplitude",
|
|
"GENESISFLOW_MaxValidDeltaToF",
|
|
"GENESISFLOW_MaxValidToF",
|
|
"GENESISFLOW_MinValidAmplitude",
|
|
"GENESISFLOW_MinValidToF",
|
|
"GENESISFLOW_Timeout",
|
|
"CUSTOMER_Locale",
|
|
"SENSUSRADIO_EncryptionKey"
|
|
|
|
};
|
|
|
|
file.RecoveryRegisters.Where(d => Critical.Contains(d.RegisterIdent)).ToList().ForEach(f => { f.IsCritical = true; f.ReadBackValue = f.WriteValue; });
|
|
|
|
var onlyIfDefault = new List<String>()
|
|
{
|
|
"METROLOGYASST_PulseWeight",
|
|
"METROLOGYASST_PressureUnits",
|
|
"METROLOGYASST_TemperatureUnits",
|
|
"METROLOGYASST_PulseLength",
|
|
"METROLOGYASST_PulseMode",
|
|
"CUSTOMER_LeakTimeThreshold",
|
|
"CUSTOMER_ExcessFlowTimeThreshold",
|
|
"CUSTOMER_ReverseFlowTimeThreshold",
|
|
"CUSTOMER_TemperatureHighThreshold",
|
|
"CUSTOMER_TemperatureHighDelay",
|
|
"CUSTOMER_TemperatureLowThreshold",
|
|
"CUSTOMER_TemperatureLowDelay",
|
|
"CUSTOMER_PressureHighThreshold",
|
|
"CUSTOMER_PressureHighDelay",
|
|
"CUSTOMER_PressureLowThreshold",
|
|
"CUSTOMER_PressureLowDelay",
|
|
"CUSTOMER_LeakFlowThreshold",
|
|
"METROLOGYASST_PressureOffset",
|
|
|
|
};
|
|
|
|
file.RecoveryRegisters.Where(d => onlyIfDefault.Contains(d.RegisterIdent)).ToList().ForEach(f => f.Access = RegisterRecoveryAccess.SetIfDefault);
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, file);
|
|
}
|
|
|
|
[Route("AddNewUser"), HttpGet]
|
|
public async Task<HttpResponseMessage> AddNewUser(String FullName, String LogInName, String Domain, String HardwareId = null, String PasswordHash = null)
|
|
{
|
|
|
|
return await Task.Run(() =>
|
|
{
|
|
try
|
|
{
|
|
var ret = new System.Collections.Generic.List<UserInformation>();
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var sb = new StringBuilder();
|
|
sb.AppendLine($" insert into FWUpdateUser ");
|
|
sb.AppendLine($" select ");
|
|
sb.AppendLine($" '{FullName}', ");
|
|
sb.AppendLine($" '{LogInName}', ");
|
|
sb.AppendLine($" '{Domain}', ");
|
|
|
|
sb.AppendLine(string.IsNullOrEmpty(HardwareId) ? "null , " : $"'{HardwareId}' ,");
|
|
sb.AppendLine(string.IsNullOrEmpty(PasswordHash) ? "null , " : $"'{PasswordHash}' ,");
|
|
|
|
sb.AppendLine($" '{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss +01:00")}', ");
|
|
sb.AppendLine($" '{DateTimeOffset.UtcNow.ToString("yyyy-MM-dd HH:mm:ss +01:00")}', ");
|
|
sb.AppendLine($" 0 ");
|
|
|
|
sb.AppendLine($" select @@IDENTITY ");
|
|
|
|
var dbResult = dataacces.ExecuteQuery(sb.ToString());
|
|
|
|
|
|
|
|
|
|
var newID = Convert.ToInt32(dbResult.Rows[0][0].ToString());
|
|
return Request.CreateResponse(HttpStatusCode.OK, newID);
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex);
|
|
}
|
|
|
|
|
|
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|