老马和老马
前几天,马斯克不仅发推文表示自己最喜欢的加密货币可能是狗狗币,其还把自己的Twitter账户简介改为了“狗狗币前CEO ”。
一代“带货达人”,“硅谷钢铁侠”,好像当了首富之后有点任性啊,这和马老板有的一拼啊,两个老马都这么让人眼前一亮,这让我想起了宋丹丹小品中的一句话:“他们名人都这样”,有钱之后就任性,这玩意好像写在人们的基因里了。
马斯克为币圈的一阵摇旗呐喊,直接带起了一波上涨行情,有人欢喜有人愁,真是太热闹了。当然,小弟作为一个不炒币的小屌丝,别人的热闹,与我无关,毕竟又穷又胆小,但是,作为一个上班的IT男,如何在老板眼皮子低下对数字货币盯盘,这个还是可以考虑考虑的,毕竟这是个分分钟几百万上下的生意,为韭菜服务是一个很重要的使命。
怎样躲老板
作为一个android开发者,你的工作就是在android studio上写代码,离开了android studio页面,基本上就可以认为你摸鱼了,而android studio又是基于idea开发的,如何摸鱼盯盘又不被发现,idea插件无疑就是一个最好的选择了。
开发
效果
由效果可以看出,这里在idea中创建了一个ToolWindow,在ToolWindow中展示货币信息,每隔30秒进行一次刷新。
API的获取
行情信息的api通过非小号公共api获取的,文档见:** https://github.com/xiaohao2019/API-docs/blob/master/PublicApi_CN.md **
数据的获取
通过上面的api,定时获取加密货币的行情数据,然后通过EventBus将数据用事件包装发出去,然后在BitCoinWindow中接收数据并展示。
object TickerNetDataApi {
private var isStart = false
private var lastTickerDataList: List? = null
fun scheduleGetTickerData() {
if (!isStart) {
val scheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
scheduledExecutorService.scheduleAtFixedRate(Runnable {
getTickerData()
}, 0, 30, TimeUnit.SECONDS)
isStart = true
}
}
fun getTickerData() {
val uri = "https://fxhapi.feixiaohao.com/public/v1/ticker"
val paratmers: MutableList =
ArrayList()
paratmers.add(BasicNameValuePair("start", "0"))
paratmers.add(BasicNameValuePair("limit", "100"))
try {
val tickerDataList = makeAPICall(uri, paratmers)
tickerDataList?.let {
val lastData = lastTickerDataList
if (lastData == null || lastData[0].last_updated > it[0].last_updated) {
org.greenrobot.eventbus.EventBus.getDefault().post(TickerDataEvent(tickerDataList))
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
@Throws(URISyntaxException::class, IOException::class)
fun makeAPICall(uri: String, parameters: List?): List? {
var responseContent: String? = ""
val query = URIBuilder(uri)
query.addParameters(parameters)
val client = HttpClients.createDefault()
val request = HttpGet(query.build())
request.setHeader(HttpHeaders.ACCEPT, "application/json")
val response = client.execute(request)
response.use { response ->
println(response.statusLine)
val entity = response.entity
responseContent = EntityUtils.toString(entity)
EntityUtils.consume(entity)
}
return responseContent?.let {
GsonUtil.fromJson(it, object : TypeToken>() {
}.type)
}
}
}
创建ToolWindow
ToolWindow是ide的子window,通常用来展示一些信息,比较常见的就是android开发过程中的项目结构目录
创建ToolWindow的方法还是很简单的,首先要创建BitCoinToolWindowFactory继承自ToolWindowFactory
class BitCoinToolWindowFactory : ToolWindowFactory {
override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) {
val bitCoinWindow = BitCoinWindow()
val contentFactory = ContentFactory.SERVICE.getInstance()
val content =
contentFactory.createContent(bitCoinWindow.getContent(), "", false)
toolWindow.contentManager.addContent(content)
TickerNetDataApi.scheduleGetTickerData()
}
}
其中BitCoinWindow就是用来展示信息的UI,idea插件一般是使用swing来构建UI的,这里用JTableLayout进行UI表格的展示
package com.skateboard.coinsplugin.ui
import com.skateboard.coinsplugin.data.TickerDataEvent
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.*
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTable
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableColumn
class BitCoinWindow {
private lateinit var contentPanel: JPanel
private lateinit var contentScrollPane: JScrollPane
private lateinit var coinsTable: JTable
private val columnList =
listOf("排名", "名称", "最新价格", "24h的成交额", "流通市值", "流通数量", "总发行量", "最大发行量", "1小时涨跌幅", "24小时涨跌幅", "7天涨跌幅", "更新时间")
init {
EventBus.getDefault().register(this)
val tableModel = coinsTable.model as DefaultTableModel
for (index in columnList.indices) {
val column = columnList[index]
tableModel.addColumn(column)
}
coinsTable.autoResizeMode = JTable.AUTO_RESIZE_OFF
}
fun getContent(): JPanel = contentPanel
//更新数据
@Subscribe(threadMode = ThreadMode.MAIN)
fun onTickerDataEvent(event: TickerDataEvent) {
if (coinsTable.rowCount <= 0) {
for (tickerData in event.tickerDataList) {
val tableModel = coinsTable.model as DefaultTableModel
tableModel.addRow(tickerData.toArray())
}
} else {
val tableModel = coinsTable.model as DefaultTableModel
val rowVector = Vector>()
for (tickerData in event.tickerDataList) {
rowVector.add(tickerData.toVector())
}
tableModel.setDataVector(rowVector, Vector(columnList))
coinsTable.repaint()
}
fitTableColumns(coinsTable)
}
//适配表格宽度
private fun fitTableColumns(myTable: JTable) {
val header = myTable.tableHeader
val rowCount = myTable.rowCount
val columns = myTable.columnModel.columns
while (columns.hasMoreElements()) {
val column = columns.nextElement() as TableColumn
val col = header.columnModel.getColumnIndex(column.identifier);
var width = (myTable.tableHeader.defaultRenderer.getTableCellRendererComponent(
myTable, column.identifier
, false, false, -1, col
).preferredSize.getWidth()).toInt()
for (row in 0 until rowCount) {
val preferedWidth = (myTable.getCellRenderer(row, col).getTableCellRendererComponent(
myTable,
myTable.getValueAt(row, col), false, false, row, col
).preferredSize.getWidth()).toInt()
width = width.coerceAtLeast(preferedWidth)
}
header.resizingColumn = column
column.width = width + myTable.intercellSpacing.width;
}
}
}
在配置文件中注册
在plugin.xml中注册刚才创建的BitCoinToolWindowFactory
org.example.bitcoinplugin
比特币行情插件
YourCompany
most HTML tags may be used
]]>
com.intellij.modules.platform
这里ToolWindow的名称叫做绿油油,这个名称感觉和这个插件的气质比较符合。
最后
当然,这个插件不可能作为真正的盯盘工具的,因为这个接口有些不稳定,而且不够及时,只是作为idea插件编写的一个小demo,各位大佬不要太较真,另外附上项目地址
** https://github.com/skateboard1991/Chives **