//public bool ProcessInteractionLogChanges2() //{ // TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); // DirectoryInfo info = new DirectoryInfo(Configurations.GetAbsoluteInteractionLogFilePath()); // FileInfo[] files = info.GetFiles(); // long max_times = 0; current_interactions.Clear(); // for (int counter = 0; counter < files.Count(); counter++) // { // long time = 0; // try { time = Convert.ToInt64(files[counter].Name.Substring(2)); } // catch (Exception) { continue; } // if (time > Configurations.last_change_interaction_files) // { // try // { // Stream writer = File.OpenWrite(files[counter].FullName); // writer.Close(); // these two lines are to check if it is being used by another process or not // StreamReader reader = new StreamReader(files[counter].FullName); // string[] contents = reader.ReadToEnd().Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); // reader.Close(); // int n = contents.Count(); // List<Interaction_Log> new_logs = new List<Interaction_Log>(); // for (int counter_c = 0; counter_c < n; counter_c++) // { // string[] ilog = contents[counter_c].Split(new char[] { '\t' }); // if (ilog.Count() < 6) // { // continue; // } // Interaction_Log log = new Interaction_Log(); // log.date = Convert.ToDateTime(ilog[0]); // log.details = ilog[5]; // log.touch_id = Convert.ToInt32(ilog[2]); // log.touch_x = Convert.ToDouble(ilog[3]); // log.touch_y = Convert.ToDouble(ilog[4]); // int log_type_id = (from t in db.Interaction_Types // where t.type == ilog[1] // select t.id).Single<int>(); // log.type = log_type_id; // //new_logs.Add(log); // db.Interaction_Logs.InsertOnSubmit(log); // if (counter_c == Configurations.max_submit_changes) // db.SubmitChanges(); // } // //db.Interaction_Logs.InsertAllOnSubmit(new_logs); // db.SubmitChanges(); // } // catch (Exception e) { Log.WriteErrorLog(e); continue; } // if (max_times < time) max_times = time; // current_interactions.Add(files[counter].Name); // } // } // if (max_times != 0) Configurations.last_change_interaction_files = max_times; // return true; //} public int ProcessInteractionLogChanges3() { int result = 0; TableTopDataClassesDataContext db = Configurations.GetTableTopDB(); DirectoryInfo info = new DirectoryInfo(Configurations.GetAbsoluteInteractionLogFilePath()); FileInfo[] files = info.GetFiles(); DateTime max_times = Configurations.last_change_interaction_files; for (int counter = 0; counter < files.Count(); counter++) { DateTime time; try { time = DateTime.ParseExact(files[counter].Name, "yyMMdd", System.Globalization.CultureInfo.InvariantCulture); } catch (Exception) { continue; } if (time >= Configurations.last_change_interaction_files) { if (time > max_times) { max_times = time; } StreamReader reader = new StreamReader(files[counter].FullName); while (!reader.EndOfStream) { string ilog = reader.ReadLine(); byte[] mem_data = Convert.FromBase64String(ilog); MemoryStream str_mem = new MemoryStream(mem_data); BinaryFormatter bformatter = new BinaryFormatter(); Interaction_Log_Serializable i = (Interaction_Log_Serializable)bformatter.Deserialize(str_mem); if (i.id > Configurations.last_change_interaction_files_id) { Interaction_Log i2 = new Interaction_Log(); i2.date = i.date; i2.details = i.details; i2.technical_info = i.technical_info; i2.touch_id = i.touch_id; i2.touch_x = i.touch_x; i2.touch_y = i.touch_y; i2.type = i.type; db.Interaction_Logs.InsertOnSubmit(i2); try { db.SubmitChanges(); } catch (Exception ex) { Log.WriteErrorLog(ex); continue; } Configurations.last_change_interaction_files_id = i.id; result++; } } reader.Close(); } } Configurations.last_change_interaction_files = max_times; return(result); }
//public static long GetCurrentUnixTimestampSeconds() //{ // return (long)(DateTime.UtcNow - unix_epoch).TotalSeconds; //} //public static DateTime DateTimeFromUnixTimestampSeconds(long seconds) //{ // return unix_epoch.AddSeconds(seconds); //} public static bool download_file(string file_name_to_download, string file_name_to_save) { try { System.IO.FileStream file_stream = new System.IO.FileStream(Configurations.GetAbsoluteInteractionLogFilePath() + "d_" + file_name_to_save, System.IO.FileMode.CreateNew); file_stream.Close(); WebClient client = new WebClient(); client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); client.DownloadFile(file_name_to_download, Configurations.GetAbsoluteInteractionLogFilePath() + "d_" + file_name_to_save); return(true); } catch (Exception e) { Log.WriteErrorLog(e); return(false); } }
private int CombineAndSendInteractions(List <Interaction_Log> logs, TableTopDataClassesDataContext db) { int r = 0; for (int counter = 0; counter < logs.Count; counter++) { if (Configurations.sync_interactions_to_server) { SInteractionLog result = server_api.CreateInteractionRecord(logs[counter].type, logs[counter].details, logs[counter].touch_id, logs[counter].touch_x, logs[counter].touch_y, logs[counter].date.ToString()); if (result == null) { if (RESTService.Last_Exception != null) { Log.WriteErrorLog(RESTService.Last_Exception); RESTService.Last_Exception = null; } } else { r++; var existings = from i in db.Interaction_Logs where i.id == logs[counter].id select i; Interaction_Log l = existings.First <Interaction_Log>(); l.technical_info = result.id.ToString(); db.SubmitChanges(); } } if (Configurations.sync_interactions_to_dropbox) { //serialize in file try { Interaction_Log_Serializable log = new Interaction_Log_Serializable(); log.id = logs[counter].id; log.touch_id = logs[counter].touch_id; log.touch_x = logs[counter].touch_x; log.touch_y = logs[counter].touch_y; log.date = logs[counter].date; log.details = logs[counter].details; log.technical_info = logs[counter].technical_info; log.type = logs[counter].type; MemoryStream str_mem = new MemoryStream(); BinaryFormatter bformatter = new BinaryFormatter(); bformatter.Serialize(str_mem, log); string ilog = Convert.ToBase64String(str_mem.ToArray()); string fname = log.date.Date.ToString("yyMMdd"); StreamWriter writer = new StreamWriter(Configurations.GetAbsoluteInteractionLogFilePath() + fname, true); writer.WriteLine(ilog); writer.Close(); } catch (Exception ex) { Log.WriteErrorLog(ex); continue; } r++; } } // make a file //string fname = Configurations.GetAbsoluteInteractionLogFilePath() + "i_" + Configurations.GetUnixTimestampMillis(logs[0].date).ToString(); //StreamWriter writer = new StreamWriter(fname); //for (int counter = 0; counter < logs.Count; counter++) //{ // Interaction_Log l = logs[counter]; // string ilog = l.date.ToString() + "\t" + l.Interaction_Type.type + "\t" + l.touch_id.ToString() + "\t" + // l.touch_x.ToString() + "\t" + l.touch_y.ToString() + "\t" + l.details.Replace('\r', ' ').Replace('\n', ' ').Replace('\t', ' '); // writer.WriteLine(ilog); //} //writer.Close(); // send the file //long start_date = Configurations.GetUnixTimestampMillis(logs[0].date); long end_date = Configurations.GetUnixTimestampMillis(logs[logs.Count - 1].date); //string result = server_api.CreateInteractionFile(start_date, end_date, "i_" + Configurations.GetUnixTimestampMillis(logs[0].date), fname); // set the last_interaction_id to last one in the list // if (result == "OK") Configurations.last_interaction_id = logs[logs.Count - 1].id; return(r); }