コード例 #1
0
ファイル: tag_search.go プロジェクト: GeoNet/mtr
func (a *tagSearch) fieldState() <-chan *weft.Result {
	out := make(chan *weft.Result)
	go func() {
		defer close(out)
		var err error
		var rows *sql.Rows

		if rows, err = dbR.Query(`SELECT deviceID, typeID, time, value
					FROM field.state_tag
					JOIN field.state USING (devicePK, typePK)
					JOIN field.device USING (devicePK)
					JOIN field.state_type USING (typePK)
					WHERE tagPK = (SELECT tagPK FROM mtr.tag WHERE tag = $1)`, a.tag); err != nil {
			out <- weft.InternalServerError(err)
			return
		}
		defer rows.Close()

		var tm time.Time

		for rows.Next() {
			var fs mtrpb.FieldState

			if err = rows.Scan(&fs.DeviceID, &fs.TypeID, &tm, &fs.Value); err != nil {
				out <- weft.InternalServerError(err)
				return
			}

			fs.Seconds = tm.Unix()

			a.tagResult.FieldState = append(a.tagResult.FieldState, &fs)
		}

		out <- &weft.StatusOK
		return
	}()
	return out
}
コード例 #2
0
ファイル: field_state.go プロジェクト: GeoNet/mtr
func fieldStateProto(r *http.Request, h http.Header, b *bytes.Buffer) *weft.Result {
	var err error
	var rows *sql.Rows

	if rows, err = dbR.Query(`SELECT deviceID, typeID, time, value
				FROM field.state
				JOIN field.device USING (devicePK)
				JOIN field.state_type USING (typePK)`); err != nil {
		return weft.InternalServerError(err)
	}

	var fr mtrpb.FieldStateResult
	var t time.Time

	for rows.Next() {
		var s mtrpb.FieldState

		if err = rows.Scan(&s.DeviceID, &s.TypeID, &t, &s.Value); err != nil {
			return weft.InternalServerError(err)
		}

		// Convert from Go's time.Time to unix seconds since epoch discarding nanosecs
		s.Seconds = t.Unix()

		fr.Result = append(fr.Result, &s)
	}

	var by []byte
	if by, err = proto.Marshal(&fr); err != nil {
		return weft.InternalServerError(err)
	}

	b.Write(by)

	return &weft.StatusOK
}