Skip to content

Commit

Permalink
Fixes for 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
dechristopher committed Dec 24, 2017
1 parent 580212d commit cdfa7ce
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
28 changes: 18 additions & 10 deletions src/ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ import (
"time"

u "github.com/dechristopher/meraki-ap-crypto-ticker/src/util"
cron "github.com/robfig/cron"
cron "github.com/jasonlvhit/gocron"
)

var (
// BTCPrice is current BTC price
BTCPrice = "0"
// BTCTrendPCT percent change 24h
BTCTrendPCT = ""

// ETHPrice is current ETH price
ETHPrice = "0"
// ETHTrendPCT percent change 24h
ETHTrendPCT = ""

// BothCoins is set if both coins are enabled
BothCoins = false
)
Expand Down Expand Up @@ -57,9 +55,17 @@ func main() {
}

// Otherwise, we start the service to run on the specified interval
c := cron.New()
c.AddFunc("0 */"+strconv.Itoa(u.Conf.UpdateInterval)+" * * * *", func() { setTicker() })
c.Start()
setTicker() // do it upon invocation and then wait the interval

if interval, errParse := strconv.ParseUint(strconv.Itoa(u.Conf.UpdateInterval), 10, 64); errParse != nil {
u.LogErr(errParse.Error())
os.Exit(1)
return
} else {
cron.Every(interval).Minutes().Do(setTicker)
u.Log("Started! " + strconv.Itoa(u.Conf.UpdateInterval) + " minute update intervals.")
<-cron.Start()
}
}

// Hits the Meraki API to set the ticker SSID
Expand All @@ -74,7 +80,7 @@ func setTicker() {
u.Log("Setting ticker...")

ssidName := u.GenSSID(BTCPrice, BTCTrendPCT, ETHPrice, ETHTrendPCT, BothCoins)
fmt.Println("SSID: " + ssidName)
u.Log("SETTING: " + ssidName)

var jsonStr = []byte(`{"name":"` + ssidName + `", "enabled": true}`)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonStr))
Expand All @@ -88,9 +94,10 @@ func setTicker() {
}
defer resp.Body.Close()

fmt.Println("response Status:", resp.Status)
// debug junk
//fmt.Println("response Status:", resp.Status)
//fmt.Println("response Headers:", resp.Header)
/*body, _ :=*/ ioutil.ReadAll(resp.Body)
//body, _ := ioutil.ReadAll(resp.Body)
//fmt.Println("response Body:", string(body))

u.Log("Ticker set!")
Expand All @@ -102,18 +109,19 @@ func pullPrice() error {
var myClient = &http.Client{Timeout: 10 * time.Second}

//r, err := myClient.Get("https://blockchain.info/ticker")
// get the top two cryptocurrencies converted to the desired currency (currently happens to be BTC and ETH)
r, err := myClient.Get("https://api.coinmarketcap.com/v1/ticker/?convert=" + u.Conf.Currency + "&limit=2")
if err != nil {
return err
}
defer r.Body.Close()

// old code as a backup in case coinmarketcap stops existing
/*body, err := ioutil.ReadAll(r.Body)
var f interface{}
json.Unmarshal(body, &f)
m := f.(map[string]interface{})
listing := m[u.Conf.Currency].(map[string]interface{})
price := fmt.Sprintf("%v", listing["last"])
symbol := fmt.Sprintf("%v", listing["symbol"])*/

Expand Down
4 changes: 2 additions & 2 deletions src/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ func trendToArrow(trend string) string {

func fmtTrend(trend string) string {
if strings.Contains(trend, "-") {
return trend
return trend + "%"
}
return "+" + trend
return "+" + trend + "%"
}

// GenSSID takes prices and trends and generates the SSID of the ticker network
Expand Down

0 comments on commit cdfa7ce

Please sign in to comment.