Compare commits

..

2 Commits

Author SHA1 Message Date
chteufleur 26baa07126 Compatibility with golang 1.6 2016-04-19 20:39:04 +02:00
chteufleur a601d25b35 Add XMPP support and watching log into web page 2016-04-19 20:36:38 +02:00
11 changed files with 58 additions and 22 deletions

View File

@ -44,7 +44,7 @@ func (c *MainController) Prepare() {
func (c *MainController) Get() {
c.Data["sensors"] = getLastTemps()
c.TplNames = "index.tpl"
c.TplName = "index.tpl"
}
func getLastTemps() []SensorPrint {

View File

@ -27,7 +27,7 @@ func (c *LoginController) Get() {
return
}
c.TplNames = "login.tpl"
c.TplName = "login.tpl"
}
func (c *LoginController) Post() {

View File

@ -45,7 +45,7 @@ func (c *ViewRelayController) Get() {
}
}
c.TplNames = "relay.tpl"
c.TplName = "relay.tpl"
}
func (c *ViewRelayController) Post() {

View File

@ -36,7 +36,7 @@ func (c *SensorsController) Get() {
c.Data["sensors"] = sensors
c.Data["relays"] = relays
c.Data["compteurs"] = compteurs
c.TplNames = "sensors.tpl"
c.TplName = "sensors.tpl"
}
func (c *SensorsController) Post() {

View File

@ -69,7 +69,7 @@ func (c *ViewTeleinfoController) Get() {
c.Data["dataPower"] = template.JS(dataPower)
}
c.TplNames = "teleinfo.tpl"
c.TplName = "teleinfo.tpl"
}

View File

@ -24,7 +24,7 @@ func (c *UserController) Prepare() {
}
func (c *UserController) Get() {
c.TplNames = "user.tpl"
c.TplName = "user.tpl"
}
func (c *UserController) Post() {

View File

@ -19,7 +19,7 @@ type WebSocketController struct {
func (c *WebSocketController) Prepare() {
sess := c.GetSession(variables.SessionName)
if sess == nil {
c.Redirect(variables.LoginRouteNoRegex+variables.UserRoute, 302)
c.Redirect(variables.LoginRouteNoRegex+variables.ViewLogRoute, 302)
} else {
c.Data["IsAuthentificated"] = true
}
@ -32,7 +32,7 @@ func (c *WebSocketController) Prepare() {
// Get method handles GET requests for WebSocketController.
func (c *WebSocketController) Get() {
c.Data["hostWS"] = "ws://"+c.Ctx.Request.Host+variables.WebSocketLogRoute
c.TplNames = "watchlog.tpl"
c.TplName = "watchlog.tpl"
}

View File

@ -41,7 +41,7 @@ func (c *ViewTempController) Get() {
}
c.Data["dataTemp"] = template.JS(dataTemp)
c.TplNames = "temp.tpl"
c.TplName = "temp.tpl"
}
/*

32
main.go
View File

@ -7,6 +7,7 @@ import (
_ "git.kingpenguin.tk/chteufleur/datahouse.git/routers"
"git.kingpenguin.tk/chteufleur/datahouse.git/xmpp"
"git.kingpenguin.tk/chteufleur/datahouse.git/watchlog"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"github.com/astaxie/beego"
@ -14,6 +15,8 @@ import (
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
_ "github.com/mattn/go-sqlite3"
"time"
)
var (
@ -26,12 +29,12 @@ func init() {
db := ""
if database.DatabaseInstance == database.MySQL {
// MySQL
orm.RegisterDriver(database.MySQL, orm.DR_MySQL)
orm.RegisterDriver(database.MySQL, orm.DRMySQL)
url = database.UserDB + ":" + database.PwdDB + "@/" + database.DataBase + "?charset=utf8"
db = "mysql"
} else {
// SQLite
orm.RegisterDriver(database.SQLITE, orm.DR_Sqlite)
orm.RegisterDriver(database.SQLITE, orm.DRSqlite)
url = "datahouse.db"
db = "sqlite3"
}
@ -54,7 +57,32 @@ func main() {
if !user.IsUserExist("admin") {
user.AddUser("admin", "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918")
}
/*
go xmpp_manager.Run()
go watchlog.Run()
*/
go reborn()
time.Sleep(1 * time.Second)
models.ChanRuns <- xmpp_manager.EndRun
time.Sleep(1 * time.Second)
models.ChanRuns <- watchlog.EndRun
beego.Run()
}
func reborn() {
for {
action := <- models.ChanRuns
switch action {
case xmpp_manager.EndRun:
go xmpp_manager.Run()
log.Info("XMPP Started")
case watchlog.EndRun:
go watchlog.Run()
log.Info("Watchlog Started")
}
}
}

View File

@ -4,9 +4,15 @@ import (
"github.com/gorilla/websocket"
"github.com/hpcloud/tail"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"container/list"
"time"
)
const (
EndRun = "EndRunWatchlog"
)
var (
@ -19,6 +25,8 @@ func Run() {
for line := range t.Lines {
broadcastWebSocket(line.Text)
}
time.Sleep(30 * time.Second)
models.ChanRuns <- EndRun
}

View File

@ -6,10 +6,9 @@ import (
"github.com/astaxie/beego/logs"
"git.kingpenguin.tk/chteufleur/go-xmpp.git"
"git.kingpenguin.tk/chteufleur/datahouse.git/models"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/relay"
"git.kingpenguin.tk/chteufleur/datahouse.git/models/variables"
"time"
)
@ -29,6 +28,8 @@ const (
Type_unsubscribed = "unsubscribed"
Type_probe = "probe"
Type_error = "error"
EndRun = "EndRunXMPP"
)
var (
@ -42,7 +43,7 @@ var (
stream = new(xmpp.Stream)
client = new(xmpp.XMPP)
Debug = false
Debug = true
)
func init() {
@ -59,14 +60,14 @@ func must(v interface{}, err error) interface{} {
func Run() {
// Create stream and configure it as a component connection.
log.Info("XMPP Run()")
jid = must(xmpp.ParseJID(JidStr)).(xmpp.JID)
stream = must(xmpp.NewStream(jid.Domain+":"+serverPort, &xmpp.StreamConfig{LogStanzas: Debug})).(*xmpp.Stream)
client = must(xmpp.NewClientXMPP(stream, jid, passwdStr, &xmpp.ClientConfig{InsecureSkipVerify: true})).(*xmpp.XMPP)
mainXMPP()
time.Sleep(5 * time.Second)
Run()
log.Debug("xmpp.Run Ended")
models.ChanRuns <- EndRun
}
@ -75,11 +76,6 @@ func mainXMPP() {
for x := range client.In {
switch v := x.(type) {
case *xmpp.Presence:
case *xmpp.Message:
log.Info("Message received : %s", v.Body)
case *xmpp.Iq:
// TODO check if the caller has privilege to ask
switch v.PayloadName().Space {
@ -94,6 +90,10 @@ func mainXMPP() {
log.Info("recv: %v", x)
}
}
// Send deconnexion
SendPresence(Status_offline, Type_unavailable, "")
log.Debug("mainXMPP Ended")
}