use crate::tcp::{helper::price, Tdx};
use crate::bytes_helper::{u16_from_le_bytes, u32_from_le_bytes};
#[derive(Debug, Clone)]
pub struct SecurityQuotes<'d> {
pub send: Box<[u8]>,
pub stocks: Vec<(u16, &'d str)>,
pub response: Vec<u8>,
pub data: Vec<QuoteData>,
}
impl<'d> Default for SecurityQuotes<'d> {
fn default() -> Self {
Self::new(vec![(0, "000001")])
}
}
impl<'d> SecurityQuotes<'d> {
pub fn new(stocks: Vec<(u16, &'d str)>) -> Self {
let count = stocks.len();
assert!(count > 0 && count <= 80, "股票数量必须在1-80之间");
let pkg_len = (count * 7 + 12) as u16;
let mut send = [0u8; Self::LEN];
send[0..22].copy_from_slice(Self::SEND);
send[6..8].copy_from_slice(&pkg_len.to_le_bytes());
send[8..10].copy_from_slice(&pkg_len.to_le_bytes());
send[20..22].copy_from_slice(&(count as u16).to_le_bytes());
let mut pos = 22;
for (market, code) in &stocks {
send[pos] = *market as u8;
send[pos + 1..pos + 7].copy_from_slice(code.as_bytes());
pos += 7;
}
Self {
send: send.into(),
stocks,
response: Vec::new(),
data: Vec::with_capacity(count),
}
}
}
impl<'a> Tdx for SecurityQuotes<'a> {
type Item = [QuoteData];
const SEND: &'static [u8] = &[
0x0c, 0x01, 0x20, 0x63, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x05, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, ];
const TAG: &'static str = "股票行情快照";
const LEN: usize = 22 + 80 * 7;
fn send(&mut self) -> &[u8] {
let actual_len = 22 + self.stocks.len() * 7;
&self.send[..actual_len]
}
fn parse(&mut self, v: Vec<u8>) {
if v.len() < 4 {
eprintln!("⚠️ 行情数据长度不足: {} 字节(需要至少 4 字节)", v.len());
self.response = v;
self.data = Vec::new();
return;
}
let mut pos = 0;
pos += 2;
let num_stocks = u16_from_le_bytes(&v, pos);
pos += 2;
self.data = Vec::with_capacity(num_stocks as usize);
for i in 0..num_stocks {
if pos + 70 > v.len() {
eprintln!("⚠️ 行情数据不完整,只解析了 {}/{} 只股票", i, num_stocks);
break;
}
let quote = parse_quote(&v, &mut pos);
self.data.push(quote);
}
self.response = v;
}
fn result(&self) -> &Self::Item {
&self.data
}
}
fn parse_quote(data: &[u8], pos: &mut usize) -> QuoteData {
let _market = data[*pos] as u16;
*pos += 1;
let code_bytes = &data[*pos..*pos + 6];
*pos += 6;
let code = unsafe { std::str::from_utf8_unchecked(code_bytes) };
let code = String::from(code); let _active1 = u16_from_le_bytes(data, *pos);
*pos += 2;
let price_rel = price(data, pos);
let _last_close_diff = price(data, pos);
let _open_diff = price(data, pos);
let _high_diff = price(data, pos);
let _low_diff = price(data, pos);
let _reversed_bytes0 = price(data, pos);
let _reversed_bytes1 = price(data, pos);
let vol = price(data, pos);
let _cur_vol = price(data, pos);
let amount_raw = u32_from_le_bytes(data, *pos);
*pos += 4;
let amount = vol_amount(amount_raw as i32);
let _s_vol = price(data, pos);
let _b_vol = price(data, pos);
let _reversed_bytes2 = price(data, pos);
let _reversed_bytes3 = price(data, pos);
let bid1 = price(data, pos);
let ask1 = price(data, pos);
let bid1_vol = price(data, pos);
let ask1_vol = price(data, pos);
let bid2 = price(data, pos);
let ask2 = price(data, pos);
let bid2_vol = price(data, pos);
let ask2_vol = price(data, pos);
let bid3 = price(data, pos);
let ask3 = price(data, pos);
let bid3_vol = price(data, pos);
let ask3_vol = price(data, pos);
let bid4 = price(data, pos);
let ask4 = price(data, pos);
let bid4_vol = price(data, pos);
let ask4_vol = price(data, pos);
let bid5 = price(data, pos);
let ask5 = price(data, pos);
let bid5_vol = price(data, pos);
let ask5_vol = price(data, pos);
let _reversed_bytes4 = u16_from_le_bytes(data, *pos);
*pos += 2;
let _reversed_bytes5 = price(data, pos);
let _reversed_bytes6 = price(data, pos);
let _reversed_bytes7 = price(data, pos);
let _reversed_bytes8 = price(data, pos);
let reversed_bytes9 = u16_from_le_bytes(data, *pos) as f64 / 100.0;
let _active2 = u16_from_le_bytes(data, *pos + 2);
*pos += 4;
let price_calc = price_rel as f64 / 100.0;
QuoteData {
code,
name: String::new(), price: price_calc,
preclose: 0.0, open: price_calc, high: price_calc, low: price_calc, vol: vol as f64 / 100.0,
amount,
bid1: bid1 as f64 / 100.0,
ask1: ask1 as f64 / 100.0,
bid1_vol: bid1_vol as f64 / 100.0,
ask1_vol: ask1_vol as f64 / 100.0,
bid2: bid2 as f64 / 100.0,
ask2: ask2 as f64 / 100.0,
bid2_vol: bid2_vol as f64 / 100.0,
ask2_vol: ask2_vol as f64 / 100.0,
bid3: bid3 as f64 / 100.0,
ask3: ask3 as f64 / 100.0,
bid3_vol: bid3_vol as f64 / 100.0,
ask3_vol: ask3_vol as f64 / 100.0,
bid4: bid4 as f64 / 100.0,
ask4: ask4 as f64 / 100.0,
bid4_vol: bid4_vol as f64 / 100.0,
ask4_vol: ask4_vol as f64 / 100.0,
bid5: bid5 as f64 / 100.0,
ask5: ask5 as f64 / 100.0,
bid5_vol: bid5_vol as f64 / 100.0,
ask5_vol: ask5_vol as f64 / 100.0,
change: 0.0,
change_percent: reversed_bytes9,
time: 0,
}
}
fn vol_amount(ivol: i32) -> f64 {
crate::tcp::helper::vol_amount(ivol)
}
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct QuoteData {
pub code: String,
pub name: String,
pub price: f64,
pub preclose: f64,
pub open: f64,
pub high: f64,
pub low: f64,
pub vol: f64,
pub amount: f64,
pub change: f64,
pub change_percent: f64,
pub bid1: f64,
pub ask1: f64,
pub bid1_vol: f64,
pub ask1_vol: f64,
pub bid2: f64,
pub ask2: f64,
pub bid2_vol: f64,
pub ask2_vol: f64,
pub bid3: f64,
pub ask3: f64,
pub bid3_vol: f64,
pub ask3_vol: f64,
pub bid4: f64,
pub ask4: f64,
pub bid4_vol: f64,
pub ask4_vol: f64,
pub bid5: f64,
pub ask5: f64,
pub bid5_vol: f64,
pub ask5_vol: f64,
pub time: u32,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_security_quotes_default() {
let quotes = SecurityQuotes::default();
assert_eq!(quotes.stocks.len(), 1);
assert_eq!(quotes.stocks[0].0, 0);
assert_eq!(quotes.stocks[0].1, "000001");
}
#[test]
fn test_security_quotes_new() {
let stocks = vec![(0, "000001"), (1, "600000")];
let quotes = SecurityQuotes::new(stocks);
assert_eq!(quotes.stocks.len(), 2);
}
#[test]
#[should_panic(expected = "股票数量必须在1-80之间")]
fn test_security_quotes_empty() {
SecurityQuotes::new(vec![]);
}
#[test]
fn test_connection() {
if std::env::var("RUSTDX_SKIP_INTEGRATION_TESTS").is_ok() {
println!("⚠️ 跳过集成测试 (RUSTDX_SKIP_INTEGRATION_TESTS 已设置)");
return;
}
println!("⚠️ 集成测试需要手动验证(需要实际TCP连接)");
}
}