Beispiel #1
0
        private async Task DeviceInfo(DeviceMessage devMessage)
        {
            var action = devMessage.Action.ToUpperInvariant();

            switch (action)
            {
            case "CAPABILITIES":
            {
                var resultId = 0;
                var result   = new StringBuilder();

                if (Accelerometer.GetDefault() != null)
                {
                    resultId += 1;
                    result.Append("A");
                }

                if (Gyrometer.GetDefault() != null)
                {
                    resultId += 2;
                    result.Append("G");
                }

                var accessStatus = await Geolocator.RequestAccessAsync();

                if (accessStatus == GeolocationAccessStatus.Allowed)
                {
                    resultId += 4;
                    result.Append("L");
                }

                if (Compass.GetDefault() != null)
                {
                    resultId += 8;
                    result.Append("M");
                }

                if (OrientationSensor.GetDefault() != null)
                {
                    resultId += 16;
                    result.Append("O");
                }

                if (LightSensor.GetDefault() != null)
                {
                    resultId += 32;
                    result.Append("P");
                }

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        ResultId = resultId,
                        Result   = result.ToString()
                    });

                break;
            }

            case "DATETIME":
            {
                var utcNow = DateTime.UtcNow;
                var now    = DateTime.Now;
                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        ResultD = (utcNow - new DateTime(1970, 1, 1)).TotalSeconds,
                        Result  = utcNow.ToString("s") + "Z",
                        Offset  = TimeZoneInfo.Local.GetUtcOffset(now).TotalMinutes
                    });

                break;
            }

            case "NAME":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.FriendlyName
                    });

                break;
            }

            case "OS":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.OperatingSystem
                    });

                break;
            }

            case "FWVER":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.SystemFirmwareVersion
                    });

                break;
            }

            case "HWVER":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.SystemHardwareVersion
                    });

                break;
            }


            case "PRODUCTNAME":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.SystemProductName
                    });

                break;
            }


            case "MANUFACTURER":
            {
                var deviceInfo = new EasClientDeviceInformation();

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = deviceInfo.SystemManufacturer
                    });

                break;
            }

            case "GET":
            {
                if (string.IsNullOrWhiteSpace(devMessage.Key))
                {
                    await SendResult(new DeviceResultMessage(devMessage) { ResultId = -2 });

                    return;
                }

                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        Result = appSettings.GetValueOrDefault(devMessage.Message, devMessage.Key)
                    });

                break;
            }

            case "SET":
            {
                if (string.IsNullOrWhiteSpace(devMessage.Key))
                {
                    await SendResult(new DeviceResultMessage(devMessage) { ResultId = -2 });

                    return;
                }

                var original = appSettings.GetValueOrDefault(string.Empty, devMessage.Key);
                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        ResultId = appSettings.AddOrUpdateValue(devMessage.Message, devMessage.Key) ? 0 : -1,
                        Result   = original
                    });

                appSettings.ReportChanged(devMessage.Key);

                break;
            }

            case "DELETE":
            {
                if (string.IsNullOrWhiteSpace(devMessage.Key))
                {
                    await SendResult(new DeviceResultMessage(devMessage) { ResultId = -2 });

                    return;
                }

                var result = appSettings.Remove(devMessage.Key);
                await SendResult(new DeviceResultMessage(devMessage)
                    {
                        ResultId = result ? 0 : 1
                    });

                break;
            }
            }
        }
        private const uint baseMinimum = 100; //ms

#pragma warning disable CS1998                // Async method lacks 'await' operators and will run synchronously
        public async void Start()
#pragma warning restore CS1998                // Async method lacks 'await' operators and will run synchronously
        {
            if (SensorSwitches.A != null)
            {
                if (SensorSwitches.A.Value > 0)
                {
                    if (accelerometer == null)
                    {
                        accelerometer = Accelerometer.GetDefault();
                    }

                    if (accelerometer != null)
                    {
                        accelerometer.ReportInterval = Math.Max(Math.Max(baseMinimum, (uint)SensorSwitches.Interval), accelerometer.MinimumReportInterval);
                        if (SensorSwitches.A.Value != 1)
                        {
                            accelerometer.ReadingChanged += NewAcc;
                        }

                        this[ACCELERATOR].Id    = SensorSwitches.Id;
                        this[ACCELERATOR].Delta = SensorSwitches.Delta;

                        if (SensorSwitches.A.Value != 3)
                        {
                            SensorSwitches.A = null;
                            NewAcc(accelerometer, null);
                        }
                        else
                        {
                            SensorSwitches.A = null;
                        }
                    }
                }
                else
                {
                    if (accelerometer != null)
                    {
                        accelerometer.ReadingChanged -= NewAcc;
                        NewAcc(null, null);
                    }

                    SensorSwitches.A = null;
                }
            }

            if (SensorSwitches.G != null)
            {
                if (SensorSwitches.G.Value > 0)
                {
                    if (gyrometer == null)
                    {
                        gyrometer = Gyrometer.GetDefault();
                    }

                    if (gyrometer != null)
                    {
                        gyrometer.ReportInterval = Math.Max(Math.Max(baseMinimum, (uint)SensorSwitches.Interval), gyrometer.MinimumReportInterval);
                        if (SensorSwitches.G.Value != 1)
                        {
                            gyrometer.ReadingChanged += NewGyro;
                        }

                        this[GYROSCOPE].Id    = SensorSwitches.Id;
                        this[GYROSCOPE].Delta = SensorSwitches.Delta;

                        if (SensorSwitches.G.Value != 3)
                        {
                            SensorSwitches.G = null;
                            NewGyro(gyrometer, null);
                        }
                        else
                        {
                            SensorSwitches.G = null;
                        }
                    }
                }
                else
                {
                    if (gyrometer != null)
                    {
                        gyrometer.ReadingChanged -= NewGyro;
                        NewGyro(null, null);
                    }

                    SensorSwitches.G = null;
                }
            }

            if (SensorSwitches.M != null)
            {
                if (SensorSwitches.M.Value > 0)
                {
                    if (compass == null)
                    {
                        compass = Compass.GetDefault();
                    }

                    if (compass != null)
                    {
                        compass.ReportInterval = Math.Max(Math.Max(baseMinimum, (uint)SensorSwitches.Interval), compass.MinimumReportInterval);

                        if (SensorSwitches.M.Value != 1)
                        {
                            compass.ReadingChanged += NewCom;
                        }

                        this[COMPASS].Id    = SensorSwitches.Id;
                        this[COMPASS].Delta = SensorSwitches.Delta;

                        if (SensorSwitches.M.Value != 3)
                        {
                            SensorSwitches.M = null;
                            NewCom(compass, null);
                        }
                        else
                        {
                            SensorSwitches.M = null;
                        }
                    }
                }
                else
                {
                    if (compass != null)
                    {
                        compass.ReadingChanged -= NewCom;
                        NewCom(null, null);
                    }

                    SensorSwitches.M = null;
                }
            }

            if (SensorSwitches.L != null)
            {
                if (SensorSwitches.L.Value > 0)
                {
                    if (geolocator == null)
                    {
                        geolocator = new Geolocator();
                    }

                    if (geolocator != null)
                    {
                        geolocator.ReportInterval = 30 * 60 * 1000;

                        this[LOCATION].Id    = SensorSwitches.Id;
                        this[LOCATION].Delta = SensorSwitches.Delta;

                        if (SensorSwitches.L.Value != 1)
                        {
                            geolocator.PositionChanged += NewLoc;
                        }

                        if (SensorSwitches.L.Value != 3)
                        {
                            SensorSwitches.L = null;
                            NewLoc(geolocator, null);
                        }
                        else
                        {
                            SensorSwitches.L = null;
                        }
                    }
                }
                else
                {
                    if (geolocator != null)
                    {
                        geolocator.PositionChanged -= NewLoc;
                        NewLoc(null, null);
                    }

                    SensorSwitches.L = null;
                }
            }

            if (SensorSwitches.Q != null)
            {
                if (SensorSwitches.Q.Value > 0)
                {
                    if (orientation == null)
                    {
                        orientation = OrientationSensor.GetDefault();
                    }

                    if (orientation != null)
                    {
                        this[QUANTIZATION].Id    = SensorSwitches.Id;
                        this[QUANTIZATION].Delta = SensorSwitches.Delta;

                        orientation.ReportInterval = Math.Max(Math.Max(baseMinimum, (uint)SensorSwitches.Interval), orientation.MinimumReportInterval);
                        if (SensorSwitches.Q.Value != 1)
                        {
                            orientation.ReadingChanged += NewQuan;
                        }

                        if (SensorSwitches.Q.Value != 3)
                        {
                            SensorSwitches.Q = null;
                            NewQuan(orientation, null);
                        }
                        else
                        {
                            SensorSwitches.Q = null;
                        }
                    }
                }
                else
                {
                    if (orientation != null)
                    {
                        orientation.ReadingChanged -= NewQuan;
                        NewQuan(null, null);
                    }

                    SensorSwitches.Q = null;
                }
            }

            if (SensorSwitches.P != null)
            {
                if (SensorSwitches.P.Value > 0)
                {
                    if (lightSensor == null)
                    {
                        lightSensor = LightSensor.GetDefault();
                    }

                    if (lightSensor != null)
                    {
                        this[LIGHTSENSOR].Id    = SensorSwitches.Id;
                        this[LIGHTSENSOR].Delta = SensorSwitches.Delta;

                        lightSensor.ReportInterval = Math.Max(Math.Max(baseMinimum, (uint)SensorSwitches.Interval), lightSensor.MinimumReportInterval);
                        if (SensorSwitches.P.Value != 1)
                        {
                            lightSensor.ReadingChanged += NewLight;
                        }

                        if (SensorSwitches.P.Value != 3)
                        {
                            SensorSwitches.P = null;
                            NewLight(lightSensor, null);
                        }
                        else
                        {
                            SensorSwitches.P = null;
                        }
                    }
                }
                else
                {
                    if (lightSensor != null)
                    {
                        lightSensor.ReadingChanged -= NewLight;
                        NewQuan(null, null);
                    }

                    SensorSwitches.P = null;
                }
            }
        }
