/// <summary> /// Construct an excel file from a datatable /// </summary> public static Attachment BuildAttachment(DataTable dt, string attachment_filename, string worksheet_name) { // Construct an excel file into a temp file on disk string tempfn = Path.GetTempFileName() + ".xlsx"; try { // Okay, let's put it into an XLSX file using (var wb = new XLWorkbook()) { // Populate a table within this file AddWorksheetTable(wb, dt, worksheet_name); // Save to disk wb.SaveAs(tempfn); } // Here's your attachment Attachment a = new Attachment(tempfn, new System.Net.Mime.ContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")); a.Name = attachment_filename; return(a); // Failed to build an excel file, log it } catch (Exception ex) { SensorProject.LogException("Build Excel File", ex); } // Something failed return(null); }
public override void Execute(IDataStore datastore, ISensor sensor, ICondition condition, SensorCollectEventArgs args) { if (!String.IsNullOrEmpty(KlipfolioId)) { try { TimeSpan ts = DateTime.UtcNow - LastUploadTime; if (args.Data != null) { // Filter to just the amount we care about DateTime end = DateTime.UtcNow; DateTime start = DateTime.MinValue; if (UploadDataWindow != ViewTimeframe.AllTime) { start = end.AddMinutes(-(int)UploadDataWindow); } var list = datastore.RetrieveData(sensor, start, end, false); // Determine the correct URL string UploadUrl = String.Format("https://app.klipfolio.com/api/1/datasources/{0}/data", KlipfolioId); //if (SensorProject.Current.Notifications.UploadReport<SensorData>(list.Data, false, UploadUrl, HttpVerb.PUT, // SensorProject.Current.KlipfolioUsername, SensorProject.Current.KlipfolioPassword)) { // LastUploadTime = DateTime.UtcNow; //} } // Catch problems in uploading } catch (Exception ex) { string headline = String.Format("Error uploading {0} ({1}) to Klipfolio", this.Name, this.Identity); SensorProject.LogException(headline, ex); } } }
/// <summary> /// Upgrade a database to the latest available version. /// All changescripts must be repeatable. /// </summary> /// <param name="conn"></param> private void UpgradeDatabase() { SQLiteConnection conn = null; try { // First, does the database exist? If not, create it if (!File.Exists(_sqlite_filename)) { SQLiteConnection.CreateFile(_sqlite_filename); } // Open it using (conn = new SQLiteConnection(_connstring)) { conn.Open(); // Create initial tables and indexes conn.Execute(Resource1.schema_measurements_table); conn.Execute(Resource1.schema_exceptions_table); conn.Execute(Resource1.schema_measurements_index); conn.Execute(Resource1.schema_exceptions_index); } } catch (Exception ex) { SensorProject.LogException("Upgrading database to latest version", ex); } }
/// <summary> /// Upload this report to a URL data source on the Internet /// </summary> /// <param name="dt"></param> /// <param name="url"></param> /// <param name="username"></param> /// <param name="password"></param> protected bool InternalUploadReport(byte[] contents, string url, HttpVerb verb, string username, string password) { if (String.IsNullOrWhiteSpace(url)) { return(false); } // Is this object intended to upload to a web resource? try { using (var client = new HttpClient()) { // Assemble headers if (!String.IsNullOrEmpty(username) && !String.IsNullOrEmpty(password)) { string authInfo = username + ":" + password; client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.Default.GetBytes(authInfo))); } // Send the HTTP message HttpResponseMessage msg = null; if (verb == HttpVerb.POST) { msg = client.PostAsync(url, new ByteArrayContent(contents)).Result; } else if (verb == HttpVerb.PUT) { msg = client.PutAsync(url, new ByteArrayContent(contents)).Result; } else { throw new NotImplementedException(); } // Read response and log it var s = msg.Content.ReadAsStringAsync().Result; if (msg.IsSuccessStatusCode) { SensorProject.LogMessage("Uploaded successfully to " + url); } else { SensorProject.LogMessage("Error uploading to " + url + ": " + s); } return(true); } // Catch any problems } catch (Exception ex) { SensorProject.LogException("Uploading report", ex); return(false); } }
public override void Load() { using (var f = File.Open(_filename, FileMode.Open)) { using (var b = new BinaryReader(f)) { // Get the version number token int version = b.ReadInt32(); // Get all records while the file has more data - this data is appended endlessly try { while (f.Position < f.Length) { _data.Add(ReadRecord(b)); } } catch (Exception ex) { SensorProject.LogException("BinarySensorDataFile.Load", ex); } b.Close(); } f.Close(); } _dirty = false; }
private void SensorProjectChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { // Only one save at a time, to avoid conflicting writes lock (this) { try { // Serialize to a temporary file, to avoid losing data if we crash in the middle of this if (File.Exists(_temp_filename)) { File.Delete(_temp_filename); } SensorProject.Current.Serialize(_temp_filename); // Rename file if (File.Exists(_filename)) { File.Move(_filename, _old_filename); } File.Move(_temp_filename, _filename); // If anything blew up, keep the old file } catch (Exception ex) { SensorProject.LogException("Serialization", ex); if (File.Exists(_old_filename)) { File.Move(_old_filename, _filename); } // Remove the old filename if it's still there } finally { if (File.Exists(_old_filename)) { File.Delete(_old_filename); } } } }