mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-11 18:32:50 +00:00
getting close to finishing temperature data api
This commit is contained in:
187
model/daily_sensor/daily_sensor.go
Normal file
187
model/daily_sensor/daily_sensor.go
Normal file
@@ -0,0 +1,187 @@
|
||||
package daily_sensor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/mgerb42/mywebsite/db"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
collection = "daily_sensor"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
MaxTemp float64 `json:"maxtemp" bson:"maxtemp"`
|
||||
MinTemp float64 `json:"mintemp" bson:"mintemp"`
|
||||
Location string `json:"location" bson:"location"`
|
||||
Month string `json:"month" bson:"month"`
|
||||
Day int `json:"day" bson:"day"`
|
||||
Year int `json:"year" bson:"year"`
|
||||
}
|
||||
|
||||
//convert struct to json string
|
||||
func (s *Data) toJson() string {
|
||||
|
||||
b, err := json.MarshalIndent(s, "", " ")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
|
||||
}
|
||||
|
||||
func (s *Data) StoreData() error {
|
||||
|
||||
if db.Mongo.Connected() {
|
||||
|
||||
log.Println("Inserting data into " + collection)
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
// Insert Datas
|
||||
err := c.Insert(s)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Data) UpdateData() error {
|
||||
|
||||
if db.Mongo.Connected() {
|
||||
|
||||
log.Println("Updating data")
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
colQuerier := bson.M{"location": s.Location}
|
||||
change := bson.M{"$set": bson.M{"maxtemp": s.MaxTemp, "mintemp": s.MinTemp}}
|
||||
|
||||
err := c.Update(colQuerier, change)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func GetDailySensorInfo(sensor_location string) (Data, error) {
|
||||
|
||||
d := Data{}
|
||||
|
||||
day := time.Now().Day()
|
||||
month := time.Now().Month().String()
|
||||
year := time.Now().Year()
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Find(bson.M{"location": sensor_location, "day": day, "month": month, "year": year}).One(&d)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetAllSensorInfo(sensor_location string) ([]Data, error) {
|
||||
d := []Data{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Find(bson.M{"location": sensor_location}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllSensorInfoByYear(sensor_location string, year int) ([]Data, error) {
|
||||
d := []Data{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Find(bson.M{"location": sensor_location, "year": year}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
func GetAllSensorInfoByMonth(sensor_location string, year int, month string) ([]Data, error) {
|
||||
d := []Data{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Find(bson.M{"location": sensor_location, "year": year, "month": month}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
DB_URL = "localhost"
|
||||
)
|
||||
|
||||
type DataStore struct {
|
||||
session *mgo.Session
|
||||
}
|
||||
|
||||
func NewSession() *DataStore {
|
||||
|
||||
d := new(DataStore)
|
||||
s, err := mgo.Dial(DB_URL)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
d.session = s
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (ds *DataStore) TestStore(fname string, lname string) {
|
||||
session := ds.session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
// Collection People
|
||||
c := session.DB("test").C("people")
|
||||
|
||||
// Insert Datas
|
||||
c.Insert(&Person{FirstName: fname, LastName: lname, Timestamp: time.Now()})
|
||||
|
||||
}
|
||||
|
||||
func (ds *DataStore) SearchName(fname string) []Person {
|
||||
|
||||
session := ds.session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
// Collection People
|
||||
c := session.DB("test").C("people")
|
||||
|
||||
var results []Person
|
||||
|
||||
err := c.Find(bson.M{"firstname": fname}).All(&results)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
/*func (ds *DataStore) ucol() *mgo.Collection {
|
||||
session = ds.session.Copy()
|
||||
defer session.Close()
|
||||
}
|
||||
*/
|
||||
|
||||
//func (ds *DataStore) UserExist(user string) bool { ... }
|
||||
@@ -1,13 +0,0 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Person struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
LastName string
|
||||
FirstName string
|
||||
Timestamp time.Time
|
||||
}
|
||||
149
model/raw_sensor/raw_sensor.go
Normal file
149
model/raw_sensor/raw_sensor.go
Normal file
@@ -0,0 +1,149 @@
|
||||
package raw_sensor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/mgerb42/mywebsite/db"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
collection = "temperatures"
|
||||
)
|
||||
|
||||
type Data struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
Temperature float64 `json:"temperature" bson:"temperature"`
|
||||
Location string `json:"location" bson:"location"`
|
||||
Updated time.Time `json:"updated" bson:"updated"`
|
||||
}
|
||||
|
||||
//convert struct to json string
|
||||
func (s *Data) toJson() string {
|
||||
|
||||
b, err := json.MarshalIndent(s, "", " ")
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error)
|
||||
}
|
||||
|
||||
return string(b)
|
||||
|
||||
}
|
||||
|
||||
func (s *Data) StoreData() error {
|
||||
|
||||
if db.Mongo.Connected() {
|
||||
|
||||
log.Println("Inserting data into " + collection)
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
// Insert Datas
|
||||
err := c.Insert(s)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//handle queries for all sensors page
|
||||
//********************************************************************************
|
||||
type DataStore_AllSensors struct {
|
||||
ID string `json:"location" bson:"_id"`
|
||||
Temperature float64 `json:"temperature" bson:"temperature"`
|
||||
Updated time.Time `json:"updated" bson:"updated"`
|
||||
}
|
||||
|
||||
func GetAllSensors() ([]DataStore_AllSensors, error) {
|
||||
|
||||
s := []DataStore_AllSensors{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Pipe([]bson.M{{"$group": bson.M{"_id": "$location", "temperature": bson.M{"$last": "$temperature"},
|
||||
"updated": bson.M{"$last": "$updated"}}},
|
||||
bson.M{"$sort": bson.M{"_id": 1}}}).All(&s)
|
||||
|
||||
if err != nil {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
||||
} else {
|
||||
return s, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
//********************************************************************************
|
||||
|
||||
//get sensor information by location
|
||||
//********************************************************************************
|
||||
type DataStore_SensorByLocation struct {
|
||||
Id sensorByLocation `json:"_id" bson:"_id"`
|
||||
}
|
||||
|
||||
type sensorByLocation struct {
|
||||
Year int `json:"year" bson:"year"`
|
||||
Month int `json:"month" bson:"month"`
|
||||
Location string `json:"location" bson:"location"`
|
||||
}
|
||||
|
||||
/*
|
||||
func GetSensorInfoByLocation(sensor_location string) ([]DataStore_SensorByLocation, error) {
|
||||
|
||||
s := []DataStore_SensorByLocation{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Pipe([]bson.M{{"$project": bson.M{"location": "$location", "year": bson.M{"$year": "$updated"}, "month": bson.M{"$month": "$updated"}}},
|
||||
bson.M{"$match": bson.M{"location": sensor_location}},
|
||||
bson.M{"$group": bson.M{"_id": bson.M{"year": "$year", "month": "$month", "location": "$location"}}},
|
||||
bson.M{"$sort": bson.M{"_id.year": -1, "_id.month": -1}}}).All(&s)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
return s, nil
|
||||
|
||||
} else {
|
||||
return s, errors.New("Query failed")
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
//********************************************************************************
|
||||
|
||||
/*************************
|
||||
|
||||
testStore := model.SensorData{
|
||||
ID: bson.NewObjectId(),
|
||||
Temperature: 34.2,
|
||||
Humidity: 33.22,
|
||||
Location: "Grand Meadow",
|
||||
Updated: time.Now(),
|
||||
}
|
||||
|
||||
**************************/
|
||||
Reference in New Issue
Block a user