· 6 years ago · Nov 25, 2019, 07:36 PM
1public static List<string> GenerateMap(string data)
2 {
3 try
4 {
5 // Routes class instance
6 Routes ruti = Routes.RoutesInstance;
7 // A nested list that comes from the proverka_baza method that is filles with route order an route vehicles
8 // The number of nested lists is the number of vehicles, the nested list is the order of the cities of that vehicle
9 var listo = ruti.myList_proverka_baza;
10 // If you are calling this method from the RFID device, and you only want to plot a route that already exists in database
11 // Fills in nested list with route order from db
12 if (data.Contains(";"))
13 {
14 FinansovoEntities _entities = new FinansovoEntities();
15 // Find route demands in db
16 var routesEq = _entities.Route.Where(r => (r.Demands.Equals(data) == true)).ToList();
17 // If it doesnt exist, return warning
18 bool isEmpty = !routesEq.Any();
19 if (isEmpty)
20 {
21 var list = new List<string>();
22 list.Add("You are trying to draw an non-existent map in db");
23 return list;
24 }
25 // Otherwise find route order saved in db to fill in nested list listo
26 else
27 {
28 // All data for route order is in Route and RouteDetails
29 var rDetails = from r in routesEq
30 join rd in _entities.RouteDetails on r.IdRoute equals rd.IdRoute
31 select new { rd, r };
32
33 var rIdS = rDetails.Select(r => r.rd).GroupBy(rd => rd.IdRoute);
34 List<int?> routeL = new List<int?>();
35 List<List<int>> routesL = new List<List<int>>();
36 foreach (var id in rIdS)
37 {
38 var key = id.Key;
39 routeL = _entities.RouteDetails.Where(rd => rd.IdRoute == key).Select(rd => rd.IdOe).ToList();
40 var temp_int = routeL.Where(x => x != null).Cast<int>().ToList();
41 ruti.demandTodb_proverka_baza = rDetails.Where(o => o.rd.IdRoute == key).Select(a => a.r.Demands).ToString();
42 routesL.Add(temp_int);
43 List<int> list = routeL.Cast<int>().ToList();
44 ruti.myList_proverka_baza.Add(list);
45 }
46 listo = routesL;
47 ruti.demandTodb_proverka_baza = data;
48 // Change data so that we can continue for plotting the route
49 data = "Ве молиме почекајте до 30 секунди, рутата се калкулира";
50 }
51 }
52 // Declared variable for API response
53 object response_API;
54 // Declare all needed variables
55 var html = "";
56 // A string that represents the demand of the route
57 string db = ruti.demandTodb_proverka_baza;
58 bool shto = true;
59 // If you want to call python through cmd
60 if (shto)
61 {
62 // Take the first line of python_exe_path.txt file, hence there is the python.exe path in the system
63 var lines = File.ReadLines(Path.Combine(HttpContext.Current.Server.MapPath("~/Files/python_exe_path.txt")));
64 string python_path = lines.First();
65 // Finds the baza_true.py path in the app
66 string baza_true = Path.Combine(HttpContext.Current.Server.MapPath("~/Files/baza_true.py"));
67 // If you haven't selected any city to vist
68 if (data == "Ве молиме селектирајте продажен салон")
69 {
70 html = "";
71 }
72 // If route exist in db
73 else if (data == "Ве молиме почекајте до 30 секунди, рутата се калкулира")
74 {
75 // In order to plot the route we need to parse the nested list into a format the python script accepts
76 string str = "";
77 // A loop that converts the nested list [[1, 2], [4, 5, 6], [9, 8]] into format 1;2|4;5;6|9;8
78 foreach (var d in listo)
79 {
80 List<int> asList = d.ToList();
81 var result = String.Join(";", asList.ToArray());
82 str = String.Concat(str, result);
83 str = String.Concat(str, "|");
84 }
85 // Deletes the last "|" char
86 str = str.Substring(0, str.Length - 1);
87 // The exact order of arguments that go into cmd to call the python script:
88 // script_name.py "demand_written_in_db" "exact order of cities to be visited"
89 // Try to call cmd process
90 try
91 {
92 //html = run_cmd(python_path, baza_true, db, str);
93 //html = run_batch(db, str, "");
94 response_API = run_APIasync(db, str, "");
95 html = response_API.html;
96 }
97 // If anything fails
98 catch
99 {
100 var list = new List<string>();
101 list.Add("Python went wrong!!! \n" + html);
102 return list;
103 }
104 }
105 // If the route does not exist in db
106 else if (data == "Ве молиме почекајте до 1 минута, рутата се калкулира")
107 {
108 var result = "";
109 try {
110 // Finds the baza_false.py path in the app
111 string baza_false = Path.Combine(HttpContext.Current.Server.MapPath("~/Files/baza_false.py"));
112 // The exact order of arguments that go into cmd to call the python script:
113 // script_name.py "demand_written_in_db"
114 //result = run_cmd(python_path, baza_false, db, "");
115 //result = run_batch(db, "", "");
116 response_API = run_APIasync(db, "", "");
117 // We divide the route order from the html string with the & char
118 // var ruti_tmp = result.Split('&').ToList();
119 // // The route order
120 // var ruti_vo_baza = ruti_tmp.First();
121 // ruti_tmp.RemoveAt(0);
122 // // The rest of the html string that is going to be displayed
123 // foreach (var a in ruti_tmp)
124 // {
125 // html = html + a;
126 // }
127 var ruti_vo_baza = response_API.solution_as_string;
128 html = response_API.html;
129 // We split the route order by vehicles i.e. 1,2|4,5,6|9,8 ==> ["1,2", "4,5,6", "9,8"]
130 var stL = ruti_vo_baza.Split('|').ToList();
131 ruti_vo_baza = ruti_vo_baza.Replace("|", ";"); // 1,2|4,5,6|9,8 ==> 1,2;4,5,6;9,8
132 var inputDemand = ruti_vo_baza.Replace(",", ";"); // 1,2;4,5,6;9,8 ==> 1;2;4;5;6;9;8
133 var inputDemandL = inputDemand.Split(';').ToList(); // 1;2;4;5;6;9;8 ==> ["1", "2", "4", "5", "6", "9", "8"]
134 var podredena_lista = inputDemandL.Select(int.Parse).ToList(); // ["1", "2", "4", "5", "6", "9", "8"] ==> [1, 2, 4, 5, 6, 9, 8]
135 podredena_lista.Sort(); // [1, 2, 4, 5, 6, 9, 8] ==> [1, 2, 4, 5, 6, 9, 8]
136 podredena_lista.Insert(0, podredena_lista[podredena_lista.Count - 1]);
137 podredena_lista.RemoveAt(podredena_lista.Count - 1);
138 // If in case we decide we want to save the route in db, this object goes to the btnZapisi_Click method
139 ruti.demandTodb = String.Join(";", podredena_lista.Select(item => item.ToString()).ToArray()); // [1, 2, 4, 5, 6, 9, 8] ==> 1;2;4;5;6;9;8
140 foreach (var money in stL)
141 {
142 // Takes one of the strings in stL, ex "1,2" from ["1,2", "4,5,6", "9,8"] and creates a list of strings ["1", "2"]
143 var time = money.Split(',').ToList();
144 List<int> integers = time.ConvertAll(int.Parse); // ["1", "2"] ==> [1, 2]
145 // Checks if the first integer is warehouse ID
146 if (Math.Floor(Math.Log10(integers[0]) + 1) == 5)
147 {
148 integers.RemoveAt(0);
149 }
150 // Checks if the last integer is warehouse ID
151 if (Math.Floor(Math.Log10(integers[integers.Count - 1]) + 1) == 5)
152 {
153 integers.RemoveAt(integers.Count - 1);
154 }
155 // Adds that list into the nested list of routes
156 ruti.myList.Add(integers);
157 }
158 }
159 catch
160 {
161 html = "";
162 var list = new List<string>();
163 list.Add("Python went wrong!!! \n" + result);
164 return list;
165 }
166 // This script tag is added to the html string for frontend purposes
167 html += "<script> document.onkeydown = function(e) { if (e.keyCode == '27') { window.parent.postMessage(\"1\", '*'); } }; </script>";
168 }
169 ruti.html = html;
170 }
171 // Create a list to send to frontend (frontend issues)
172 // This is a list that shows the route order in frontend popup (in progress)
173 //string txt = Path.Combine(HttpContext.Current.Server.MapPath("~/Files/routes_order.txt"));
174 //var text = File.ReadAllText(txt, Encoding.UTF8);
175 List<string> meh = new List<string>();
176 ruti.html = html;
177 meh.Add(html);
178 // meh.Add(text);
179 return meh;
180 }
181 catch (Exception ex)
182 {
183 var list = new List<string>();
184 list.Add("Something Csharpy went wrong!!! \n" + ex.ToString());
185 return list;
186 }
187 }