using System; using System.IO; using System.Collections.Generic; using System.Collections; using System.Text; using System.Data.Sql; using System.Data.SqlClient; namespace Sensor.Gateway.DataXfer { /// /// Class accepts a Gateway Xfer file and /// inserts it in the Sensor Stage DB /// public class GatewayFileProcessor { string path, fname, connString, fullFileName, msg; int numBytes; int downloadId; SqlConnection conn; StageDBUpdater dbUpdater; string fileStatus = ""; public GatewayFileProcessor(string connString) { this.connString = connString; initProcessor(); } public GatewayFileProcessor(string path, string fname, int numBytes, string connString) { this.path = path; this.fname = fname; this.fullFileName = path + @"\" + fname; this.numBytes = numBytes; this.connString = connString; initProcessor(); } public string ProcessFile() { int fType = GetFileType(); // Read all the records in the file. StreamReader rdr = null; try { // Insert record in the Download Table first downloadId = dbUpdater.InsertNewDownloadRecord(fname, numBytes); string line; rdr = new StreamReader(fullFileName); if (fType == Constants.STORES_FILE) { ProcessStoresFile(rdr); } else { while ((line = rdr.ReadLine()) != null) { if (fType == Constants.GLOBALS_FILE) { ProcessGlobalsRecord(line); } if (fType == Constants.NETWORK_FILE) { ProcessNetworkRecord(line); } if (fType == Constants.WEATHER_FILE) { ProcessWeatherRecord(line); } } } } catch (Exception ex) { msg += "\n" + ex.ToString() + "\n"; } finally { if (rdr != null) { rdr.Close(); } } string outmsg = msg + "\n" + dbUpdater.getErrorMessage(); return outmsg; } public void InsertStore(Decompressor d) { if (d != null) { try { d.decompress(); } catch (IncompleteRecordException ire) { msg += "warn: incomplete record at the end of " + d.NodeId+"\n"; } catch (Exception e) { msg += "Decompression failed for " + d.NodeId + "\n" + e.ToString(); return; } ArrayList flashRecordColl = d.getFlashRecords(); if (flashRecordColl != null) { dbUpdater.InsertFlashRecords(flashRecordColl); } ArrayList journalRecordColl = d.getJournalRecords(); if (journalRecordColl != null) { for (int i = 0; i < journalRecordColl.Count; i++) { JournalRecord jr = (JournalRecord)journalRecordColl[i]; dbUpdater.InsertJournalRecord(jr); } } ArrayList anchorRecordColl = d.getAnchorRecords(); if (anchorRecordColl != null) { //msg += "Inserting "+anchorRecordColl.Count + "Anchor records for " + d.NodeId; dbUpdater.InsertAnchorRecords(anchorRecordColl); } ArrayList gpsRecordColl = d.getGPSRecords(); if (gpsRecordColl != null) { dbUpdater.InsertGPSAnchorRecords(gpsRecordColl); } } } public void ProcessStoresFile(StreamReader rdr) { string line = null; string lastLine = null; int decompressorNode = -1; Decompressor d = null; while ((line = rdr.ReadLine()) != null) { while (line.Trim().Length == 0 && ((line = rdr.ReadLine()) != null)) ; if (line == null) { break; } //when a new node is encountered Int16 lineNode = Int16.Parse(line.Split(new char[] { ' ' })[0]); if( lineNode != decompressorNode){ //decompress the previous node's store and write it to DB InsertStore(d); FlashRecord fr = dbUpdater.GetLastFlashRecord(lineNode); //create a new decompressor, move to next node d = new Decompressor(downloadId, fr, lineNode); decompressorNode = lineNode; } //read the line into the decompressor if ( (line.Trim().Length > 0 ) &&(lastLine == null || (!line.Equals(lastLine)))) { d.addLine(line); } lastLine = line; } //final node InsertStore(d); } public void ProcessStoresRecord(string record) { ArrayList flashRecordColl = RecordParser.ParseStoresRecord(downloadId, record); if (flashRecordColl != null) dbUpdater.InsertFlashRecords(flashRecordColl); } public void ProcessGlobalsRecord(string record) { JournalRecord jr = RecordParser.ParseGlobalsRecord(downloadId, record); if (jr != null) dbUpdater.InsertJournalRecord(jr); } public void ProcessNetworkRecord(string record) { NetworkRecord nwrcd = RecordParser.ParseNetworkRecord(downloadId, record); if (nwrcd != null) dbUpdater.InsertNetworkRecord(nwrcd); } public void ProcessWeatherRecord(string record) { WeatherRecord weatherRecord = RecordParser.ParseWeatherRecord(downloadId, record); if (weatherRecord != null) dbUpdater.InsertWeatherRecord(weatherRecord); } public int GetFileType() { if (fname.LastIndexOf(Constants.GLOBALS_STRING) > 0) { return Constants.GLOBALS_FILE; } if (fname.LastIndexOf(Constants.STORES_STRING) > 0) { return Constants.STORES_FILE; } if (fname.LastIndexOf(Constants.NETWORK_STRING) > 0) { return Constants.NETWORK_FILE; } if (fname.LastIndexOf(Constants.WEATHER_STRING) > 0) { return Constants.WEATHER_FILE; } return -1; } public void initProcessor () { // Initialize the return message as the name of the file. msg = fname; try { conn = new SqlConnection(connString); dbUpdater = new StageDBUpdater(conn); } catch (SqlException ex) { msg += "\n" + ex.ToString() + "\n"; throw new Exception(ex.ToString(), ex); } } public void appendToFileStatus(string msg) { fileStatus += msg; } public string getFileStatus() { return fileStatus; } public static void Main(string[] args) { GatewayFileProcessor gfp = new GatewayFileProcessor("C:\\Documents and Settings\\carlson\\My Documents","store_6",0,""); gfp.ProcessFile(); } } }