/// <summary> /// Creates the set request file. /// </summary> /// <param name="portId">The port id.</param> /// <param name="param">The param.</param> /// <returns></returns> public static SmlFile CreateSetRequestFile(string portId, DateTimeParam param) { var handler = new SmlHandler(portId); var smlFile = new SmlFile(); handler.AddOpenRequest(smlFile); handler.AddSetDateTimeParamRequest(smlFile, param); handler.AddCloseRequest(smlFile); return smlFile; }
/// <summary> /// Adds the set date time param request. /// </summary> /// <param name="reqFile">The req file.</param> /// <param name="param">The param.</param> private void AddSetDateTimeParamRequest(SmlFile reqFile, DateTimeParam param) { var timeToSet = param.Data.Value; var localOffset = (short)TimeZoneInfo.Local.BaseUtcOffset.TotalMinutes; var seasonTimeOffset = timeToSet.IsDaylightSavingTime() ? (short)60 : (short)0; var msg = new Core.Sml.Messages.Message() { TransactionId = new[] { (byte)(reqFile.Count + 1) }, GroupNo = 0x00, SetProcParameterRequest = new Core.Sml.Messages.SetProcParameterRequest() { TreePath = new List<Core.Obis.ObisId>() { new Core.Obis.ObisId((ulong)param.ObisCode) }, Tree = new Tree(new Core.Obis.ObisId((ulong)param.ObisCode), new Time(timeToSet.ToUniversalTime()) { Type = TimeType.LocalTimestamp, LocalOffset = localOffset, SeasonTimeOffset = seasonTimeOffset }) } }; //var obisId = (ObisId)param.ObisCode; //var paramTreePath = new SmlTreePath(obisId); //var tree = new SmlTree { ParameterName = new SmlOctetstring(ObisUtil.GetBytes(obisId)) }; //if (param.Data.HasValue == false) //{ // throw new ArgumentNullException("param"); //} //var smlTime = new SmlTime { Type = SmlTime.Choice.LocalTimestamp, UtcTime = param.Data.Value }; //tree.ParameterValue = new SmlProcParValue(smlTime); //var msg = SmlMessageFactory.SetProcParamReq(paramTreePath, tree); //var transactionId = new byte[] { (byte)(reqFile.Count + 1) }; //msg.TransactionId = new SmlOctetstring(transactionId); //msg.GroupNo = 0x00; reqFile.Add(msg); }
public Task<DateTimeParam> ReadParameter(DateTimeParam param) { throw new NotImplementedException(); }
public Task WriteParameter(DateTimeParam param, bool enableManufacturerMode = false) { throw new NotImplementedException("Basemeter has no DateTime"); }
/// <summary> /// Writes the parameter. /// </summary> /// <param name="param">The param.</param> public async Task WriteParameter(DateTimeParam param, bool enableManufacturerMode = false) { if (!param.Data.HasValue) { return; } logger.Info( "Writing Date/Time parameter {0} with value {1}, kind: {2}, DLS: {3}", (ObisId)param.ObisCode, param.Data, param.Data.Value.Kind, param.Data.Value.IsDaylightSavingTime()); if (param.Data.Value.Kind != DateTimeKind.Local) { // convert to localtime always (to get the right timezone info) param.Data = param.Data.Value.ToLocalTime(); } await Retry.DoTask( async () => { var requestFile = SmlHandler.CreateSetRequestFile(portName, param); await SendRequestFile(requestFile, enableManufacturerMode).ConfigureAwait(false); if (writeOnlyParams.Contains(param.ObisCode)) { return; } var result = await ReadParameter(new DateTimeParam { ObisCode = param.ObisCode }); var timeToSet = param.Data.Value.ToLocalTime(); var timeOfMeter = result.Data.Value.ToLocalTime(); if ((timeToSet - timeOfMeter).Duration() > TimeSpan.FromSeconds(100)) { throw new DataException( string.Format( "Failed to write and verify {0:X12}: written: {1}, read: {2}", param.ObisCode, param.Data, result.Data)); } }); }
/// <summary> /// Reads the parameter. /// </summary> /// <param name="param">The param.</param> /// <returns></returns> public async Task<DateTimeParam> ReadParameter(DateTimeParam param) { logger.Info("Reading Date/Time parameter."); return await Retry.Do( async () => { var requestFile = SmlHandler.CreateGetRequestFile(portName, param.ObisCode); var resVal = await SendFile(requestFile).ConfigureAwait(false); CheckAttentionResponse(param.ObisCode, resVal); var val = resVal as Time; if (val != null) { if (val.Type == TimeType.LocalTimestamp || val.Type == TimeType.Timestamp) { param.Data = val.UtcTime; param.ReadAt = DateTime.UtcNow; logger.Info("Responding with meter Date/Time '{0}', processed at '{1}'.", param.Data, param.ReadAt); return param; } } logger.Info("No response for reading Date/Time."); throw new NullReferenceException(); }); }
/// <summary> /// Writes the date and time. /// <remarks>An offset to the host time base can be specified in order to minimize time deviations due to client/server communication. /// </remarks> /// </summary> /// <param name="clientId">The client id.</param> /// <param name="communicationPort">The communication port.</param> /// <param name="hostOffsetSeconds">The offset in seconds to the host date/time. /// <remarks> If the intention is to set the current host time to the meter, then the hostOffsetSeconds should be '0'.</remarks></param> public async Task WriteDateAndTimeWithHostOffset(Guid clientId, CommunicationPort communicationPort, int hostOffsetSeconds) { CheckConfiguration(communicationPort, clientId); // wait until a new second has begun var sw = Stopwatch.StartNew(); while (DateTime.UtcNow.Millisecond > 100 && sw.ElapsedMilliseconds < 2000) { Thread.Sleep(10); } sw.Reset(); var dt = new DateTimeParam() { ObisCode = (long)ObisId.CurrentMeterTime, Data = DateTime.UtcNow.AddSeconds(hostOffsetSeconds) }; await ClientComListDict[communicationPort.ComPortName].ComThread.WriteParameter(dt); }
/// <summary> /// Writes the date and time. /// </summary> /// <param name="clientId">The client id.</param> /// <param name="communicationPort">The communication port.</param> /// <param name="newTime">The new time.</param> public async Task WriteDateAndTime(Guid clientId, CommunicationPort communicationPort, DateTime newTime) { CheckConfiguration(communicationPort, clientId); var dt = new DateTimeParam() { ObisCode = (long)ObisId.CurrentMeterTime, Data = newTime }; await ClientComListDict[communicationPort.ComPortName].ComThread.WriteParameter(dt); }
public async Task<DateTimeParam> ReadDateAndTimeParam(Guid clientId, CommunicationPort communicationPort) { CheckConfiguration(communicationPort, clientId); var dt = new DateTimeParam() { ObisCode = (long)ObisId.CurrentMeterTime, Data = null }; dt = await ClientComListDict[communicationPort.ComPortName].ComThread.ReadParameter(dt); if (dt.Data.HasValue == false) { throw new InvalidDataException("No result for date/time available."); } return dt; }