AGV&MAP

mail@pastecode.io avatar
unknown
plain_text
3 years ago
10 kB
6
Indexable
Never
using Newtonsoft.Json;
using System;
using System.IO;
using System.Net;
using Newtonsoft.Json.Converters;
using System.Globalization;
using System.Collections.Generic;

namespace AGV
{
    public partial class My_AGV
    {
        [JsonProperty("StatusCode")]
        public long StatusCode { get; set; }

        [JsonProperty("Config")]
        public Config Config { get; set; }
    }

    public partial class Config
    {
        [JsonProperty("Pos")]
        public Pos Pos { get; set; }

        [JsonProperty("RunPara")]
        public RunPara RunPara { get; set; }

        [JsonProperty("Attitude")]
        public Attitude Attitude { get; set; }

        [JsonProperty("Shelves")]
        public Attitude Shelves { get; set; }

        [JsonProperty("Status")]
        public Status Status { get; set; }

        [JsonProperty("Battery")]
        [JsonConverter(typeof(ParseStringConverter))]
        public long Battery { get; set; }

        [JsonProperty("AgvLogIndex")]
        public AgvLogIndex AgvLogIndex { get; set; }

        [JsonProperty("AgvScript")]
        public List<AgvScript> AgvScript { get; set; }
    }

    public partial class AgvLogIndex
    {
        [JsonProperty("IsProgress")]
        public bool IsProgress { get; set; }

        [JsonProperty("ScriptIdx")]
        public long ScriptIdx { get; set; }

        [JsonProperty("RunIdx")]
        public long RunIdx { get; set; }

        [JsonProperty("ErrorIdx")]
        public long ErrorIdx { get; set; }
    }

    public partial class AgvScript
    {
        [JsonProperty("Type")]
        public long Type { get; set; }

        [JsonProperty("Parameter")]
        public long Parameter { get; set; }

        [JsonProperty("Code")]
        public long Code { get; set; }
    }

    public partial class Attitude
    {
        [JsonProperty("X")]
        public double X { get; set; }

        [JsonProperty("Y")]
        public double Y { get; set; }

        [JsonProperty("Yaw")]
        public double Yaw { get; set; }

        [JsonProperty("Code")]
        public string Code { get; set; }
    }

    public partial class Pos
    {
        [JsonProperty("X")]
        public double X { get; set; }

        [JsonProperty("Y")]
        public double Y { get; set; }

        [JsonProperty("A")]
        public double A { get; set; }
    }

    public partial class RunPara
    {
        [JsonProperty("Speed")]
        public long Speed { get; set; }

        [JsonProperty("Acceleration")]
        public long Acceleration { get; set; }

        [JsonProperty("Deceleration")]
        public long Deceleration { get; set; }

        [JsonProperty("Jerk")]
        public long Jerk { get; set; }
    }

    public partial class Status
    {
        [JsonProperty("State")]
        public string State { get; set; }

        [JsonProperty("IsJogSearch")]
        public bool IsJogSearch { get; set; }

        [JsonProperty("IsLiftUp")]
        public bool IsLiftUp { get; set; }

        [JsonProperty("IsMoving")]
        public bool IsMoving { get; set; }

        [JsonProperty("IsAxisReady")]
        public bool IsAxisReady { get; set; }

        [JsonProperty("IsTurnAxisReady")]
        public bool IsTurnAxisReady { get; set; }

        [JsonProperty("IsLiftAxisReady")]
        public bool IsLiftAxisReady { get; set; }

        [JsonProperty("IsAxisMoving")]
        public bool IsAxisMoving { get; set; }

        [JsonProperty("IsTurnAxisMoving")]
        public bool IsTurnAxisMoving { get; set; }

        [JsonProperty("IsScriptCmd")]
        public bool IsScriptCmd { get; set; }

        [JsonProperty("IsLiftAxisMoving")]
        public bool IsLiftAxisMoving { get; set; }

        [JsonProperty("IsAxisCoupling")]
        public bool IsAxisCoupling { get; set; }

        [JsonProperty("IsTurnAxisCoupling")]
        public bool IsTurnAxisCoupling { get; set; }

        [JsonProperty("IsAxis3Coupling")]
        public bool IsAxis3Coupling { get; set; }

        [JsonProperty("IsServoON")]
        public bool IsServoOn { get; set; }

        [JsonProperty("IsReady")]
        public bool IsReady { get; set; }

        [JsonProperty("IsError")]
        public bool IsError { get; set; }

