-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tools.cs
111 lines (95 loc) · 3.15 KB
/
Tools.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using Exceptionless;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using TeslaLogger;
namespace OVMS
{
class Tools
{
public static readonly System.Globalization.CultureInfo ciEnUS = new System.Globalization.CultureInfo("en-US");
public static readonly System.Globalization.CultureInfo ciDeDE = new System.Globalization.CultureInfo("de-DE");
public static void SetThreadEnUS()
{
Thread.CurrentThread.CurrentCulture = ciEnUS;
}
public static bool IsPropertyExist(dynamic settings, string name)
{
if (settings == null)
return false;
if (!(settings is IDictionary<string, object>))
return false;
return settings is IDictionary<string, object> dictionary && dictionary.ContainsKey(name);
}
public static bool IsMono()
{
return GetMonoRuntimeVersion() != "NULL";
}
public static string GetMonoRuntimeVersion()
{
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName != null)
{
return displayName.Invoke(null, null).ToString();
}
}
return "NULL";
}
public static bool IsDocker()
{
try
{
string filename = "/tmp/teslalogger-DOCKER";
if (File.Exists(filename))
{
return true;
}
}
catch (Exception ex)
{
ex.ToExceptionless().FirstCarUserID().Submit();
Logfile.ExceptionWriter(ex, "IsDocker");
}
return false;
}
}
public static class EventBuilderExtension
{
static String lastFirstCar;
public static EventBuilder FirstCarUserID(this EventBuilder v)
{
try
{
if (lastFirstCar != null)
return v.SetUserIdentity(lastFirstCar);
using (MySqlConnection con = new MySqlConnection(DBHelper.DBConnectionstring))
{
con.Open();
using (MySqlCommand cmd = new MySqlCommand("SELECT tesla_token FROM cars where length(tesla_token) >= 8 limit 1", con))
{
object o = cmd.ExecuteScalar()?.ToString();
if (o is String && o.ToString().Length >= 8)
{
lastFirstCar = o.ToString();
return v.SetUserIdentity(o.ToString());
}
}
}
}
catch (Exception ex)
{
Logfile.Log(ex.ToString());
}
return v;
}
}
}