diff --git a/Cargo.toml b/Cargo.toml index bbc366d..9af561f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "qatrader-rs" +name = "qatrader_rs" version = "0.2.0" authors = ["yutiansut "] edition = "2018" @@ -29,4 +29,5 @@ ndarray = "0.13.0" # ndarray wsq = '0.9.1' crossbeam = "0.7" crossbeam-channel = "0.4" -crossbeam-utils = "0.7" \ No newline at end of file +crossbeam-utils = "0.7" +uuid = { version = "0.8", features = ["serde", "v4"] } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1270993..d73d4a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,20 +15,24 @@ extern crate crossbeam_utils; use crossbeam_channel::bounded; use crossbeam_utils::thread::scope; - +use crate::qawebsocket::{ReqOrder, ReqCancel, ReqTransfer}; +use serde_json::value::Value; +extern crate uuid; +use uuid::Uuid; +use serde::Deserializer; fn main() { let (s1, r1) = bounded(0); - let user_name = "000001".to_string(); + let user_name = "000002".to_string(); { thread::spawn(move || { let mut client = qaeventmq::QAEventMQ{ - amqp: "amqp://admin:admin@127.0.0.1:5672/".to_string(), + amqp: "amqp://admin:admin@192.168.2.118:5672/".to_string(), exchange: "QAORDER_ROUTER".to_string(), model: "direct".to_string(), routing_key: user_name.clone(), @@ -37,7 +41,7 @@ fn main() { qaeventmq::QAEventMQ::consume(client, s1).unwrap(); }); } - let user_name = "000001".to_string(); + let user_name = "000002".to_string(); let mut ws = WebSocket::new(move |out| { qawebsocket::QAtradeR{ @@ -58,7 +62,66 @@ fn main() { let data = r1.recv().unwrap(); println!("receive !!{:?}",data); - sender.send(format!("{}", data)).unwrap(); + let resx:Value = serde_json::from_str(&data).unwrap(); + + let topic = resx["topic"].as_str(); + println!("topic !!{:?}",topic); + match topic.unwrap() { + "sendorder" => { + let order_id = uuid::Uuid::new_v4(); + println!("this is sendorder {:?}",resx); + let order = ReqOrder{ + aid: "insert_order".to_string(), + user_id: resx["account_cookie"].as_str().unwrap().parse().unwrap(), + order_id: order_id.to_string(), + exchange_id: resx["exchange_id"].as_str().unwrap().parse().unwrap(), + instrument_id: resx["code"].as_str().unwrap().parse().unwrap(), + direction: resx["order_direction"].as_str().unwrap().parse().unwrap(), + offset: resx["order_offset"].as_str().unwrap().parse().unwrap(), + volume: resx["volume"].as_i64().unwrap(), + price_type: "LIMIT".to_string(), + limit_price: resx["price"].as_f64().unwrap(), + volume_condition: "ANY".to_string(), + time_condition: "GFD".to_string() + }; + let b = serde_json::to_string(&order).unwrap(); + println!("Pretend to send {:?}", b); + + sender.send(b).unwrap(); + + } + "cancel_order" => { + let cancelorder = ReqCancel{ + aid: "cancel_order".to_string(), + user_id: resx["account_cookie"].as_str().unwrap().parse().unwrap(), + order_id: resx["order_id"].as_str().unwrap().parse().unwrap() + }; + let b = serde_json::to_string(&cancelorder).unwrap(); + println!("Pretend to send cancel {:?}", b); + + sender.send(b).unwrap(); + } + "transfer" => { + let transfermsg = ReqTransfer { + aid: "req_transfer".to_string(), + bank_id: "".to_string(), + future_account: "".to_string(), + future_password: "".to_string(), + bank_password: "".to_string(), + currency: "".to_string(), + amount: 0.0 + }; + let b = serde_json::to_string(&transfermsg).unwrap(); + println!("Pretend to send cancel {:?}", b); + + sender.send(b).unwrap(); + } + + _ => { + println!("non receive! {:?}", resx) + } + } +// sender.send(format!("{}", data)).unwrap(); } diff --git a/src/qawebsocket.rs b/src/qawebsocket.rs index db9ffec..d1700c8 100644 --- a/src/qawebsocket.rs +++ b/src/qawebsocket.rs @@ -61,89 +61,89 @@ impl Handler for QAtradeR{ #[derive(Serialize, Deserialize, Debug)] pub struct Peek { - aid: String, + pub aid: String, } #[derive(Serialize, Deserialize, Debug)] pub struct Broker { - aid: String, - brokers: Vec, + pub aid: String, + pub brokers: Vec, } #[derive(Serialize, Deserialize, Debug)] pub struct ReqLogin { - aid: String, - bid: String, - user_name: String, - password: String + pub aid: String, + pub bid: String, + pub user_name: String, + pub password: String } #[derive(Serialize, Deserialize, Debug)] pub struct ReqOrder { - aid: String, - user_id:String, - order_id: String, - exchange_id: String, - instrument_id: String, - direction: String, - offset: String, - volume: String, - price_type: String, - limit_price: String, - volume_condition: String, - time_condition: String, + pub aid: String, + pub user_id:String, + pub order_id: String, + pub exchange_id: String, + pub instrument_id: String, + pub direction: String, + pub offset: String, + pub volume: i64, + pub price_type: String, + pub limit_price: f64, + pub volume_condition: String, + pub time_condition: String, } #[derive(Serialize, Deserialize, Debug)] pub struct ReqCancel { - aid: String, - user_id:String, - order_id: String + pub aid: String, + pub user_id:String, + pub order_id: String } #[derive(Serialize, Deserialize, Debug)] pub struct ReqQueryBank { - aid: String, - bank_id: String, - future_account: String, - future_password: String, - bank_password: String, - currency: String + pub aid: String, + pub bank_id: String, + pub future_account: String, + pub future_password: String, + pub bank_password: String, + pub currency: String } #[derive(Serialize, Deserialize, Debug)] pub struct ReqQuerySettlement { - aid: String, - trading_day: i32 + pub aid: String, + pub trading_day: i32 } #[derive(Serialize, Deserialize, Debug)] pub struct ReqChangePassword { - aid: String, - old_password: String, - new_password: String + pub aid: String, + pub old_password: String, + pub new_password: String } #[derive(Serialize, Deserialize, Debug)] pub struct ReqTransfer { - aid: String, - bank_id: String, - future_account: String, - future_password: String, - bank_password: String, - currency: String, - amount: f64 + pub aid: String, + pub bank_id: String, + pub future_account: String, + pub future_password: String, + pub bank_password: String, + pub currency: String, + pub amount: f64 } #[derive(Serialize, Deserialize, Debug)] pub struct RtnData { - aid: String, - data: Vec + pub aid: String, + pub data: Vec }