mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-10 09:52:51 +00:00
merging back into one repo - not going to host on app engine - google won't allow me to connect to external database
This commit is contained in:
200
server/model/daily_sensor/daily_sensor.go
Normal file
200
server/model/daily_sensor/daily_sensor.go
Normal file
@@ -0,0 +1,200 @@
|
||||
package daily_sensor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"mywebsite/server/db"
|
||||
)
|
||||
|
||||
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 int `json:"month" bson:"month"`
|
||||
MonthName string `json:"monthname" bson:"monthname"`
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
//function to update the daily temperature max or min for a location
|
||||
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, "month": s.Month, "monthname": s.MonthName, "day": s.Day, "year": s.Year}
|
||||
change := bson.M{"$set": bson.M{"maxtemp": s.MaxTemp, "mintemp": s.MinTemp}}
|
||||
|
||||
err := c.Update(colQuerier, change)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
//get the current daily sensor reading to compare
|
||||
//if the max or min temp needs to be updated
|
||||
func GetDailySensorInfo(sensor_location string) (Data, error) {
|
||||
|
||||
d := Data{}
|
||||
t := time.Now()
|
||||
|
||||
day := t.Day()
|
||||
month := int(t.Month())
|
||||
monthname := t.Month().String()
|
||||
year := t.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, "monthname": monthname, "year": year}).One(&d)
|
||||
|
||||
if err != nil {
|
||||
log.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}).Sort("-year, -month").All(&d)
|
||||
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
|
||||
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
//return all daily temperature readings per year for a location
|
||||
//sort by most recent year and oldest month
|
||||
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.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location, "year": year}},
|
||||
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
//return all temperature readings for a specific month and location
|
||||
func GetAllSensorInfoByMonth(sensor_location string, year int, monthname 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.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location, "year": year, "monthname": monthname}},
|
||||
{"$sort": bson.M{"year": -1, "month": 1}}}).All(&d)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
139
server/model/raw_sensor/raw_sensor.go
Normal file
139
server/model/raw_sensor/raw_sensor.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package raw_sensor
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"gopkg.in/mgo.v2/bson"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"mywebsite/server/db"
|
||||
)
|
||||
|
||||
const (
|
||||
collection = "raw_sensor"
|
||||
)
|
||||
|
||||
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