-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIscp_JPEGSubscriber.cs
More file actions
100 lines (87 loc) · 3.34 KB
/
Iscp_JPEGSubscriber.cs
File metadata and controls
100 lines (87 loc) · 3.34 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
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/**
* JPEG DataFormat ( byte array )
* JPEG Data
*/
public class Iscp_JPEGSubscriber : MonoBehaviour, ITexture2D
{
[SerializeField]
private string dataType = "jpeg";
[SerializeField]
private string dataName = "v1/1/jpeg";
[SerializeField]
[IntdashLabel("Node IDiEdge UUIDj")]
private string nodeId = "";
public ImageParts TargetComponent;
public string ReceivedTime;
public string ServerTime;
public double Latency;
public Texture2D Texture { set; get; }
// MEMO: Basically, IscpConnection attachment from Inspector is not required, but can be specified.
[SerializeField] private IscpConnection iscp;
private IscpDownstream downstream;
// Called when the script is first activated.
private void Awake()
{
// Setup iSCP
if (this.iscp == null)
{
this.iscp = IscpConnection.GetOrCreateSharedInstance();
}
// Register subscriber
downstream = this.iscp.RegisterDownstream(nodeId, dataName, dataType, OnReceiveDataPoints);
}
// Received data points events.
private void OnReceiveDataPoints(DateTime baseTime, iSCP.Model.DataPointGroup[] dataPointGroups)
{
foreach (var group in dataPointGroups)
{
foreach (var d in group.DataPoints)
{
Dispatcher.RunOnMainThread(() =>
{
if (this == null) return;
if (!this.enabled) return;
try
{
long time = baseTime.AddTicks(d.ElapsedTime).Ticks;
var now = DateTime.UtcNow.ToLocalTime();
ReceivedTime = DateTime.UtcNow.ToLocalTime().ToString("HH:mm:ss.ffffff");
var serverTime = new DateTime(time).ToLocalTime();
ServerTime = serverTime.ToString("HH:mm:ss.ffffff");
Latency = (now.Ticks - serverTime.Ticks).TicksToSeconds();
// Extract the necessary data from the data format.
var jpegData = d.Payload;
// Decoding and other transformations may be required for visualization.
if (Texture == null)
{
Texture = new Texture2D(2, 2);
}
Texture.LoadImage(jpegData);
Texture.Apply(true, false);
// Visualize
if (TargetComponent != null)
{
if (TargetComponent.PreviewTexture == null)
{
if (TargetComponent.gameObject.activeInHierarchy)
{
TargetComponent.SetPreviewTexture(Texture);
}
}
}
}
catch (Exception e)
{
Debug.LogError("Failed to deserialize jpeg message. " + e.Message);
}
});
}
}
}
private void OnEnable() { }
private void OnDisable() { }
}