Пример #1
0
        private string DecodeResponse(string data)
        {
            string result = "";

            result = data;



            // PowerStatus
            if (data.StartsWith("PWR"))
            {
                int newStatus = 0;
                if (data == "PWR0")
                {
                    newStatus = 1;     //on
                }
                else
                {
                    newStatus = 0;     //off

                    _lastSong             = "";
                    _lastStation          = "";
                    _lastInputSource      = InputSource.Unknown;
                    _lastInputAudioSignal = InputAudioSignal.Unknown;
                    _lastVolume           = 0;
                    _lastListeningMode    = "";
                }

                if (newStatus != _lastPowerStatus)
                {
                    if (newStatus == 1)
                    {
                        _lastPowerStatus = newStatus;
                        result           = "Power is ON";
                        SendToMqtt("Power", "on");
                    }
                    else
                    {
                        result = "Power is OFF";
                        SendToMqtt("Power", "off");
                        SendToMqtt("Source", "");
                        SendToMqtt("Volume", "-80.5");
                        SendToMqtt("WebRadioSong", "");
                        SendToMqtt("WebRadioStation", "");
                        SendToMqtt("ListeningMode", "");
                        SendToMqtt("InputAudioSignal", "");
                        _lastPowerStatus = newStatus;
                    }

                    int    deviceId = 148;
                    string url      = $"http://rpi2a.bem.lan:8080/json.htm?type=command&param=udevice&idx={deviceId.ToString()}&nvalue={newStatus.ToString()}&svalue=";
                    SendApiCall(url).Wait(1000);
                }
                else
                {
                    if (newStatus == 1)
                    {
                        result = "Power is ON, Status Unchanged!";
                    }
                    else
                    {
                        result = "Power is OFF, Status Unchanged!";
                    }
                }
            }

            // InputSource
            if (data.StartsWith("FN"))
            {
                InputSource inputSource = InputSource.Unknown;
                if (Int32.TryParse(data.Substring(2), out int i))
                {
                    try
                    {
                        inputSource = (InputSource)i;
                    }
                    catch
                    {
                        //
                    }
                }

                if (inputSource != _lastInputSource)
                {
                    bool   isWebRadio = false;
                    double newVolume  = 0;

                    InputSourceProperties inputSourceProperties = _inputSourceProperties.FirstOrDefault(q => q.Source == inputSource);

                    string inputName = inputSource.ToString();

                    if (inputSourceProperties != null)
                    {
                        newVolume  = inputSourceProperties.Volume;
                        isWebRadio = inputSourceProperties.IsWebradio;
                        inputName  = inputSourceProperties.Name;
                    }
                    else
                    {
                        newVolume = -60;
                    }

                    /*
                     * switch (inputSource)
                     * {
                     *  case InputSource.INTERNET_RADIO:
                     *  case InputSource.FAVORITES:
                     *      newVolume = -60;
                     *      isWebRadio = true;
                     *      inputName = "WebRadio";
                     *      break;
                     *  case InputSource.SAT_CBL: //DreamBox
                     *      newVolume = -60;
                     *      inputName = "Volumio";
                     *      break;
                     *  case InputSource.BD:  //Kodi
                     *      newVolume = -30;
                     *      inputName = "Kodi";
                     *      break;
                     *  case InputSource.DVR_BDR:  //ChromeCast
                     *      newVolume = -40;
                     *      inputName = "ChromeCast";
                     *      break;
                     *  case InputSource.DVD:  //AndroidTV
                     *      newVolume = -30;
                     *      inputName = "AndroidTV";
                     *      break;
                     *  case InputSource.TUNER:  //Tuner
                     *      newVolume = -40;
                     *      inputName = "Tuner";
                     *      break;
                     *  default:
                     *      newVolume = -60;
                     *      break;
                     * }
                     */

                    if (!isWebRadio)
                    {
                        SendToMqtt("WebRadioSong", "");
                        SendToMqtt("WebRadioStation", "");
                        _lastStation = "";
                        _lastSong    = "";
                    }

                    if (newVolume < 0)
                    {
                        result  = $"SourceChange: {inputSource.ToString()} - ";
                        result += SetVolume(newVolume, "VSX-1123");
                    }
                    else
                    {
                        result = $"SourceChange: {inputSource.ToString()}";
                    }

                    SendToMqtt("Source", inputName);

                    _lastInputSource = inputSource;
                }
            }

            //
            // InputAudioSignal
            if (data.StartsWith("AST"))
            {
                // AST6401000000000000000000000111111010000000000012400000000
                string           frequency        = "";
                InputAudioSignal inputAudioSignal = InputAudioSignal.Unknown;
                if (Int32.TryParse(data.Substring(3, 2), out int i))
                {
                    try
                    {
                        inputAudioSignal = (InputAudioSignal)i;
                    }
                    catch
                    {
                        //
                    }


                    switch (data.Substring(5, 2))
                    {
                    case "00": frequency = "32kHz"; break;

                    case "01": frequency = "44.1kHz"; break;

                    case "02": frequency = "48kHz"; break;

                    case "03": frequency = "88.2kHz"; break;

                    case "04": frequency = "96kHz"; break;

                    case "05": frequency = "176.4kHz"; break;

                    case "06": frequency = "192kHz"; break;

                    case "07": frequency = "Unknown"; break;

                    default:
                        break;
                    }
                }

                result = $"AudioSignalChange: {inputAudioSignal.ToString()} {frequency}";

                if (inputAudioSignal != _lastInputAudioSignal)
                {
                    _lastInputAudioSignal = inputAudioSignal;
                    SendToMqtt("InputAudioSignal", inputAudioSignal.ToString());
                }
            }

            // Volume
            if (data.StartsWith("VOL"))
            {
                double volume = double.Parse(data.Substring(3));
                volume = -80.5 + 0.5 * volume;

                if (volume != _lastVolume)
                {
                    result      = $"Volume is set to {volume.ToString()} dB [{data.Substring(3)}]";
                    _lastVolume = volume;

                    int    deviceId = 90;
                    string url      = $"http://rpi2a.bem.lan:8080/json.htm?type=command&param=udevice&idx={deviceId.ToString()}&nvalue=0&svalue={volume.ToString()}";
                    SendApiCall(url).Wait(1000);

                    SendToMqtt("Volume", volume.ToString());
                }
                else
                {
                    result = $"Volume is set to {volume.ToString()} dB [{data.Substring(3)}], Unchanged.";
                }
            }

            // Display - Info
            if (data.StartsWith("FL00"))
            {
                result = "FL00: " + Encoding.ASCII.GetString(ConvertStringToByteArray(data.Substring(4)));
            }

            // Display - Title
            if (data.StartsWith("FL02"))
            {
                //result = "FL02: " + Encoding.ASCII.GetString(ConvertStringToByteArray(data.Substring(4)));
                result = "";     // for now - Avoid SPAM
            }


            /*
             *  -- Response NETWORK meta data
             *  GBH*<CR+LF>
             *  GCH*<CR+LF>
             *  GDH*<CR+LF>
             *  GEH*<CR+LF>"
             *  GHH*<CR+LF>
             */
            if (data.StartsWith("GBH"))
            {
                //result = $"GBH: START `Response NETWORK meta data` ({result})";
                result = "";     // for now - Avoid SPAM
            }
            if (data.StartsWith("GHH"))
            {
                //result = $"GHH: END `Response NETWORK meta data` ({result})";
                result = "";     // for now - Avoid SPAM
            }
            if (data.StartsWith("GCH"))
            {
                result = "";
            }
            if (data.StartsWith("GDH"))
            {
                result = "";
            }

            // WebRadio Song
            // GEH01020"Lo Moon  - Real Love "
            if (data.StartsWith("GEH"))
            {
                string text = null;
                if (data.Length > 8)
                {
                    text = data.Substring(8).Replace("\"", "").Trim().Replace("  ", " ").Replace("  ", " ");
                }
                result = "";

                if (!String.IsNullOrWhiteSpace(text))
                {
                    switch (data.Substring(0, 8))
                    {
                    case "GEH01020":          //Song
                        if (_lastSong != text && !text.StartsWith("(c)"))
                        {
                            _lastSong = text;
                            result    = "NewWebRadioSong: " + text;
                            int deviceId = 149;
                            text = System.Net.WebUtility.UrlEncode(text);
                            string url = $"http://rpi2a.bem.lan:8080/json.htm?type=command&param=udevice&idx={deviceId.ToString()}&nvalue=0&svalue={text}";
                            SendApiCall(url).Wait(1000);

                            SendToMqtt("WebRadioSong", text);
                        }
                        break;

                    case "GEH03021":          //Artist
                        result = "NewArtist: " + text;
                        break;

                    case "GEH04022":          //Station
                        if (_lastStation != text)
                        {
                            _lastStation = text;
                            result       = "NewStation: " + text;

                            SendToMqtt("WebRadioStation", text);
                        }
                        break;

                    default:
                        break;
                    }
                }
            }


            // LM020d ???
            if (data.StartsWith("LM"))
            {
                string listeningMode = GetLmResult(data.Substring(2));
                result = $"Listening Mode: {listeningMode} [{data}]";
                if (listeningMode != _lastListeningMode)
                {
                    SendToMqtt("ListeningMode", listeningMode);
                    _lastListeningMode = listeningMode;
                }
                else
                {
                    result += " (Unchanged)";
                }
            }


            // Ignore VTA and AU_
            if (data.StartsWith("VTA") || data.StartsWith("AU"))
            {
                result = "";
            }

            if (!String.IsNullOrWhiteSpace(result))
            {
                try
                {
                    System.IO.File.AppendAllText(_logFile, $"{DateTime.Now.ToString("yyyMMdd.HHmmss")}: {result}\r\n");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            return(result);
        }
Пример #2
0
            public static int Main(String[] args)
            {
                AssemblyLoadContext.Default.Unloading += SigTermEventHandler; //register sigterm event handler. Don't forget to import System.Runtime.Loader!
                Console.CancelKeyPress += CancelHandler;                      //register sigint event handler

                List <InputSourceProperties> inputSources = new List <InputSourceProperties>();

                IConfiguration config;
                string         logFile = "";

                try
                {
                    config = new ConfigurationBuilder()
                             .AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true)
                             .Build();
                    logFile = $"/home/piopi/piopi.log";
                }
                catch
                {
                    config = new ConfigurationBuilder()
                             .AddJsonFile($"/home/bem/Projects/BeM_Apps/PioPi/appsettings.json", optional: false, reloadOnChange: true)
                             .Build();
                    logFile = $"/home/bem/Projects/BeM_Apps/PioPi/piopi.log";
                }

                foreach (string item in Enum.GetNames(typeof(PioPi.PioVSX.InputSource)))
                {
                    string name = config[$"InputSource:{item}:Name"];

                    if (name != null)
                    {
                        int  volume     = -1;
                        bool isWebradio = false;

                        int.TryParse(config[$"InputSource:{item}:Volume"], out volume);
                        isWebradio = ((config[$"InputSource:{item}:IsWebRadio"])?.ToLower() == "true");


                        InputSourceProperties inputSource = new InputSourceProperties();
                        inputSource.Source     = (PioPi.PioVSX.InputSource)Enum.Parse(typeof(PioPi.PioVSX.InputSource), item);
                        inputSource.Name       = name;
                        inputSource.Volume     = volume;
                        inputSource.IsWebradio = isWebradio;

                        inputSources.Add(inputSource);
                    }
                }



                _pioVSX = new PioVSX(inputSources, logFile);
                _pioVSX.SendInformationEvent += InformationEventHandler;
                _pioVSX.SendResponseEvent    += ResponseEventHandler;
                _pioVSX.Start();

                StartClient();

                while (IsRunning)
                {
                    Thread.Sleep(2000);
                }

                _pioVSX.Stop();
                _pioVSX.SendInformationEvent -= InformationEventHandler;
                _pioVSX.SendResponseEvent    -= ResponseEventHandler;
                _pioVSX = null;

                return(0);
            }