add logtr.sosf for sos straight to specific matrix
parent
73ccc22fd5
commit
a38a627f5a
13
config.json
13
config.json
|
|
@ -1,7 +1,16 @@
|
||||||
{
|
{
|
||||||
"Log": {
|
"Log": {
|
||||||
"Path": "/tmp/truckstop.log",
|
"Path": "/tmp/truckstop.log",
|
||||||
"Level": "err"
|
"Level": "deb",
|
||||||
|
"SOSMatrix": {
|
||||||
|
"ReceiveEnabled": true,
|
||||||
|
"Mock": true,
|
||||||
|
"Homeserver": "https://m.bltrucks.top",
|
||||||
|
"Username": "@bot.m.bltrucks.top",
|
||||||
|
"Token": "mvDWB96KXMF8XhOam8EC5XVdQvSEw0CDeClcSWocBcYkwZX3FPNWZ5uOnQk2EmT1cjpzfeuD7gDYPPjOuyZlI3bE9TE35UjNOlZgi0Tugm25s91iVsbIF6kMZsCIhVMSmEf6w3jxX6wQYOWvmDZ4mu6f5c8wr221EMDcOpEzQV09d1zuBSWgKLBgjqAkYHJZ5dTRIWpEDpPgujhOFZa2ld1HiAOxrJKlIrlfDBN0CUsTlGOGplujDAr4VtpFzNRS",
|
||||||
|
"Device": "TGNIOGKATZ",
|
||||||
|
"Room": "!OYZqtInrBCn1cyz90D:m.bltrucks.top"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"Interval": {
|
"Interval": {
|
||||||
"Input": "5s..10s",
|
"Input": "5s..10s",
|
||||||
|
|
@ -67,7 +76,7 @@
|
||||||
"Mock": true,
|
"Mock": true,
|
||||||
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
|
"LoadPageURIFormat": "https://ntgvision.com/LoadDetails?loadId=%d",
|
||||||
"Username": "noeasyrunstrucking@gmail.com",
|
"Username": "noeasyrunstrucking@gmail.com",
|
||||||
"Password": "thumper123"
|
"Password": "thumper1234"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,10 +10,21 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type Matrix struct {
|
||||||
|
ReceiveEnabled bool
|
||||||
|
Mock bool
|
||||||
|
Homeserver string
|
||||||
|
Username string
|
||||||
|
Token string
|
||||||
|
Device string
|
||||||
|
Room string
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Log struct {
|
Log struct {
|
||||||
Path string
|
Path string
|
||||||
Level logtr.Level
|
Level logtr.Level
|
||||||
|
SOSMatrix Matrix
|
||||||
}
|
}
|
||||||
Interval struct {
|
Interval struct {
|
||||||
Input Duration
|
Input Duration
|
||||||
|
|
@ -49,15 +60,7 @@ type Config struct {
|
||||||
Clients map[string]Client
|
Clients map[string]Client
|
||||||
Storage []string
|
Storage []string
|
||||||
Message struct {
|
Message struct {
|
||||||
Matrix struct {
|
Matrix Matrix
|
||||||
ReceiveEnabled bool
|
|
||||||
Mock bool
|
|
||||||
Homeserver string
|
|
||||||
Username string
|
|
||||||
Token string
|
|
||||||
Device string
|
|
||||||
Room string
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Once bool
|
Once bool
|
||||||
Brokers struct {
|
Brokers struct {
|
||||||
|
|
@ -117,7 +120,7 @@ func AllStates() []State {
|
||||||
return states
|
return states
|
||||||
}
|
}
|
||||||
|
|
||||||
func Refresh() error {
|
func Refresh(soser func() logtr.SOSer) error {
|
||||||
b, err := ioutil.ReadFile(configPath())
|
b, err := ioutil.ReadFile(configPath())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -132,6 +135,9 @@ func Refresh() error {
|
||||||
live.db.Close()
|
live.db.Close()
|
||||||
}
|
}
|
||||||
live = c
|
live = c
|
||||||
|
if soser != nil {
|
||||||
|
logtr.SetSOSer(soser())
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -145,7 +151,7 @@ func Set(other Config) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ioutil.WriteFile(configPath(), b, os.ModePerm)
|
ioutil.WriteFile(configPath(), b, os.ModePerm)
|
||||||
Refresh()
|
Refresh(nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) DB() storage.DB {
|
func (c *Config) DB() storage.DB {
|
||||||
|
|
|
||||||
33
logtr/log.go
33
logtr/log.go
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -18,8 +19,13 @@ const (
|
||||||
DEBUG = Level(8)
|
DEBUG = Level(8)
|
||||||
INFO = Level(9)
|
INFO = Level(9)
|
||||||
ERROR = Level(10)
|
ERROR = Level(10)
|
||||||
|
SOS = Level(15)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type SOSer interface {
|
||||||
|
Send(string) error
|
||||||
|
}
|
||||||
|
|
||||||
func (l Level) MarshalJSON() ([]byte, error) {
|
func (l Level) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(l.String())
|
return json.Marshal(l.String())
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +40,7 @@ func (l *Level) UnmarshalJSON(b []byte) error {
|
||||||
if len(s) > 3 {
|
if len(s) > 3 {
|
||||||
s = s[:3]
|
s = s[:3]
|
||||||
}
|
}
|
||||||
for i := 0; i < int(ERROR)+5; i++ {
|
for i := 0; i < int(SOS)+5; i++ {
|
||||||
l2 := Level(i)
|
l2 := Level(i)
|
||||||
if l2.String() == s {
|
if l2.String() == s {
|
||||||
*l = l2
|
*l = l2
|
||||||
|
|
@ -48,6 +54,7 @@ var logger io.Writer = os.Stderr
|
||||||
var loggerPath string = ""
|
var loggerPath string = ""
|
||||||
var lock = &sync.Mutex{}
|
var lock = &sync.Mutex{}
|
||||||
var level Level = INFO
|
var level Level = INFO
|
||||||
|
var ansoser SOSer = nil
|
||||||
|
|
||||||
func SetLogpath(p string) {
|
func SetLogpath(p string) {
|
||||||
lock.Lock()
|
lock.Lock()
|
||||||
|
|
@ -63,18 +70,34 @@ func SetLogpath(p string) {
|
||||||
loggerPath = p
|
loggerPath = p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SetSOSer(another SOSer) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
|
ansoser = another
|
||||||
|
}
|
||||||
|
|
||||||
func SetLevel(l Level) {
|
func SetLevel(l Level) {
|
||||||
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
level = l
|
level = l
|
||||||
}
|
}
|
||||||
|
|
||||||
func logf(l Level, format string, args []interface{}) {
|
func logf(l Level, format string, args []interface{}) {
|
||||||
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("15:04:05"), l.String(), strings.TrimSpace(format))
|
format = fmt.Sprintf("%v: %v: %s\n", time.Now().Format("15:04:05"), l.String(), strings.TrimSpace(format))
|
||||||
lock.Lock()
|
cLevel := level
|
||||||
defer lock.Unlock()
|
cAnsoser := ansoser
|
||||||
if level <= l {
|
if l >= cLevel {
|
||||||
fmt.Fprintf(os.Stderr, format, args...)
|
fmt.Fprintf(os.Stderr, format, args...)
|
||||||
}
|
}
|
||||||
fmt.Fprintf(logger, format, args...)
|
fmt.Fprintf(logger, format, args...)
|
||||||
|
log.Printf("l==sos=%v, ansoser!=nil=%v", l == SOS, ansoser != nil)
|
||||||
|
if l == SOS && cAnsoser != nil {
|
||||||
|
cAnsoser.Send(fmt.Sprintf(format, args...))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func SOSf(format string, args ...interface{}) {
|
||||||
|
logf(SOS, format, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Infof(format string, args ...interface{}) {
|
func Infof(format string, args ...interface{}) {
|
||||||
|
|
@ -107,6 +130,8 @@ func (l Level) String() string {
|
||||||
return "DEB"
|
return "DEB"
|
||||||
case VERBOSE:
|
case VERBOSE:
|
||||||
return "VER"
|
return "VER"
|
||||||
|
case SOS:
|
||||||
|
return "SOS"
|
||||||
default:
|
default:
|
||||||
return "?"
|
return "?"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
6
main.go
6
main.go
|
|
@ -21,7 +21,7 @@ import (
|
||||||
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
var stateFinder = regexp.MustCompile(`[A-Za-z]+`)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := config.Refresh(); err != nil {
|
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
if config.Get().Message.Matrix.ReceiveEnabled {
|
if config.Get().Message.Matrix.ReceiveEnabled {
|
||||||
|
|
@ -33,7 +33,7 @@ func main() {
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
time.Sleep(config.Get().Interval.Input.Get())
|
time.Sleep(config.Get().Interval.Input.Get())
|
||||||
if err := config.Refresh(); err != nil {
|
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
||||||
logtr.Errorf("failed parsing config: %v", err)
|
logtr.Errorf("failed parsing config: %v", err)
|
||||||
} else {
|
} else {
|
||||||
if config.Get().Message.Matrix.ReceiveEnabled {
|
if config.Get().Message.Matrix.ReceiveEnabled {
|
||||||
|
|
@ -247,7 +247,7 @@ func _main() error {
|
||||||
|
|
||||||
func _mainOne() error {
|
func _mainOne() error {
|
||||||
logtr.Debugf("config.refreshing...")
|
logtr.Debugf("config.refreshing...")
|
||||||
if err := config.Refresh(); err != nil {
|
if err := config.Refresh(message.NewSOSMatrix); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
logtr.Debugf("once...")
|
logtr.Debugf("once...")
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func TestImageUpload(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := config.Refresh(); err != nil {
|
if err := config.Refresh(nil); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
got, err := UploadImage(b)
|
got, err := UploadImage(b)
|
||||||
|
|
|
||||||
|
|
@ -23,15 +23,24 @@ type Matrix struct {
|
||||||
continuation string
|
continuation string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewSOSMatrix() logtr.SOSer {
|
||||||
|
conf := config.Get().Log.SOSMatrix
|
||||||
|
return newMatrix(conf, "0")
|
||||||
|
}
|
||||||
|
|
||||||
func NewMatrix() Matrix {
|
func NewMatrix() Matrix {
|
||||||
conf := config.Get().Message.Matrix
|
conf := config.Get().Message.Matrix
|
||||||
|
return newMatrix(conf, GetMatrixContinuation())
|
||||||
|
}
|
||||||
|
|
||||||
|
func newMatrix(conf config.Matrix, cont string) Matrix {
|
||||||
return Matrix{
|
return Matrix{
|
||||||
homeserver: conf.Homeserver,
|
homeserver: conf.Homeserver,
|
||||||
username: conf.Username,
|
username: conf.Username,
|
||||||
token: conf.Token,
|
token: conf.Token,
|
||||||
room: conf.Room,
|
room: conf.Room,
|
||||||
mock: conf.Mock,
|
mock: conf.Mock,
|
||||||
continuation: GetMatrixContinuation(),
|
continuation: cont,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue