Exemplo n.º 1
0
        public void ActivateInputDeviceDelegate(string objectIdentifier, IReactPromise <JSValue.Void> promise)
        {
            if (!GetObject(objectIdentifier, out StarPrinter nativeObject))
            {
                promise.Reject(new ReactError());
                return;
            }

            nativeObject.InputDeviceDelegate.Connected += (sender, e) =>
            {
                var parameter = new Dictionary <string, JSValue>();
                parameter.Add(EventParameter.KeyIdentifier, objectIdentifier);

                InputDeviceConnected(parameter);
            };

            nativeObject.InputDeviceDelegate.Disconnected += (sender, e) =>
            {
                var parameter = new Dictionary <string, JSValue>();
                parameter.Add(EventParameter.KeyIdentifier, objectIdentifier);

                InputDeviceDisconnected(parameter);
            };

            nativeObject.InputDeviceDelegate.DataReceived += (sender, e) =>
            {
                var parameter = new Dictionary <string, JSValue>();
                parameter.Add(EventParameter.KeyIdentifier, objectIdentifier);
                parameter.Add(EventParameter.KeyInputDeviceData, StarIO10ValueConverter.ToJSValue(e.Data.ToArray()));

                InputDeviceDataReceived(parameter);
            };

            promise.Resolve();
        }
        public void Init(string[] interfaceTypes, IReactPromise <string> promise)
        {
            List <InterfaceType> nativeInterfaceTypes = new List <InterfaceType>();

            foreach (string interfaceType in interfaceTypes)
            {
                if (!StarIO10ValueConverter.ToInterfaceType(interfaceType, out InterfaceType nativeInterfaceType))
                {
                    promise.Reject(new ReactError());
                    return;
                }

                nativeInterfaceTypes.Add(nativeInterfaceType);
            }

            try
            {
                IStarDeviceDiscoveryManager nativeObject = StarDeviceDiscoveryManagerFactory.Create(nativeInterfaceTypes);

                SetObject(nativeObject, out string objectIdentifier);

                nativeObject.PrinterFound += (sender, e) =>
                {
                    if (!StarIO10ValueConverter.ToString(e.Printer.ConnectionSettings.InterfaceType, out string interfaceTypeString) ||
                        !StarIO10ValueConverter.ToString(e.Printer.Information.Model, out string modelString) ||
                        !StarIO10ValueConverter.ToString(e.Printer.Information.Emulation, out string emulationString))
                    {
                        return;
                    }

                    var parameter = new Dictionary <string, JSValue>();
                    parameter.Add(EventParameter.KeyIdentifier, objectIdentifier);
                    parameter.Add(EventParameter.KeyInterfaceType, interfaceTypeString);
                    parameter.Add(EventParameter.KeyConnectionIdentifier, e.Printer.ConnectionSettings.Identifier);
                    parameter.Add(EventParameter.KeyModel, modelString);
                    parameter.Add(EventParameter.KeyEmulation, emulationString);
                    parameter.Add(EventParameter.KeyReserved, StarIO10ValueConverter.ToJSValue(e.Printer.Information.Reserved));

                    PrinterFound(parameter);
                };
                nativeObject.DiscoveryFinished += (sender, e) =>
                {
                    var parameter = new Dictionary <string, JSValue>();
                    parameter.Add(EventParameter.KeyIdentifier, objectIdentifier);

                    DiscoveryFinished(parameter);
                };

                promise.Resolve(objectIdentifier);
            }
            catch (StarIO10Exception e)
            {
                StarIO10ErrorWrapper.SetObject(e, out string exceptionIdentifier);
                promise.Reject(new ReactError()
                {
                    Code = exceptionIdentifier, Exception = e
                });
            }
        }