private bool CheckEnable(ISession session, bool formLoading) { MeteringValue value = session.SpbMeteringValueRepository.Fetch().OrderByDescending(v => v.When).Take(1).AsEnumerable().SingleOrDefault(); if (value != null) { if ((DateTime.UtcNow - value.When).TotalDays < _WeekLength) { MessageBox.Show("Показания приборов учёта вводились недавно. В данный момент ввод новых показаний не требуется.", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); return(false); } } if (formLoading) { CurrentMeteringValue currentValue = session.SpbCurrentMeteringValueRepository.Fetch().AsEnumerable().SingleOrDefault(); if (currentValue != null) { tb_Night.Text = currentValue.Night.ToString(); tb_Day.Text = currentValue.Day.ToString(); tb_KitchenCold.Text = currentValue.KitchenCold.ToString(); tb_KitchenHot.Text = currentValue.KitchenHot.ToString(); tb_BathroomCold.Text = currentValue.BathroomCold.ToString(); tb_BathroomHot.Text = currentValue.BathroomHot.ToString(); } } return(true); }
static void Main(string[] args) { IBus myBus = null; Console.WriteLine("IoT Device is starting"); try { var connection = new ConnectionConfiguration { Port = 5671, UserName = "******", Password = "******", Product = "IoT Hub" }; var hostConfiguration = new HostConfiguration(); hostConfiguration.Host = "10.0.1.50"; hostConfiguration.Port = 5671; hostConfiguration.Ssl.Enabled = true; hostConfiguration.Ssl.ServerName = "rabbitServer"; hostConfiguration.Ssl.CertPath = @"/home/keycert.p12"; //hostConfiguration.Ssl.CertPath = @"c:\temp\keycert.p12"; hostConfiguration.Ssl.CertPassphrase = "strenggeheim"; hostConfiguration.Ssl.AcceptablePolicyErrors = SslPolicyErrors.RemoteCertificateNameMismatch | SslPolicyErrors.RemoteCertificateChainErrors; connection.Hosts = new List <HostConfiguration> { hostConfiguration }; connection.Validate(); myBus = RabbitHutch.CreateBus(connection, services => { }); //myBus = RabbitHutch.CreateBus("host=10.0.1.72"); Console.WriteLine($"Connected with RabbitMQ {myBus.IsConnected}"); if (myBus.IsConnected) { Console.WriteLine("Please enter the count of metering values:"); var countAsString = Console.ReadLine(); var count = Convert.ToInt32(countAsString); var random = new Random(DateTime.Now.Second); for (int i = 0; i < count; i++) { var value = random.Next(1, 1800); MeteringValue mv = new MeteringValue { Timestamp = DateTime.UtcNow, ObisCode = "1-1:1.6.0", Value = value, Host = System.Net.Dns.GetHostName() }; myBus.Publish(mv); Console.WriteLine($"Send datagram {i}"); } } } catch (Exception e) { Console.WriteLine(e); } finally { myBus?.Dispose(); } Console.ReadLine(); }
private void bt_Send_Click(object sender, EventArgs e) { try { using (ISession session = _SessionFactory.OpenSession()) { using (session.Transaction = session.BeginTransaction(IsolationLevel.Snapshot)) { if (!CheckEnable(session, false)) { Close(); } int day = GetTextBoxIntValue(tb_Day); int night = GetTextBoxIntValue(tb_Night); int kitchenCold = GetTextBoxIntValue(tb_KitchenCold); int kitchenHot = GetTextBoxIntValue(tb_KitchenHot); int bathroomCold = GetTextBoxIntValue(tb_BathroomCold); int bathroomHot = GetTextBoxIntValue(tb_BathroomHot); CurrentMeteringValue currentValue = session.SpbCurrentMeteringValueRepository.Fetch().AsEnumerable().SingleOrDefault(); DialogResult dialogResult; if (currentValue != null) { int diffDay = Math.Abs(currentValue.Day - day); int diffNight = Math.Abs(currentValue.Night - night); int diffKitchenCold = Math.Abs(currentValue.KitchenCold - kitchenCold); int diffKitchenHot = Math.Abs(currentValue.KitchenHot - kitchenHot); int diffBathroomCold = Math.Abs(currentValue.BathroomCold - bathroomCold); int diffBathroomHot = Math.Abs(currentValue.BathroomHot - bathroomHot); dialogResult = MessageBox.Show( string.Format("Разница между предсказанными и введёнными значениями.{0}{0}" + "Дневное энергопотребление.{0}- Предсказанное: {1}, введённое: {2}, разница (абс. зн.): {3}.{0}" + "Ночное энергопотребление.{0}- Предсказанное: {4}, введённое: {5}, разница (абс. зн.): {6}.{0}" + "Холодная вода (кухня).{0}- Предсказанное: {7}, введённое: {8}, разница (абс. зн.): {9}.{0}" + "Горячая вода (кухня).{0}- Предсказанное: {10}, введённое: {11}, разница (абс. зн.): {12}.{0}" + "Холодная вода (ванная).{0}- Предсказанное: {13}, введённое: {14}, разница (абс. зн.): {15}.{0}" + "Горячая вода (ванная).{0}- Предсказанное: {16}, введённое: {17}, разница (абс. зн.): {18}.{0}" + "{0}Подтверждаете отправку введённых значений?", Environment.NewLine, currentValue.Day, day, diffDay, currentValue.Night, night, diffNight, currentValue.KitchenCold, kitchenCold, diffKitchenCold, currentValue.KitchenHot, kitchenHot, diffKitchenHot, currentValue.BathroomCold, bathroomCold, diffBathroomCold, currentValue.BathroomHot, bathroomHot, diffBathroomHot), "Подтверждение", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2); } else { dialogResult = DialogResult.Yes; } if (dialogResult != DialogResult.Yes) { return; } MeteringValue value = new MeteringValue { Day = day, Night = night, KitchenCold = kitchenCold, KitchenHot = kitchenHot, BathroomCold = bathroomCold, BathroomHot = bathroomHot }; WriteResult result = session.SpbMeteringValueRepository.Store(value); if (result.ResultType == WriteResultType.Ok) { MessageBox.Show("Показания приборов учёта успешно переданы. Спасибо!", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new InvalidOperationException(string.Format("Ошибка сохранения, код результата {0}, сообщение: {1}.", result.ResultType, result.Message)); } session.Transaction.Commit(); session.Transaction = null; } } } catch (Exception ex) { ShowError(ex); } }