Skip to content

Commit

Permalink
Transformer to apax v31 (#282)
Browse files Browse the repository at this point in the history
* db s7 read write attribute

* namespace in plc to lower and sdk to 2311

* db instance readWrite pragma, add to yaml other platforms

* added  to structs readWrite pragma

* pragmas to new line before instance

* remove DTL

* try to fix read api date time for tia proj

* Date  min values for tia projects

- Needs to be verified for min and max rule used in HMI

* web api date

* web api time

* web api time of day for tia

* web api LTimeOfDay

* web api dateTime tia

* eTergetPlatform refactor to eTargetProjectPlatform

* tia DTL type integration to transformer

* fix refactor eTargetProjectPlatform, fix transformer test

* prevent passing null password

---------

Co-authored-by: blazej.kuhajda <[email protected]>
Co-authored-by: PTKu <[email protected]>
  • Loading branch information
3 people authored Feb 6, 2024
1 parent 942edaf commit 538f6ef
Show file tree
Hide file tree
Showing 19 changed files with 1,151 additions and 1,398 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,26 @@ ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
_plcReadRequestData = WebApiConnector.CreateReadRequest(Symbol, _webApiConnector.DBName);
return _plcReadRequestData;
}

}

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PlcWriteRequestData
{
get
{
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDateTIA(CyclicToWrite), _webApiConnector.DBName);
break;
case eTargetProjectPlatform.SIMATICAX:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
break;
default:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
break;
}

return _plcWriteRequestData;
}
}
Expand Down Expand Up @@ -87,8 +98,30 @@ private DateOnly GetFromBinary(string value)

private DateOnly GetFromBinary(long value)
{
var val = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(val).AddYears(1969));
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
int val = ((int)value) - 1;
return DateOnly.FromDayNumber(val).AddYears(1989);

case eTargetProjectPlatform.SIMATICAX:
var valAx = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(valAx).AddYears(1969));

default:
var valdef = value / 100;
return DateOnly.FromDateTime(DateTime.FromBinary(valdef).AddYears(1969));
}
}

private long GetFromDateTIA(DateOnly date)
{
if (date <= TIAMinValue)
date = TIAMinValue;

var retval = date.ToDateTime(TimeOnly.MinValue) - TIAMinValue.ToDateTime(TimeOnly.MinValue);

return (long)retval.TotalDays;
}

private string GetFromDate(DateOnly date)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public WebApiDateTime(ITwinObject parent,

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PeekPlcWriteRequestData => _plcWriteRequestData ?? WebApiConnector.CreateWriteRequest(Symbol, CyclicToWrite, _webApiConnector.DBName);

/// <inheritdoc />
ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
{
Expand All @@ -51,23 +51,49 @@ ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
_plcReadRequestData = WebApiConnector.CreateReadRequest(Symbol, _webApiConnector.DBName);
return _plcReadRequestData;
}

}

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PlcWriteRequestData
{
get
{
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetTIAJObjectFromDate(CyclicToWrite), _webApiConnector.DBName);
break;

case eTargetProjectPlatform.SIMATICAX:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
break;

default:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, GetFromDate(CyclicToWrite), _webApiConnector.DBName);
break;
}

return _plcWriteRequestData;
}
}

/// <inheritdoc />
public void Read(string value)
{
UpdateRead(GetFromBinary(value));
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
UpdateRead(ParseFromTIAJson(value));
break;

case eTargetProjectPlatform.SIMATICAX:
UpdateRead(GetFromBinary(value));
break;

default:
UpdateRead(GetFromBinary(value));
break;
}
}

/// <inheritdoc />
Expand All @@ -76,6 +102,29 @@ public override async Task<DateTime> GetAsync()
return await _webApiConnector.ReadAsync<DateTime>(this);
}

private DateTime ParseFromTIAJson(string value)
{
try
{
var val = Newtonsoft.Json.JsonConvert.DeserializeObject<DateTimeTia>(value);
return new DateTime(val.year, val.month, val.day, val.hour, val.minute, (int)(val.second), (int)((val.second * 1000) % 1000), DateTimeKind.Local);
}
catch (Exception)
{
//swallow
}

return MinValueTIA;
}

private JObject GetTIAJObjectFromDate(DateTime dateTime)
{
if (dateTime <= MinValueTIA)
dateTime = MinValueTIA;

return JObject.FromObject(new DateTimeTia(dateTime));
}

private DateTime GetFromBinary(string value)
{
if (long.TryParse(value, out var val))
Expand Down Expand Up @@ -106,4 +155,29 @@ public override async Task<DateTime> SetAsync(DateTime value)
{
return await _webApiConnector.WriteAsync(this, value);
}
}

