using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace light { internal static class Program { /// /// program for generating light intensity setting /// [STAThread] static void Main() { Lamps lamps = new Lamps(); lamps.generate(); } } internal class Intensity { private int area_id; private DateTime time; private double intensity; public Intensity() { } public Intensity(int area, DateTime time, double intensity) { this.area_id = area; this.intensity = intensity; this.time = time; } public DateTime getTime() { return this.time; } public double getIntensity() { return this.intensity; } public int getArea() { return this.area_id; } } public class Lamps { private static string dbConnection=@"Data Source =(LocalDB)\MSSQLLocalDB; AttachDbFilename =G:\Users\Tyrqi\Documents\skola\BAKALÁŘSKÁ PRÁCE\projekt\light12\light12\mojedb.mdf; Integrated Security = True"; private static int hoursInDay = 24; private static double MOVEMENT_INTENSITY_MINIMAL = 0.5; private static double MOVEMENT_INTENSITY_MIDDLE = 1; private static double MOVEMENT_INTENSITY_MAXIMAL = 1.5; private static int START = 16; private static int END = 8; private static int AREA_COUNT = 1; public Lamps() { } //Method count intensity setting for each area in city public void generate() { int area_id = 1; for (int i = 1; i < AREA_COUNT; i++) { double[] movementsIntensity = countMovementsIntensity(i); if(movementsIntensity==null || isEmpty(movementsIntensity)) continue; Intensity[] intensity = getLightIntensityByMovements(movementsIntensity, area_id); saveNewLightIntensity(intensity); } } // menthod check, if there is any movevents detected in area private bool isEmpty(double[] intensity) { foreach (double d in intensity) { if (d != 0) return false; } return true; } //method create list of light intensities for each hour interval private Intensity[] getLightIntensityByMovements(double[] movements, int area) { Intensity[] intensity = new Intensity[hoursInDay]; for (int i=0;i= MOVEMENT_INTENSITY_MINIMAL) intensity = 0.5; if (movementintensity >= MOVEMENT_INTENSITY_MIDDLE) intensity = 0.8; if (movementintensity > MOVEMENT_INTENSITY_MAXIMAL) intensity = 1; return intensity; } public double[] countMovementsIntensity(int area_id) { int dayCount = 1; int LampsInArea = lampsInArea(area_id); List allMovements= allwalk(area_id); if (allMovements.Count > 0) { DateTime firstDay = allMovements.Min(); DateTime lastDay = allMovements.Max(); dayCount = (int)(lastDay - firstDay).TotalDays + 1; List[] movementsByHours = divideByHours(allMovements); double[] movementsIntensityForArea = getMovementsIntensity(movementsByHours, LampsInArea, dayCount); return movementsIntensityForArea; } else return null; } //method count movement intensity from detected walk through double[] getMovementsIntensity(List[] movementsByHours, int lampsInArea, int dayCount) { double[] movementsIntensity = new double[movementsByHours.Length]; for (int i = 0; i < movementsByHours.Length; i++) { movementsIntensity[i] = (double)movementsByHours[i].Count() /((double)lampsInArea* dayCount); } return movementsIntensity; } //method for inicializing movements list private void inicializeMovementsByHours(List[] movementsByHours) { for(int i = 0; i < movementsByHours.Length; i++) { movementsByHours[i] = new List(); } } // sort movement to groups by hours List[] divideByHours(List allMovements) { List[] movementsByHours = new List[hoursInDay]; inicializeMovementsByHours(movementsByHours); foreach (DateTime movement in allMovements) { movementsByHours[movement.Hour].Add(movement); } return movementsByHours; } // return count of the lamps in area public int lampsInArea(int area_id) { int lampsCount = 0; using (SqlConnection conn = new SqlConnection(dbConnection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT count (lamp_id) FROM lamps where area_id=@ID", conn)) { cmd.Parameters.Add("@ID", SqlDbType.Int); cmd.Parameters["@ID"].Value = area_id; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { lampsCount = reader.GetInt32(0); } } } } return lampsCount; } //read movements from database for selected area public List allwalk( int area_id) { List allMovement = new List(); using (SqlConnection conn = new SqlConnection(dbConnection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT time FROM movements m join lamps l on l.lamp_id=m.lamp_id where l.area_id=@ID ", conn)) { cmd.Parameters.Add("@ID", SqlDbType.Int); cmd.Parameters["@ID"].Value = area_id; using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { allMovement.Add((DateTime)reader.GetSqlDateTime(0)); } } } } return allMovement; } //insert one intensity setting to database private void insert(Intensity intensity, DateTime saveTime) { using (SqlConnection conn = new SqlConnection(dbConnection)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("INSERT into intensitySetting (area_id, time, intensity, saveDate)values (@AREA,@TIME,@INTENSITY,@SAVE)", conn)) { cmd.Parameters.Add("@AREA", SqlDbType.Int); cmd.Parameters["@AREA"].Value = intensity.getArea(); cmd.Parameters.Add("@TIME", SqlDbType.DateTime); cmd.Parameters["@TIME"].Value = intensity.getTime(); cmd.Parameters.Add("@INTENSITY", SqlDbType.Float); cmd.Parameters["@INTENSITY"].Value = intensity.getIntensity(); cmd.Parameters.Add("@SAVE", SqlDbType.DateTime); cmd.Parameters["@SAVE"].Value = saveTime; cmd.ExecuteNonQuery(); } } } // save list of intensity setting for area void saveNewLightIntensity(Intensity[] intensity) { // getlightStartHour List ChangedIntensity = new List(); double lastIntensity = 0; for (int i=0;i= START || intensity[hour].getTime().Hour < END) && lastIntensity!= intensity[hour].getIntensity()) { ChangedIntensity.Add(intensity[hour]); lastIntensity= intensity[hour].getIntensity(); } } foreach (Intensity oneRow in ChangedIntensity) { insert(oneRow, DateTime.Now); } } } }