Exemple #1
0
        private IProtocol ExecuteSoundCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                // 播放音调
                if (command is SoundCommands.PlayPitchCommand)
                {
                    var cmd = (SoundCommands.PlayPitchCommand)command;
                    PlayPitch(cmd.pitch, cmd.duration);
                    result.code = 0;
                    break;
                }

                // 播放频率
                if (command is SoundCommands.PlayFrequencyCommand)
                {
                    var cmd = (SoundCommands.PlayFrequencyCommand)command;
                    PlayFrequency(cmd.frequency, cmd.duration);
                    result.code = 0;
                    break;
                }

                // 停止播放
                if (command is SoundCommands.StopCommand)
                {
                    // var cmd = (SoundCommands.StopCommand)command;
                    StopPlay();
                    result.code = 0;
                    break;
                }
            }while (false);
            return(result);
        }
Exemple #2
0
        protected override IProtocol ExecuteUKitCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command); do

            {
                if (command is UKitCommands.ColorRecognitionCommand)
                {
                    debug = command.debug;
                    var cmd = (UKitCommands.ColorRecognitionCommand)command;
                    if (LineTrace(out var distance, out Color rgb))
                    {
                        result.SetDatas(Misc.Nearly(cmd.color, rgb));
                        result.code = 0;

                        DebugUtility.Log(LoggerTags.Project, "ColorRecognitionCommand Reacted : {0}m", distance);
                    }
                    break;
                }
                if (command is UKitCommands.ColorRecognitionRGBCommand)
                {
                    debug = command.debug;
                    //var cmd = (UKitCommands.UltrasonicCommand)command;
                    if (LineTrace(out var distance, out Color rgb))
                    {
                        result.SetDatas((int)(rgb.r * 255), (int)(rgb.g * 255), (int)(rgb.b * 255));
                        result.code = 0;

                        DebugUtility.Log(LoggerTags.Project, "ColorRecognitionRGBCommand Reacted : {0}m", distance);
                    }
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #3
0
        public static ICommand CreateCommand(ExploreProtocol protocol)
        {
            int device = protocol.device;
            int mode   = protocol.mode;
            int id     = protocol.id;
            var datas  = protocol.datas;

            if (msExplorerCreators.TryGetValue(device, out var creator))
            {
                var result = creator(protocol);
                if (result != null)
                {
                    result.host    = protocol.host;
                    result.device  = device;
                    result.id      = id;
                    result.cmdMode = mode;
                    result.uuid    = protocol.uuid;
                    result.debug   = protocol.debug;
                    return(result);
                }
                else
                {
                    DebugUtility.LogError(LoggerTags.Project, "Failed to create command, Unsupport protocol: {0}", protocol.ToString());
                }
            }
            else
            {
                DebugUtility.LogError(LoggerTags.Project, "Failed to create command, Missing device: {0}, Unsupport protocol: {1}", device, protocol.ToString());
            }

            return(null);
        }
Exemple #4
0
        private bool Populate(ExploreProtocol result)
        {
            if (buttonState == EButtonSensorState.None)
            {
                result.code = 1;
                return(false);
            }

            result.code = 0;
            switch (buttonState)
            {
            case EButtonSensorState.Click:
                // 点击
                result.SetDatas(1);
                break;

            case EButtonSensorState.DoubleClick:
                // 双击
                result.SetDatas(2);
                break;

            case EButtonSensorState.LongPress:
                // 长按
                result.SetDatas(3);
                break;

            default:
                result.SetDatas(1);
                break;
            }
            return(true);
        }
Exemple #5
0
        private static ExploreProtocol CreateResponse(ProtocolCode code, int device, int mode, int id, string uuid)
        {
            var protocol = new ExploreProtocol();

            protocol.dataType = ExploreProtocolDataType.DTObject;
            protocol.code     = code == ProtocolCode.Success ? 0 : 1;
            protocol.device   = device;
            protocol.mode     = mode;
            protocol.id       = id;
            protocol.uuid     = uuid;
            protocol.res      = true;
            return(protocol);
        }
Exemple #6
0
        private IProtocol ExecuteMotorCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is MotorCommands.VelocitySettingCommand)
                {
                    var cmd = (MotorCommands.VelocitySettingCommand)command;
                    jobStatus = WorkStatus.RotationAlways;
                    var job = (RotationAlwaysJob)currentJob;
                    job.velocity = cmd.velocity;
                    result.code  = 0;
                    break;
                }

                if (command is MotorCommands.PWMSettingCommand)
                {
                    var cmd = (MotorCommands.PWMSettingCommand)command;
                    jobStatus = WorkStatus.RotationAlways;
                    var job = (RotationAlwaysJob)currentJob;
                    job.velocity = cmd.pwm;
                    result.code  = 0;
                    break;
                }

                if (command is MotorCommands.ReadVelocityCommand)
                {
                    var cmd = (MotorCommands.ReadVelocityCommand)command;
                    int v   = 0;
                    if (jobStatus == WorkStatus.RotationAlways)
                    {
                        var job = (RotationAlwaysJob)currentJob;
                        v = (int)job.velocity;
                    }
                    result.SetDatas((int)v);
                    result.code = 0;
                    break;
                }

                if (command is MotorCommands.StopCommand)
                {
                    var cmd = (MotorCommands.StopCommand)command;
                    Stop();
                    result.code = 0;
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #7
0
        protected bool ExecuteUKitCommands(ICommand command, out IProtocol result)
        {
            bool resImme = true;
            var  result2 = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is UKitCommands.ButtonCommand)
                {
                    debug = command.debug;
                    if (!Populate(result2))
                    {
                        resImme = false;
                    }
                    break;
                }
            } while (false);

            result = result2;
            return(resImme);
        }
