Skip to content

Commit

Permalink
Telegram remote control and monitoring integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Kasmetski committed Oct 9, 2017
1 parent 89a6680 commit 0c370ce
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 28 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Simple Go program for automatically hard reset a computer with Raspberry Pi or other mini-computer from the GPIO pins
#### Still in progress

![console](screenshot.png)
![console](docs/screenshot.png)

I'm using 5V relay and checking the miners with ping command. If there is no answer - hard-reset with the GPIO pins.

Expand All @@ -13,7 +13,7 @@ I'm using 5V relay and checking the miners with ping command. If there is no ans
### How-to
I'm using Raspberry Pi with 5V relay. Soon I will add detailed instruction but you can find a lot of information how to use 5v relay with Raspberry Pi. This is the basics.

![console](raspberrypi-5v-relay.jpeg)
![console](docs/raspberrypi-5v-relay.jpeg)

Depending of your Raspberry Pi version, download one of the archives
`arm6` - (Raspberry Pi A, A+, B, B+, Zero)
Expand All @@ -34,7 +34,9 @@ Configuration for this program is stored in file config.json .
Example config.json:
```
{
"WaitSeconds": 1800,
"WaitSeconds": 1800, //time in seconds
"StartupCheck" : true, //check miners on program startup
"Log" : true, //If you want output log - true
"Miners": [
{ "Name": "machine 1", "Pin": "40", "Ip": "192.168.0.100", "Info": "R9 290's" },
{ "Name": "machine 2", "Pin": "38", "Ip": "192.168.0.101", "Info": "RX480's" }
Expand Down
29 changes: 16 additions & 13 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
{
"WaitSeconds": 1800,
"StartupCheck" : true,
"Log" : true,
"Log" : true,
"TgBotActivate" : false,
"TgAPIKey" : "API-KEY",
"TgAdminUserName" : "USERNAME",
"Miners": [
{ "Name": "machine 1", "Pin": "40", "Ip": "192.168.0.100", "Info": "R9 290's" },
{ "Name": "machine 2", "Pin": "38", "Ip": "192.168.0.101", "Info": "RX480's" },
{ "Name": "machine 3", "Pin": "37", "Ip": "192.168.0.102", "Info": "RX480's" },
{ "Name": "machine 4", "Pin": "36", "Ip": "192.168.0.103", "Info": "RX480's" },
{ "Name": "machine 5", "Pin": "35", "Ip": "192.168.0.104", "Info": "RX480's" },
{ "Name": "machine 6", "Pin": "33", "Ip": "192.168.0.105", "Info": "RX480's" },
{ "Name": "machine 7", "Pin": "32", "Ip": "192.168.0.106", "Info": "RX480's" },
{ "Name": "machine 8", "Pin": "31", "Ip": "192.168.0.107", "Info": "RX480's" },
{ "Name": "machine 9", "Pin": "29", "Ip": "192.168.0.108", "Info": "RX480's" },
{ "Name": "machine 10", "Pin": "22", "Ip": "192.168.0.109", "Info": "RX480's" },
{ "Name": "machine 11", "Pin": "18", "Ip": "192.168.0.110", "Info": "RX480's" },
{ "Name": "machine 12", "Pin": "16", "Ip": "192.168.0.111", "Info": "RX480's" }
{ "Name": "machine 0", "Pin": "40", "Ip": "192.168.0.100", "Info": "R9 290's" },
{ "Name": "machine 1", "Pin": "38", "Ip": "192.168.0.101", "Info": "RX480's" },
{ "Name": "machine 2", "Pin": "37", "Ip": "192.168.0.102", "Info": "RX480's" },
{ "Name": "machine 3", "Pin": "36", "Ip": "192.168.0.103", "Info": "RX480's" },
{ "Name": "machine 4", "Pin": "35", "Ip": "192.168.0.104", "Info": "RX480's" },
{ "Name": "machine 5", "Pin": "33", "Ip": "192.168.0.105", "Info": "RX480's" },
{ "Name": "machine 6", "Pin": "32", "Ip": "192.168.0.106", "Info": "RX480's" },
{ "Name": "machine 7", "Pin": "31", "Ip": "192.168.0.107", "Info": "RX480's" },
{ "Name": "machine 8", "Pin": "29", "Ip": "192.168.0.108", "Info": "RX480's" },
{ "Name": "machine 9", "Pin": "22", "Ip": "192.168.0.109", "Info": "RX480's" },
{ "Name": "machine 10", "Pin": "18", "Ip": "192.168.0.110", "Info": "RX480's" },
{ "Name": "machine 11", "Pin": "16", "Ip": "192.168.0.111", "Info": "RX480's" }
]
}
18 changes: 12 additions & 6 deletions configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ type MinerConfig struct {

//ConfigurationFile struct to parse config.json
type ConfigurationFile struct {
WaitSeconds int // Period of the timer checks in seconds
StartupCheck bool // Check miners on startup
Log bool //Enable or disable logging
Miners []MinerConfig // An array of the
WaitSeconds int // Period of the timer checks in seconds
StartupCheck bool // Check miners on startup
Log bool //Enable or disable logging
TgBotActivate bool //Enable or disable Telegram bot
TgAPIKey string //Telegram Api key for bot communicationg
TgAdminUserName string //Telegram Username which will control the bot
Miners []MinerConfig // An array of the
}

//Config is the global Config variable
var Config ConfigurationFile

//ReadConfig - read and parse the config file
func ReadConfig() (configFile ConfigurationFile) {
//get binary dir
//os.Args doesn't work the way we want with "go run". You can use next line
//for local dev, but use the original for production.
//dir, err := filepath.Abs("./")
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
dir, err := filepath.Abs("./")
//dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes.
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,35 @@ func main() {
r := raspi.NewAdaptor()

//Read configuration file
configFile := ReadConfig()
Config = ReadConfig()

//parse machines to []Rig struct
miningRigs := make([]Rig, 0)
for _, m := range configFile.Miners {
for _, m := range Config.Miners {
log.Notice("minerConfig:", m)
miningRigs = append(miningRigs, Rig{m.Name, gpio.NewRelayDriver(r, m.Pin), m.IP, m.Info})
}

log.Notice("Configured rigs: ", len(miningRigs))

//Logging machines in two outputs - console & external file
if configFile.Log {
LogMachines()
if Config.Log {
go LogMachines()
}

if Config.TgBotActivate {
go TelegramBot(miningRigs)
}
//Gobot work func
work := func() {
log.Notice("HELLO! I WILL KEEP YOUR MINING RIGS ONLINE!")

//Check machines on startup without waiting the timer. Use with caution. After a power failure, RPI could be ready faster than your machines and start restarting them without need.
if configFile.StartupCheck {
if Config.StartupCheck {
CheckMachines(miningRigs)
}

timer := time.Duration(configFile.WaitSeconds) * time.Second
timer := time.Duration(Config.WaitSeconds) * time.Second
log.Notice("Starting timer: ", timer)

//Check the machines periodically
Expand Down
81 changes: 81 additions & 0 deletions telegram.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import "time"
import "github.com/go-telegram-bot-api/telegram-bot-api"
import "strconv"
import "fmt"

func TelegramBot(rigs []Rig) {
machines := rigs
bot, err := tgbotapi.NewBotAPI(Config.TgAPIKey)
if err != nil {
log.Fatal("Problem with the API key. Fix the problem or disable the TelegramBot from the config file")
}

bot.Debug = false

log.Noticef("TelegramBot Authorteleized on account %s", bot.Self.UserName)

u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates, err := bot.GetUpdatesChan(u)
time.Sleep(time.Millisecond * 500)
updates.Clear()

for update := range updates {
if update.Message == nil {
continue
}
log.Noticef("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.IsCommand() {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
commandArgs := update.Message.CommandArguments()

if update.Message.From.UserName != Config.TgAdminUserName {
msg.Text = "You are not my master"
} else {
args, err := strconv.ParseInt(commandArgs, 10, 32)
if err != nil {
log.Error(err)
}

switch update.Message.Command() {
case "help":
msg.Text = "there is no help for the people here\nbut you can try /status /miners /turnon /turnoff /restart /ping"

case "status":
msg.Text = "I'm fine. Thanks for asking"

case "miners":
msg.Text = printConf()

case "ping":
msg.Text = fmt.Sprintf("Machine online status: %t", machines[args].Ping())

case "restart":
machines[args].Restarter()
msg.Text = fmt.Sprintf("Machine %d was restarted", args)

case "turnon":
machines[args].TurnOn()
msg.Text = fmt.Sprintf("Machine %d started", args)

case "turnoff":
machines[args].ForceShutDown()
msg.Text = fmt.Sprintf("Machine %d was turnedoff", args)

default:
msg.Text = "I don't know that command, try /help"
}
}

bot.Send(msg)
}
}
}

//return string with raw data from config file
func printConf() string {
return fmt.Sprintf("Config:\n%+v", Config)
}

0 comments on commit 0c370ce

Please sign in to comment.