149 lines
5.9 KiB
C#
149 lines
5.9 KiB
C#
using ByteArrayStyle;
|
|
using Logic.ProductionToProductMapper.Cordonel;
|
|
using Service.Core;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Http;
|
|
using Xylem.Common.Hardware.WaterMeter.Genesis.DataPackages;
|
|
|
|
namespace Service.MeterProcessState.Controllers
|
|
{
|
|
[RoutePrefix("api/RegisterWatch")]
|
|
public class RegisterWatchController : ApiController
|
|
{
|
|
|
|
|
|
public static class GlobalConfig
|
|
{
|
|
public static Lazy<string> connectionString = new Lazy<string>(()
|
|
=>
|
|
System.Configuration.ConfigurationManager.ConnectionStrings["default"].ConnectionString
|
|
);
|
|
}
|
|
|
|
|
|
|
|
[Route("GetRegisterHistory"), HttpGet]
|
|
public async Task<HttpResponseMessage> GetRegisterHistory(string PcbID, string Address)
|
|
{
|
|
try
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
sb.AppendLine(" declare @PcbID as nvarchar(50) ");
|
|
sb.AppendLine(" declare @Adress as nvarchar(50) ");
|
|
|
|
sb.AppendLine($" set @PcbID = '{PcbID}' ");
|
|
if (!string.IsNullOrEmpty(Address))
|
|
{
|
|
sb.AppendLine($" set @Adress = '{Address}' ");
|
|
}
|
|
|
|
sb.AppendLine(" SELECT TOP (1000) [ID] ");
|
|
sb.AppendLine(" ,[PcbId] ");
|
|
sb.AppendLine(" ,[Address] ");
|
|
sb.AppendLine(" ,[Value] ");
|
|
sb.AppendLine(" ,[date] ");
|
|
sb.AppendLine(" FROM [Auftrag].[dbo].[GenesisMeterRegisterHistory] ");
|
|
sb.AppendLine(" where PcbId = @PcbID ");
|
|
sb.AppendLine(" and (@Adress is null or [Address] = @Adress ) ");
|
|
sb.AppendLine(" order by [Address] desc, [date] desc ");
|
|
|
|
var ret = new List<RegisterHistory>();
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() =>
|
|
{
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var dt = dataacces.ExecuteQuery(sb.ToString());
|
|
foreach (DataRow row in dt.Rows)
|
|
{
|
|
var adressP =ByteStyler.Parse(row["Address"].ToString(), false);
|
|
var value = new byte[] { };
|
|
if (row["Value"].ToString() != "NULL")
|
|
{
|
|
|
|
|
|
String[] arr = row["Value"].ToString().Split('-');
|
|
Byte[] array = new Byte[arr.Length];
|
|
for (Int32 i = arr.Length - 1; i >= 0; i--) array[i] = Convert.ToByte(arr[i], 16);
|
|
array = array.Reverse().ToArray();
|
|
|
|
|
|
value = array;
|
|
}
|
|
else
|
|
{
|
|
value = null;
|
|
}
|
|
ret.Add(new RegisterHistory((string)row["PcbId"], adressP, value, (DateTime)row["date"]));
|
|
}
|
|
}
|
|
return ret;
|
|
}));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
[Route("RegisterWrite"), HttpPost]
|
|
public async Task<HttpResponseMessage> RegisterWrite(string PcbID, string Address, string value)
|
|
{
|
|
try
|
|
{
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
var dt = dataacces.ExecuteQuery($"INSERT INTO [dbo].[GenesisMeterRegisterHistory] ([PcbId] ,[Address] ,[Value] ,[date]) VALUES( '{ PcbID}','{Address }' ,'{value }', CURRENT_TIMESTAMP)");
|
|
}
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => { return "store"; }));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex);
|
|
}
|
|
}
|
|
|
|
|
|
[Route("FullRegisters"), HttpPost]
|
|
public async Task<HttpResponseMessage> FullRegisters(string PcbID, [FromBody] List<RegisterToDb> MeterRegisterDic)
|
|
{
|
|
try
|
|
{
|
|
using (var dataacces = new SqlDataAccess(GlobalConfig.connectionString.Value))
|
|
{
|
|
foreach (var store in MeterRegisterDic)
|
|
{
|
|
var valueText = "";
|
|
var adressText = BitConverter.ToString(store.Adresse);
|
|
|
|
if (store.Value != null)
|
|
{
|
|
valueText = BitConverter.ToString(store.Value);
|
|
}
|
|
|
|
var dt = dataacces.ExecuteQuery($"INSERT INTO [dbo].[GenesisMeterRegisterHistory] ([PcbId] ,[Address] ,[Value] ,[date]) VALUES( '{ PcbID}','{adressText }' ,'{valueText }', CURRENT_TIMESTAMP)");
|
|
}
|
|
|
|
}
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, await Task.Run(() => { return "store"; }));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex);
|
|
}
|
|
}
|
|
|
|
}
|
|
} |