Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformer to apax v31 #282

Merged
merged 17 commits into from
Feb 6, 2024
Merged
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
Loading