Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JSON import function #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions server/main/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strconv"
"strings"
"time"
"bufio"

"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -327,6 +328,7 @@ func Run() (err error) {
if UseMQTT {
r.GET("/api/v1/mqtt/:family", handlerMQTT) // handler for setting MQTT
}
r.POST("/import", handlerImport) // import data from json file
r.POST("/data", handlerData) // typical data handler
r.POST("/passive", handlerReverse) // typical data handler
r.POST("/learn", handlerFIND) // backwards-compatible with FIND for learning
Expand Down Expand Up @@ -626,6 +628,48 @@ func handlerNow(c *gin.Context) {
c.String(200, strconv.Itoa(int(time.Now().UTC().UnixNano()/int64(time.Millisecond))))
}

func handlerImport(c *gin.Context){
file, header , err := c.Request.FormFile("import_file")
filename := header.Filename
fmt.Println(filename)
if err != nil {
err = errors.Wrap(err, "problem with file loading")
c.Redirect(http.StatusMovedPermanently, "/")
c.HTML(http.StatusNoContent , "login.tmpl", gin.H{
"ImportMessage": "Import failed!",
})
}
defer file.Close()
justSave := true // only save - no learning
scanner := bufio.NewScanner(file)
for scanner.Scan() {
var d models.SensorData
// read each line - parse json and put into the database
err := json.Unmarshal([]byte(scanner.Text()), &d)
if err != nil {
err = errors.Wrap(err, "problem with json parsing")
continue
}

err = d.Validate()
if err != nil {
err = errors.Wrap(err, "problem validating data")
continue
}

// process data
err = processSensorData(d, justSave)
if err != nil {
continue
}
}

c.Redirect(http.StatusMovedPermanently, "/")
c.HTML(http.StatusOK, "login.tmpl", gin.H{
"ImportMessage": "Done!",
})
}

func handlerData(c *gin.Context) {
message, err := func(c *gin.Context) (message string, err error) {
justSave := c.DefaultQuery("justsave", "0") == "1"
Expand Down
68 changes: 50 additions & 18 deletions server/main/templates/login.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,58 @@
</head>

<body>
<form class="form-signin" method="post" action="/">
<div class="text-center mb-4">
<img class="mb-4" src="/static/img/find3-promo-black.png" alt="" width="180">
<h1 class="h3 mb-3 font-weight-normal">Login to dashboard</h1>
<p>Enter your family name to continue.</p>
{{ with .Message }}
<div class="alert alert-info">
{{.}}
<div class="form-signin">
<form method="post" action="/">
<div class="text-center mb-4">
<img class="mb-4" src="/static/img/find3-promo-black.png" alt="" width="180">
<h1 class="h3 mb-3 font-weight-normal">Login to dashboard</h1>
<p>Enter your family name to continue.</p>
{{ with .Message }}
<div class="alert alert-info">
{{.}}
</div>
{{ end}}
</div>

<div class="form-label-group">
<input type="text" id="inputFamily" name="inputFamily" class="form-control" placeholder="Email address" required autofocus>
<label for="inputFamily">Family</label>
</div>
{{ end}}
</div>

<div class="form-label-group">
<input type="text" id="inputFamily" name="inputFamily" class="form-control" placeholder="Email address" required autofocus>
<label for="inputFamily">Family</label>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

<hr>

<div class="card">
<button class="btn btn-secondary btn-block" name="submit" id="import-box" type="submit">Database import</button>
{{ with .ImportMessage }}
<div class="alert alert-info">
{{.}}
</div>
{{ end}}
<div class="card-body" id="import-body" style="display:none;">
<p class="font-weight-light">Select JSON file for database import</p>
<form action="/import" method="post" enctype="multipart/form-data">
<div class="form-group">
<input type="file" name="import_file" id="validatedCustomFile" required>
</div>
<button class="btn btn-primary btn-block" name="submit" type="submit">Import</button>
</form>
</div>
</div>
<p class="mt-5 mb-3 text-muted text-center">© 2015-2018</p>
</div>

<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
<p class="mt-5 mb-3 text-muted text-center">&copy; 2015-2018</p>
</form>
<script src="/static/js/vendor/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$( "#import-box" ).click(function() {
if ($("#import-body").is(":visible")){
$("#import-body").hide();
}else{
$("#import-body" ).show();
}
});
</script>
</body>

</html>
</html>