Beispiel #3
0
        private void ToggleSensors(SensorMessage sensorsMessage)
        {
            Sensors.SensorSwitches.Id = sensorsMessage.Id;

            foreach (var sensorItem in sensorsMessage.Sensors)
            {
                if (sensorItem.A != null)
                {
                    sensorsMessage.Type = 'A';
                    if (Accelerometer.GetDefault() == null)
                    {
                        throw new UnsupportedSensorException();
                    }

                    Sensors.SensorSwitches.A = sensorItem.A.Value;
                }
                else if (sensorItem.G != null)
                {
                    sensorsMessage.Type = 'G';
                    if (Gyrometer.GetDefault() == null)
                    {
                        throw new UnsupportedSensorException();
                    }

                    Sensors.SensorSwitches.G = sensorItem.G.Value;
                }
                else if (sensorItem.M != null)
                {
                    sensorsMessage.Type = 'M';
                    if (Compass.GetDefault() == null)
                    {
                        throw new UnsupportedSensorException();
                    }

                    Sensors.SensorSwitches.M = sensorItem.M.Value;
                }
                else if (sensorItem.L != null)
                {
                    sensorsMessage.Type      = 'L';
                    Sensors.SensorSwitches.L = sensorItem.L.Value;
                }
                else if (sensorItem.Q != null)
                {
                    sensorsMessage.Type = 'Q';
                    if (OrientationSensor.GetDefault() == null)
                    {
                        throw new UnsupportedSensorException();
                    }

                    Sensors.SensorSwitches.Q = sensorItem.Q.Value;
                }
                else if (sensorItem.P != null)
                {
                    sensorsMessage.Type = 'P';
                    if (LightSensor.GetDefault() == null)
                    {
                        throw new UnsupportedSensorException();
                    }

                    Sensors.SensorSwitches.P = sensorItem.P.Value;
                }

                //outside of scope - applies to last only
                Sensors.SensorSwitches.Delta    = sensorItem.Delta;
                Sensors.SensorSwitches.Interval = sensorItem.Interval;
            }

            Sensors.Start();
        }
        /// <summary>
        /// このページがフレームに表示されるときに呼び出されます。
        /// </summary>
        /// <param name="e">このページにどのように到達したかを説明するイベント データ。Parameter
        /// プロパティは、通常、ページを構成するために使用します。</param>
        async protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // 光センサー
            lightSensor = LightSensor.GetDefault();
            if (lightSensor != null)
            {
                lightSensor.ReadingChanged += MainPage_ReadingChanged;
            }
            else
            {
                TextLight.Text = @"光センサーはありません";
            }

            //加速度センサー
            accelerometer = Accelerometer.GetDefault();
            if (accelerometer != null)
            {
                accelerometer.ReadingChanged += MainPage_ReadingChanged;
                accelerometer.Shaken         += MainPage_Shaken;
            }
            else
            {
                TextAccelerometer.Text = @"加速度センサーはありません";
            }

            // ジャイロメーター
            gyrometer = Gyrometer.GetDefault();
            if (gyrometer != null)
            {
                gyrometer.ReadingChanged += MainPage_ReadingChanged;
            }
            else
            {
                TextGyrometer.Text = @"ジャイロメーターはありません";
            }

            // 傾斜センサー
            inclinometer = Inclinometer.GetDefault();
            if (inclinometer != null)
            {
                inclinometer.ReadingChanged += MainPage_ReadingChanged;
            }
            else
            {
                TextInclinometer.Text = @"傾斜センサーはありません";
            }

            // 方位センサー
            orientationSensor = OrientationSensor.GetDefault();
            if (orientationSensor != null)
            {
                orientationSensor.ReadingChanged += MainPage_ReadingChanged;
            }
            else
            {
                TextOrientation.Text = @"方位センサーはありません";
            }

            // 簡易方位センサー
            simpleOrientationSensor = SimpleOrientationSensor.GetDefault();
            if (simpleOrientationSensor != null)
            {
                simpleOrientationSensor.OrientationChanged += MainPage_OrientationChanged;
            }
            else
            {
                TextSimpleOrientation.Text = @"簡易方位センサーはありません";
            }

            // コンパス
            compass = Compass.GetDefault();
            if (compass != null)
            {
                compass.ReadingChanged += MainPage_ReadingChanged;
            }
            else
            {
                TextCompass.Text = @"コンパスはありません";
            }

            //try {
            //    geo.DesiredAccuracy = PositionAccuracy.High;
            //    var pos = await geo.GetGeopositionAsync();
            //    TextGeolocation.Text = string.Format( @"{0} {1} {2} {3} {4} {5} {6}",
            //        pos.Coordinate.Latitude, pos.Coordinate.Longitude, pos.Coordinate.Accuracy );
            //}
            //catch ( Exception ex ) {
            //    MessageDialog dlg=new MessageDialog( ex.Message );
            //    dlg.ShowAsync();
            //}
        }