Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Windows.Threading;
- using NguyenThanhTung;
- namespace ThuPhiGiaoThongLocalClient
- {
- public class AppController : CoreBase
- {
- private AppController()
- {
- init();
- }
- private static AppController _share = null;
- /// <summary>
- /// Singleton : chỉ cho phép tạo một đối tượng
- /// </summary>
- /// <returns></returns>
- public static AppController share()
- {
- if (_share == null)
- {
- _share = new AppController();
- }
- return _share;
- }
- //
- protected TagReader _tagReader = null;
- public TagReader tagReader
- {
- get
- {
- return _tagReader;
- }
- }
- protected LanClient _lanClient = new LanClient();
- public LanClient lanClient
- {
- get
- {
- return _lanClient;
- }
- }
- protected Customer defaultCustomer = new Customer()
- {
- name = "N/A"
- };
- protected Vehicle defaultVehicle = new Vehicle()
- {
- name = "N/A",
- vehicleTypeName = "N/A",
- id_number = "N/A"
- };
- protected CustomerLog defaultCustomerRecent = new CustomerLog()
- {
- userName = "N/A"
- };
- protected Tag defaultTag = new Tag()
- {
- code = "N/A"
- };
- protected long defaultPriceToPay = 0;
- protected Customer _currentCustomer = null;
- protected Vehicle _currentVehicle = null;
- protected CustomerLog _currentCustomerRecent = null;
- protected Tag _currentTag = null;
- protected long _currentPriceToPay = 0;
- public Customer currentCustomer
- {
- get
- {
- if (_currentCustomer == null)
- return defaultCustomer;
- return _currentCustomer;
- }
- set
- {
- _currentCustomer = value;
- notifyPropertyChanged("currentCustomer");
- }
- }
- public Vehicle currentVehicle
- {
- get
- {
- if (_currentVehicle == null)
- return defaultVehicle;
- return _currentVehicle;
- }
- set
- {
- _currentVehicle = value;
- notifyPropertyChanged("currentVehicle");
- }
- }
- public CustomerLog currentCustomerRecent
- {
- get
- {
- if (_currentCustomerRecent == null)
- return defaultCustomerRecent;
- return _currentCustomerRecent;
- }
- set
- {
- _currentCustomerRecent = value;
- notifyPropertyChanged("currentCustomerRecent");
- }
- }
- public Tag currentTag
- {
- get
- {
- if (_currentTag == null)
- return defaultTag;
- return _currentTag;
- }
- set
- {
- _currentTag = value;
- notifyPropertyChanged("currentTag");
- }
- }
- public long currentPriceToPay
- {
- get
- {
- return _currentPriceToPay;
- }
- set
- {
- _currentPriceToPay = value;
- notifyPropertyChanged("currentPriceToPay");
- }
- }
- public Device device { get; set; }
- public List<Device> devices { get; set; }
- protected int _deviceType = 0;
- public int deviceType
- {
- get
- {
- return _deviceType;
- }
- set
- {
- _deviceType = value;
- notifyPropertyChanged("deviceType");
- notifyPropertyChanged("deviceTypeText");
- }
- }
- public string deviceTypeText
- {
- get
- {
- switch (deviceType)
- {
- case 1:
- return "Pass Gate";
- case 2:
- return "Enter Gate";
- case 3:
- return "Exit Gate";
- default:
- return "N/A";
- }
- }
- }
- protected int _totalVehicleCount = 0;
- public int totalVehicleCount
- {
- get
- {
- return _totalVehicleCount;
- }
- set
- {
- _totalVehicleCount = value;
- notifyPropertyChanged("totalVehicleCount");
- }
- }
- protected long _totalFee = 0;
- public long totalFee
- {
- get
- {
- return _totalFee;
- }
- set
- {
- _totalFee = value;
- notifyPropertyChanged("totalFee");
- }
- }
- protected List<VehicleType> _vehicleTypes = null;
- public List<VehicleType> vehicleTypes
- {
- get
- {
- if (_vehicleTypes == null)
- {
- Api.getVehicleTypeList(delegate(List<VehicleType> __vehicleTypes)
- {
- vehicleTypes = __vehicleTypes;
- }, Api.defaultErrorCallback);
- }
- return _vehicleTypes;
- }
- set
- {
- _vehicleTypes = value;
- notifyPropertyChanged("vehicleTypes");
- }
- }
- protected bool _autoMakeTransaction = true;
- public bool autoMakeTransaction
- {
- get
- {
- return _autoMakeTransaction;
- }
- set
- {
- _autoMakeTransaction = value;
- notifyPropertyChanged("autoMakeTransaction");
- }
- }
- protected void init()
- {
- _lanClient = new LanClient();
- totalVehicleCount = 0;
- totalFee = 0;
- }
- public bool connectToLocalServer(string host, int port)
- {
- Api.setClient(lanClient);
- _lanClient.connectToServer(host, port);
- return true;
- }
- public void disconnectToLocalServer()
- {
- Callback callback = delegate
- {
- _lanClient.disconnect();
- _lanClient = new LanClient();
- };
- Api.disconnect(delegate
- {
- callback();
- }, delegate(string errorMessage)
- {
- callback();
- });
- }
- public bool startTagReader()
- {
- _tagReader = new TagReader();
- _tagReader.init();
- return true;
- }
- // log
- public event Callback<string> onLogged;
- public void log(string message)
- {
- if (onLogged != null)
- onLogged(message);
- }
- public void handleCustomerInfo(System.Windows.Window window)
- {
- //kiểm tra xem có khách chưa
- if (currentCustomer == null)
- {
- log("Cannot identify customer");
- return;
- }
- log("customer identified");
- // nếu có khách rồi thì gửi yêu cầu tạo giao dịch
- bool result = Util.confirm("Make transactions?", "Confirm");
- //nếu result = false ==> tạo giao dịch thất bại ==> thoát
- if (!result)
- {
- window.Dispatcher.BeginInvoke(new Action(delegate
- {
- new ManualTransactionWindow().ShowDialog();
- }));
- return; //thoát
- }
- /*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
- Hàm này truyền vào id và 2 method callback success và error:
- * Nếu request thành công thì thực hiện hàm callback success, và ngược lại
- * Ở trạng thái này thì không mất tiền giao dịch
- */
- if (deviceType == (int)DeviceType.Enter) //Pass = 1, Enter = 2, Exit = 3, chưa hiểu enter và pass để định nghĩa cái gì
- {
- Api.sendNoticeVehicleEnter(currentCustomer.id, delegate()
- {
- //call back success
- log("Customer's action saved");
- totalVehicleCount++; // đêm số xe lưu thông
- GateController.open(); // mở cổng
- }, delegate(string errorMessage)
- {
- //callback error
- Util.alert(errorMessage);
- });
- }
- else
- {
- /*Nếu không ở trạng thái enter thì tiến hành thực hiện giao dịch
- 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
- * Hàm tạo giao dịch truyền vào id và 2 method callback success và error:
- * Nếu request thành công thì thực hiện hàm callback success, và ngược lại
- * ở trạng thái này thì phải mất phí giao dịch
- */
- // make transaction
- if (currentVehicle.money >= currentPriceToPay)
- {
- Api.makeTransaction(currentCustomer.id, delegate(long transactionId)
- {
- //callback success
- log("Transaction success");
- totalVehicleCount++; // đếm số lượng xe đã lưu thông
- totalFee += currentPriceToPay; //tính số tiền thu được của xe này
- GateController.open(); // tiến hành mở cổng cho xe lưu thông
- }, delegate(string errorMessage)
- {
- //callback error
- Util.alert(errorMessage);
- log("Transaction error");
- });
- }
- else
- {
- log("Customer's account is not enough for payment. Make transaction manually");
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement