Exemplo n.º 1
0
        /// <summary>
        /// Callback method that MdsLib calls when connection of a device has completed
        /// </summary>
        /// <param name="MACaddress"></param>
        /// <param name="serial"></param>
        public void OnConnectionComplete(string MACaddress, string serial)
        {
            Debug.WriteLine($"SUCCESS MdsConnectionListener OnConnectionComplete callback called: MACaddress {MACaddress} Serial {serial}");
            var uuid = GetUuidFromMACAddress(MACaddress);

            this.UuidToSerialMapper.TryAdd(uuid, serial);

            DeviceConnectionComplete?.Invoke(this, new MdsConnectionListenerEventArgs(serial, uuid));
        }
        /// <summary>
        /// Callback method that MdsLib calls when a device connects or disconnects
        /// </summary>
        /// <param name="mdsevent">details of device connection/disconnection</param>
        public void OnDeviceConnectionEvent(MDSEvent mdsevent)
        {
            var method = ((NSString)mdsevent.BodyDictionary.ValueForKey(new NSString("Method")));

            if (method == new NSString("POST"))
            {
                // Device connected
                var bodyDict = (NSDictionary)mdsevent.BodyDictionary.ValueForKey(new NSString("Body"));
                var serial   = ((NSString)bodyDict.ValueForKey(new NSString("Serial"))).ToString();
                var connDict = (NSDictionary)bodyDict.ValueForKey(new NSString("Connection"));
                var uuid     = ((NSString)connDict.ValueForKey(new NSString("UUID"))).ToString();

                var uniqueIDGuid = new Guid(uuid);
                this.UuidToSerialMapper.TryAdd(uniqueIDGuid, serial);

                Debug.WriteLine($"MdsConnectionListener OnDeviceConnectionEvent CONNECTED: Serial {serial}");
                DeviceConnected?.Invoke(this, new MdsConnectionListenerBLEConnectedEventArgs(uuid));
                DeviceConnectionComplete?.Invoke(this, new MdsConnectionListenerEventArgs(serial, uniqueIDGuid));
            }
            else if (method == new NSString("DEL"))
            {
                // Device disconnected
                var bodyDict = (NSDictionary)mdsevent.BodyDictionary.ValueForKey(new NSString("Body"));
                var serial   = ((NSString)bodyDict.ValueForKey(new NSString("Serial"))).ToString();

                // Get the matching Uuid
                var uniqueIDGuid = this.UuidToSerialMapper.First(kv => kv.Value == serial).Key;

                Debug.WriteLine($"MdsConnectionListener OnDeviceConnectionEvent DISCONNECTED: Serial {serial}");
                DeviceDisconnected?.Invoke(this, new MdsConnectionListenerEventArgs(serial, uniqueIDGuid));
            }
            else
            {
                throw new MdsException($"OnDeviceConnectionEvent unexpected method: {method}");
            }
        }