001package org.vishia.guiViewCfg;
002
003import java.io.File;
004
005import org.vishia.byteData.ByteDataSymbolicAccessReadConfig;
006import org.vishia.byteData.RawDataAccess;
007import org.vishia.mainCmd.Report;
008import org.vishia.util.FileSystem;
009
010
011public class OamOutFileReader 
012{
013        /**Index (fast access) of all variable which are contained in the cfg-file. */
014        private final ByteDataSymbolicAccessReadConfig accessOamVariable;
015        
016        /**Associated class which shows the values */
017        //private final OamShowValues showValues;
018        
019        boolean dataValid = false;
020        
021        //private final Map<String, String> indexUnknownVariable = new TreeMap<String, String>();
022        
023        
024        
025        final Report log;
026
027        private final File fileOam;
028        
029        /**To detect whether the file is new. */
030        private long lastTimeFile = 0;
031        
032        /**To detect timeout while file waiting. */
033        private long lastTimeAccess = System.currentTimeMillis();
034        
035        private int timeDiffCheckAccess = 3000;
036        
037        /**To detect whether a new curve point should show. */
038        private long lastTimeCurve = 0;
039        
040        /**The distance between curve points in milliseconds. */
041        private long timeUnitCurve = 1000;
042        
043        
044        int checkWithoutNewdata;        
045        
046        
047        private RawDataAccess dataAccess = new RawDataAccess(); 
048        
049        byte[] binData = new byte[1024];
050        
051        private RawDataAccess dataAccessUcell = new RawDataAccess(); 
052        
053        byte[] binDataUcell = new byte[1152];
054        
055        public OamOutFileReader(
056                String sFileOamValues
057                , String sFileOamUcell
058                , Report log
059        , OamShowValues showValues
060        )
061        { this.log = log;
062                //this.showValues = showValues;
063                accessOamVariable = new ByteDataSymbolicAccessReadConfig(log);
064          dataAccess.assignClear(binData);
065                dataAccess.setBigEndian(true);
066                dataAccessUcell.assignClear(binDataUcell);
067                dataAccessUcell.setBigEndian(true);
068                fileOam = new File(sFileOamValues);
069        
070        }
071        
072        
073        public void readVariableCfg()
074        { int nrofVariable = accessOamVariable.readVariableCfg("GUI/oamVar.cfg");
075          if( nrofVariable>0){
076                log.writeInfoln("success read " + nrofVariable + " variables from file \"GUI/oamVar.cfg\".");
077    }
078        }
079        
080        
081        
082        
083        /**checks whether new data are received.
084         * This routine is called in {@link ViewCfg#execute()} in the main thread.
085         */
086        public void checkData()
087        {
088                long fileTime = fileOam.lastModified();
089                @SuppressWarnings("unused")
090    int nrofBytes;
091                if(fileTime != lastTimeFile){
092                        lastTimeFile = fileTime;
093      if((nrofBytes = FileSystem.readBinFile(fileOam, binData)) >0){
094        ////showValues.show(binData);
095      }
096      timeDiffCheckAccess = 3000;  //timeout set to 3 seconds.
097                  checkWithoutNewdata = 0;
098      lastTimeAccess = System.currentTimeMillis();
099                } else {
100                        checkWithoutNewdata +=1;
101                        if((System.currentTimeMillis() - lastTimeAccess) > timeDiffCheckAccess){
102                                //5 seconds delay:
103                                log.writeError("no data found, checked " + checkWithoutNewdata + "times. Tested file:"
104                                        + fileOam.getAbsolutePath() 
105                                        + (fileOam.exists() ? " - exists, timestamp = " + fileTime : " - not found."));
106                          lastTimeAccess = System.currentTimeMillis();
107                          checkWithoutNewdata = 0;
108                          timeDiffCheckAccess = 20000;  //new message after 20 seconds only.
109                  }
110                }
111                
112                long currentTime = System.currentTimeMillis();
113                if(currentTime - lastTimeCurve >= timeUnitCurve){
114                        if(lastTimeCurve == 0){ 
115                                lastTimeCurve = currentTime;  //the first time. 
116                        } else { 
117                                lastTimeCurve += timeUnitCurve;
118                        }
119                        //writeCurveRoutine.run();
120                }
121        }
122        
123        
124        void stop(){}
125        
126}