Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Windows.Threading;
  7. using NguyenThanhTung;
  8.  
  9. namespace ThuPhiGiaoThongLocalClient
  10. {
  11.     public class AppController : CoreBase
  12.     {
  13.         private AppController()
  14.         {
  15.             init();
  16.         }
  17.         private static AppController _share = null;
  18.         /// <summary>
  19.         /// Singleton : chỉ cho phép tạo một đối tượng
  20.         /// </summary>
  21.         /// <returns></returns>
  22.         public static AppController share()
  23.         {
  24.             if (_share == null)
  25.             {
  26.                 _share = new AppController();
  27.             }
  28.             return _share;
  29.         }
  30.  
  31.         //
  32.  
  33.         protected TagReader _tagReader = null;
  34.         public TagReader tagReader
  35.         {
  36.             get
  37.             {
  38.                 return _tagReader;
  39.             }
  40.         }
  41.  
  42.         protected LanClient _lanClient = new LanClient();
  43.         public LanClient lanClient
  44.         {
  45.             get
  46.             {
  47.                 return _lanClient;
  48.             }
  49.         }
  50.  
  51.         protected Customer defaultCustomer = new Customer()
  52.         {
  53.             name = "N/A"
  54.         };
  55.         protected Vehicle defaultVehicle = new Vehicle()
  56.         {
  57.             name = "N/A",
  58.             vehicleTypeName = "N/A",
  59.             id_number = "N/A"
  60.         };
  61.         protected CustomerLog defaultCustomerRecent = new CustomerLog()
  62.         {
  63.             userName = "N/A"
  64.         };
  65.         protected Tag defaultTag = new Tag()
  66.         {
  67.             code = "N/A"
  68.         };
  69.         protected long defaultPriceToPay = 0;
  70.  
  71.         protected Customer _currentCustomer = null;
  72.         protected Vehicle _currentVehicle = null;
  73.         protected CustomerLog _currentCustomerRecent = null;
  74.         protected Tag _currentTag = null;
  75.         protected long _currentPriceToPay = 0;
  76.  
  77.         public Customer currentCustomer
  78.         {
  79.             get
  80.             {
  81.                 if (_currentCustomer == null)
  82.                     return defaultCustomer;
  83.                 return _currentCustomer;
  84.             }
  85.             set
  86.             {
  87.                 _currentCustomer = value;
  88.                 notifyPropertyChanged("currentCustomer");
  89.             }
  90.         }
  91.         public Vehicle currentVehicle
  92.         {
  93.             get
  94.             {
  95.                 if (_currentVehicle == null)
  96.                     return defaultVehicle;
  97.                 return _currentVehicle;
  98.             }
  99.             set
  100.             {
  101.                 _currentVehicle = value;
  102.                 notifyPropertyChanged("currentVehicle");
  103.             }
  104.         }
  105.         public CustomerLog currentCustomerRecent
  106.         {
  107.             get
  108.             {
  109.                 if (_currentCustomerRecent == null)
  110.                     return defaultCustomerRecent;
  111.                 return _currentCustomerRecent;
  112.             }
  113.             set
  114.             {
  115.                 _currentCustomerRecent = value;
  116.                 notifyPropertyChanged("currentCustomerRecent");
  117.             }
  118.         }
  119.         public Tag currentTag
  120.         {
  121.             get
  122.             {
  123.                 if (_currentTag == null)
  124.                     return defaultTag;
  125.                 return _currentTag;
  126.             }
  127.             set
  128.             {
  129.                 _currentTag = value;
  130.                 notifyPropertyChanged("currentTag");
  131.             }
  132.         }
  133.         public long currentPriceToPay
  134.         {
  135.             get
  136.             {
  137.                 return _currentPriceToPay;
  138.             }
  139.             set
  140.             {
  141.                 _currentPriceToPay = value;
  142.                 notifyPropertyChanged("currentPriceToPay");
  143.             }
  144.         }
  145.  
  146.         public Device device { get; set; }
  147.         public List<Device> devices { get; set; }
  148.  
  149.         protected int _deviceType = 0;
  150.         public int deviceType
  151.         {
  152.             get
  153.             {
  154.                 return _deviceType;
  155.             }
  156.             set
  157.             {
  158.                 _deviceType = value;
  159.                 notifyPropertyChanged("deviceType");
  160.                 notifyPropertyChanged("deviceTypeText");
  161.             }
  162.         }
  163.         public string deviceTypeText
  164.         {
  165.             get
  166.             {
  167.                 switch (deviceType)
  168.                 {
  169.                     case 1:
  170.                         return "Pass Gate";
  171.                     case 2:
  172.                         return "Enter Gate";
  173.                     case 3:
  174.                         return "Exit Gate";
  175.                     default:
  176.                         return "N/A";
  177.                 }
  178.             }
  179.         }
  180.  
  181.         protected int _totalVehicleCount = 0;
  182.         public int totalVehicleCount
  183.         {
  184.             get
  185.             {
  186.                 return _totalVehicleCount;
  187.             }
  188.             set
  189.             {
  190.                 _totalVehicleCount = value;
  191.                 notifyPropertyChanged("totalVehicleCount");
  192.             }
  193.         }
  194.  
  195.         protected long _totalFee = 0;
  196.         public long totalFee
  197.         {
  198.             get
  199.             {
  200.                 return _totalFee;
  201.             }
  202.             set
  203.             {
  204.                 _totalFee = value;
  205.                 notifyPropertyChanged("totalFee");
  206.             }
  207.         }
  208.  
  209.         protected List<VehicleType> _vehicleTypes = null;
  210.         public List<VehicleType> vehicleTypes
  211.         {
  212.             get
  213.             {
  214.                 if (_vehicleTypes == null)
  215.                 {
  216.                     Api.getVehicleTypeList(delegate(List<VehicleType> __vehicleTypes)
  217.                     {
  218.                         vehicleTypes = __vehicleTypes;
  219.                     }, Api.defaultErrorCallback);
  220.                 }
  221.                 return _vehicleTypes;
  222.             }
  223.             set
  224.             {
  225.                 _vehicleTypes = value;
  226.                 notifyPropertyChanged("vehicleTypes");
  227.             }
  228.         }
  229.  
  230.         protected bool _autoMakeTransaction = true;
  231.         public bool autoMakeTransaction
  232.         {
  233.             get
  234.             {
  235.                 return _autoMakeTransaction;
  236.             }
  237.             set
  238.             {
  239.                 _autoMakeTransaction = value;
  240.                 notifyPropertyChanged("autoMakeTransaction");
  241.             }
  242.         }
  243.  
  244.         protected void init()
  245.         {
  246.             _lanClient = new LanClient();
  247.             totalVehicleCount = 0;
  248.             totalFee = 0;
  249.         }
  250.  
  251.         public bool connectToLocalServer(string host, int port)
  252.         {
  253.             Api.setClient(lanClient);
  254.             _lanClient.connectToServer(host, port);
  255.             return true;
  256.         }
  257.  
  258.         public void disconnectToLocalServer()
  259.         {
  260.             Callback callback = delegate
  261.             {
  262.                 _lanClient.disconnect();
  263.                 _lanClient = new LanClient();
  264.             };
  265.             Api.disconnect(delegate
  266.             {
  267.                 callback();
  268.             }, delegate(string errorMessage)
  269.             {
  270.                 callback();
  271.             });
  272.         }
  273.  
  274.         public bool startTagReader()
  275.         {
  276.             _tagReader = new TagReader();
  277.             _tagReader.init();
  278.             return true;
  279.         }
  280.  
  281.         // log
  282.  
  283.         public event Callback<string> onLogged;
  284.  
  285.         public void log(string message)
  286.         {
  287.             if (onLogged != null)
  288.                 onLogged(message);
  289.         }
  290.  
  291.         public void handleCustomerInfo(System.Windows.Window window)
  292.         {
  293.             //kiểm tra xem có khách chưa
  294.             if (currentCustomer == null)
  295.             {
  296.                 log("Cannot identify customer");
  297.                 return;
  298.             }
  299.             log("customer identified");
  300.             // nếu có khách rồi thì gửi yêu cầu tạo giao dịch
  301.             bool result = Util.confirm("Make transactions?", "Confirm");
  302.             //nếu result = false ==> tạo giao dịch thất bại ==> thoát
  303.             if (!result)
  304.             {
  305.                 window.Dispatcher.BeginInvoke(new Action(delegate
  306.                 {
  307.                     new ManualTransactionWindow().ShowDialog();
  308.                 }));
  309.                 return; //thoát
  310.             }
  311.             /*Nếu xe ở trạng thái enter, thì tiến hành gửi thông báo thông tin của device : device_id và customer_id
  312.              Hàm này truyền vào id và 2 method callback success và error:
  313.              * Nếu request thành công thì thực hiện hàm callback success, và ngược lại
  314.              * Ở trạng thái này thì không mất tiền giao dịch
  315.              */
  316.            
  317.             if (deviceType == (int)DeviceType.Enter) //Pass = 1, Enter = 2, Exit = 3, chưa hiểu enter và pass để định nghĩa cái gì
  318.             {
  319.                 Api.sendNoticeVehicleEnter(currentCustomer.id, delegate()
  320.                 {
  321.                     //call back success
  322.                     log("Customer's action saved");
  323.                     totalVehicleCount++;   // đêm số xe lưu thông
  324.                     GateController.open();  // mở cổng
  325.                 }, delegate(string errorMessage)
  326.                 {
  327.                     //callback error
  328.                     Util.alert(errorMessage);
  329.                 });
  330.             }
  331.             else
  332.             {
  333.                 /*Nếu không ở trạng thái enter thì tiến hành thực hiện giao dịch
  334.                  Nếu số tiền hiện có của xe lớn hơn số tiền phải trả thì thực hiện thanh toán, không thì hiển thị thông báo không đủ tiền
  335.                  * Hàm tạo giao dịch truyền vào id và 2 method callback success và error:
  336.                  * Nếu request thành công thì thực hiện hàm callback success, và ngược lại
  337.                  * ở trạng thái này thì phải mất phí giao dịch
  338.                  */
  339.                 // make transaction
  340.                 if (currentVehicle.money >= currentPriceToPay)
  341.                 {
  342.                     Api.makeTransaction(currentCustomer.id, delegate(long transactionId)
  343.                     {
  344.                         //callback success
  345.                         log("Transaction success");
  346.                         totalVehicleCount++; // đếm số lượng xe đã lưu thông
  347.                         totalFee += currentPriceToPay;  //tính số tiền thu được của xe này
  348.                         GateController.open();         // tiến hành mở cổng cho xe lưu thông
  349.                     }, delegate(string errorMessage)
  350.                     {
  351.                         //callback error
  352.                         Util.alert(errorMessage);
  353.                         log("Transaction error");
  354.                     });
  355.                 }
  356.                 else
  357.                 {
  358.                     log("Customer's account is not enough for payment. Make transaction manually");
  359.                 }
  360.             }
  361.         }
  362.     }
  363. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement