-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintdash_RoutePlayback.cs
More file actions
90 lines (83 loc) · 3.33 KB
/
Copy pathintdash_RoutePlayback.cs
File metadata and controls
90 lines (83 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Newtonsoft.Json.Linq;
using System;
using UnityEngine;
/// <summary>
/// intdash v1 String Data Format
/// 7 6 5 4 3 2 1 0
/// +---+---+---+---+---+---+---+---+
/// 000 | ID Length |
/// +---+---+---+---+---+---+---+---+ - - - -
/// 001 | ID | ^
/// + + | ID Length
/// : : |
/// + + |
/// | | v
/// +---+---+---+---+---+---+---+---+ - - - -
/// 0 | Data |
/// + +
/// : :
/// </summary>
public class intdash_RoutePlayback : MonoBehaviour
{
[SerializeField]
private string dataType = "10";
[SerializeField]
private string dataName = "1/location";
[SerializeField]
[IntdashLabel("Node IDiEdge UUIDj")]
private string nodeId = "";
public RouteParts TargetComponent;
// MEMO: Basically, IntdashPlaybackManager attachment from Inspector is not required, but can be specified.
[SerializeField] private IntdashPlaybackManagerSample playback;
// Called when the script is first activated.
private void Awake()
{
// Setup playback manager.
if (this.playback == null)
{
this.playback = IntdashPlaybackManagerSample.GetOrCreateSharedInstance();
}
// Register playback data.
var filter = $"{dataType}:{dataName}";
var playbackId = playback.RegisterBulkPlaybackData(nodeId, filter, OnReceiveBulkPlaybackData);
}
private void OnDestroy() { }
// After playback starts, data within the period set in the IntdashPlaybackManagerSample can be sequentially acquired.
// Refer to IntdashPlaybackManagerSample.RequestBulkDataDuration for the period to request at a time.
private void OnReceiveBulkPlaybackData(Pb.DataResponseProto[] dataPoints)
{
Debug.Log($"OnReceiveBulkPlaybackData {dataPoints.Length} points - intdash_RoutePlayback");
try
{
foreach (var point in dataPoints)
{
var time = point.Time.ToDateTime();
var data = point.DataPayload.ToByteArray();
// Once data is received, the necessary data is extracted from the data format and visualized.
byte[] payload;
{
var len = (int)data[0];
payload = new byte[data.Length - (1 + len)];
Array.Copy(data, (1 + len), payload, 0, payload.Length);
}
var locationData = System.Text.Encoding.UTF8.GetString(payload);
// Json Parse
var location = Vector3.zero;
{
JObject json = JObject.Parse(locationData);
location.x = json["x"].ToObject<float>();
location.z = json["z"].ToObject<float>();
}
// Visualize
if (TargetComponent != null)
{
TargetComponent.AddPoint(location, time.Ticks);
}
}
}
catch (Exception e)
{
Debug.LogError("Failed to deserialize location message. " + e.Message);
}
}
}