Exemple #8
0
        protected override IProtocol ExecuteUKitCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is UKitCommands.InfraredCommand)
                {
                    debug = command.debug;
                    if (LineTrace(out var distance))
                    {
                        result.SetDatas((int)(distance * 100.0f));
                        result.code = 0;

                        DebugUtility.Log(LoggerTags.Project, "InfraredCommand Reacted : {0}m", distance);
                    }
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #9
0
        protected override IProtocol ExecuteUKitCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is UKitCommands.SoundCommand)
                {
                    debug = command.debug;
                    if (GetEnvironment(out var env))
                    {
                        //var cmd = (UKitCommands.SoundCommand)command;
                        result.SetDatas(Mathf.CeilToInt(env.soundVolume));
                        result.code = 0;
                        DebugUtility.Log(LoggerTags.Project, "SoundCommand Reacted : {0}", env.soundVolume);
                    }
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #10
0
        protected override IProtocol ExecuteUKitCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is UKitCommands.TemperatureCommand)
                {
                    debug = command.debug;
                    if (GetEnvironment(out var env))
                    {
                        var cmd = (UKitCommands.TemperatureCommand)command;
                        if (cmd.mode == 0)
                        {
                            var temp = Mathf.CeilToInt(env.temperature);
                            result.SetDatas(temp);
                            DebugUtility.Log(LoggerTags.Project, "TemperatureCommand Reacted : {0}C", temp.ToString());
                        }
                        else if (cmd.mode == 1)
                        {
                            var f = Mathf.CeilToInt(C2F(env.temperature));
                            result.SetDatas(f);
                            DebugUtility.Log(LoggerTags.Project, "TemperatureCommand Reacted : {0}F", f.ToString());
                        }
                        else if (cmd.mode == 2)
                        {
                            var f = Mathf.CeilToInt(env.humidity);
                            result.SetDatas(f);
                            DebugUtility.Log(LoggerTags.Project, "TemperatureCommand Reacted : {0}%", f.ToString());
                        }
                        result.code = 0;
                    }
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #11
0
        protected virtual IProtocol ExecutePatrolCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is PatrolSensorCommands.PatrolSensorBasicCommand)
                {
                    debug = command.debug;
                    var cmd = (PatrolSensorCommands.PatrolSensorBasicCommand)command;
                    if (LineTrace(out var distance, out Color rgb))
                    {
                        float gray = Misc.ToGray(rgb);
                        result.SetDatas(gray >= 0.5f && cmd.isGray);
                        result.code = 0;

                        DebugUtility.Log(LoggerTags.Project, "PatrolSensorBasicCommand Reacted : {0}m", distance);
                    }
                    break;
                }
            } while (false);
            return(result);
        }