public class DateTimeTia
{
public DateTimeTia()
{
}

public DateTimeTia(DateTime fromDateTime)
{
this.year = fromDateTime.Year;
this.month = fromDateTime.Month;
this.day = fromDateTime.Day;
this.hour = fromDateTime.Hour;
this.minute = fromDateTime.Minute;
this.second = (double)fromDateTime.Second;
this.second = second + (((double)fromDateTime.Millisecond) / 1000);
}

public int year { get; set; }
public int month { get; set; }
public int day { get; set; }
public int hour { get; set; }
public int minute { get; set; }
public double second { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ public WebApiLTimeOfDay(ITwinObject parent,

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PeekPlcWriteRequestData => _plcWriteRequestData ?? WebApiConnector.CreateWriteRequest(Symbol, CyclicToWrite, _webApiConnector.DBName);



/// <inheritdoc />
ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
{
Expand All @@ -52,25 +51,57 @@ ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
_plcReadRequestData = WebApiConnector.CreateReadRequest(Symbol, _webApiConnector.DBName);
return _plcReadRequestData;
}

}

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PlcWriteRequestData
{
get
{
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, CyclicToWrite.TotalNanoseconds.ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
break;

case eTargetProjectPlatform.SIMATICAX:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
break;

default:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
break;
}

return _plcWriteRequestData;
}
}

/// <inheritdoc />
public void Read(string value)
{
if (long.TryParse(value, out var val))
switch (_webApiConnector.TargetPlatform)
{
UpdateRead(TimeSpan.FromMilliseconds(val / 1000000));
case eTargetProjectPlatform.TIAPORTAL:
if (long.TryParse(value, out var valTia))
{
UpdateRead(TimeSpan.FromMicroseconds(valTia / 1000)); // value in nanoseconds
}
break;

case eTargetProjectPlatform.SIMATICAX:
if (long.TryParse(value, out var valAx))
{
UpdateRead(TimeSpan.FromMilliseconds(valAx / 1000000));
}
break;

default:
if (long.TryParse(value, out var valdef))
{
UpdateRead(TimeSpan.FromMilliseconds(valdef / 1000000));
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ ApiPlcWriteRequest IWebApiPrimitive.PlcWriteRequestData
{
get
{
// TODO: review this casting to string... reason: there is some problem while creating reuqest from long.
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, ToMicroSeconds(CyclicToWrite), _webApiConnector.DBName);
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, ToMilliseconds(CyclicToWrite), _webApiConnector.DBName);
break;
case eTargetProjectPlatform.SIMATICAX:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, ToMicroSeconds(CyclicToWrite), _webApiConnector.DBName);
break;
default:
// TODO: review this casting to string... reason: there is some problem while creating reuqest from long.
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, ToMicroSeconds(CyclicToWrite), _webApiConnector.DBName);
break;

}

return _plcWriteRequestData;
}
}
Expand All @@ -72,10 +85,10 @@ public void Read(string value)
{
switch (_webApiConnector.TargetPlatform)
{
case eTargetPlatform.S71500:
case eTargetProjectPlatform.TIAPORTAL:
UpdateRead(TimeSpan.FromMilliseconds(val));
break;
case eTargetPlatform.SIMATICAX:
case eTargetProjectPlatform.SIMATICAX:
UpdateRead(TimeSpan.FromMilliseconds(ToMilliseconds(val)));
break;

Expand All @@ -84,6 +97,12 @@ public void Read(string value)
}


private long ToMilliseconds(TimeSpan value)
{
return (long)value.TotalMilliseconds;
}


private long ToMilliseconds(long nanoseconds)
{
return nanoseconds / 1000000L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public WebApiTimeOfDay(ITwinObject parent,

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PeekPlcWriteRequestData => _plcWriteRequestData ?? WebApiConnector.CreateWriteRequest(Symbol, CyclicToWrite, _webApiConnector.DBName);

/// <inheritdoc />
ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
{
Expand All @@ -52,15 +52,28 @@ ApiPlcReadRequest IWebApiPrimitive.PlcReadRequestData
_plcReadRequestData = WebApiConnector.CreateReadRequest(Symbol, _webApiConnector.DBName);
return _plcReadRequestData;
}

}

/// <inheritdoc />
ApiPlcWriteRequest IWebApiPrimitive.PlcWriteRequestData
{
get
{
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (long)CyclicToWrite.TotalMilliseconds, _webApiConnector.DBName);
break;

case eTargetProjectPlatform.SIMATICAX:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
break;

default:
_plcWriteRequestData = WebApiConnector.CreateWriteRequest(Symbol, (CyclicToWrite.TotalMilliseconds * 1000000).ToString(CultureInfo.InvariantCulture), _webApiConnector.DBName);
break;
}

return _plcWriteRequestData;
}
}
Expand All @@ -70,7 +83,20 @@ public void Read(string value)
{
if (long.TryParse(value, out var val))
{
UpdateRead(TimeSpan.FromMilliseconds(val / 1000000));
switch (_webApiConnector.TargetPlatform)
{
case eTargetProjectPlatform.TIAPORTAL:
UpdateRead(TimeSpan.FromMilliseconds(val));
break;

case eTargetProjectPlatform.SIMATICAX:
UpdateRead(TimeSpan.FromMilliseconds(val / 1000000));
break;

default:
UpdateRead(TimeSpan.FromMilliseconds(val / 1000000));
break;
}
}
}

Expand Down
Loading

0 comments on commit 538f6ef

Please sign in to comment.