using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using System.Linq; using System.Text; using System.Threading.Tasks; using Txgy.EWS.Client.IDAL; namespace Txgy.EWS.Client.DAL { public class LocalDataAccess : ILocalDataAccess { SQLiteConnection conn = null; SQLiteCommand comm = null; SQLiteDataAdapter adapter = null; SQLiteTransaction trans = null; private void Dispose() { if (trans != null) { trans.Rollback(); trans.Dispose(); trans = null; } if (adapter != null) { adapter.Dispose(); adapter = null; } if (comm != null) { comm.Dispose(); comm = null; } if (conn != null) { conn.Close(); conn.Dispose(); conn = null; } } private bool Connection() { try { if (conn == null) conn = new SQLiteConnection("data source=data.db"); conn.Open(); return true; } catch { return false; } } public List GetLocalFileInfo() { if (this.Connection()) { try { string sql = "select file_name,file_md5 from file_version"; adapter = new SQLiteDataAdapter(sql, conn); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return dataTable.AsEnumerable().Select(d => new string[] { d.Field("file_name"), d.Field("file_md5") }).ToList(); } catch (Exception) { throw; } finally { this.Dispose(); } } return null; } public List GetIcons() { if (this.Connection()) { try { string sql = "select unicode from icons"; adapter = new SQLiteDataAdapter(sql, conn); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return dataTable.AsEnumerable().Select(d => d.Field("unicode")).ToList(); } catch (Exception) { throw; } finally { this.Dispose(); } } return null; } public int GetClientType() { if (this.Connection()) { try { string sql = "select client_type from settings"; comm = new SQLiteCommand(sql, conn); var query = comm.ExecuteScalar(); return query == null ? 0 : int.Parse(query.ToString()); } catch (Exception) { throw; } finally { this.Dispose(); } } return 0; } public string GetRoads() { if (this.Connection()) { try { string sql = "select * from road_info"; adapter = new SQLiteDataAdapter(sql, conn); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return Newtonsoft.Json.JsonConvert.SerializeObject((from q in dataTable.AsEnumerable() select new { RoadId = q.Field("_id"), RoadName = q.Field("road_name"), RoadType = q.Field("road_type") }).ToList()); } catch (Exception) { throw; } finally { this.Dispose(); } } return "[]"; } public string GetDevices(int roadId) { if (this.Connection()) { try { string sql = "select * from device_info where road_id = " + roadId; adapter = new SQLiteDataAdapter(sql, conn); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); return Newtonsoft.Json.JsonConvert.SerializeObject((from q in dataTable.AsEnumerable() select new { DeviceName = q.Field("device_name"), IP = q.Field("ip"), Port = q.Field("port") }).ToList()); } catch (Exception) { throw; } finally { this.Dispose(); } } return "[]"; } } }