Exemple #12
0
        private IProtocol ExecuteCommand(ICommand command)
        {
            var result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            if (command is EyeCommands.DisableLightCommand)
            {
                LightOff();
                result.code = 0;
            }
            else if (command is EyeCommands.EnableLightCommand)
            {
                var cmd = (EyeCommands.EnableLightCommand)command;
                LightUp(cmd.color);
                result.code = 0;
            }
            else if (command is EyeCommands.CustomLightCommand)
            {
                var cmd = (EyeCommands.CustomLightCommand)command;
                ShowCustomLight(cmd.colors, cmd.duration);
                result.code = 0;
            }
            else if (command is EyeCommands.PredefineLightColorCommand)
            {
                var cmd = (EyeCommands.PredefineLightColorCommand)command;
                ShowPredefineLight(cmd.mode, cmd.count);
                result.code = 0;
            }
            else if (command is EyeCommands.ExpressionCommand)
            {
                var cmd = (EyeCommands.ExpressionCommand)command;
                ShowExpression(cmd.mode, cmd.color, cmd.count);
                result.code = 0;
            }

            return(result);
        }
Exemple #13
0
 /// <summary>
 /// 超声波传感器
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookAsonic(ExploreProtocol protocol)
 {
 }
Exemple #14
0
 /// <summary>
 /// 红外传感器
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookFrared(ExploreProtocol protocol)
 {
 }
Exemple #15
0
        public static ExploreProtocol Create(ExploreProtocolDataType type, string jsonStr, string context = "")
        {
            try
            {
                var node = JsonMapper.ToObject(jsonStr);
                if (node != null)
                {
                    int device = -1;
                    if (node.ContainsKey("device"))
                    {
                        device = (int)node["device"];
                    }

                    int mode = -1;
                    if (node.ContainsKey("mode"))
                    {
                        mode = (int)node["mode"];
                    }

                    int id = -1;
                    if (node.ContainsKey("id"))
                    {
                        id = (int)node["id"];
                    }

                    int code = -1;
                    if (node.ContainsKey("code"))
                    {
                        code = (int)node["code"];
                    }

                    string uuid = string.Empty;
                    if (node.ContainsKey("uuid"))
                    {
                        uuid = ((string)node["uuid"]);                        //.ToLower();
                    }
                    bool debug = false;
                    if (node.ContainsKey("debug"))
                    {
                        debug = (int)node["debug"] == 1;
                    }

                    ExploreProtocol p = new ExploreProtocol();
                    p.dataType = type;
                    p.device   = device;
                    p.mode     = mode;
                    p.id       = id;
                    p.code     = code;
                    p.uuid     = uuid;
                    p.debug    = debug;

                    if (node.ContainsKey("data"))
                    {
                        var datas = node["data"];                         // it's array of int
                        switch (type)
                        {
                        case ExploreProtocolDataType.DTObject:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out object result);
                                    return(result);
                                }, ArrayHelper <object> .Empty);
                            break;
                        }

                        case ExploreProtocolDataType.DTInt:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out int result);
                                    return(result);
                                }, ArrayHelper <int> .Empty);
                            break;
                        }

                        case ExploreProtocolDataType.DTFloat:
                        {
                            p.datas = LitJsonHelper.ParseJsonArray(datas, jsonParam =>
                                {
                                    LitJsonHelper.SafeConvert(jsonParam, out float result);
                                    return(result);
                                }, ArrayHelper <float> .Empty);
                            break;
                        }
                        }
                    }
                    return(p);
                }
            }
            catch (System.Exception ex)
            {
                DebugUtility.LogError(LoggerTags.Module, "Failure to create json data , Context : {0}, Stack : {1}", context, ex.StackTrace);
            }
            return(null);
        }
Exemple #16
0
 /// <summary>
 /// 亮度传感器
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookLuminance(ExploreProtocol protocol)
 {
     protocol.GetParamf(0, out luminanceValue);
     mWaitForProtocol = false;
 }
Exemple #17
0
        protected virtual IProtocol ExecuteUKitCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            return(result);
        }
