// Copyright 2013-2014 Fuzamei tech Ltd. All rights reserved. package tick // 本文件实现yunbi数据源的tick数据获取下载和保存 import ( "log" "net/http" "time" "tickserver/markinfo" "tickserver/server/market" coinapi "github.com/nntaoli/crypto_coin_api" "github.com/nntaoli/crypto_coin_api/yunbi" ) var yunbiInss = []int{ markinfo.BTCCNY, markinfo.ETCCNY, markinfo.ETHCNY, } // YunbiDS 实现数据源dataSource接口的定义 type YunbiDS struct { *DSBase conf *DsConf yunbi *yunbi.YunBi lastprice []float64 lastvolume []float64 } func init() { drivers[Yunbi] = newYunbiDS } func newYunbiDS(conf *DsConf) (DataSource, error) { log.Println("newYunbiDS") yds := &YunbiDS{ DSBase: NewDsBase(conf), conf: conf, lastprice: make([]float64, len(yunbiInss)), lastvolume: make([]float64, len(yunbiInss)), } yds.insMap = yunbiInsMap() yds.yunbi = yunbi.New(&http.Client{}, "", "") return yds, nil } func (yds *YunbiDS) Name() string { return Yunbi } func (yds *YunbiDS) Run() { log.Println("YunbiDS.run") for { for k, _ := range yunbiInss { yds.getYunbiData(k) time.Sleep(time.Second) } } } func yunbiInsMap() map[int64]*Instrument { insMap := make(map[int64]*Instrument) for _, id := range yunbiInss { x, _ := markinfo.SymbolName(id) u, _ := markinfo.SymbolUint(x) ins := &Instrument{ Id: int64(id), Name: x, ExId: Btc, Type: market.Btcs, PriceInc: u, StartTime: time.Now().Unix() * 1000, } insMap[int64(id)] = ins } return insMap } func (yds *YunbiDS) getYunbiData(index int) { var currency coinapi.CurrencyPair if yunbiInss[index] == markinfo.BTCCNY { currency = coinapi.BTC_CNY } if yunbiInss[index] == markinfo.ETCCNY { currency = coinapi.ETC_CNY } if yunbiInss[index] == markinfo.ETHCNY { currency = coinapi.ETH_CNY } ticker, _ := yds.yunbi.GetTicker(currency) if ticker != nil && (ticker.Last != yds.lastprice[index] || ticker.Vol != yds.lastvolume[index]) { yds.lastprice[index] = ticker.Last yds.lastvolume[index] = ticker.Vol mk := &Market{} mk.Type = IntYunbi mk.InsId = int64(yunbiInss[index]) mk.Timestamp = int64(ticker.Date) * 1000 var ask, bid PP ask[0] = ticker.Sell bid[0] = ticker.Buy mk.Asks = append(mk.Asks, ask) mk.Bids = append(mk.Bids, bid) mk.High = ticker.High mk.LastPrice = ticker.Last mk.Low = ticker.Low mk.LastVolume = ticker.Vol yds.Save(mk) } }