        [JsonProperty("IsLaserStop")]
        public bool IsLaserStop { get; set; }

        [JsonProperty("IsChargeing")]
        public bool IsChargeing { get; set; }

        [JsonProperty("IsLaserEnable")]
        public bool IsLaserEnable { get; set; }

        [JsonProperty("IsEmergencyStop")]
        public bool IsEmergencyStop { get; set; }

        [JsonProperty("IsVirAxisReady")]
        public bool IsVirAxisReady { get; set; }

        [JsonProperty("IsLeftAxisReady")]
        public bool IsLeftAxisReady { get; set; }

        [JsonProperty("IsRightAxisReady")]
        public bool IsRightAxisReady { get; set; }

        [JsonProperty("IsLeftMotorGearIN")]
        public bool IsLeftMotorGearIn { get; set; }

        [JsonProperty("IsRightMotorGearIN")]
        public bool IsRightMotorGearIn { get; set; }

        [JsonProperty("IsScriptStart")]
        public bool IsScriptStart { get; set; }

        [JsonProperty("IsScriptFinish")]
        public bool IsScriptFinish { get; set; }

        [JsonProperty("IsScriptPause")]
        public bool IsScriptPause { get; set; }

        [JsonProperty("IsScriptStop")]
        public bool IsScriptStop { get; set; }

        [JsonProperty("IsUpdatePos")]
        public bool IsUpdatePos { get; set; }
    }

    public partial class My_AGV
    {
        public static My_AGV FromJson(string json) => JsonConvert.DeserializeObject<My_AGV>(json, AGV.Converter.Settings);
    }

    public static class Serialize
    {
        public static string ToJson(this My_AGV self) => JsonConvert.SerializeObject(self, AGV.Converter.Settings);
    }

    internal static class Converter
    {
        public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
        {
            MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
            DateParseHandling = DateParseHandling.None,
            Converters =
            {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
        };
    }

    internal class ParseStringConverter : JsonConverter
    {
        public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);

        public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null) return null;
            var value = serializer.Deserialize<string>(reader);
            long l;
            if (Int64.TryParse(value, out l))
            {
                return l;
            }
            throw new Exception("Cannot unmarshal type long");
        }

        public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
        {
            if (untypedValue == null)
            {
                serializer.Serialize(writer, null);
                return;
            }
            var value = (long)untypedValue;
            serializer.Serialize(writer, value.ToString());
            return;
        }

        public static readonly ParseStringConverter Singleton = new ParseStringConverter();
    }


    class Program
    {
        
        static void Main(string[] args)
        {
            var result="";
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://163.17.137.234:50200/AGV/SendAgvCmd");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"Name\":\"ITRI_3-1\"," +
                              "\"Cmd\":\"500\"}";

                streamWriter.Write(json);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                result = streamReader.ReadToEnd();
                Console.WriteLine(result);
                Console.WriteLine("\n \n \n \n ");

            }

            var Itri_3_1 = My_AGV.FromJson(result);
            Console.WriteLine("StatusCode = "+Itri_3_1.StatusCode+"\n");
            Console.WriteLine("Att_X = " + Itri_3_1.Config.Attitude.X + "\n");
            Console.WriteLine("Att_Y = " + Itri_3_1.Config.Attitude.Y + "\n");
            Console.WriteLine("Att_Yaw = " + Itri_3_1.Config.Attitude.Yaw + "\n");
            Console.WriteLine("Att_Code = " + Itri_3_1.Config.Attitude.Code + "\n");


            char car_x = Itri_3_1.Config.Attitude.Code[1];
            char car_y = Itri_3_1.Config.Attitude.Code[4];
            //Console.WriteLine(car_x+" "+ car_y);
            //Console.WriteLine((int)Char.GetNumericValue(car_x));

            string[,] map1 = new string[7, 7] {{"X","*","*","*","架","腦","D"},
                                               {"X","架","*","*","*","*","*"},
                                               {"X","架","*","*","*","*","*"},
                                               {"X","架","*","*","*","*","*"},
                                               {"X","*","*","*","*","*","*"},
                                               {"X","*","*","*","*","*","牆"},
                                               {"X","*","*","*","*","充","牆"},};
            map1[(int)Char.GetNumericValue(car_x), (int)Char.GetNumericValue(car_y)] = "車";

            for (int i  = 0; i<7; i++)
            {
                

                for (int j = 0; j < 7; j++)
                {
                    Console.Write(map1[i,j] +"\t");

                    if (j == 6)
                        Console.Write("\n");
                }

                

            }


        }
    }
}