-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathconfig.ts
More file actions
178 lines (158 loc) · 6.42 KB
/
config.ts
File metadata and controls
178 lines (158 loc) · 6.42 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
declare var plugins: any; // cordova plugins
module Funbit.Ets.Telemetry {
// if you change port number here make sure that
// you change it inside Ets2Telemetry.exe.config as well
var serverPort: number = 25555;
export interface IConfiguration {
skins: ISkinConfiguration[];
serverIp: string;
}
export interface ISkinConfiguration {
name: string;
title: string;
author: string;
width: number;
height: number;
}
interface IPrefsPlugin {
fetch(ok: Function, fail: Function, key: string);
store(ok: Function, fail: Function, key: string, value: string);
}
interface IInsomniaPlugin {
keepAwake();
}
export class Strings {
// app.ts
public static dashboardHtmlLoadFailed: string = 'Failed to load dashboard.html for skin: ';
// menu.ts
public static connecting = 'Connecting...';
public static connected = 'Connected';
public static disconnected = 'Disconnected';
public static enterServerIpMessage = 'Please enter server IP address (aa.bb.cc.dd)';
public static incorrectServerIpFormat = 'Entered server IP or hostname has incorrect format.';
// dashboard.ts
public static dayOfTheWeek = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
];
public static noTimeLeft = 'Overdue';
public static disconnectedFromServer = 'Disconnected from server';
public static couldNotConnectToServer = 'Could not connect to the server';
public static connectedAndWaitingForDrive = 'Connected, waiting for the driver...';
public static connectingToServer = 'Connecting to the server...';
}
export class Configuration implements IConfiguration {
public skins: ISkinConfiguration[];
public serverIp: string;
public initialized: JQueryDeferred<Configuration>;
private anticacheSeed: number = Date.now();
private prefs: IPrefsPlugin;
private insomnia: IInsomniaPlugin;
private static instance: Configuration;
public static getInstance(): Configuration {
if (!Configuration.instance) {
Configuration.instance = new Configuration();
}
return Configuration.instance;
}
public static getParameter(name: string) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
var results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
public getSkinConfiguration(): ISkinConfiguration {
var skinName = Configuration.getParameter('skin');
if (skinName) {
for (var i = 0; i < this.skins.length; i++) {
if (this.skins[i].name == skinName)
return this.skins[i];
}
}
return null;
}
public reload(newServerIp: string, done: Function = null, fail: Function = null) {
if (!newServerIp)
return;
this.serverIp = newServerIp;
this.prefs.store(() => { }, () => { }, 'serverIp', this.serverIp);
$.ajax({
url: this.getUrlInternal('/config.json?seed=' + this.anticacheSeed++),
dataType: 'json',
timeout: 3000
}).done(json => {
this.skins = json.skins;
if (done) done();
}).fail(() => {
this.skins = [];
if (fail) fail();
});
}
public static isCordovaAvailable(): boolean {
return document.URL.indexOf('http://') === -1 && document.URL.indexOf('https://') === -1;
}
private getUrlInternal(path: string): string {
return "http://" + this.serverIp + ":" + serverPort + path;
}
public static getUrl(path: string): string {
return Configuration.getInstance().getUrlInternal(path);
}
public getSkinResourceUrl(skinConfig: ISkinConfiguration, name: string): string {
return Configuration.getUrl('/skins/' +
skinConfig.name + '/' + name + '?seed=' + this.anticacheSeed++);
}
private initialize() {
if (!this.serverIp)
this.initialized.resolve(this);
this.reload(this.serverIp,
() => this.initialized.resolve(this),
() => this.initialized.resolve(this));
}
constructor() {
this.initialized = $.Deferred<Configuration>();
this.skins = [];
// initialize structures
if (!Configuration.isCordovaAvailable()) {
this.insomnia = {
keepAwake: () => { }
};
this.prefs = {
fetch: () => { },
store: () => { }
};
} else {
this.insomnia = <IInsomniaPlugin>plugins.insomnia;
this.prefs = <IPrefsPlugin>plugins.appPreferences;
// turn off sleep mode
this.insomnia.keepAwake();
}
// if server IP was passed in the query string use it then
var ip = Configuration.getParameter('ip');
if (ip) {
this.serverIp = ip;
this.initialize();
return;
}
this.serverIp = '';
if (!Configuration.isCordovaAvailable()) {
// if cordova is not available then
// we are in desktop environment
// so we use current host name as our IP
this.serverIp = window.location.hostname;
this.initialize();
} else {
this.prefs.fetch(savedIp => {
this.serverIp = savedIp;
this.initialize();
}, () => {
this.initialize();
}, 'serverIp');
}
}
}
}