예제 #1
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
        private static WindowState HandleTempAndSI(HomeConfig config, float temperature, float si)
        {
            if (si >= config.Threshold.SI)
            {
                Open(config);
                return(WindowState.Opened);
            }

            if (temperature >= config.Threshold.Temperature)
            {
                Close(config);
                return(WindowState.Closed);
            }

            return(WindowState.None);
        }
예제 #2
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
        private static HomeConfig GetConfig()
        {
            HomeConfig config = null;

            if (File.Exists(HomeConfig.ConfigPath))
            {
                using (var sr = new StreamReader(HomeConfig.ConfigPath))
                {
                    config = JsonConvert.DeserializeObject <HomeConfig>(sr.ReadToEnd());
                }
            }

            if (config == null)
            {
                config = new HomeConfig();
                Console.WriteLine("There is no config file");

                Console.WriteLine("Please provide the DHT sensor type");
                Console.WriteLine("DHT11 = 0");
                Console.WriteLine("DHT12 = 1");
                Console.WriteLine("DHT21 = 2");
                Console.WriteLine("DHT22 = 3");
                Console.WriteLine("AM2301 = 4");
                Console.WriteLine("AM2302 = 5");
                Console.Write("DHT sensor type : ");

                if (int.TryParse(Console.ReadLine(), out var dhtNum))
                {
                    config.DhtType = (DhtType)dhtNum;
                }

                Console.Write("DHT sensor pin : ");

                if (int.TryParse(Console.ReadLine(), out var pin))
                {
                    config.DhtSensorPin = (BcmPin)pin;
                }
            }

            return(config);
        }
예제 #3
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
        private static async Task <WinofyClient> PrepareClientAsync(HomeConfig config)
        {
            var client = new WinofyClient();

            if (!string.IsNullOrEmpty(config.AuthorizationToken))
            {
                var result = await client.ValidateTokenAsync(config.Username, config.AuthorizationToken);

                if (!result.Valid)
                {
                    config.AuthorizationToken = null;
                }
            }

            while (string.IsNullOrEmpty(config.AuthorizationToken))
            {
                Console.Write("Username : "******"Password : "******"Failed to login. Please check your username and password");
                }
            }

            client.SetAuthorizationToken(config.AuthorizationToken);

            if (string.IsNullOrEmpty(config.Device?.Id))
            {
                Console.WriteLine("This device may not be registered");
                Console.WriteLine("Tell us the device name and description");
                Console.Write("Name : ");
                var name = Console.ReadLine();
                Console.Write("Description : ");
                var desc = Console.ReadLine();

                var device = new Device()
                {
                    Name         = name,
                    Description  = desc,
                    Notification = true
                };

                var result = await client.RegisterDeviceAsync(device, true);

                if (result.Success)
                {
                    config.Device = device;
                }
                else
                {
                    Console.WriteLine("Failed to register this device. See messages from the server");
                    Console.WriteLine(result.Message.ToString());
                }
            }

            using (var sw = new StreamWriter(HomeConfig.ConfigPath))
            {
                sw.Write(JsonConvert.SerializeObject(config, Formatting.Indented));
            }

            return(client);
        }
예제 #4
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
 private static void Close(HomeConfig config)
 {
     SetPWM(config.PWM2, true);
     SetPWM(config.PWM1, false);
     Thread.Sleep(config.PWM2.DelayMs);
 }
예제 #5
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
 private static void Open(HomeConfig config)
 {
     SetPWM(config.PWM1, true);
     SetPWM(config.PWM2, false);
     Thread.Sleep(config.PWM1.DelayMs);
 }
예제 #6
0
파일: Program.cs 프로젝트: SKKbySSK/winofy
        static async Task Main(string[] args)
        {
            if (args.Contains("-dht"))
            {
                TestDHT();
                return;
            }

            if (args.Contains("-d7s"))
            {
                TestD7S();
                return;
            }

            HomeConfig config = GetConfig();
            var        client = await PrepareClientAsync(config);

            await client.RecordAsync(config.Device.Id, 40, Axes.XY, 30, 0.99f, WindowState.Opened);

            Pi.Init <BootstrapWiringPi>();

            using (var dht = DhtSensor.Create(config.DhtType, Pi.Gpio[config.DhtSensorPin]))
            {
                dht.Start();
                dht.OnDataAvailable += Dht_OnDataAvailable;

                Console.WriteLine("Preparing vibration sensor...");
                var d7s = new D7s(config.D7sAddress);
                d7s.Reset();

                while (d7s.GetState() != D7sState.Normal)
                {
                    Thread.Yield();
                }

                Console.WriteLine("Preparing DHT sensor...");
                while (!humidity.HasValue || !temperature.HasValue)
                {
                    Thread.Yield();
                }

                Console.WriteLine("Devices preparation finished");
                Console.WriteLine("Recording vibration, temperature, humidty will be started");

                var devId = config.Device?.Id;
                while (true)
                {
                    Thread.Sleep(config.RecordingIntervalMs);

                    try
                    {
                        var ax   = d7s.GetAxis();
                        var si   = d7s.ReadSI();
                        var h    = (float)humidity.Value;
                        var temp = (float)temperature.Value;
                        Console.WriteLine($"温度:{temp}℃, 湿度:{h}%, 軸:{ax}, SI値:{si}");

                        if (!string.IsNullOrEmpty(devId))
                        {
                            var window = HandleTempAndSI(config, temp, si);

                            switch (window)
                            {
                            case WindowState.Opened:
                                Console.WriteLine("Window will be opened");
                                break;

                            case WindowState.Closed:
                                Console.WriteLine("Window will be closed");
                                break;
                            }

                            await client.RecordAsync(devId, si, (Axes)ax, temp, h, window);
                        }
                    }
                    catch (HardwareException hw)
                    {
                        Console.WriteLine("Failed to read SI value");
                    }
                }
            }
        }