2025-03-17 15:03:24 +00:00
using Newtonsoft.Json ;
2021-10-01 09:09:20 +00:00
using Newtonsoft.Json.Converters ;
using Newtonsoft.Json.Linq ;
using System ;
using System.Collections.Generic ;
using System.Globalization ;
using System.IO ;
using System.Linq ;
using System.Net ;
using System.Net.Http ;
using System.Text ;
using System.Threading.Tasks ;
using System.Web.Http ;
using Xylem.Common.CommonCore.Configuration ;
using Xylem.Common.CommonCore.Exceptions.Db ;
using Xylem.Common.CommonCore.Exceptions.WaterMeter ;
2022-08-24 09:42:44 +00:00
using Xylem.Common.CommonCore.Exceptions.Web ;
2023-03-06 14:10:37 +00:00
using Xylem.Common.Hardware.WaterMeter.Genesis.GenesisPwd ;
2022-09-23 11:52:57 +00:00
using Xylem.Common.Logic.ServiceCore ;
2021-10-01 09:09:20 +00:00
using Xylem.Common.Logic.SoftwareAccessHelper ;
namespace Xylem.Common.Service.MeterProcessState.Controllers
{
[RoutePrefix("api/Password")]
public class PasswordController : ApiController
{
public static class GlobalConfig
{
public static Lazy < String > connectionString = new Lazy < String > ( ( )
= >
System . Configuration . ConfigurationManager . ConnectionStrings [ "default" ] . ConnectionString
) ;
}
[Route("SetPassword"), HttpGet]
2022-10-19 10:58:08 +00:00
public async Task < HttpResponseMessage > SetPassword ( String PcbId , String password , Boolean keepOldPw = false )
2021-10-01 09:09:20 +00:00
{
try
{
2023-01-24 09:08:37 +00:00
if ( keepOldPw = = true )
{
throw new ApplicationException ( "Not Suppoted keepOldPw" ) ;
}
2021-10-01 09:09:20 +00:00
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
StringBuilder sb = new StringBuilder ( ) ;
2026-01-13 10:11:54 +00:00
sb . AppendLine ( $"update auftrag.dbo.Genesis_PcbPassword set [password] ='{password}' where PcbId='{PcbId}' " ) ;
2024-02-26 09:25:31 +00:00
2021-10-01 09:09:20 +00:00
var dt = dataAccess . ExecuteQuery ( sb . ToString ( ) ) ;
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return "store" ; } ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex ) ;
}
}
public class PwProcessorId
{
2022-10-19 10:58:08 +00:00
public PwProcessorId ( String password , UInt64 ? processorId = null )
2021-10-01 09:09:20 +00:00
{
Password = password ;
ProcessorId = processorId ;
}
2022-10-19 10:58:08 +00:00
public PwProcessorId ( String password )
2021-10-01 09:09:20 +00:00
{
Password = password ;
}
public PwProcessorId ( )
{
}
public UInt64 ? ProcessorId ;
2022-10-19 10:58:08 +00:00
public String Password ;
2021-10-01 09:09:20 +00:00
}
public String BuildSkeletonKey ( UInt64 processorUid )
{
return $"{(UInt16)((processorUid >> 48) ^ (processorUid >> 32)):X4}" +
$"{(UInt32)((processorUid >> 16) ^ processorUid):X8}" ;
}
[Route("GetPwProcessorId"), HttpGet]
public async Task < PwProcessorId > GetPasswordLU ( String PcbId )
{
return await Task . Run ( async ( ) = >
{
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
//try
//{
// var dt = dataAccess.ExecuteQuery($"SELECT top 10 [DS_ID] ,[ID_PCB] ,[ID_TEST] ,[JSON] FROM [MyOraDB]..[DELTACHEF].[GENESIS_PCBFUNCTIONALTEST_PD] where [ID_PCB] = '{PcbId}' order by ID_TEST desc");
// foreach (var item in dt.Select())
// {
// var jsonString = item["JSON"].ToString();
// var o = JsonConvert.DeserializeObject<JObject>(jsonString);
// var logon = from p in o["TestStepResults"]["$values"] select p;
// var logonnote = logon.Where(a => a["TestStepName"].ToString() == "Genesis IrDA Logon").ToList();
// var list = logonnote.Children().Where(a => ((JProperty)a).Name == "TestValue").First().Values().First();
// var pwString = list.ToString();
// var bList = new List<Byte>();
// var pidNode = o.GetValue("TestBoardSerialNr").ToString();
// pidNode = pidNode.Replace("-", "").Substring(8,16);
// var pId = Convert.ToUInt64(pidNode, 16);
// var checkPW = BuildSkeletonKey(pId);
// int NumberChars = pwString.Length;
// byte[] bytes = new byte[NumberChars / 2];
// for (int i = 0; i < NumberChars; i += 2)
// {
// bytes[i / 2] = Convert.ToByte(pwString.Substring(i, 2), 16);
// }
// return new PwProcessorId(ASCIIEncoding.ASCII.GetString(bytes));
// }
//}
//catch (Exception e)
//{
//}
try
{
2023-03-06 14:10:37 +00:00
var searchKey = "Flash TestFirmware" ;
2025-08-14 09:18:48 +00:00
var dt = dataAccess . ExecuteQuery ( $"select top 1 [DS_ID] ,[ID_PCB] ,[ID_TEST] ,[JSON] from OPENQUERY (MyOraDB, 'SELECT DS_ID ,ID_PCB ,ID_TEST ,JSON FROM DELTACHEF.GENESIS_PCBMETROLOGYTEST_PD where ID_PCB = ''{PcbId}'' ') order by cast(ID_Test as int) desc " ) ; // SELECT top 1 [DS_ID] ,[ID_PCB] ,[ID_TEST] ,[JSON] FROM [MyOraDB]..[DELTACHEF].[GENESIS_PCBMETROLOGYTEST_PD] where [ID_PCB] = '{PcbId}' order by cast(ID_Test as int) desc
2023-03-06 14:10:37 +00:00
if ( ! dt . Select ( ) . Any ( ) )
{
2025-08-14 09:18:48 +00:00
dt = dataAccess . ExecuteQuery ( $"select top 1 [DS_ID] ,[ID_PCB] ,[ID_TEST] ,[JSON] from OPENQUERY (MyOraDB, 'SELECT DS_ID ,ID_PCB ,ID_TEST ,JSON FROM DELTACHEF.GENESIS_PCBRADIOTEST_PD where ID_PCB = ''{PcbId}'' ') order by cast(ID_Test as int) desc" ) ; // SELECT top 1 [DS_ID] ,[ID_PCB] ,[ID_TEST] ,[JSON] FROM [MyOraDB]..[DELTACHEF].[GENESIS_PCBRADIOTEST_PD] where [ID_PCB] = '{PcbId}' order by cast(ID_Test as int) desc
2023-03-06 14:10:37 +00:00
searchKey = "Flash Firmware" ;
}
2021-10-01 09:09:20 +00:00
foreach ( var item in dt . Select ( ) )
{
var jsonString = item [ "JSON" ] . ToString ( ) ;
var o = JsonConvert . DeserializeObject < JObject > ( jsonString ) ;
try
{
2023-03-06 14:10:37 +00:00
var pwString = o [ "JsonObject" ] [ searchKey ] . ToString ( ) ;
2021-10-01 09:09:20 +00:00
//FE081F0 E5250E5D084FA024 00 04 80 00 70 03 48 9E
//0x 0413F059069BEB24
2024-01-16 09:59:03 +00:00
//FE081F0 D1 B1 FF 63 07 75 74 24 00 04 80 00 70 03 48 9E
2022-04-06 08:42:16 +00:00
//pwString = "FE081F0 F2 30 F0 59 03 66 10 24 00 04 80 00 70 03 48 9A";
//pwString = "FE081F0 16 31 F0 59 06 9B EB 24 00 04 80 00 70 03 48 9A";
2021-10-01 09:09:20 +00:00
//pwString = "FE081F0 B3 F3 0D 5C 05 C3 9B 24 00 04 80 00 70 03 48 9E";
//pwString = "FE081F0 C3 30 F0 59 03 66 10 24 00 04 80 00 70 03 48 9A";
2024-02-26 09:25:31 +00:00
// pwString = "FE081F0 48 A8 94 62 05 AC 53 24 00 04 80 00 70 03 48 9E";
2021-10-01 09:09:20 +00:00
//E79E9F08AFBE
var pId = Convert . ToUInt64 ( pwString . Replace ( " " , "" ) . Substring ( 7 , 16 ) , 16 ) ;
2024-01-16 09:59:03 +00:00
//FE081F0 D1 B1 FF 63 07 75 74 24 00 04 80 00 70 03 48 9E
//FE081F0 36 BB FF 63 08 19 F3 24 00 04 80 00 70 03 48 9E -> "3DFB7AF7D8C9"
//FE081F0 D1 B1 FF 63 07 75 74 24 00 04 80 00 70 03 48 9E -> 517316F8D22E
2021-10-01 09:09:20 +00:00
var s = BuildSkeletonKey ( pId ) ;
var r = "" ;
r + = s [ 10 ] ;
r + = s [ 11 ] ;
r + = s [ 8 ] ;
r + = s [ 9 ] ;
r + = s [ 6 ] ;
r + = s [ 7 ] ;
r + = s [ 4 ] ;
r + = s [ 5 ] ;
r + = s [ 2 ] ;
r + = s [ 3 ] ;
r + = s [ 0 ] ;
r + = s [ 1 ] ;
return new PwProcessorId ( r , pId ) ;
}
catch ( Exception )
{
2022-06-13 12:49:22 +00:00
2021-10-01 09:09:20 +00:00
}
var logon = from p in o [ "TestStepResults" ] [ "$values" ] select p ;
2023-03-06 14:10:37 +00:00
var logonnote = logon . Where ( a = > a [ "TestStepName" ] . ToString ( ) = = "searchKey" ) . ToList ( ) ;
2021-10-01 09:09:20 +00:00
var list = logonnote . Children ( ) . Where ( a = > ( ( JProperty ) a ) . Name = = "TestValue" ) . First ( ) . Values ( ) . First ( ) ;
var spId = Convert . ToUInt64 ( list . ToString ( ) . Replace ( " " , "" ) . Substring ( 7 , 16 ) , 16 ) ;
var ss = BuildSkeletonKey ( spId ) ;
var rr = "" ;
rr + = ss [ 10 ] ;
rr + = ss [ 11 ] ;
rr + = ss [ 8 ] ;
rr + = ss [ 9 ] ;
rr + = ss [ 6 ] ;
rr + = ss [ 7 ] ;
rr + = ss [ 4 ] ;
rr + = ss [ 5 ] ;
rr + = ss [ 2 ] ;
rr + = ss [ 3 ] ;
rr + = ss [ 0 ] ;
rr + = ss [ 1 ] ;
return new PwProcessorId ( rr , spId ) ;
// return new PwProcessorId(ASCIIEncoding.ASCII.GetString(bytes));
}
}
catch ( Exception e )
{
return new PwProcessorId ( e . Message + e . StackTrace ) ;
2022-06-13 12:49:22 +00:00
2021-10-01 09:09:20 +00:00
}
return new PwProcessorId ( ) ;
2022-06-13 12:49:22 +00:00
2021-10-01 09:09:20 +00:00
}
} ) ;
}
2025-07-15 14:25:38 +00:00
[Route("GetExportData")]
public async Task < HttpResponseMessage > GetExportData ( String PcbId )
{
try
{
var sbQuery = new StringBuilder ( ) ;
var ret = new List < PwdExport > ( ) ;
sbQuery . AppendLine ( $" SELECT TOP(1000) [Id] ,[OrderNumber] ,[LineNumer] ,[ManufactureDate] " +
$" ,[SerialNumber] ,[PcbId] ,[Password] ,[Low] ,[Mid] ,[High] ,[ShipDate] " +
$" ,[ExportDate] ,[PartNumber] ,[MeterSize] ,[MeterLength] " +
$" FROM [PushExt].[dbo].[CordonelExport] " +
$" where [PcbId] = '{PcbId}'" ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( sbQuery . ToString ( ) ) ;
foreach ( var item in dt . Select ( ) )
{
ret . Add ( new PwdExport ( ( Int32 ) item [ "Id" ] , ( String ) item [ "Password" ] , ( ( DateTimeOffset ) item [ "ExportDate" ] ) . DateTime ) ) ;
}
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return ret ; } ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
[Route("RunExportData")]
public async Task < HttpResponseMessage > RunExportData ( String PcbId )
{
try
{
var sbQuery = new StringBuilder ( ) ;
var ret = new List < PwdExport > ( ) ;
sbQuery . AppendLine ( $"exec [PushExportRepair] " + PcbId + ";" ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( sbQuery . ToString ( ) ) ;
foreach ( var item in dt . Select ( ) )
{
ret . Add ( new PwdExport ( ( Int32 ) item [ "Id" ] , ( String ) item [ "Password" ] , ( ( DateTime ) item [ "ExportDate" ] ) ) ) ;
}
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return ret ; } ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2021-10-01 09:09:20 +00:00
[Route("GetPassword")]
2022-10-19 10:58:08 +00:00
public async Task < HttpResponseMessage > GetPassword ( String PcbId , Int32 Llv = 8 )
2021-10-01 09:09:20 +00:00
{
try
{
var returnPw = "Not assigned" ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
2022-10-19 10:58:08 +00:00
String selectQuery = $"select [password] , [isLocked],[KeySetId] ,[orderNumber],[radioAdress] from dbo.Genesis_PcbPassword where PcbId like '{PcbId}'" ;
2021-10-01 09:09:20 +00:00
var dt = dataAccess . ExecuteQuery ( selectQuery ) ;
if ( dt . Rows . Count = = 0 )
{
var luPW = await GetPasswordLU ( PcbId ) ;
if ( ! string . IsNullOrEmpty ( luPW . Password ) )
{
var ProcessorIdString = luPW . ProcessorId . HasValue ? $"'{luPW.ProcessorId.Value}'" : "null" ;
dt = dataAccess . ExecuteQuery ( $"INSERT INTO [dbo].[Genesis_PcbPassword] " +
$" ([PcbId] " +
$" ,[Password] " +
$" ,[KeySetId] " +
$" ,[orderNumber] " +
$" ,[radioAdress] " +
$" ,[Bemerkung] " +
$" ,[KitronSkeletonKey] " +
$" ,[ProcessorID] " +
$" ,[IsLocked]) " +
$" VALUES " +
$" ('{PcbId}' " +
$" ,'{luPW.Password}' " +
$" ,NULL " +
$" ,NULL " +
$" ,NULL " +
$" ,'AutoImport' " +
$" ,'{luPW.Password}' " +
$" ,{ProcessorIdString} " +
$" ,0); " +
$" { Environment.NewLine}" +
$" { selectQuery}" ) ;
}
}
foreach ( var item in dt . Select ( ) )
{
returnPw = item [ "password" ] . ToString ( ) ;
2022-10-19 10:58:08 +00:00
if ( ! ( Boolean ) item [ "isLocked" ] )
2021-10-01 09:09:20 +00:00
{
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return returnPw ; } ) ) ;
}
if ( string . IsNullOrEmpty ( item [ "KeySetId" ] . ToString ( ) ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new NoKeyStoredException ( new Guid ( "3A963893-5D8D-4A61-9D65-4B927A9331A5" ) ) ) ;
}
if ( ! Request . Headers . Contains ( "X-Node-Token" ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new TokenRequiredException ( new Guid ( "A1ED92AE-8341-4829-9A18-8249D927E777" ) ) ) ;
}
var passwords = getKeys ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , item [ "orderNumber" ] . ToString ( ) , item [ "password" ] . ToString ( ) ) ;
//pw servcie stores 1,2,X,4-8
if ( Llv ! = 3 )
{
returnPw = passwords . Keys [ Llv < = 2 ? Llv : Llv + 1 ] . KeyKey ;
if ( returnPw . Length > 12 )
{
returnPw = returnPw . Substring ( 0 , 12 ) ;
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return returnPw ; } ) ) ;
}
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new LvlNotSupportedException ( new Guid ( "CEDF523B-681F-4084-93A3-C66487BA3564" ) ) ) ;
}
}
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( new Guid ( "23924333-5565-473B-BB66-ECFF8A76B429" ) ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
public class MeterPasswordContainer
{
public Int64 ? orderNumber = null ;
public Int64 ? radioAdress = null ;
2022-10-19 10:58:08 +00:00
public String Skeleton = "" ;
2023-01-24 15:36:01 +00:00
public String Password = "" ;
2022-10-19 10:58:08 +00:00
public List < Byte [ ] > listOfHashes = new List < Byte [ ] > ( ) ;
public List < Byte [ ] > listOfPasswords = new List < Byte [ ] > ( ) ;
2021-10-01 09:09:20 +00:00
2022-10-19 10:58:08 +00:00
public String KeySetId { get ; internal set ; }
2023-02-27 09:53:10 +00:00
public String PasswordFileIdent { get ; internal set ; }
2021-10-01 09:09:20 +00:00
}
2022-10-19 10:58:08 +00:00
private MeterPw getKeys ( String Token , String orderNumber , String radioAdress )
2021-10-01 09:09:20 +00:00
{
2025-01-10 13:57:59 +00:00
//var _orderNumber = SQLConnection
// .CreateSQLConnection(GlobalConfig.connectionString.Value)
// .CreateCommand("SELECT orderNumber FROM Genesis_PcbPassword WHERE radioAdress = @radioAdress")
// .SetParameter(nameof(radioAdress), radioAdress)
// .FirstOrDefault(x => x.GetValue<string>(0));
//if (!string.IsNullOrWhiteSpace(_orderNumber) && orderNumber != _orderNumber)
//{
// orderNumber = _orderNumber;
//}
2021-10-01 09:09:20 +00:00
ServicePointManager . ServerCertificateValidationCallback + = ( sender , certificate , chain , sslPolicyErrors ) = >
{
return true ;
} ;
//var Url = $"{ServiceUrls.KeyServerCustom()}devices
var Url = $"https://nodes.sms-esaap.com:8081/api/v3/custom/keysetkeys?orderNumber={orderNumber}&radioAdress={radioAdress}" ;
2024-07-18 06:42:01 +00:00
//Url = $"https://nodes.sms-esaap.com:8081/api/v3/custom/keysetkeys?setId={da948379-2aa1-4a47-911f-8031a20ecebf}&radioAdress={radioAdress}";//
2021-10-01 09:09:20 +00:00
2022-10-19 10:58:08 +00:00
var header = new Dictionary < String , String > ( ) ;
2021-10-01 09:09:20 +00:00
header . Add ( "Cache-Control" , "no-cache" ) ;
header . Add ( "X-Node-Token" , Token ) ; //"d5b8c0b2-81f6-4421-80d0-44eb2093e616");
try
{
2023-08-02 10:38:58 +00:00
var responseJson = LocalWebRequest . GetRequest ( url : Url , header : header , timeoutMs : 80000 ) ;
2021-10-01 09:09:20 +00:00
responseJson = responseJson . Trim ( '"' ) ;
return MeterPw . FromJson ( responseJson ) ;
}
catch ( Exception ex )
{
throw new ApplicationException ( "Fatal exception while getting keys from server" , ex ) ;
}
}
2023-11-27 15:54:53 +00:00
[Route("GetPasswordHashFileContentDebug")]
public async Task < HttpResponseMessage > GetPasswordHashFileContentDebug ( String PcbId )
{
try
{
var sbKeys = new StringBuilder ( ) ;
var listOfHashes = new List < Byte [ ] > ( ) ;
var sbOutPut = new StringBuilder ( ) ;
var sbQuery = new StringBuilder ( ) ;
sbQuery . AppendLine ( $" SELECT " ) ;
sbQuery . AppendLine ( $" [Seriennummer] " ) ;
sbQuery . AppendLine ( $" ,mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" ,[Adresse] " ) ;
sbQuery . AppendLine ( $" ,AuftragPosSNr.AuftragNr " ) ;
sbQuery . AppendLine ( $" ,Auftrag.FertigungsauftragNr " ) ;
sbQuery . AppendLine ( $" ,[GeschlossenDatum] " ) ;
sbQuery . AppendLine ( $" ,[Vergabe_am] " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.KeySetId " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.orderNumber " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.Password " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.KitronSkeletonKey as Skeleton " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.ProcessorID as ProcessorUid " ) ;
sbQuery . AppendLine ( $" FROM [Auftrag].[dbo].[Genesis_Meter] meter " ) ;
sbQuery . AppendLine ( $" inner join MapPcbIdToSerialNumber mps" +
$" on mps.MapPcbIdToSerialNumber_SerialNumber = meter.Seriennummer and mps.MapPcbIdToSerialNumber_Deleted = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AuftragPositionSerienNr] AuftragPosSNr " +
$" on AuftragPosSNr.SerienNr = meter.Seriennummer and [Pruefgangnr] = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AlleAuftragPositionen] Auftrag" +
$" on Auftrag.AuftragNr = AuftragPosSNr.AuftragNr and AuftragPosSNr.PositionNr = Auftrag.PositionNr " ) ;
sbQuery . AppendLine ( $" left outer join[dbo].[Genesis_PcbPassword] passwordtbl" +
$" on passwordtbl.PcbId = mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" where mps.MapPcbIdToSerialNumber_PcbId = '{PcbId}' " ) ;
var result = new MeterPwdDb ( ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( sbQuery . ToString ( ) ) ;
foreach ( var item in dt . Select ( ) )
{
result . OrderNumber = ( Int32 ) item [ "FertigungsauftragNr" ] ;
result . RadioAddress = ( Int64 ) item [ "Adresse" ] ;
result . Skeleton = ( String ) item [ "Skeleton" ] ;
result . Password = ( String ) item [ "Password" ] ;
result . PasswordFileIdent = ( result . RadioAddress . HasValue & & result . RadioAddress . Value ! = 0 ) ? result . RadioAddress . Value . ToString ( ) : PcbId ;
if ( item [ "KeySetId" ] . GetType ( ) ! = typeof ( System . DBNull ) )
{
result . KeySetId = ( String ) item [ "KeySetId" ] ;
}
sbKeys . AppendLine ( $"PcbId:{PcbId}" ) ;
sbKeys . AppendLine ( $"ProcessorUid:{(String)item[" ProcessorUid "]}" ) ;
sbKeys . AppendLine ( $"Skeleton:{(String)item[" Skeleton "]}" ) ;
sbKeys . AppendLine ( $"Password:{(String)item[" Password "]}" ) ;
}
}
if ( ! result . OrderNumber . HasValue )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( new Guid ( "160B4C44-6EF7-41DC-A48D-4B4105B4779B" ) ) ) ;
}
Request . Headers . Add ( "X-Node-Token" , "d5b8c0b2-81f6-4421-80d0-44eb2093e616" ) ;
if ( ! Request . Headers . Contains ( "X-Node-Token" ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new TokenRequiredException ( new Guid ( "77895D13-CA58-48AB-8C5E-9CDB7F80C69D" ) ) ) ;
}
if ( string . IsNullOrEmpty ( result . KeySetId ) )
{
result . KeySetId = CreateKeySet ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , 7 ) ;
//mapp keyset
MapKeySet ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , result . KeySetId , result . OrderNumber . Value , result . PasswordFileIdent ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery (
$"update auftrag.[dbo].[Genesis_PcbPassword] set KeySetId = '{result.KeySetId}', [orderNumber] = {result.OrderNumber.Value}, [radioAdress] = {result.PasswordFileIdent} where PcbId = '{PcbId}'; select * from auftrag.[dbo].[Genesis_PcbPassword] where PcbId = '{PcbId}'" ) ;
}
}
var passwords = getKeys ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , result . OrderNumber . Value . ToString ( ) , result . PasswordFileIdent ) ;
//pw lvl
passwords . Keys . Insert ( 2 , new Key ( ) { Index = 2 , KeyKey = result . Skeleton } ) ;
var pwdLVL8 = "" ;
if ( passwords . Keys . Count > 7 )
{
pwdLVL8 = passwords . Keys [ 7 ] . KeyKey . Substring ( 0 , 12 ) ;
}
foreach ( var keyLVL in passwords . Keys )
{
using ( System . Security . Cryptography . SHA1Managed sha1 = new System . Security . Cryptography . SHA1Managed ( ) )
{
var rawKey = keyLVL . KeyKey ;
if ( rawKey . Length > 12 )
{
rawKey = rawKey . Substring ( 0 , 12 ) ;
}
if ( result . ListOfPasswords . Count < = 7 )
{
result . ListOfPasswords . Add ( Encoding . UTF8 . GetBytes ( rawKey ) ) ;
var hash = sha1 . ComputeHash ( Encoding . UTF8 . GetBytes ( rawKey ) ) ;
result . ListOfHashes . Add ( hash ) ;
}
}
}
sbKeys . AppendLine ( "passwords:" ) ;
foreach ( var item in result . ListOfPasswords )
{
sbKeys . AppendLine ( BitConverter . ToString ( item ) ) ;
}
sbKeys . AppendLine ( "hashes:" ) ;
foreach ( var item in result . ListOfHashes )
{
sbKeys . AppendLine ( BitConverter . ToString ( item ) ) ;
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = >
{
return result ;
} ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2021-10-01 09:09:20 +00:00
2025-12-16 12:52:29 +00:00
[Route("SetPasswordFix")]
public async Task < HttpResponseMessage > SetPasswordFix ( String PcbId )
{
try
{
var sbKeys = new StringBuilder ( ) ;
var listOfHashes = new List < Byte [ ] > ( ) ;
var sbOutPut = new StringBuilder ( ) ;
var sbQuery = new StringBuilder ( ) ;
sbQuery . AppendLine ( $" SELECT distinct " ) ;
sbQuery . AppendLine ( $" [Seriennummer] " ) ;
sbQuery . AppendLine ( $" ,mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" ,[Adresse] " ) ;
sbQuery . AppendLine ( $" ,AuftragPosSNr.AuftragNr " ) ;
sbQuery . AppendLine ( $" ,Auftrag.FertigungsauftragNr " ) ;
sbQuery . AppendLine ( $" ,[GeschlossenDatum] " ) ;
sbQuery . AppendLine ( $" ,[Vergabe_am] " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.KeySetId " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.orderNumber " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.Password " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.KitronSkeletonKey as Skeleton " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.ProcessorID as ProcessorUid " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.radioAdress " ) ;
sbQuery . AppendLine ( $" FROM [Auftrag].[dbo].[Genesis_Meter] meter " ) ;
sbQuery . AppendLine ( $" inner join MapPcbIdToSerialNumber mps" +
$" on mps.MapPcbIdToSerialNumber_SerialNumber = meter.Seriennummer and mps.MapPcbIdToSerialNumber_Deleted = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AuftragPositionSerienNr] AuftragPosSNr " +
$" on AuftragPosSNr.SerienNr = meter.Seriennummer and [Pruefgangnr] = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AlleAuftragPositionen] Auftrag" +
$" on Auftrag.AuftragNr = AuftragPosSNr.AuftragNr and AuftragPosSNr.PositionNr = Auftrag.PositionNr " ) ;
sbQuery . AppendLine ( $" left outer join[dbo].[Genesis_PcbPassword] passwordtbl" +
$" on passwordtbl.PcbId = mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" where mps.MapPcbIdToSerialNumber_PcbId = '{PcbId}' " ) ;
var result = new MeterPwdDb ( ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( sbQuery . ToString ( ) ) ;
if ( dt . Rows . Count ! = 1 )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( "Pcb not unique, mapping problem" , new Guid ( "160B4C44-6EF7-41DC-A48D-4B4105B4779B" ) ) ) ;
}
foreach ( var item in dt . Select ( ) )
{
result . OrderNumber = ( Int32 ) item [ "FertigungsauftragNr" ] ;
result . RadioAddress = ( Int64 ) item [ "Adresse" ] ;
result . Skeleton = ( String ) item [ "Skeleton" ] ;
result . Password = ( String ) item [ "Password" ] ;
if ( item [ "radioAdress" ] . GetType ( ) ! = typeof ( System . DBNull ) )
{
result . PasswordFileIdent = ( String ) item [ "radioAdress" ] . ToString ( ) ;
}
else
{
result . PasswordFileIdent = ( result . RadioAddress . HasValue & & result . RadioAddress . Value ! = 0 ) ? result . RadioAddress . Value . ToString ( ) : PcbId ;
}
if ( item [ "KeySetId" ] . GetType ( ) ! = typeof ( System . DBNull ) )
{
result . KeySetId = ( String ) item [ "KeySetId" ] ;
}
}
}
if ( ! result . OrderNumber . HasValue )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( new Guid ( "160B4C44-6EF7-41DC-A48D-4B4105B4779B" ) ) ) ;
}
Request . Headers . Add ( "X-Node-Token" , "d5b8c0b2-81f6-4421-80d0-44eb2093e616" ) ;
if ( ! Request . Headers . Contains ( "X-Node-Token" ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new TokenRequiredException ( new Guid ( "77895D13-CA58-48AB-8C5E-9CDB7F80C69D" ) ) ) ;
}
if ( string . IsNullOrEmpty ( result . KeySetId ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new NoKeyStoredException ( new Guid ( "45444447-CA58-48AB-8C5E-9CDB7F80C69D" ) ) ) ;
}
var passwords = getKeys ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , result . OrderNumber . Value . ToString ( ) , result . PasswordFileIdent ) ;
//pw lvl
passwords . Keys . Insert ( 2 , new Key ( ) { Index = 2 , KeyKey = result . Skeleton } ) ;
var pwdLVL8 = "" ;
if ( passwords . Keys . Count > 7 )
{
pwdLVL8 = passwords . Keys [ 7 ] . KeyKey . Substring ( 0 , 12 ) ;
}
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery (
$"update auftrag.[dbo].[Genesis_PcbPassword] set Password = '{pwdLVL8}' where PcbId = '{PcbId}'; " ) ;
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = >
{
return true ;
} ) ) ;
}
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2021-10-01 09:09:20 +00:00
[Route("GetPasswordHashFileContent")]
public async Task < HttpResponseMessage > GetPasswordHashFileContent ( String PcbId )
{
try
{
2023-11-27 15:54:53 +00:00
var sbKeys = new StringBuilder ( ) ;
2022-10-19 10:58:08 +00:00
var listOfHashes = new List < Byte [ ] > ( ) ;
2021-10-01 09:09:20 +00:00
var sbOutPut = new StringBuilder ( ) ;
var sbQuery = new StringBuilder ( ) ;
2024-08-06 13:45:06 +00:00
sbQuery . AppendLine ( $" SELECT distinct " ) ;
2021-10-01 09:09:20 +00:00
sbQuery . AppendLine ( $" [Seriennummer] " ) ;
sbQuery . AppendLine ( $" ,mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" ,[Adresse] " ) ;
sbQuery . AppendLine ( $" ,AuftragPosSNr.AuftragNr " ) ;
sbQuery . AppendLine ( $" ,Auftrag.FertigungsauftragNr " ) ;
sbQuery . AppendLine ( $" ,[GeschlossenDatum] " ) ;
sbQuery . AppendLine ( $" ,[Vergabe_am] " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.KeySetId " ) ;
sbQuery . AppendLine ( $" ,passwordtbl.orderNumber " ) ;
2023-01-24 15:36:01 +00:00
sbQuery . AppendLine ( $" ,passwordtbl.Password " ) ;
2022-07-13 15:39:04 +00:00
sbQuery . AppendLine ( $" ,passwordtbl.KitronSkeletonKey as Skeleton " ) ;
2021-10-01 09:09:20 +00:00
sbQuery . AppendLine ( $" ,passwordtbl.ProcessorID as ProcessorUid " ) ;
2024-08-06 13:45:06 +00:00
sbQuery . AppendLine ( $" ,passwordtbl.radioAdress " ) ;
2021-10-01 09:09:20 +00:00
sbQuery . AppendLine ( $" FROM [Auftrag].[dbo].[Genesis_Meter] meter " ) ;
sbQuery . AppendLine ( $" inner join MapPcbIdToSerialNumber mps" +
$" on mps.MapPcbIdToSerialNumber_SerialNumber = meter.Seriennummer and mps.MapPcbIdToSerialNumber_Deleted = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AuftragPositionSerienNr] AuftragPosSNr " +
$" on AuftragPosSNr.SerienNr = meter.Seriennummer and [Pruefgangnr] = 0 " ) ;
sbQuery . AppendLine ( $" inner join[Auftrag].[dbo].[AlleAuftragPositionen] Auftrag" +
$" on Auftrag.AuftragNr = AuftragPosSNr.AuftragNr and AuftragPosSNr.PositionNr = Auftrag.PositionNr " ) ;
sbQuery . AppendLine ( $" left outer join[dbo].[Genesis_PcbPassword] passwordtbl" +
$" on passwordtbl.PcbId = mps.MapPcbIdToSerialNumber_PcbId " ) ;
sbQuery . AppendLine ( $" where mps.MapPcbIdToSerialNumber_PcbId = '{PcbId}' " ) ;
2024-02-26 09:25:31 +00:00
2021-10-01 09:09:20 +00:00
2023-03-06 14:10:37 +00:00
var result = new MeterPwdDb ( ) ;
2021-10-01 09:09:20 +00:00
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( sbQuery . ToString ( ) ) ;
2024-08-06 13:45:06 +00:00
if ( dt . Rows . Count ! = 1 )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( "Pcb not unique, mapping problem" , new Guid ( "160B4C44-6EF7-41DC-A48D-4B4105B4779B" ) ) ) ;
}
2021-10-01 09:09:20 +00:00
foreach ( var item in dt . Select ( ) )
{
2023-03-06 14:10:37 +00:00
result . OrderNumber = ( Int32 ) item [ "FertigungsauftragNr" ] ;
result . RadioAddress = ( Int64 ) item [ "Adresse" ] ;
2022-10-19 10:58:08 +00:00
result . Skeleton = ( String ) item [ "Skeleton" ] ;
2023-01-24 15:36:01 +00:00
result . Password = ( String ) item [ "Password" ] ;
2024-08-06 13:45:06 +00:00
if ( item [ "radioAdress" ] . GetType ( ) ! = typeof ( System . DBNull ) )
{
result . PasswordFileIdent = ( String ) item [ "radioAdress" ] . ToString ( ) ;
}
else
{
result . PasswordFileIdent = ( result . RadioAddress . HasValue & & result . RadioAddress . Value ! = 0 ) ? result . RadioAddress . Value . ToString ( ) : PcbId ;
}
2021-10-01 09:09:20 +00:00
if ( item [ "KeySetId" ] . GetType ( ) ! = typeof ( System . DBNull ) )
{
2022-10-19 10:58:08 +00:00
result . KeySetId = ( String ) item [ "KeySetId" ] ;
2021-10-01 09:09:20 +00:00
}
2024-02-26 09:25:31 +00:00
2021-10-01 09:09:20 +00:00
}
}
2023-11-27 15:54:53 +00:00
if ( result . OrderNumber = = 3219628 )
{
result . PasswordFileIdent = "1" + result . PasswordFileIdent ;
}
2024-02-26 09:25:31 +00:00
2023-03-06 14:10:37 +00:00
if ( ! result . OrderNumber . HasValue )
2021-10-01 09:09:20 +00:00
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new PcbIdNotException ( new Guid ( "160B4C44-6EF7-41DC-A48D-4B4105B4779B" ) ) ) ;
}
Request . Headers . Add ( "X-Node-Token" , "d5b8c0b2-81f6-4421-80d0-44eb2093e616" ) ;
if ( ! Request . Headers . Contains ( "X-Node-Token" ) )
{
return Request . CreateResponse ( HttpStatusCode . InternalServerError , new TokenRequiredException ( new Guid ( "77895D13-CA58-48AB-8C5E-9CDB7F80C69D" ) ) ) ;
}
if ( string . IsNullOrEmpty ( result . KeySetId ) )
{
result . KeySetId = CreateKeySet ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , 7 ) ;
//mapp keyset
2023-03-06 14:10:37 +00:00
MapKeySet ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , result . KeySetId , result . OrderNumber . Value , result . PasswordFileIdent ) ;
2021-10-01 09:09:20 +00:00
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery (
2023-03-06 14:10:37 +00:00
$"update auftrag.[dbo].[Genesis_PcbPassword] set KeySetId = '{result.KeySetId}', [orderNumber] = {result.OrderNumber.Value}, [radioAdress] = {result.PasswordFileIdent} where PcbId = '{PcbId}'; select * from auftrag.[dbo].[Genesis_PcbPassword] where PcbId = '{PcbId}'" ) ;
2021-10-01 09:09:20 +00:00
}
}
2023-03-06 14:10:37 +00:00
var passwords = getKeys ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , result . OrderNumber . Value . ToString ( ) , result . PasswordFileIdent ) ;
2021-10-01 09:09:20 +00:00
//pw lvl
passwords . Keys . Insert ( 2 , new Key ( ) { Index = 2 , KeyKey = result . Skeleton } ) ;
2022-06-13 12:49:22 +00:00
var pwdLVL8 = "" ;
if ( passwords . Keys . Count > 7 )
{
pwdLVL8 = passwords . Keys [ 7 ] . KeyKey . Substring ( 0 , 12 ) ;
}
2024-02-26 09:25:31 +00:00
2021-10-01 09:09:20 +00:00
foreach ( var keyLVL in passwords . Keys )
{
using ( System . Security . Cryptography . SHA1Managed sha1 = new System . Security . Cryptography . SHA1Managed ( ) )
{
var rawKey = keyLVL . KeyKey ;
if ( rawKey . Length > 12 )
{
rawKey = rawKey . Substring ( 0 , 12 ) ;
}
2023-03-06 14:10:37 +00:00
if ( result . ListOfPasswords . Count < = 7 )
2021-10-01 09:09:20 +00:00
{
2023-03-06 14:10:37 +00:00
result . ListOfPasswords . Add ( Encoding . UTF8 . GetBytes ( rawKey ) ) ;
2024-02-26 09:25:31 +00:00
2021-10-01 09:09:20 +00:00
var hash = sha1 . ComputeHash ( Encoding . UTF8 . GetBytes ( rawKey ) ) ;
2023-03-06 14:10:37 +00:00
result . ListOfHashes . Add ( hash ) ;
2021-10-01 09:09:20 +00:00
}
}
}
2023-11-27 15:54:53 +00:00
sbKeys . AppendLine ( $"PcbiD: {PcbId}" ) ;
sbKeys . AppendLine ( "passwords:" ) ;
foreach ( var item in result . ListOfPasswords )
2021-10-01 09:09:20 +00:00
{
2023-11-27 15:54:53 +00:00
sbKeys . AppendLine ( BitConverter . ToString ( item ) ) ;
}
sbKeys . AppendLine ( "hashes:" ) ;
foreach ( var item in result . ListOfHashes )
{
sbKeys . AppendLine ( BitConverter . ToString ( item ) ) ;
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = >
2024-02-26 09:25:31 +00:00
{
2021-10-01 09:09:20 +00:00
return result ;
} ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2024-02-26 09:25:31 +00:00
[Route("GetRadioKey")]
public async Task < HttpResponseMessage > GetRadioKey ( String PcbId )
{
2025-03-12 09:23:28 +00:00
throw new Exception ( "Use finalcheck get encyprion key" ) ;
2024-02-26 09:25:31 +00:00
try
{
var lst = new List < Tuple < int , long , int > > ( ) ;
2024-07-18 06:42:01 +00:00
//lst.Add(new Tuple<int, long, int>(22735455, 10402001195, 3187517));
//lst.Add(new Tuple<int, long, int>(22735458, 10402001198, 3187517));
//lst.Add(new Tuple<int, long, int>(22735459, 10402001199, 3187517));
//lst.Add(new Tuple<int, long, int>(22750101, 10402001361, 3190243));
//lst.Add(new Tuple<int, long, int>(22750102, 10402001362, 3190243));
//lst.Add(new Tuple<int, long, int>(22750103, 10402001363, 3190243));
//lst.Add(new Tuple<int, long, int>(22773935, 10402003482, 3194465));
//lst.Add(new Tuple<int, long, int>(22773936, 10402003483, 3194465));
//lst.Add(new Tuple<int, long, int>(22773937, 10402003484, 3194465));
//lst.Add(new Tuple<int, long, int>(22773939, 10402003486, 3194465));
//lst.Add(new Tuple<int, long, int>(22773940, 10402003487, 3194465));
//lst.Add(new Tuple<int, long, int>(22773941, 10402003488, 3194465));
//lst.Add(new Tuple<int, long, int>(22773942, 10402003489, 3194465));
//lst.Add(new Tuple<int, long, int>(22773943, 10402003490, 3194465));
//lst.Add(new Tuple<int, long, int>(23719324, 10402004169, 3205719));
//lst.Add(new Tuple<int, long, int>(23719325, 10402004170, 3205719));
//lst.Add(new Tuple<int, long, int>(23719326, 10402004171, 3205719));
//lst.Add(new Tuple<int, long, int>(23719327, 10402004172, 3205719));
//lst.Add(new Tuple<int, long, int>(23719328, 10402004173, 3205719));
//lst.Add(new Tuple<int, long, int>(23719329, 10402004174, 3205719));
//lst.Add(new Tuple<int, long, int>(23719353, 10402004198, 3205720));
//lst.Add(new Tuple<int, long, int>(23719355, 10402004200, 3205720));
//lst.Add(new Tuple<int, long, int>(23719361, 10402004206, 3205720));
//lst.Add(new Tuple<int, long, int>(23719365, 10402004210, 3205721));
//lst.Add(new Tuple<int, long, int>(23719366, 10402004211, 3205721));
//lst.Add(new Tuple<int, long, int>(23719367, 10402004212, 3205721));
//lst.Add(new Tuple<int, long, int>(23719368, 10402004213, 3205721));
//lst.Add(new Tuple<int, long, int>(23719369, 10402004214, 3205721));
//lst.Add(new Tuple<int, long, int>(23719370, 10402004215, 3205721));
//lst.Add(new Tuple<int, long, int>(23719374, 10402004219, 3205722));
//lst.Add(new Tuple<int, long, int>(23719375, 10402004220, 3205722));
//lst.Add(new Tuple<int, long, int>(23719376, 10402004221, 3205722));
//lst.Add(new Tuple<int, long, int>(23719377, 10402004222, 3205722));
//lst.Add(new Tuple<int, long, int>(23719378, 10402004223, 3205722));
//lst.Add(new Tuple<int, long, int>(23719379, 10402004224, 3205722));
//lst.Add(new Tuple<int, long, int>(23719380, 10402004225, 3205722));
//lst.Add(new Tuple<int, long, int>(23719381, 10402004226, 3205722));
//lst.Add(new Tuple<int, long, int>(23719382, 10402004227, 3205722));
//lst.Add(new Tuple<int, long, int>(23719383, 10402004228, 3205722));
//lst.Add(new Tuple<int, long, int>(23719384, 10402004229, 3205722));
//lst.Add(new Tuple<int, long, int>(23719385, 10402004230, 3205722));
//lst.Add(new Tuple<int, long, int>(23719387, 10402004232, 3205722));
//lst.Add(new Tuple<int, long, int>(23719388, 10402004233, 3205722));
//lst.Add(new Tuple<int, long, int>(23719389, 10402004234, 3205722));
//lst.Add(new Tuple<int, long, int>(23719390, 10402004235, 3205722));
//lst.Add(new Tuple<int, long, int>(23719391, 10402004236, 3205722));
//lst.Add(new Tuple<int, long, int>(23719392, 10402004237, 3205722));
//lst.Add(new Tuple<int, long, int>(23719393, 10402004238, 3205722));
//lst.Add(new Tuple<int, long, int>(23719394, 10402004239, 3205722));
//lst.Add(new Tuple<int, long, int>(23719395, 10402004240, 3205722));
//lst.Add(new Tuple<int, long, int>(23719396, 10402004241, 3205722));
//lst.Add(new Tuple<int, long, int>(23719465, 10402004264, 3205759));
//lst.Add(new Tuple<int, long, int>(23719466, 10402004265, 3205759));
//lst.Add(new Tuple<int, long, int>(23719467, 10402004266, 3205759));
//lst.Add(new Tuple<int, long, int>(23719468, 10402004267, 3205759));
//lst.Add(new Tuple<int, long, int>(23719469, 10402004268, 3205759));
//lst.Add(new Tuple<int, long, int>(23719470, 10402004269, 3205759));
//lst.Add(new Tuple<int, long, int>(23719471, 10402004270, 3205759));
//lst.Add(new Tuple<int, long, int>(23719472, 10402004271, 3205759));
//lst.Add(new Tuple<int, long, int>(23719473, 10402004272, 3205759));
//lst.Add(new Tuple<int, long, int>(23719479, 10402004278, 3205759));
//lst.Add(new Tuple<int, long, int>(23719480, 10402004279, 3205759));
//lst.Add(new Tuple<int, long, int>(23719481, 10402004280, 3205759));
//lst.Add(new Tuple<int, long, int>(23719482, 10402004281, 3205759));
//lst.Add(new Tuple<int, long, int>(23720650, 10402004369, 3206400));
//lst.Add(new Tuple<int, long, int>(23720651, 10402004370, 3206401));
//lst.Add(new Tuple<int, long, int>(23720744, 10402004377, 3206443));
//lst.Add(new Tuple<int, long, int>(23749514, 10402004737, 3210028));
//lst.Add(new Tuple<int, long, int>(23749516, 10402004739, 3210028));
//lst.Add(new Tuple<int, long, int>(23749517, 10402004740, 3210029));
//lst.Add(new Tuple<int, long, int>(23749518, 10402004741, 3210029));
//lst.Add(new Tuple<int, long, int>(23749519, 10402004742, 3210029));
//lst.Add(new Tuple<int, long, int>(23749520, 10402004743, 3210029));
//lst.Add(new Tuple<int, long, int>(23749521, 10402004744, 3210029));
//lst.Add(new Tuple<int, long, int>(23749523, 10402004745, 3210033));
//lst.Add(new Tuple<int, long, int>(23749524, 10402004746, 3210033));
//lst.Add(new Tuple<int, long, int>(23749525, 10402004747, 3210033));
//lst.Add(new Tuple<int, long, int>(23749526, 10402004748, 3210033));
//lst.Add(new Tuple<int, long, int>(23749527, 10402004749, 3210033));
//lst.Add(new Tuple<int, long, int>(23749528, 10402004750, 3210033));
//lst.Add(new Tuple<int, long, int>(23749529, 10402004751, 3210033));
//lst.Add(new Tuple<int, long, int>(23749530, 10402004752, 3210033));
//lst.Add(new Tuple<int, long, int>(23749531, 10402004753, 3210033));
//lst.Add(new Tuple<int, long, int>(23749532, 10402004754, 3210033));
//lst.Add(new Tuple<int, long, int>(23766896, 10402005092, 3213958));
//lst.Add(new Tuple<int, long, int>(23766897, 10402005093, 3213958));
//lst.Add(new Tuple<int, long, int>(23766898, 10402005094, 3213958));
//lst.Add(new Tuple<int, long, int>(23766899, 10402005095, 3213958));
//lst.Add(new Tuple<int, long, int>(23766900, 10402005096, 3213958));
//lst.Add(new Tuple<int, long, int>(23766901, 10402005097, 3213958));
//lst.Add(new Tuple<int, long, int>(23766902, 10402005098, 3213958));
//lst.Add(new Tuple<int, long, int>(23766903, 10402005099, 3213958));
//lst.Add(new Tuple<int, long, int>(23766904, 10402005100, 3213958));
//lst.Add(new Tuple<int, long, int>(23766905, 10402005101, 3213958));
2024-02-26 09:25:31 +00:00
var lstResult = new List < String > ( ) ;
Request . Headers . Add ( "X-Node-Token" , "d5b8c0b2-81f6-4421-80d0-44eb2093e616" ) ;
foreach ( var item in lst )
{
var Url = $"https://nodes.sms-esaap.com/keygenproxy/api/v2/custom/keysets" ;
var http = ( HttpWebRequest ) WebRequest . Create ( Url ) ;
http . Timeout = 20000 ;
http . Headers . Add ( "Cache-Control" , "no-cache" ) ;
http . Headers . Add ( "X-Node-Token" , Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) ) ;
String postData = "{'keysCount': 1}" ;
ASCIIEncoding encoding = new ASCIIEncoding ( ) ;
Byte [ ] byte1 = encoding . GetBytes ( postData ) ;
// Set the content type of the data being posted.
http . ContentType = "application/json; charset=utf-8" ;
http . Method = "POST" ;
// Set the content length of the string being posted.
http . ContentLength = byte1 . Length ;
Stream newStream = http . GetRequestStream ( ) ;
newStream . Write ( byte1 , 0 , byte1 . Length ) ;
newStream . Close ( ) ;
var response = ( HttpWebResponse ) http . GetResponse ( ) ;
var responseStream = response . GetResponseStream ( ) ;
if ( responseStream = = null )
{
throw new InvalidOperationException ( ) ;
}
if ( response . StatusCode ! = HttpStatusCode . OK )
{
}
var KeySetId = "" ;
using ( var sr = new StreamReader ( responseStream ) )
{
var responseJson = sr . ReadToEnd ( ) ;
responseJson = responseJson . Trim ( '"' ) ;
var res = MeterPw . ArrayFromJson ( responseJson ) ;
KeySetId = res [ 0 ] . SetId . ToString ( ) ;
}
MapKeySet ( Request . Headers . GetValues ( "X-Node-Token" ) . First ( ) , KeySetId , item . Item3 , item . Item2 . ToString ( ) ) ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var strIndex = $"{item.Item1} | {item.Item3} | {item.Item2}" ;
var dt = dataAccess . ExecuteQuery (
$"UPDATE [Genesis_Meter] SET FunkschluesselIndex ='{strIndex}', RadioKeyIndex = '{KeySetId}' where Seriennummer= '{ item.Item1}'" ) ;
lstResult . Add ( strIndex + KeySetId ) ;
}
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = >
{
return lstResult ;
} ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2024-10-15 09:36:17 +00:00
[Route("RepairMapKeySet")]
public String RepairMapKeySet ( String KeySetID = "2aece5d3-8acd-471c-a3d5-13e2866be441" , string radio = "10402007187" , string deviceID = "8SEN2024791544" , string Order = "3232771" )
{
try
{
//keysets
var Url = $"{ServiceUrls.KeyServerCustom()}keysetkeys/" ;
Url = "https://nodes.sms-esaap.com/keygenproxy/api/v2/keys" ;
var header = new Dictionary < String , String > ( ) ;
header . Add ( "Cache-Control" , "no-cache" ) ;
header . Add ( "X-Node-Token" , "d5b8c0b2-81f6-4421-80d0-44eb2093e616" ) ;
string postData = $"[{{\" KeyIndex \ ":\"{KeySetID}\", \"OrderNumber\":\"{Order}\", \"Device\":\"{deviceID}\", \"radioaddress\":\"{radio}\"}}]" ;
return LocalWebRequest . PostRequestAsyncAndGetContent ( url : Url , header : header , json : postData , timeoutMs : 20000 ) ;
}
catch ( Exception ex )
{
return ex . Message ;
}
}
2024-02-26 09:25:31 +00:00
private String MapKeySet ( String Token , String KeySetID , Int64 MeterOrderNumber , string MeterPassowrdIdent )
2021-10-01 09:09:20 +00:00
{
try
{
//keysets
var Url = $"{ServiceUrls.KeyServerCustom()}keysetkeys/" ;
Url = "https://nodes.sms-esaap.com/keygenproxy/api/v3/custom/keysets/devices" ;
2022-10-19 10:58:08 +00:00
var header = new Dictionary < String , String > ( ) ;
2021-10-01 09:09:20 +00:00
header . Add ( "Cache-Control" , "no-cache" ) ;
header . Add ( "X-Node-Token" , Token ) ; //"d5b8c0b2-81f6-4421-80d0-44eb2093e616");
//string postData = $"[{{\"setId\":\"{KeySetID}\", \"orderNumber\":\"{orderNumber}\", \"radioAdress\":\"{radioAdress}\"}}]";
var postData = new MapKeySetItem ( )
2023-02-27 09:53:10 +00:00
{ orderNumber = MeterOrderNumber . ToString ( ) , radioAdress = MeterPassowrdIdent . ToString ( ) , setId = KeySetID } ;
2021-10-01 09:09:20 +00:00
var helpArray = new List < MapKeySetItem > ( ) { postData } ;
2022-09-27 13:52:49 +00:00
return LocalWebRequest . PostRequestAsyncAndGetContent ( url : Url , header : header , json : helpArray . ToArray ( ) , timeoutMs : 20000 ) ;
2021-10-01 09:09:20 +00:00
}
2024-08-06 13:45:06 +00:00
catch ( Exception ex )
2021-10-01 09:09:20 +00:00
{
2024-08-06 13:45:06 +00:00
return ex . Message ;
2021-10-01 09:09:20 +00:00
}
}
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class MapKeySetItem
{
2022-10-19 10:58:08 +00:00
public String setId { get ; set ; }
public String orderNumber { get ; set ; }
public String radioAdress { get ; set ; }
2021-10-01 09:09:20 +00:00
}
2022-10-19 10:58:08 +00:00
private String CreateKeySet ( String Token , Int32 CountKeys )
2021-10-01 09:09:20 +00:00
{
var Url = $"https://nodes.sms-esaap.com/keygenproxy/api/v3/custom/keysets" ;
var http = ( HttpWebRequest ) WebRequest . Create ( Url ) ;
http . Timeout = 20000 ;
http . Headers . Add ( "Cache-Control" , "no-cache" ) ;
http . Headers . Add ( "X-Node-Token" , Token ) ;
2022-10-19 10:58:08 +00:00
String postData = $"{{\" setCount \ ": 1,\"keysPerSet\": {CountKeys} }}" ;
2021-10-01 09:09:20 +00:00
ASCIIEncoding encoding = new ASCIIEncoding ( ) ;
2022-10-19 10:58:08 +00:00
Byte [ ] byte1 = encoding . GetBytes ( postData ) ;
2021-10-01 09:09:20 +00:00
// Set the content type of the data being posted.
http . ContentType = "application/json; charset=utf-8" ;
http . Method = "POST" ;
// Set the content length of the string being posted.
http . ContentLength = byte1 . Length ;
Stream newStream = http . GetRequestStream ( ) ;
newStream . Write ( byte1 , 0 , byte1 . Length ) ;
newStream . Close ( ) ;
var response = ( HttpWebResponse ) http . GetResponse ( ) ;
var responseStream = response . GetResponseStream ( ) ;
if ( responseStream = = null )
{
throw new InvalidOperationException ( ) ;
}
if ( response . StatusCode ! = HttpStatusCode . OK )
{
}
using ( var sr = new StreamReader ( responseStream ) )
{
var responseJson = sr . ReadToEnd ( ) ;
responseJson = responseJson . Trim ( '"' ) ;
var res = MeterPw . ArrayFromJson ( responseJson ) ;
return res [ 0 ] . SetId . ToString ( ) ;
}
}
public partial class MeterPw
{
[JsonProperty("SetId")]
public Guid SetId { get ; set ; }
[JsonProperty("Keys")]
public List < Key > Keys { get ; set ; }
}
public partial class Key
{
[JsonProperty("Index")]
2022-10-19 10:58:08 +00:00
public Int64 Index { get ; set ; }
2021-10-01 09:09:20 +00:00
[JsonProperty("Key")]
2022-10-19 10:58:08 +00:00
public String KeyKey { get ; set ; }
2021-10-01 09:09:20 +00:00
}
public partial class MeterPw
{
public static MeterPw Empty ( ) = > new MeterPw ( ) { SetId = Guid . Empty , Keys = new List < Key > ( ) } ;
2022-10-19 10:58:08 +00:00
public static MeterPw FromJson ( String json ) = > JsonConvert . DeserializeObject < MeterPw > ( json , Converter . Settings ) ;
public static MeterPw [ ] ArrayFromJson ( String json ) = > JsonConvert . DeserializeObject < MeterPw [ ] > ( json , Converter . Settings ) ;
2021-10-01 09:09:20 +00:00
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling . Ignore ,
DateParseHandling = DateParseHandling . None ,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles . AssumeUniversal }
} ,
} ;
}
[Route("GetKeySetId")]
public async Task < HttpResponseMessage > GetKeySetId ( String PcbId )
{
try
{
var returnKeySetId = "Not assigned" ;
using ( var dataAccess = new SqlDataAccess ( GlobalConfig . connectionString . Value ) )
{
var dt = dataAccess . ExecuteQuery ( $"select [KeySetId] from dbo.Genesis_PcbPassword where PcbId like '{PcbId}'" ) ;
foreach ( var item in dt . Select ( ) )
{
returnKeySetId = item [ "KeySetId" ] . ToString ( ) ;
}
}
return Request . CreateResponse ( HttpStatusCode . OK , await Task . Run ( ( ) = > { return returnKeySetId ; } ) ) ;
}
catch ( Exception ex )
{
return Request . CreateResponse ( HttpStatusCode . ExpectationFailed , ex . ToString ( ) + "<br >" + ex . InnerException . ToString ( ) ) ;
}
}
2025-07-15 14:25:38 +00:00
public class PwdExport
{
public int ID ;
public String PWD ;
public DateTime ExportDate ;
2021-10-01 09:09:20 +00:00
2025-07-15 14:25:38 +00:00
public PwdExport ( int id , string pwd , DateTime exportDate )
{
this . ID = id ;
this . PWD = pwd ;
this . ExportDate = exportDate ;
}
}
2021-10-01 09:09:20 +00:00
}
}