Inheritance: IPort, IRunnable
コード例 #1
0
ファイル: Raspberry.cs プロジェクト: HakanL/animatroller
        private void Initialize(string hostEntry, int listenPort)
        {
            var hostParts = hostEntry.Split(':');
            if (hostParts.Length != 2)
                throw new ArgumentException("Requires a host entry with this format [IP:port]");

            this.hostName = hostParts[0];
            this.hostPort = int.Parse(hostParts[1]);

            var ipHostEntry = System.Net.Dns.GetHostAddresses(this.hostName);
            this.oscClient = new OscClient(ipHostEntry.First(), this.hostPort);

            this.DigitalInputs = new PhysicalDevice.DigitalInput[8];
            for (int index = 0; index < this.DigitalInputs.Length; index++)
                this.DigitalInputs[index] = new PhysicalDevice.DigitalInput();

            this.DigitalOutputs = new PhysicalDevice.DigitalOutput[8];
            for (int index = 0; index < this.DigitalOutputs.Length; index++)
                WireupOutput(index);

            this.audioTrackStart = new Subject<string>();

            this.Motor = new PhysicalDevice.MotorWithFeedback((target, speed, timeout) =>
            {
                this.oscClient.Send("/motor/exec", 1, target, (int)(speed * 100), timeout.TotalSeconds.ToString("F0"));
            });

            this.oscServer = new OscServer(listenPort);
            this.oscServer.RegisterAction("/init", msg =>
                {
                    if (!CheckIdempotence(msg))
                        return;

                    log.Info("Raspberry is up");
                });

            this.oscServer.RegisterAction("/audio/trk/done", msg =>
                {
                    if (!CheckIdempotence(msg))
                        return;

                    log.Debug("Audio track done");
                    RaiseAudioTrackDone();
                });

            this.oscServer.RegisterAction("/video/done", msg =>
            {
                if (!CheckIdempotence(msg))
                    return;

                log.Debug("Video done");
                RaiseVideoTrackDone();
            });

            this.oscServer.RegisterAction<string>("/audio/bg/start", (msg, data) =>
            {
                if (!CheckIdempotence(msg))
                    return;

                if (data.Count() >= 2)
                {
                    string track = data.Skip(1).First();
                    log.Debug("Playing background track {0}", track);
                    this.audioTrackStart.OnNext(track);
                }
            });

            this.oscServer.RegisterAction<string, int>("/input", (msg, id, data) =>
                {
                    if (!CheckIdempotence(msg))
                        return;

                    if (data.Count() >= 2)
                    {
                        var values = data.ToArray();
                        log.Info("Input {0} set to {1}", values[0], values[1]);

                        if (values[0] >= 0 && values[0] <= 7)
                            this.DigitalInputs[values[0]].Trigger(values[1] != 0);
                    }
                });

            this.oscServer.RegisterAction("/motor/feedback", msg =>
                {
                    if (!CheckIdempotence(msg))
                        return;

                    if (msg.Data.Count() >= 2)
                    {
                        var values = msg.Data.ToArray();

                        int motorChn = int.Parse(values[0].ToString());
                        string motorPos = values[1].ToString();

                        if (motorPos == "FAIL")
                        {
                            log.Info("Motor {0} failed", motorChn);

                            if (motorChn == 1)
                                this.Motor.Trigger(null, true);
                        }
                        else
                        {
                            if (motorPos.StartsWith("S"))
                            {
                                int pos = int.Parse(motorPos.Substring(1));
                                log.Info("Motor {0} starting at position {1}", motorChn, pos);
                            }
                            else if (motorPos.StartsWith("E"))
                            {
                                int pos = int.Parse(motorPos.Substring(1));
                                log.Info("Motor {0} ending at position {1}", motorChn, pos);

                                if (motorChn == 1)
                                    this.Motor.Trigger(pos, false);
                            }
                            else
                            {
                                int pos = int.Parse(motorPos);
                                log.Debug("Motor {0} at position {1}", motorChn, pos);
                            }
                        }
                    }
                });

            this.DigitalInputs = new PhysicalDevice.DigitalInput[8];
            for (int index = 0; index < this.DigitalInputs.Length; index++)
                this.DigitalInputs[index] = new PhysicalDevice.DigitalInput();

            Executor.Current.Register(this);
        }