Exemple #18
0
        public IProtocol ExecuteMainControlCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                if (command is MiscCommands.SetIDCommand)
                {
                    var cmd = (MiscCommands.SetIDCommand)command;
                    result.code = ChangeID(cmd.targetDevice, cmd.oldID, cmd.newID);
                    break;
                }

                if (command is MiscCommands.QueryDeviceCommand)
                {
                    var cmd = (MiscCommands.QueryDeviceCommand)command;
                    result.code = QueryDevice(result);
                    break;
                }

                if (command is MiscCommands.QueryDeviceUpgradableCommand)
                {
                    var cmd = (MiscCommands.QueryDeviceUpgradableCommand)command;
                    break;
                }

                if (command is MiscCommands.StopAllDevicesCommand)
                {
                    var cmd = (MiscCommands.StopAllDevicesCommand)command;
                    Stop();
                    result.code = 0;
                    break;
                }

                if (command is MiscCommands.GetVersionCommand)
                {
                    var cmd = (MiscCommands.GetVersionCommand)command;
                    result.SetDatas("v1.2.4", "v2");
                    result.code = 0;
                    break;
                }

                if (command is MiscCommands.StartUpgradeCommand)
                {
                    var cmd = (MiscCommands.StartUpgradeCommand)command;

                    break;
                }

                if (command is MiscCommands.TickUpgradeCommand)
                {
                    var cmd = (MiscCommands.TickUpgradeCommand)command;

                    break;
                }

                if (command is MiscCommands.EndUpgradeCommand)
                {
                    var cmd = (MiscCommands.EndUpgradeCommand)command;

                    break;
                }
            }while (false);
            return(result);
        }
Exemple #19
0
 /// <summary>
 /// 温湿度传感器,湿度回传
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookMidity(ExploreProtocol protocol)
 {
     protocol.GetParamf(0, out humidityValue);
     mWaitForProtocol = false;
 }
Exemple #20
0
 /// <summary>
 /// 板载按钮
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookButton(ExploreProtocol protocol)
 {
 }
Exemple #21
0
 /// <summary>
 /// 温湿度传感器,温度回传
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookRature(ExploreProtocol protocol)
 {
     protocol.GetParamf(0, out temperatureValue);
     mWaitForProtocol = false;
 }
Exemple #22
0
 /// <summary>
 /// RGB 传感器- 判定指定色
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookRGBRecogSensor(ExploreProtocol protocol)
 {
 }
Exemple #23
0
 /// <summary>
 /// 巡线传感器 - 判断深浅色
 /// </summary>
 /// <param name="protocol"></param>
 private void OnHookPatrolSensor(ExploreProtocol protocol)
 {
 }
Exemple #24
0
        private IProtocol ExecuteServoCommands(ICommand command)
        {
            ExploreProtocol result = ExploreProtocol.CreateResponse(ProtocolCode.Failure, command);

            do
            {
                // 舵机角模式
                if (command is ServoCommands.RotateCommand)
                {
                    var cmd = (ServoCommands.RotateCommand)command;

                    jobStatus = WorkStatus.Rotation;
                    var job = (RotationJob)currentJob;
                    job.accumulationTime = 0.0f;
                    job.duration         = cmd.duration;
                    var rawAngle = localEulerAnglesY;
                    rawAngle       = Misc.Convert(convertMode, rawAngle);
                    job.startAngle = rawAngle;
                    job.endAngle   = cmd.angel;
                    result.code    = 0;
                    break;
                }

                // 舵机轮模式
                if (command is ServoCommands.RotationAlwaysCommand)
                {
                    var cmd = (ServoCommands.RotationAlwaysCommand)command;
                    jobStatus = WorkStatus.RotationAlways;
                    var job = (RotationAlwaysJob)currentJob;
                    if (cmd.forward)
                    {
                        job.velocity = cmd.velocity;
                    }
                    else
                    {
                        job.velocity = cmd.velocity * -1.0f;
                    }
                    result.code = 0;
                    break;
                }

                // 停止舵机
                if (command is ServoCommands.StopCommand)
                {
                    var cmd = (ServoCommands.StopCommand)command;
                    Stop();
                    result.code = 0;
                    break;
                }

                // 读取舵机角度
                if (command is ServoCommands.ReadRotationCommand)
                {
                    var cmd = (ServoCommands.ReadRotationCommand)command;
                    if (!cmd.enable)
                    {
                        jobStatus = WorkStatus.Idle;
                    }

                    Vector2 angleRange = new Vector2(-118.0f, 118.0f);
                    float   angle      = localEulerAnglesY;
                    angle = Misc.Convert(convertMode, angle);
                    ProtocolCode code = ProtocolCode.Success;
                    if (angle < angleRange.x || angle > angleRange.y)
                    {
                        code = ProtocolCode.Failure;
                    }
                    result.SetDatas((int)angle);
                    result.code = code == ProtocolCode.Success ? 0 : 1;
                    break;
                }
            } while (false);

            return(result);
        }
Exemple #25
0
 /// <summary>
 /// 光传感器
 /// </summary>
 /// <param name="protocol"></param>
 private void OnRecvLuminanceSensor(ExploreProtocol protocol)
 {
 }