diff --git a/Jacdac.NET.Playground/Jacdac.NET.Playground.csproj b/Jacdac.NET.Playground/Jacdac.NET.Playground.csproj index 1fbc1e7..5c7a83a 100644 --- a/Jacdac.NET.Playground/Jacdac.NET.Playground.csproj +++ b/Jacdac.NET.Playground/Jacdac.NET.Playground.csproj @@ -7,10 +7,6 @@ - - - - diff --git a/Jacdac.NET.Playground/Program.cs b/Jacdac.NET.Playground/Program.cs index 59d544a..d9e401c 100644 --- a/Jacdac.NET.Playground/Program.cs +++ b/Jacdac.NET.Playground/Program.cs @@ -7,9 +7,14 @@ using Microsoft.Extensions.Logging; using System; using System.Linq; using System.Threading; +#if LOGGING using Jacdac.Logging; +#endif + using System.Collections.Generic; +#if IOTHUB using Microsoft.Azure.Devices.Client; +#endif namespace Jacdac.Playground { @@ -29,14 +34,18 @@ namespace Jacdac.Playground services.Add(new SettingsServer(settings)); if (args.Contains("prototest")) services.Add(new ProtoTestServer()); +#if SOUNDS if (args.Contains("sounds")) services.Add(new SoundPlayerServer(new NetCoreAudioSoundPlayer("sounds"))); +#endif +#if IOTHUB if (args.Contains("iothub")) { var hub = new AzureIoTHubClient(TransportType.Mqtt_Tcp_Only, settings); services.Add(new AzureIotHubHealthServer(hub)); services.Add(new JacscriptCloudServer(hub)); } +#endif // create and start bus var bus = new JDBus(null, new JDBusOptions() @@ -82,9 +91,11 @@ namespace Jacdac.Playground Console.WriteLine($"adding websocket connection to {url}"); bus.AddTransport(WebSocketTransport.Create(new Uri(url))); break; +#if LIBUSB case "libusb": Console.WriteLine($"adding libusb transport"); bus.AddTransport(Jacdac.Transports.LibUsb.LibUsbTransport.Create()); +#endif break; } } diff --git a/Jacdac/Clients/AzureIotHubHealthClient.g.cs b/Jacdac/Clients/AzureIotHubHealthClient.g.cs deleted file mode 100644 index 881de6f..0000000 --- a/Jacdac/Clients/AzureIotHubHealthClient.g.cs +++ /dev/null @@ -1,140 +0,0 @@ -/** Autogenerated file. Do not edit. */ -using Jacdac; -using System; - -namespace Jacdac.Clients -{ - /// - /// Health and diagnostics information about the Azure Iot Hub connection. - /// Implements a client for the Azure IoT Hub Health service. - /// - /// - public partial class AzureIotHubHealthClient : Client - { - public AzureIotHubHealthClient(JDBus bus, string name) - : base(bus, name, ServiceClasses.AzureIotHubHealth) - { - } - - /// - /// Tries to read the hub_name register value. - /// Something like `my-iot-hub.azure-devices.net` if available., - /// - bool TryGetHubName(out string value) - { - object[] values; - if (this.TryGetRegisterValues((ushort)AzureIotHubHealthReg.HubName, AzureIotHubHealthRegPack.HubName, out values)) - { - value = (string)values[0]; - return true; - } - else - { - value = default(string); - return false; - } - } - - /// - /// Tries to read the hub_device_id register value. - /// Device identifier in Azure Iot Hub if available., - /// - bool TryGetHubDeviceId(out string value) - { - object[] values; - if (this.TryGetRegisterValues((ushort)AzureIotHubHealthReg.HubDeviceId, AzureIotHubHealthRegPack.HubDeviceId, out values)) - { - value = (string)values[0]; - return true; - } - else - { - value = default(string); - return false; - } - } - - /// - /// Reads the connection_status register value. - /// Indicates the status of connection. A message beyond the [0..3] range represents an HTTP error code., - /// - public AzureIotHubHealthConnectionStatus ConnectionStatus - { - get - { - return (AzureIotHubHealthConnectionStatus)this.GetRegisterValue((ushort)AzureIotHubHealthReg.ConnectionStatus, AzureIotHubHealthRegPack.ConnectionStatus); - } - } - - /// - /// Reads the push_period register value. - /// How often to push data to the cloud., _: ms - /// - public uint PushPeriod - { - get - { - return (uint)this.GetRegisterValue((ushort)AzureIotHubHealthReg.PushPeriod, AzureIotHubHealthRegPack.PushPeriod); - } - set - { - - this.SetRegisterValue((ushort)AzureIotHubHealthReg.PushPeriod, AzureIotHubHealthRegPack.PushPeriod, value); - } - - } - - /// - /// Reads the push_watchdog_period register value. - /// If no message is published within given period, the device resets. - /// This can be due to connectivity problems or due to the device having nothing to publish. - /// Forced to be at least `2 * flush_period`. - /// Set to `0` to disable (default)., _: ms - /// - public uint PushWatchdogPeriod - { - get - { - return (uint)this.GetRegisterValue((ushort)AzureIotHubHealthReg.PushWatchdogPeriod, AzureIotHubHealthRegPack.PushWatchdogPeriod); - } - set - { - - this.SetRegisterValue((ushort)AzureIotHubHealthReg.PushWatchdogPeriod, AzureIotHubHealthRegPack.PushWatchdogPeriod, value); - } - - } - - /// - /// Raised when the connection status changes - /// - public event ClientEventHandler ConnectionStatusChange - { - add - { - this.AddEvent((ushort)AzureIotHubHealthEvent.ConnectionStatusChange, value); - } - remove - { - this.RemoveEvent((ushort)AzureIotHubHealthEvent.ConnectionStatusChange, value); - } - } - - /// - /// Raised when a message has been sent to the hub. - /// - public event ClientEventHandler MessageSent - { - add - { - this.AddEvent((ushort)AzureIotHubHealthEvent.MessageSent, value); - } - remove - { - this.RemoveEvent((ushort)AzureIotHubHealthEvent.MessageSent, value); - } - } - - - } -} \ No newline at end of file diff --git a/Jacdac/Clients/CloudAdapterClient.g.cs b/Jacdac/Clients/CloudAdapterClient.g.cs index 2103131..915cc46 100644 --- a/Jacdac/Clients/CloudAdapterClient.g.cs +++ b/Jacdac/Clients/CloudAdapterClient.g.cs @@ -43,21 +43,6 @@ namespace Jacdac.Clients } } - /// - /// Emitted when cloud requests to run some action. - /// - public event ClientEventHandler CloudCommand - { - add - { - this.AddEvent((ushort)CloudAdapterEvent.CloudCommand, value); - } - remove - { - this.RemoveEvent((ushort)CloudAdapterEvent.CloudCommand, value); - } - } - /// /// Emitted when we connect or disconnect from the cloud. /// diff --git a/Jacdac/Clients/LedClient.cs b/Jacdac/Clients/LedClient.cs index 690c674..38351b4 100644 --- a/Jacdac/Clients/LedClient.cs +++ b/Jacdac/Clients/LedClient.cs @@ -17,8 +17,8 @@ namespace Jacdac.Clients /// public void SetColor(uint rgb, uint transitionMilliseconds = 0) { - var speed = (uint)(transitionMilliseconds <= 0 ? 0 : Math.Max(4, 512 / transitionMilliseconds)); - this.Animate((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff, speed); + //var speed = (uint)(transitionMilliseconds <= 0 ? 0 : Math.Max(4, 512 / transitionMilliseconds)); + //this.Animate((rgb >> 16) & 0xff, (rgb >> 8) & 0xff, rgb & 0xff, speed); } } } \ No newline at end of file diff --git a/Jacdac/OutPipe.cs b/Jacdac/OutPipe.cs index 0646034..97c4e12 100644 --- a/Jacdac/OutPipe.cs +++ b/Jacdac/OutPipe.cs @@ -78,7 +78,9 @@ namespace Jacdac catch (AckException ex) { this.free(); +#pragma warning disable CA2200 // Rethrow to preserve stack details throw ex; +#pragma warning restore CA2200 // Rethrow to preserve stack details } this._count++; } diff --git a/Jacdac/Samples/SlidyBlinky.cs b/Jacdac/Samples/SlidyBlinky.cs index ecabfb1..2d77f87 100644 --- a/Jacdac/Samples/SlidyBlinky.cs +++ b/Jacdac/Samples/SlidyBlinky.cs @@ -21,10 +21,10 @@ namespace Jacdac.Samples // grab brightness var brightness = (uint)(slider.Position * 100); // blue - led.Animate(0, 0, brightness, speed); + //led.Animate(0, 0, brightness, speed); Thread.Sleep(500); // red - led.Animate(brightness, 0, 0, speed); + //led.Animate(brightness, 0, 0, speed); Thread.Sleep(500); } }; diff --git a/Jacdac/Servers/AzureIotHubHealthServer.cs b/Jacdac/Servers/AzureIotHubHealthServer.cs deleted file mode 100644 index 851e3d5..0000000 --- a/Jacdac/Servers/AzureIotHubHealthServer.cs +++ /dev/null @@ -1,88 +0,0 @@ -using System; - -namespace Jacdac.Servers -{ - public interface IAzureIoTHubHealth - { - string HubName { get; } - string HubDeviceId { get; } - AzureIotHubHealthConnectionStatus ConnectionStatus { get; } - - void Connect(); - void Disconnect(); - - void SetConnectionString(string connectionString); - - event EventHandler ConnectionStatusChanged; - - event EventHandler MessageSent; - } - - public sealed class AzureIotHubHealthServer : JDServiceServer - { - private IAzureIoTHubHealth hub; - private readonly JDDynamicRegisterServer hubNameRegister; - private readonly JDDynamicRegisterServer hubDeviceIdRegister; - private readonly JDDynamicRegisterServer connectionStatusRegister; - - public AzureIotHubHealthServer(IAzureIoTHubHealth hub, JDServiceServerOptions options = null) - : base(ServiceClasses.AzureIotHubHealth, options) - { - this.hub = hub; - - this.AddRegister(this.hubNameRegister = new JDDynamicRegisterServer((ushort)AzureIotHubHealthReg.HubName, AzureIotHubHealthRegPack.HubName, (server) => new object[] { this.hub.HubName })); - this.AddRegister(this.hubDeviceIdRegister = new JDDynamicRegisterServer((ushort)AzureIotHubHealthReg.HubDeviceId, AzureIotHubHealthRegPack.HubDeviceId, (server) => new object[] { this.hub.HubDeviceId })); - this.AddRegister(this.connectionStatusRegister = new JDDynamicRegisterServer((ushort)AzureIotHubHealthReg.ConnectionStatus, AzureIotHubHealthRegPack.ConnectionStatus, (server) => new object[] { (ushort)this.hub.ConnectionStatus })); - - // restricted command - this.AddCommand((ushort)AzureIotHubHealthCmd.SetConnectionString, this.handleSetConnectionString); - - this.hub.MessageSent += handleMessageSent; - this.hub.ConnectionStatusChanged += handleConnectionStatusChanged; - } - - public override string ToString() - { - return this.hub == null ? "disposed" : $"{this.hub.HubName} in {this.hub.HubDeviceId}: {this.hub.ConnectionStatus}"; - } - - private void handleConnectionStatusChanged(object sender, EventArgs e) - { - if (this.hub == null) - throw new ObjectDisposedException("server"); - - var payload = PacketEncoding.Pack(AzureIotHubHealthEventPack.ConnectionStatusChange, new object[] { (ushort)this.hub.ConnectionStatus }); - this.SendEvent((ushort)AzureIotHubHealthEvent.ConnectionStatusChange, payload); - } - - private void handleMessageSent(object sender, EventArgs e) - { - if (this.hub == null) - throw new ObjectDisposedException("server"); - - this.SendEvent((ushort)AzureIotHubHealthEvent.MessageSent); - } - - private void handleSetConnectionString(JDNode sender, PacketEventArgs args) - { - if (this.hub == null) - throw new ObjectDisposedException("server"); - - var value = PacketEncoding.UnPack(AzureIotHubHealthCmdPack.SetConnectionString, args.Packet.Data); - var connectionString = (string)value[0]; - - this.hub.SetConnectionString(connectionString); - } - - public override void Dispose() - { - var hub = this.hub; - if (hub != null) - { - this.hub = null; - hub.MessageSent -= handleMessageSent; - hub.ConnectionStatusChanged -= handleConnectionStatusChanged; - } - } - } -} diff --git a/Jacdac/Servers/JacscriptCloudServer.cs b/Jacdac/Servers/JacscriptCloudServer.cs deleted file mode 100644 index 6f79a71..0000000 --- a/Jacdac/Servers/JacscriptCloudServer.cs +++ /dev/null @@ -1,128 +0,0 @@ -using System; - -namespace Jacdac.Servers -{ - [Serializable] - public sealed class TwinChangedEventsArgs - { - public string Path { get; private set; } - public double Value { get; private set; } - public TwinChangedEventsArgs(string path, double value) - { - this.Path = path; - this.Value = value; - } - } - public delegate void TwinChangedEventHandler(IJacscriptCloud sender, TwinChangedEventsArgs args); - - [Serializable] - public sealed class CloudCommandEventArgs - { - public uint SequenceNumber { get; private set; } - public string Command { get; private set; } - public double[] Arguments { get; private set; } - public CloudCommandEventArgs(uint sequenceNumber, string command, double[] arguments) - { - this.SequenceNumber = sequenceNumber; - this.Command = command; - this.Arguments = arguments; - } - } - public delegate void CloudCommandEventHandler(IJacscriptCloud cloud, CloudCommandEventArgs args); - - public interface IJacscriptCloud - { - bool IsConnected { get; } - void Upload(string label, double[] value); - bool TryGetTwin(string path, out double current); - void SubscribeTwin(string path); - void AckCloudCommand(uint sequenceNumber, double[] result); - - event TwinChangedEventHandler TwinChanged; - event CloudCommandEventHandler CloudCommand; - } - - /// - /// A server implementation of the Jascript cloud service - /// - public sealed class JacscriptCloudServer : JDServiceServer - { - readonly IJacscriptCloud Cloud; - readonly JDDynamicRegisterServer connectedRegister; - - public JacscriptCloudServer(IJacscriptCloud cloud, JDServiceServerOptions options = null) : base(ServiceClasses.JacscriptCloud, options) - { - this.Cloud = cloud; - this.AddRegister(this.connectedRegister = new JDDynamicRegisterServer((ushort)JacscriptCloudReg.Connected, JacscriptCloudRegPack.Connected, (args) => new object[] { this.Cloud.IsConnected })); - this.AddCommand((ushort)JacscriptCloudCmd.Upload, this.handleUpload); - this.AddCommand((ushort)JacscriptCloudCmd.GetTwin, this.handleGetTwin); - this.AddCommand((ushort)JacscriptCloudCmd.AckCloudCommand, this.handleAckCloudCommand); - - this.Cloud.TwinChanged += handleTwinChange; - this.Cloud.CloudCommand += handleCloudCommand; - } - - public override void Dispose() - { - base.Dispose(); - this.Cloud.TwinChanged -= handleTwinChange; - this.Cloud.CloudCommand -= handleCloudCommand; - } - - private void handleCloudCommand(IJacscriptCloud cloud, CloudCommandEventArgs args) - { - var seqno = args.SequenceNumber; - var command = args.Command; - var arguments = new object[args.Arguments.Length]; - for (var i = 0; i < args.Arguments.Length; ++i) - arguments[i] = new object[] { args.Arguments[i] }; - var data = PacketEncoding.Pack(JacscriptCloudEventPack.CloudCommand, new object[] { seqno, command, arguments }); - this.SendEvent((ushort)JacscriptCloudEvent.TwinChange, data); - } - - private void handleTwinChange(IJacscriptCloud sender, TwinChangedEventsArgs args) - { - this.SendEvent((ushort)JacscriptCloudEvent.TwinChange); - } - - private void handleUpload(JDNode sender, PacketEventArgs args) - { - var pkt = args.Packet; - var data = pkt.Data; - var values = PacketEncoding.UnPack(JacscriptCloudCmdPack.Upload, data); - var label = (string)values[0]; - var value = (double[])values[1]; - - this.Cloud.Upload(label, value); - } - - private void handleGetTwin(JDNode sender, PacketEventArgs args) - { - var pkt = args.Packet; - var data = pkt.Data; - var values = PacketEncoding.UnPack(JacscriptCloudCmdPack.GetTwin, data); - var path = (string)values[0]; - - double current; - if (this.Cloud.TryGetTwin(path, out current)) - { - var reportPayload = PacketEncoding.Pack(JacscriptCloudCmdPack.GetTwinReport, new object[] { path, current }); - var reportPkt = Packet.From((ushort)JacscriptCloudCmd.GetTwin, reportPayload); - this.Device.SendPacket(reportPkt); - } - } - - private void handleAckCloudCommand(JDNode sender, PacketEventArgs args) - { - var pkt = args.Packet; - var data = pkt.Data; - var values = PacketEncoding.UnPack(JacscriptCloudCmdPack.AckCloudCommand, data); - var seqNo = (uint)values[0]; - var res = (object[])values[1]; - var results = new double[res.Length]; - for (var i = 0; i < res.Length; i++) - results[i] = (double)(((object[])res[i])[0]); - this.Cloud.AckCloudCommand(seqNo, results); - } - } -} \ No newline at end of file