Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
UsbState.cs
1using System;
2using System.Collections.Generic;
3using Microsoft.Maker.RemoteWiring;
4using Microsoft.Maker.Serial;
5using Windows.Devices.Enumeration;
6using Waher.Events;
8
10{
11 internal class UsbState : IDisposable
12 {
13 public Dictionary<string, Pin> Pins = new Dictionary<string, Pin>();
14 public DeviceInformation DeviceInformation = null;
15 public UsbSerial SerialPort = null;
16 public RemoteDevice Device = null;
17 public string Name = string.Empty;
18 public bool Ready = false;
19
20 internal void Device_DeviceConnectionLost(string message)
21 {
22 this.Ready = false;
23 Log.Error("Device connection lost: " + message, this.Name); // TODO: Retry
24 }
25
26 internal void Device_DeviceConnectionFailed(string message)
27 {
28 this.Ready = false;
29 Log.Error("Device connection failed: " + message, this.Name); // TODO: Retry, after delay
30 }
31
32 internal async void Device_DeviceReady()
33 {
34 try
35 {
36 this.Ready = true;
37 Log.Informational("Device ready.", this.Name);
38
39 UsbConnectedDevice Port = null;
40 bool Found = false;
41
42 foreach (INode Node in await MeteringTopology.Root.ChildNodes)
43 {
44 Port = Node as UsbConnectedDevice;
45 if (!(Port is null) && Port.PortName == this.Name)
46 {
47 Found = true;
48 break;
49 }
50 }
51
52 if (!Found)
53 {
54 Port = new UsbConnectedDevice()
55 {
56 NodeId = this.Name,
57 PortName = this.Name
58 };
59
60 await MeteringTopology.Root.AddAsync(Port);
61
62 Log.Informational("Device added to metering topology.", this.Name);
63 }
64
65 foreach (MeteringNode Child in await Port.ChildNodes)
66 {
67 if (Child is Pin Pin)
68 {
69 lock (this.Pins)
70 {
71 this.Pins[Pin.PinNrStr] = Pin;
72 }
73
74 Pin.Initialize();
75 }
76 }
77 }
78 catch (Exception ex)
79 {
80 Log.Exception(ex);
81 }
82 }
83
84 internal void SerialPort_ConnectionEstablished()
85 {
86 Log.Informational("Connection established.", this.Name);
87 }
88
89 internal void Device_DigitalPinUpdated(byte pin, PinState state)
90 {
91 try
92 {
93 Pin Pin = this.GetPin(pin.ToString());
94
95 if (!(Pin is null) && Pin is DigitalPin DigitalPin)
96 DigitalPin.Pin_ValueChanged(state);
97 }
98 catch (Exception ex)
99 {
100 Log.Exception(ex);
101 }
102 }
103
104 internal async void Device_AnalogPinUpdated(string pin, ushort value)
105 {
106 try
107 {
108 Pin Pin = this.GetPin(pin);
109
110 if (!(Pin is null) && Pin is AnalogInput AnalogInput)
111 await AnalogInput.Pin_ValueChanged(value);
112 }
113 catch (Exception ex)
114 {
115 Log.Exception(ex);
116 }
117 }
118
119 internal Pin GetPin(string PinNr)
120 {
121 lock (this.Pins)
122 {
123 if (this.Pins.TryGetValue(PinNr, out Pin Pin))
124 return Pin;
125 else
126 return null;
127 }
128 }
129
130 public void Dispose()
131 {
132 if (!(this.Device is null))
133 {
134 this.Device.Dispose();
135 this.Device = null;
136 }
137
138 if (!(this.SerialPort is null))
139 {
140 this.SerialPort.Dispose();
141 this.SerialPort = null;
142 }
143 }
144
145 internal void AddPin(string PinNr, Pin Pin)
146 {
147 lock (this.Pins)
148 {
149 if (!this.Pins.ContainsKey(PinNr))
150 this.Pins[PinNr] = Pin;
151 }
152
153 if (this.Ready)
154 Pin.Initialize();
155 }
156
157 internal void RemovePin(string PinNr, Pin Pin)
158 {
159 lock (this.Pins)
160 {
161 if (this.Pins.TryGetValue(PinNr, out Pin Pin2) && Pin2 == Pin)
162 this.Pins.Remove(PinNr);
163 }
164 }
165 }
166}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
Definition: Log.cs:682
static void Informational(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an informational event.
Definition: Log.cs:334
Base class for all metering nodes.
Definition: MeteringNode.cs:28
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Defines the Metering Topology data source. This data source contains a tree structure of persistent r...
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49