diff --git a/client/js/app.js b/client/js/app.js index 9560bed..fe60721 100644 --- a/client/js/app.js +++ b/client/js/app.js @@ -39,7 +39,7 @@ ReactDOM.render(( - + diff --git a/client/js/components/sensors/SensorList.js b/client/js/components/sensors/SensorList.js index c1dfa1f..afdd47a 100644 --- a/client/js/components/sensors/SensorList.js +++ b/client/js/components/sensors/SensorList.js @@ -20,8 +20,8 @@ export default class SensorList extends React.Component { this.openLink = this.openLink.bind(this); } - openLink(){ - browserHistory.push("/"); + openLink(location){ + browserHistory.push(`/sensor/${location}`); this.props.toggleOff(); } @@ -29,7 +29,7 @@ export default class SensorList extends React.Component { const date = new Date(sensor.updated); return ( -
+
{this.openLink(sensor.location)}}>

{sensor.temperature}°f

diff --git a/client/js/components/sensors/SensorList.scss b/client/js/components/sensors/SensorList.scss index 893884f..758923a 100644 --- a/client/js/components/sensors/SensorList.scss +++ b/client/js/components/sensors/SensorList.scss @@ -9,7 +9,7 @@ padding: 1em; &:hover{ - background-color: gray; + background-color: #D1D1D1; } .item + .item{ diff --git a/server/controller/api/sensor.go b/server/controller/api/sensor.go index 5dd87be..f4e5dc5 100644 --- a/server/controller/api/sensor.go +++ b/server/controller/api/sensor.go @@ -61,6 +61,7 @@ func HandleSensorRequest(w http.ResponseWriter, r *http.Request, ps httprouter.P storedData.Month = int(t.Month()) storedData.MonthName = t.Month().String() storedData.Year = t.Year() + storedData.Updated = t err := storedData.StoreData() @@ -134,21 +135,7 @@ func HandleSensorByLocation(w http.ResponseWriter, r *http.Request, ps httproute s, err := daily_sensor.GetAllSensorInfo(location) - var response string - - if err != nil { - log.Println(err) - response = "{message : \"Error loading data from database\"" - } else { - js, err := json.MarshalIndent(s, "", " ") - - if err != nil { - log.Println(err) - response = "{message : \"Error loading data from database\"" - } else { - response = string(js) - } - } + response := createResponse(s, err) fmt.Fprint(w, response) } @@ -162,21 +149,7 @@ func HandleSensorByLocationYear(w http.ResponseWriter, r *http.Request, ps httpr s, err := daily_sensor.GetAllSensorInfoByYear(location, year) - var response string - - if err != nil { - log.Println(err) - response = "{message : \"Error loading data from database\"" - } else { - js, err := json.MarshalIndent(s, "", " ") - - if err != nil { - log.Println(err) - response = "{message : \"Error loading data from database\"" - } else { - response = string(js) - } - } + response := createResponse(s, err) fmt.Fprint(w, response) } @@ -191,21 +164,41 @@ func HandleSensorByLocationMonth(w http.ResponseWriter, r *http.Request, ps http s, err := daily_sensor.GetAllSensorInfoByMonth(location, year, monthname) + response := createResponse(s, err) + + fmt.Fprint(w, response) +} + +/* +func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + + location := ps.ByName("location") + + w.Header().Set("Content-Type", "application/json") + + s, err := daily_sensor.GetUniqueSensorDates(location) + + response := createResponse(s, err) + + fmt.Fprint(w, response) +} +*/ +func createResponse(s []daily_sensor.Data, err error) string{ var response string if err != nil { log.Println(err) response = "{message : \"Error loading data from database\"" } else { - js, err := json.MarshalIndent(s, "", " ") + js, err1 := json.MarshalIndent(s, "", " ") - if err != nil { - log.Println(err) + if err1 != nil { + log.Println(err1) response = "{message : \"Error loading data from database\"" } else { response = string(js) } } - - fmt.Fprint(w, response) -} + + return response +} \ No newline at end of file diff --git a/server/model/daily_sensor/daily_sensor.go b/server/model/daily_sensor/daily_sensor.go index 99837bb..0dc8f27 100644 --- a/server/model/daily_sensor/daily_sensor.go +++ b/server/model/daily_sensor/daily_sensor.go @@ -16,22 +16,24 @@ const ( 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"` + MaxTemp float64 `json:"maxtemp,omitempty" bson:"maxtemp"` + MinTemp float64 `json:"mintemp,omitempty" bson:"mintemp"` + Location string `json:"location,omitempty" bson:"location"` + Month int `json:"month,omitempty" bson:"month"` + MonthName string `json:"monthname,omitempty" bson:"monthname"` + Day int `json:"day,omitempty" bson:"day"` + Year int `json:"year,omitempty" bson:"year"` + Updated time.Time `json:"updated" bson:"updated"` } //convert struct to json string -func (s *Data) toJson() string { +func (s *Data) ToJson() string { b, err := json.MarshalIndent(s, "", " ") if err != nil { - log.Println(err.Error) + log.Println(err) + return "{message : \"Error loading data from database\"" } return string(b) @@ -73,7 +75,7 @@ func (s *Data) UpdateData() error { 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}} + change := bson.M{"$set": bson.M{"maxtemp": s.MaxTemp, "mintemp": s.MinTemp, "updated": time.Now()}} err := c.Update(colQuerier, change) @@ -198,3 +200,31 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string) return d, errors.New("Query failed") } } + +/* +func GetUniqueSensorDates(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.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}}, + {"$project": bson.M{""}}, + {"$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") + } +} +*/ \ No newline at end of file diff --git a/server/route/route.go b/server/route/route.go index 8d7b9a6..6875f33 100644 --- a/server/route/route.go +++ b/server/route/route.go @@ -20,7 +20,8 @@ func Routes() *httprouter.Router { r.GET("/api/sensor/:location", api.HandleSensorByLocation) r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear) r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth) - + //r.GET("/api/sensor/:uniquedates", api.HandleUniqueDates) + r.GET("/discord", controller.DiscordRedirect) r.GET("/vpn", controller.VPNRedirect) r.GET("/camera", controller.CameraRedirect)