|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Oak Ridge National Laboratory. |
| 3 | + * Copyright (c) 2022 Brookhaven National Laboratory. |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/legal/epl-v10.html |
| 8 | + ******************************************************************************/ |
| 9 | +package org.phoebus.pv.sys; |
| 10 | + |
| 11 | +import org.phoebus.pv.sim.SimulatedStringPV; |
| 12 | +import org.phoebus.util.time.TimeParser; |
| 13 | +import org.phoebus.util.time.TimestampFormats; |
| 14 | + |
| 15 | +import java.time.Instant; |
| 16 | +import java.time.format.DateTimeFormatter; |
| 17 | +import java.time.temporal.TemporalAmount; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** System "time" PV |
| 21 | + * @author Kunal Shroff, Kay Kasemir, based on similar code in diirt |
| 22 | + */ |
| 23 | +public class TimeOffsetPV extends SimulatedStringPV |
| 24 | +{ |
| 25 | + private DateTimeFormatter formatter; |
| 26 | + private TemporalAmount temporalAmount; |
| 27 | + private double updateRate; |
| 28 | + |
| 29 | + /** |
| 30 | + * Factory method for creating a time pv with user defined offset, format, and update rate. |
| 31 | + * |
| 32 | + * @param name pv name |
| 33 | + * @param parameters a list of optional parameters defining timeoffset, format, update rate |
| 34 | + * @return a new TimeOffsetPV |
| 35 | + */ |
| 36 | + public static TimeOffsetPV forParameters(final String name, final List<String> parameters) throws Exception { |
| 37 | + if(parameters != null && !parameters.isEmpty()) { |
| 38 | + if(parameters.size() == 1) { |
| 39 | + return new TimeOffsetPV(name, parameters.get(0), TimestampFormats.SECONDS_FORMAT, 1.0); |
| 40 | + } else if (parameters.size() == 2) { |
| 41 | + return new TimeOffsetPV(name, parameters.get(0), TimestampFormats.SECONDS_FORMAT, Double.valueOf(parameters.get(1))); |
| 42 | + } else if (parameters.size() == 3) { |
| 43 | + DateTimeFormatter formatter; |
| 44 | + switch (parameters.get(1).toLowerCase()) { |
| 45 | + case "full": |
| 46 | + formatter = TimestampFormats.FULL_FORMAT; |
| 47 | + break; |
| 48 | + case "milli": |
| 49 | + formatter = TimestampFormats.MILLI_FORMAT; |
| 50 | + break; |
| 51 | + case "seconds": |
| 52 | + formatter = TimestampFormats.SECONDS_FORMAT; |
| 53 | + break; |
| 54 | + case "datetime": |
| 55 | + formatter = TimestampFormats.DATETIME_FORMAT; |
| 56 | + break; |
| 57 | + case "date": |
| 58 | + formatter = TimestampFormats.DATE_FORMAT; |
| 59 | + break; |
| 60 | + case "time": |
| 61 | + formatter = TimestampFormats.TIME_FORMAT; |
| 62 | + break; |
| 63 | + default: |
| 64 | + formatter = TimestampFormats.SECONDS_FORMAT; |
| 65 | + break; |
| 66 | + } |
| 67 | + return new TimeOffsetPV(name, parameters.get(0), formatter, Double.valueOf(parameters.get(2))); |
| 68 | + } |
| 69 | + } else { |
| 70 | + return new TimeOffsetPV(name, "now", TimestampFormats.SECONDS_FORMAT, 1.0); |
| 71 | + } |
| 72 | + throw new Exception("sim://timeOffset needs no parameters or (offset, update_seconds) or (offset, format, update_seconds)"); |
| 73 | + } |
| 74 | + |
| 75 | + public TimeOffsetPV(final String name, final String offset, final DateTimeFormatter formatter, final double updateRate) { |
| 76 | + super(name); |
| 77 | + this.temporalAmount = TimeParser.parseTemporalAmount(offset); |
| 78 | + this.formatter = formatter; |
| 79 | + this.updateRate = updateRate; |
| 80 | + start(updateRate); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public String compute() |
| 85 | + { |
| 86 | + return formatter.format(Instant.now().minus(temporalAmount)); |
| 87 | + } |
| 88 | +} |
0 commit comments