예제 #1
0
파일: main.go 프로젝트: tstranex/carpcomm
func RedecodeContact(contactdb *db.ContactDB, c *pb.Contact) {
	if c == nil {
		log.Fatalf("Contact not found")
	}

	log.Printf("Original contact:\n%s\n", proto.MarshalTextString(c))

	new_blobs := make([]*pb.Contact_Blob, 0)
	var freeform []byte
	for _, b := range c.Blob {
		if b.Format != nil && (*b.Format == pb.Contact_Blob_DATUM ||
			*b.Format == pb.Contact_Blob_FRAME) {
			// Strip out datums and frames.
			continue
		}
		new_blobs = append(new_blobs, b)

		if b.Format != nil && *b.Format == pb.Contact_Blob_FREEFORM {
			if freeform != nil {
				log.Fatalf("Contact contains multiple FREEFORM blobs.")
			}
			freeform = b.InlineData
		}
	}

	if freeform == nil {
		return
	}

	data, frames := telemetry.DecodeFreeform(
		*c.SatelliteId, freeform, *c.StartTimestamp)
	for i, _ := range frames {
		b := new(pb.Contact_Blob)
		b.Format = pb.Contact_Blob_FRAME.Enum()
		b.InlineData = frames[i]
		new_blobs = append(new_blobs, b)
	}
	for i, _ := range data {
		b := new(pb.Contact_Blob)
		b.Format = pb.Contact_Blob_DATUM.Enum()
		b.Datum = &data[i]
		new_blobs = append(new_blobs, b)
	}

	c.Blob = new_blobs

	log.Printf("New contact:\n%s\n", proto.MarshalTextString(c))

	err := contactdb.Store(c)
	if err != nil {
		log.Fatalf("Error storing contact: %s", err.Error())
	}
}
예제 #2
0
// Try to decode morse messages and data frames.
// This should really be done asynchronously by the telemetry pipeline.
func DecodeBlobs(
	satellite_id string, timestamp int64, blobs []pb.Contact_Blob) (
	result []*pb.Contact_Blob, err error) {

	var datums []pb.TelemetryDatum
	var new_frames [][]byte
	for _, b := range blobs {
		if b.Format == nil {
			log.Printf("Missing blob format.")
			continue
		}

		if *b.Format == pb.Contact_Blob_FREEFORM {
			d, f := telemetry.DecodeFreeform(
				satellite_id, b.InlineData, timestamp)
			datums = append(datums, d...)
			new_frames = append(new_frames, f...)
		} else if *b.Format == pb.Contact_Blob_MORSE {
			d, _ := telemetry.DecodeMorse(
				satellite_id,
				(string)(b.InlineData),
				timestamp)
			datums = append(datums, d...)
		} else if *b.Format == pb.Contact_Blob_FRAME {
			d, _ := telemetry.DecodeFrame(
				satellite_id, b.InlineData, timestamp)
			datums = append(datums, d...)
		} else {
			log.Printf("Unknown blob format: %v", *b.Format)
			return nil, NewPopulateContactError(
				http.StatusBadRequest, "Unknown format.")
		}
	}

	for _, f := range new_frames {
		b := &pb.Contact_Blob{}
		b.Format = pb.Contact_Blob_FRAME.Enum()
		b.InlineData = []byte(f)
		result = append(result, b)
	}

	for _, d := range datums {
		b := &pb.Contact_Blob{}
		b.Format = pb.Contact_Blob_DATUM.Enum()
		b.Datum = &pb.TelemetryDatum{}
		*b.Datum = d
		result = append(result, b)
	}

	return result, nil
}