Ejemplo n.º 1
0
        private void clientSub_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            try
            {
                Console.WriteLine(e.Topic);
                string data = System.Text.UTF8Encoding.UTF8.GetString(e.Message);
                Console.WriteLine(data);
                string[] topic      = e.Topic.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                Int64    recvTankId = Convert.ToInt64(topic[1].Substring(1));
                App.Log.LogDebugMessage(String.Format(@"Received Tank Id: {0}", recvTankId));
                JObject o       = JObject.Parse(data);
                UInt64  _level  = Convert.ToUInt64(o["fuel_level"]) / 100;
                UInt64  _wlevel = Convert.ToUInt64(o["water_level"]) / 100;
                UInt16  _thermo = Convert.ToUInt16(o["thermo"]);



                foreach (var _tank in CollecTank)
                {
                    if (_tank.TankId == recvTankId)
                    {
                        _tank.FuelLevel     = (Double)(_level) - _tank.OffsetLevel;
                        _tank.Capacity      = FuelInterpolation(_tank.FuelLevel, recvTankId);
                        _tank.WaterLevel    = _wlevel;
                        _tank.WaterCapacity = FuelInterpolation((Double)_wlevel, recvTankId);
                        _tank.Thermometer   = (Double)_thermo / 100;
                        if (!_tank.HasData)
                        {
                            _tank.HasData = true;
                            DataTable _dtTicket = BusinessHelper.FindLastTicketByTankId(_tank.TankId);
                            if (_dtTicket != null)
                            {
                                DataRow _dr          = _dtTicket.Rows[0];
                                Double  _endcapacity = Convert.ToDouble(_dr["endcapacity"]);
                                Double  _endlevel    = Convert.ToDouble(_dr["endlevel"]);
                                App.Log.LogDebugMessage(String.Format(@"Cap-Level: {0}-{1}", _endcapacity, _endlevel));
                                if ((_endcapacity == 0) && (_endlevel == 0))
                                {
                                    _tank.IsImportFinished = false;
                                    App.Log.LogDebugMessage(String.Format(@"Tank {0} doesn't finish importing!!!", _tank.TankId));
                                }
                            }
                        }
                        App.Log.LogDebugMessage(String.Format(_tank.CapacityText));
                        App.Log.LogDebugMessage(String.Format(_tank.WaterCapacityText));
                        App.Log.LogDebugMessage(String.Format(_tank.ThermoText));
                        break;
                    }
                }
                ObservableCollection <Model.TankModel> _FuelTanks = new ObservableCollection <Model.TankModel>(CollecTank);
                if (CollecTank != null)
                {
                    CollecTank = null;
                }

                CollecTank = _FuelTanks;
            }
            catch (Exception ex)
            {
                App.Log.LogException(ex);
            }
        }
Ejemplo n.º 2
0
        private void DoImportFinish(UInt64 tankId)
        {
            var selTank = (from _tank in CollecTank
                           where _tank.TankId == (long)tankId
                           select _tank).FirstOrDefault() as TankModel;
            DateTime _now = DateTime.Now;

            try
            {
                if (App.Demo != 1)
                {
                    if (MessageBox.Show(String.Format(@"Kết thúc nhập bồn {0}", selTank.Description), @"Nhập nhiên liệu", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.Cancel)
                    {
                        return;
                    }
                    BusinessHelper.SaveTankData(selTank.TankId, selTank.Capacity, selTank.Thermometer, selTank.WaterCapacity, (Byte)SaveDataStatus.SAVE, _now);

                    DataTable dt = BusinessHelper.GetTankList(selTank.TankId);
                    foreach (DataRow _dr in dt.Rows)
                    {
                        long tankid = Convert.ToInt64(_dr["tankid"]);
                        //BusinessHelper.UpdateTankStatus(tankid);
                        BusinessHelper.UpdatePumpStatus(tankid);
                    }
                }


                DataTable    _dtReport     = BusinessHelper.FindLastTicketByTankId(selTank.TankId);
                ImportReport _importReport = new ImportReport();
                long         _ticketId     = -1;
                long         _tankId       = -1;
                if (_dtReport != null)
                {
                    DataRow _dr = _dtReport.Rows[0];
                    _ticketId = Convert.ToInt64(_dr["ticket_id"]);
                    _tankId   = Convert.ToInt64(_dr["tank_id"]);
                    _importReport.StationName    = selTank.StationName;
                    _importReport.ReportDate     = Convert.ToDateTime(_dr["ticketdate"]);
                    _importReport.DriverName     = Convert.ToString(_dr["drivername"]);
                    _importReport.CarPlate       = Convert.ToString(_dr["carplate"]);
                    _importReport.StaffName      = Convert.ToString(_dr["staffname"]);
                    _importReport.StaffPos       = Convert.ToString(_dr["staffpos"]);
                    _importReport.TemplateBottle = Convert.ToString(_dr["templatebottle"]);
                    _importReport.ImportCapacity = Convert.ToDouble(_dr["importcapacity"]);
                    long fuelId = Convert.ToInt64(_dr["fuel_id"]);
                    _importReport.Fuel = BusinessHelper.FindFuelById(fuelId);

                    _importReport.BeginCapacity  = Convert.ToDouble(_dr["capacity"]);
                    _importReport.BeginLevel     = Convert.ToDouble(_dr["level"]);
                    _importReport.BeginTotalPump = Convert.ToUInt64(_dr["pumptotal"]);
                    _importReport.ExStore        = Convert.ToString(_dr["exstore"]);
                    _importReport.SealId         = Convert.ToString(_dr["sealid"]);
                    _importReport.SealStatus     = Convert.ToBoolean(_dr["sealstatus"]);
                }
                else
                {
                    System.Windows.MessageBox.Show(@"Phiếu không tồn tại!!!", @"Kết thúc nhập bồn", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    return;
                }
                _importReport.EndTotalPump = BusinessHelper.FindPumpTotalByTankId(selTank.TankId);
                _importReport.EndCapacity  = selTank.Capacity;
                _importReport.EndLevel     = selTank.FuelLevel;
                _importReport.RealCapacity = _importReport.EndCapacity - _importReport.BeginCapacity;

                if (!BusinessHelper.UpdateImportTicket(_ticketId, _importReport.EndTotalPump, _importReport.EndCapacity, _importReport.EndLevel))
                {
                    App.Log.LogInfoMessage(String.Format("Update ticket {0} failed", _ticketId));
                    System.Windows.MessageBox.Show(@"Lỗi trong quá trình cập nhật phiếu!!!", @"Kết thúc nhập bồn", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                    return;
                }
                else
                {
                    String _pubTopic = String.Format(@"importticket/{0}", _tankId);
                    String _pubData  = String.Format(@"{{""ticketId"": {0} }}", _ticketId);
                    if ((ClientPub != null) && (!ClientPub.IsConnected))
                    {
                        string pubClientId = Guid.NewGuid().ToString();


                        ClientPub.Connect(pubClientId, _MqttUser, _MqttPwd);
                    }
                    ClientPub.Publish(_pubTopic, Encoding.UTF8.GetBytes(_pubData), MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, false);

                    selTank.IsImportFinished = true;

                    App.Log.LogDebugMessage("Topic: " + _pubTopic + "\t Data:" + _pubData);
                }



                ImportReportWindow    importReportWnd = new ImportReportWindow();
                ViewModelImportReport vmImportReport;
                if (App.Demo == 1)
                {
                    vmImportReport = new ViewModelImportReport();
                }
                else
                {
                    vmImportReport = new ViewModelImportReport(_importReport);
                }


                importReportWnd.DataContext = vmImportReport;
                importReportWnd.ShowDialog();
            }
            catch (Exception ex)
            {
                App.Log.LogException(ex);
            }
        }