Subversion Repositories HomeAutomation

Rev

Rev 1256 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1171 arune 1
 
2
loadScript("Logic/MenuItem.js");
3
 
1175 arune 4
function Display(type, name, id)
1171 arune 5
{
1175 arune 6
    /* We must always call the parent constructor, initialization
7
       of variables could be done here, but initialize is a better place */
8
    this.Service(type, name, id);
1171 arune 9
}
10
 
1175 arune 11
/* We inherit from Service with this call, it must always be next
12
   after the constructor */
1182 arune 13
extend(Display, Service);
1175 arune 14
 
1171 arune 15
/* Declaration of instance variables, for static variables remove prototype */
16
 
1190 arune 17
/* format: dat objects, just use the time */
1189 arune 18
Display.prototype.bookFromTime = null;
19
Display.prototype.bookToTime = null;
1171 arune 20
 
21
/* the shortname for this exchangeobject */
1189 arune 22
Display.prototype.shortName = null;
1171 arune 23
 
1175 arune 24
/* the name for this display (shown on first screen) */
1189 arune 25
Display.prototype.niceName = null;
1175 arune 26
 
1172 arune 27
/* The display should regulary update and store the calendar */
1175 arune 28
Display.prototype.exchangeData = null;
1171 arune 29
 
1175 arune 30
Display.prototype.myLCDService = null;
31
Display.prototype.myRotaryService = null;
32
Display.prototype.mySoftPwmService = null;
33
Display.prototype.myInterval = null;
1239 arune 34
Display.prototype.myIntervalAlways = null;
1189 arune 35
 
1190 arune 36
/*  */
1175 arune 37
Display.prototype.exchangeCalendar = null;
1190 arune 38
/*  */
1189 arune 39
Display.prototype.exchangeCalendarFirstMenuItem = null;
40
Display.prototype.exchangeCalendarLastMenuItem = null;
1190 arune 41
Display.prototype.statusMenuItem = null;
42
Display.prototype.bookMenuItem = null;
1216 arune 43
Display.prototype.bookResultMenuItem = null;
1256 arune 44
Display.prototype.screenSaverMenuItem = null;
1175 arune 45
 
1216 arune 46
 
1190 arune 47
/* The currently displayed menuitem */
48
Display.prototype.currentMenuItem = null;
1175 arune 49
 
1203 arune 50
/* counter for calling exchange update */
51
Display.prototype.exchangeUpdateCnt = null;
1190 arune 52
 
1203 arune 53
/* counter for going to screensaver */
54
Display.prototype.screenSaverCnt = null;
55
 
56
/* counter for going to main screen */
57
Display.prototype.mainScreenCnt = null;
58
 
1248 arune 59
/* LED status */
60
Display.prototype.LEDstatus = null;
61
 
1204 arune 62
const exchangeUpdateTimeout = 50;
1203 arune 63
const mainScreenTimeout = 40;
64
const screenSaverTimeout = 120;
65
 
1212 arune 66
const bookingIncrease = 15;
67
 
1175 arune 68
/* This function must always be declared, this is where all the startup code
69
   should be placed. Gets called with arguments like what ids to use etc. */
70
Display.prototype.initialize = function(initialArguments)
71
{
72
    /* We must always call the parent initialize */
73
    this.Service.prototype.initialize.call(this, initialArguments);
74
 
75
    /* This is used for function declarations like the callbacks below */
76
    var self = this;
77
 
78
    if (!this.myInitialArguments["HD44789"])
79
    {
80
        log(this.myName + ":" + this.myId + "> Failed to initialize, HD44789-config missing from config.\n");
81
        return;
82
    }
83
 
84
    if (!this.myInitialArguments["Rotary"])
85
    {
86
        log(this.myName + ":" + this.myId + "> Failed to initialize, Rotary-config missing from config.\n");
87
        return;
88
    }
89
 
90
    if (!this.myInitialArguments["softPWM"])
91
    {
92
        log(this.myName + ":" + this.myId + "> Failed to initialize, softPWM-config missing from config.\n");
93
        return;
94
    }
95
 
96
    if (!this.myInitialArguments["Name"])
97
    {
98
        log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
99
        return;
100
    }
101
    this.niceName = this.myInitialArguments["Name"];
102
 
103
    if (!this.myInitialArguments["ShortName"])
104
    {
105
        log(this.myName + ":" + this.myId + "> Failed to initialize, Name-config missing from config.\n");
106
        return;
107
    }
108
    this.shortName = this.myInitialArguments["ShortName"];
1238 arune 109
 
110
    /* Start interval timer for sending timestamp to network. Arguments are the callback function and time in milliseconds
1250 arune 111
    used to make sure an eth-node gets its init packet (600s) */
112
    this.myIntervalAlways = new Interval(function() { self.sendTimeStamp() }, 600000);
1239 arune 113
    this.myIntervalAlways.start();
1175 arune 114
 
1203 arune 115
    this.screenSaverCnt = 0;
116
    this.mainScreenCnt = 0;
117
    this.exchangeUpdateCnt = exchangeUpdateTimeout-10;
118
 
1249 arune 119
    this.LEDstatus = "none";
1248 arune 120
 
1175 arune 121
    /* Get the LCD service that we want from the ServiceManager, it takes type, service name, service id */
122
    this.myLCDService = ServiceManager.getService("Can", "HD44789", this.myInitialArguments["HD44789"]["Id"]);
123
    /* Add a callback for when the service goes online */
124
    this.myLCDService.registerEventCallback("online", function(args) { self.lcdOnline(); });
125
    /* If the service is already online we should call the handler here */
126
    this.lcdOnline();
127
    /* Add a callback for when the service goes offline */
128
    this.myLCDService.registerEventCallback("offline", function(args) { self.lcdOffline(); });
129
 
130
    /* Get the Rotary service that we want from the ServiceManager, it takes type, service name, service id */
131
    this.myRotaryService = ServiceManager.getService("Can", "Rotary", this.myInitialArguments["Rotary"]["Id"]);
132
    /* Add a callback for when the encoder reports a new position */
133
    this.myRotaryService.registerEventCallback("newPosition", function(args) { self.rotaryPosUpdate(args); });
134
    /* Add a callback for when the encoder reports a new button status */
135
    this.myRotaryService.registerEventCallback("newBtnStatus", function(args) { self.rotaryBtnUpdate(args); });
136
    /* Add a callback for when the service goes online */
137
    this.myRotaryService.registerEventCallback("online", function(args) { self.rotaryOnline(); });
138
    /* If the service is already online we should call the handler here */
139
    this.rotaryOnline();
140
 
141
    /* Get the softPWM service that we want from the ServiceManager, it takes type, service name, service id */
142
    this.mySoftPwmService = ServiceManager.getService("Can", "softPWM", this.myInitialArguments["softPWM"]["Id"]);
143
    /* Add a callback for when the service goes online */
144
    this.mySoftPwmService.registerEventCallback("online", function(args) { self.softPwmOnline(); });
145
    /* If the service is already online we should call the handler here */
146
    this.softPwmOnline();
147
 
148
    this.exchangeCalendar = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarLookupCallback(shortname, data); });
1216 arune 149
    this.exchangeCalendarBook = new ExchangeCalendar(function(shortname, data) { self.exchangeCalendarBookCallback(shortname, data); });
1185 arune 150
 
151
    /* create the first menu item */
1189 arune 152
    this.statusMenuItem = new MenuItem(this);
1190 arune 153
    this.statusMenuItem.displayData[0] = this.lcdCenterText(this.niceName);
1233 arune 154
    this.statusMenuItem.doUpdate = function(args) { self.updateStatusMenuItem(); };
1189 arune 155
    this.currentMenuItem = this.statusMenuItem;
1256 arune 156
 
157
    /* create the screensaver menu item */
158
    this.screenSaverMenuItem = new MenuItem(this);
159
    this.screenSaverMenuItem.displayData[0] = this.lcdCenterText("");
160
    this.screenSaverMenuItem.displayData[1] = this.lcdCenterText("");
161
    this.screenSaverMenuItem.doRight = function(args) { self.changeToNext(); };
162
    this.screenSaverMenuItem.doLeft = function(args) { self.changeToPrev(); };
163
    this.screenSaverMenuItem.setNextItem(this.statusMenuItem);
164
    this.screenSaverMenuItem.setPrevItem(this.statusMenuItem);
165
 
1189 arune 166
    /* create the menuitem where you can choose to enter the booking sub-menu */
167
    this.bookMenuItem = new MenuItem(this);
1195 arune 168
    this.bookMenuItem.displayData[0] = this.lcdCenterText("Book room");
1216 arune 169
    this.bookMenuItem.displayData[1] = this.lcdCenterText("(Push button)");
1233 arune 170
    this.bookMenuItem.doUpdate = function(args) { self.updateBookMenuItem(); };
1189 arune 171
 
1185 arune 172
    /* connect the items as a linked list */
1189 arune 173
    this.statusMenuItem.setNextItem(this.bookMenuItem);
174
    this.statusMenuItem.setPrevItem(this.bookMenuItem);
1185 arune 175
    /* set the function that shall be executed when knob is turned */
1233 arune 176
    this.statusMenuItem.doRight = function(args) { self.changeToNext(); };
177
    this.statusMenuItem.doLeft = function(args) { self.changeToPrev(); };
1185 arune 178
 
179
    /* connect the items as a linked list */
1189 arune 180
    this.bookMenuItem.setNextItem(this.statusMenuItem);
181
    this.bookMenuItem.setPrevItem(this.statusMenuItem);
1185 arune 182
    /* set the function that shall be executed when knob is turned */
1233 arune 183
    this.bookMenuItem.doRight = function(args) { self.changeToNext(); };
184
    this.bookMenuItem.doLeft = function(args) { self.changeToPrev(); };
1185 arune 185
 
1189 arune 186
    /* create the menuitem where you can choose the to-time */
187
    var menuChooseTo = new MenuItem(this);
1195 arune 188
    menuChooseTo.displayData[1] = this.lcdCenterText(" Ok     Cancel ");
1233 arune 189
    menuChooseTo.doUpdate = function(args) { self.chooseBookTimeTo(); };
1185 arune 190
 
1233 arune 191
    this.bookMenuItem.doPress = function(args) { self.prepareBookingMenu(); };
1189 arune 192
    this.bookMenuItem.setDescItem(menuChooseTo);
193
 
194
    /* create the menuitem where you can choose to book the room with OK */
195
    var menuChooseOk = new MenuItem(this);
1195 arune 196
    menuChooseOk.displayData[1] = this.lcdCenterText("<Ok>    Cancel ");
1233 arune 197
    menuChooseOk.doUpdate = function(args) { self.setBookTime(); };
1189 arune 198
 
199
    /* create the menuitem where you can choose to cancel the booking */
200
    var menuChooseCancel = new MenuItem(this);
1195 arune 201
    menuChooseCancel.displayData[1] = this.lcdCenterText(" Ok    <Cancel>");
1233 arune 202
    menuChooseCancel.doUpdate = function(args) { self.setBookTime(); };
1189 arune 203
 
204
    /* create the menuitem where you can choose the from-time */
205
    var menuChooseFrom = new MenuItem(this);
1195 arune 206
    menuChooseFrom.displayData[1] = this.lcdCenterText(" Ok     Cancel ");
1233 arune 207
    menuChooseFrom.doUpdate = function(args) { self.chooseBookTimeFrom(); };
1189 arune 208
 
1216 arune 209
    /* create the menuitem where you see the result of a booking */
210
    this.bookResultMenuItem = new MenuItem(this);
211
    this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking...");
212
    this.bookResultMenuItem.displayData[1] = this.lcdCenterText("(Push to return)");
213
    this.bookResultMenuItem.setDescItem(this.bookMenuItem);
1233 arune 214
    this.bookResultMenuItem.doPress = function(args) { self.changeToDesc(); };
1216 arune 215
 
1189 arune 216
    /* connect the items as a linked list */
217
    menuChooseTo.setNextItem(menuChooseOk);
218
    menuChooseTo.setPrevItem(menuChooseFrom);
219
    /* set the function that shall be executed when knob is turned */
1233 arune 220
    menuChooseTo.doRight = function(args) { self.changeToNext(); };
221
    menuChooseTo.doLeft = function(args) { self.changeToPrev(); };
222
    menuChooseTo.doPress = function(args) { self.changeToDesc(); };
1189 arune 223
 
224
    /* connect the items as a linked list */
225
    menuChooseOk.setNextItem(menuChooseCancel);
226
    menuChooseOk.setPrevItem(menuChooseTo);
1216 arune 227
    menuChooseOk.setDescItem(this.bookResultMenuItem);
1189 arune 228
    /* set the function that shall be executed when knob is turned */
1233 arune 229
    menuChooseOk.doRight = function(args) { self.changeToNext(); };
230
    menuChooseOk.doLeft = function(args) { self.changeToPrev(); };
231
    menuChooseOk.doPress = function(args) { self.doBooking(); };
1189 arune 232
 
233
    /* connect the items as a linked list */
234
    menuChooseCancel.setNextItem(menuChooseFrom);
235
    menuChooseCancel.setPrevItem(menuChooseOk);
236
    menuChooseCancel.setDescItem(this.bookMenuItem);
237
    /* set the function that shall be executed when knob is turned */
1233 arune 238
    menuChooseCancel.doRight = function(args) { self.changeToNext(); };
239
    menuChooseCancel.doLeft = function(args) { self.changeToPrev(); };
240
    menuChooseCancel.doPress = function(args) { self.changeToDesc(); };
1189 arune 241
 
242
    /* connect the items as a linked list */
243
    menuChooseFrom.setNextItem(menuChooseTo);
244
    menuChooseFrom.setPrevItem(menuChooseCancel);
245
    /* set the function that shall be executed when knob is turned */
1233 arune 246
    menuChooseFrom.doRight = function(args) { self.changeToNext(); };
247
    menuChooseFrom.doLeft = function(args) { self.changeToPrev(); };
248
    menuChooseFrom.doPress = function(args) { self.changeToDesc(); };
1189 arune 249
 
250
    /* create the menuitem where you can modify the to-time */
251
    var menuSetTo = new MenuItem(this);
1233 arune 252
    menuSetTo.doUpdate = function(args) { self.setBookTimeTo(); };
1190 arune 253
    menuSetTo.displayData[0] = this.lcdCenterText("Set end time");
1233 arune 254
    menuSetTo.doRight = function(args) { self.incBookTimeTo(); };
255
    menuSetTo.doLeft = function(args) { self.decBookTimeTo(); };
256
    menuSetTo.doPress = function(args) { self.changeToDesc(); };
1189 arune 257
    menuSetTo.setDescItem(menuChooseTo);
258
    menuChooseTo.setDescItem(menuSetTo);
259
 
260
    /* create the menuitem where you can modify the from-time */
261
    var menuSetFrom = new MenuItem(this);
1233 arune 262
    menuSetFrom.doUpdate = function(args) { self.setBookTimeFrom(); };
1190 arune 263
    menuSetFrom.displayData[0] = this.lcdCenterText("Set start time");
1233 arune 264
    menuSetFrom.doRight = function(args) { self.incBookTimeFrom(); };
265
    menuSetFrom.doLeft = function(args) { self.decBookTimeFrom(); };
266
    menuSetFrom.doPress = function(args) { self.changeToDesc(); };
1189 arune 267
    menuSetFrom.setDescItem(menuChooseFrom);
268
    menuChooseFrom.setDescItem(menuSetFrom);
269
 
1175 arune 270
}
271
 
1216 arune 272
Display.prototype.exchangeCalendarBookCallback = function(shortname, data)
1190 arune 273
{
1216 arune 274
    if (shortname == this.shortName)
275
    {
276
        //FIXME check if data contains error tag and error message, print to log and keep previous data?
277
        if (data.result)
278
        {
279
            if (data.error)
280
            {
281
                this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking failed");
1250 arune 282
                log("Display:"+this.myId+"> Got this error from exchange server: " + data.error + "\n");
1216 arune 283
            }
284
            else
285
            {
286
                this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking succeeded");
1250 arune 287
                log("Display:"+this.myId+"> Successfully booked room\n");
1216 arune 288
 
289
                /* do an exchange lookup */
290
                this.exchangeCalendar.lookup(this.shortName);
291
            }
292
 
293
        }
294
        else if (data.error)
295
        {
296
            this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking failed");
1250 arune 297
            log("Display:"+this.myId+"> Got this error from exchange.php: " + data.error + "\n");
1216 arune 298
        }
299
        this.updateDisplay();
300
    }
301
}
302
 
303
Display.prototype.doBooking = function()
304
{
1233 arune 305
    this.bookResultMenuItem.displayData[0] = this.lcdCenterText("Booking...");
306
    if (this.currentMenuItem.descItem)
1216 arune 307
    {
1233 arune 308
        this.currentMenuItem = this.currentMenuItem.descItem;
1216 arune 309
    }
1233 arune 310
    //this.changeToDesc();
1216 arune 311
 
1233 arune 312
    var bookFrom = this.bookFromTime.getTimeShortFormated();
313
    var bookTo = this.bookToTime.getTimeShortFormated();
314
    this.exchangeCalendarBook.book(this.shortName, bookFrom, bookTo);
1216 arune 315
 
316
    //bookFromTime bookToTime
317
}
318
 
319
Display.prototype.prepareBookingMenu = function()
320
{
1233 arune 321
    this.bookFromTime = new Date();
322
    this.bookToTime = new Date();
1190 arune 323
    //this.bookFromTime.setHours(8);
324
    //this.bookToTime.setHours(8);
1233 arune 325
    if (this.bookFromTime.getMinutes() < 30)
1190 arune 326
    {
1233 arune 327
        this.bookFromTime.setMinutes(0);
328
        this.bookToTime.setMinutes(30);
1190 arune 329
    }
330
    else
331
    {
1233 arune 332
        this.bookFromTime.setMinutes(30);
333
        this.bookToTime.setHours(this.bookToTime.getHours()+1);
334
        this.bookToTime.setMinutes(0);
1190 arune 335
    }
336
 
1233 arune 337
    this.currentMenuItem = this.currentMenuItem.descItem;
1190 arune 338
}
339
 
1189 arune 340
Display.prototype.changeToDesc = function()
341
{
1233 arune 342
    if (this.currentMenuItem.descItem)
1189 arune 343
    {
1233 arune 344
        this.currentMenuItem = this.currentMenuItem.descItem;
1189 arune 345
    }
346
}
347
 
1185 arune 348
Display.prototype.changeToNext = function()
349
{
1233 arune 350
    if (this.currentMenuItem.nextItem)
1185 arune 351
    {
1233 arune 352
        this.currentMenuItem = this.currentMenuItem.nextItem;
1185 arune 353
    }
354
}
1175 arune 355
 
1185 arune 356
Display.prototype.changeToPrev = function()
357
{
1233 arune 358
    if (this.currentMenuItem.prevItem)
1185 arune 359
    {
1233 arune 360
        this.currentMenuItem = this.currentMenuItem.prevItem;
1185 arune 361
    }
362
}
1175 arune 363
 
1189 arune 364
Display.prototype.incBookTimeTo = function()
1185 arune 365
{
1189 arune 366
    //FIXME: constraints?
1233 arune 367
    if (this.bookToTime.getHours() < 23)
1194 arune 368
    {
1233 arune 369
        this.bookToTime.setMinutes(this.bookToTime.getMinutes() + bookingIncrease);
1194 arune 370
    }
1189 arune 371
}
372
 
373
Display.prototype.decBookTimeTo = function()
374
{
375
    //FIXME: constraints?
1194 arune 376
    var now = new Date();
1233 arune 377
    if (this.bookToTime.getHours() >= now.getHours())
1194 arune 378
    {
1233 arune 379
        this.bookToTime.setMinutes(this.bookToTime.getMinutes() - bookingIncrease);
1194 arune 380
    }
1189 arune 381
}
382
 
383
Display.prototype.incBookTimeFrom = function()
384
{
385
    //FIXME: constraints?
1233 arune 386
    if (this.bookFromTime.getHours() < 23)
1194 arune 387
    {
1233 arune 388
        this.bookFromTime.setMinutes(this.bookFromTime.getMinutes() + bookingIncrease);
1194 arune 389
    }
1189 arune 390
}
391
 
392
Display.prototype.decBookTimeFrom = function()
393
{
394
    //FIXME: constraints?
1194 arune 395
    var now = new Date();
1233 arune 396
    if (this.bookFromTime.getHours() >= now.getHours())
1194 arune 397
    {
1233 arune 398
        this.bookFromTime.setMinutes(this.bookFromTime.getMinutes() - bookingIncrease);
1194 arune 399
    }
1189 arune 400
}
401
 
402
Display.prototype.setBookTimeFrom = function()
403
{
1233 arune 404
    this.currentMenuItem.displayData[1] = "      <"+this.bookFromTime.getTimeShortFormated()+">       ";
1189 arune 405
}
406
 
407
Display.prototype.setBookTimeTo = function()
408
{
1233 arune 409
    this.currentMenuItem.displayData[1] = "      <"+this.bookToTime.getTimeShortFormated()+">       ";
1189 arune 410
}
411
 
412
Display.prototype.chooseBookTimeTo = function()
413
{
1233 arune 414
    this.currentMenuItem.displayData[0] = "  "+this.bookFromTime.getTimeShortFormated()+"  - <"
415
                                +this.bookToTime.getTimeShortFormated()+">  ";
1189 arune 416
}
417
 
418
Display.prototype.chooseBookTimeFrom = function()
419
{
1233 arune 420
    this.currentMenuItem.displayData[0] = " <"+this.bookFromTime.getTimeShortFormated()+"> -  "
421
                                +this.bookToTime.getTimeShortFormated()+"   ";
1189 arune 422
}
423
 
424
Display.prototype.setBookTime = function()
425
{
1233 arune 426
    this.currentMenuItem.displayData[0] = "  "+this.bookFromTime.getTimeShortFormated()+"  -  "
427
                                +this.bookToTime.getTimeShortFormated()+"   ";
1189 arune 428
}
429
 
430
Display.prototype.updateStatusMenuItem = function()
431
{
1185 arune 432
    var date = new Date();
1189 arune 433
    var dateAndTime = "" + date.getTimeShortFormated();
1233 arune 434
    this.statusMenuItem.displayData[1] = this.lcdCenterText(dateAndTime);
1185 arune 435
}
436
 
1189 arune 437
Display.prototype.updateBookMenuItem = function()
438
{
1233 arune 439
    if (this.exchangeCalendarFirstMenuItem && this.exchangeCalendarLastMenuItem)
1189 arune 440
    {
1233 arune 441
        this.statusMenuItem.setNextItem(this.exchangeCalendarFirstMenuItem);
442
        this.bookMenuItem.setPrevItem(this.exchangeCalendarLastMenuItem);
1189 arune 443
    }
1198 arune 444
    else
445
    {
1233 arune 446
        this.statusMenuItem.setNextItem(this.bookMenuItem);
447
        this.bookMenuItem.setPrevItem(this.statusMenuItem);
1198 arune 448
    }
1189 arune 449
}
1185 arune 450
 
1175 arune 451
Display.prototype.exchangeCalendarLookupCallback = function(shortname, data)
452
{
453
    if (shortname == this.shortName)
454
    {
455
        //FIXME check if data contains error tag and error message, print to log and keep previous data?
1195 arune 456
        if (data.error)
457
        {
1250 arune 458
            log("Display:"+this.myId+"> got this error from exchange.php: " + data.error + "\n");
1195 arune 459
        }
460
        else
461
        {
462
            this.exchangeData = data;
463
            this.createCalendarMenu();
464
        }
1175 arune 465
    }
466
}
467
 
1191 arune 468
/* this function parses calendar data from exchange-script and creates a menu */
1189 arune 469
Display.prototype.createCalendarMenu = function()
470
{
1234 arune 471
    /* This is used for function declarations like the callbacks below */
472
    var self = this;
473
 
1198 arune 474
    this.exchangeCalendarLastMenuItem = null;
475
    this.exchangeCalendarFirstMenuItem = null;
476
 
1189 arune 477
    if (this.exchangeData)
478
    {
479
        var lastMenuItem;
480
        for (var i = 0; i < this.exchangeData.meetings.length; i++)
481
        {
482
            /* create the menuitem for a calendar meeting */
483
            var menu = new MenuItem(this);
1204 arune 484
            menu.displayData[0] = this.lcdCenterText(this.replaceAumlauts(this.exchangeData.meetings[i].subject));
1248 arune 485
            menu.displayData[1] = this.lcdCenterText(this.exchangeData.meetings[i].start + " - "
486
                                        + this.exchangeData.meetings[i].end);
1233 arune 487
            menu.doRight = function(args) { self.changeToNext(); };
488
            menu.doLeft = function(args) { self.changeToPrev(); };
1189 arune 489
 
490
            if (i == 0)
491
            {
492
                this.exchangeCalendarFirstMenuItem = menu;
493
            }
494
            else
495
            {
496
                menu.setPrevItem(lastMenuItem);
497
                lastMenuItem.setNextItem(menu);
498
            }
499
 
500
            lastMenuItem = menu;
501
        }
1203 arune 502
        if (lastMenuItem)
503
        {
504
            this.exchangeCalendarLastMenuItem = lastMenuItem;
505
            this.exchangeCalendarLastMenuItem.setNextItem(this.bookMenuItem);
506
            this.exchangeCalendarFirstMenuItem.setPrevItem(this.statusMenuItem);
507
 
508
        }
1212 arune 509
        /* to link in the newly created menuitems with exchangeinformation the display must be showing
510
           either the statusmenu or the bookingmenu */
1216 arune 511
        if (this.currentMenuItem == this.statusMenuItem || this.currentMenuItem == this.bookMenuItem || this.currentMenuItem == this.bookResultMenuItem)
1203 arune 512
        {
1212 arune 513
            /* if there is one or more exchangemenuitems */
1203 arune 514
            if (this.exchangeCalendarFirstMenuItem && this.exchangeCalendarLastMenuItem)
515
            {
516
                this.statusMenuItem.setNextItem(this.exchangeCalendarFirstMenuItem);
517
                this.bookMenuItem.setPrevItem(this.exchangeCalendarLastMenuItem);
518
            }
1212 arune 519
            /* if not then link statusmenu to bookingmenu */
520
            else
521
            {
522
                this.statusMenuItem.setNextItem(this.bookMenuItem);
523
                this.bookMenuItem.setPrevItem(this.statusMenuItem);
524
            }
1203 arune 525
        }
1189 arune 526
    }
527
}
528
 
1204 arune 529
Display.prototype.replaceAumlauts = function(intext)
530
{
1237 arune 531
    intext = intext.replace(/Ã…/, String.fromCharCode(1));
532
    intext = intext.replace(/Ã¥/, String.fromCharCode(0));
533
    intext = intext.replace(/Ä/, String.fromCharCode(2));
1228 arune 534
    intext = intext.replace(/ä/, String.fromCharCode(225));
1237 arune 535
    intext = intext.replace(/Ö/, String.fromCharCode(3));
1228 arune 536
    intext = intext.replace(/ö/, String.fromCharCode(239));
1237 arune 537
    //intext = intext.replace(/:/, ";");
538
    //intext = intext.replace(/,/, ".");
1204 arune 539
    return intext;
540
}
541
 
1175 arune 542
Display.prototype.softPwmOnline = function()
543
{
1256 arune 544
    this.LEDstatus = "none";
1175 arune 545
}
546
 
547
Display.prototype.rotaryOnline = function()
548
{
549
}
550
 
551
Display.prototype.rotaryPosUpdate = function(SwitchId)
552
{
1203 arune 553
    this.screenSaverCnt = 0;
554
    this.mainScreenCnt = 0;
1185 arune 555
    for (var i = 0; i < this.myRotaryService.getSteps(SwitchId); i++)
1181 arune 556
    {
1185 arune 557
        if (this.myRotaryService.getDirection(SwitchId) == "Clockwise")
1181 arune 558
        {
1185 arune 559
            if (this.currentMenuItem.doRight)
560
            {
561
                this.currentMenuItem.doRight();
562
            }
1181 arune 563
        }
564
        else
565
        {
1185 arune 566
            if (this.currentMenuItem.doLeft)
567
            {
568
                this.currentMenuItem.doLeft();
569
            }
1181 arune 570
        }
571
    }
1185 arune 572
 
573
    this.updateDisplay();
1175 arune 574
}
575
 
576
Display.prototype.rotaryBtnUpdate = function(SwitchId)
577
{
1203 arune 578
    this.screenSaverCnt = 0;
579
    this.mainScreenCnt = 0;
1185 arune 580
    if (this.myRotaryService.getButtonStatus(SwitchId) == "Released")
1175 arune 581
    {
1185 arune 582
        if (this.currentMenuItem.doPress)
583
        {
584
            this.currentMenuItem.doPress();
585
        }
1203 arune 586
        this.updateDisplay();
1175 arune 587
    }
588
}
589
 
1182 arune 590
Display.prototype.updateDisplay = function()
591
{
592
    /* see if the current menuitem wants to update itself */
593
    if (this.currentMenuItem.doUpdate)
594
    {
595
        this.currentMenuItem.doUpdate();
596
    }
1185 arune 597
 
598
    /* Clear the LCD screen */
1189 arune 599
    //this.myLCDService.clearScreen();
1182 arune 600
 
601
    /* send the data for the current menuitem to display */
602
    for (var i = 0; i < this.currentMenuItem.displayData.length; i++)
603
    {
1185 arune 604
        this.myLCDService.printText(0, i, this.currentMenuItem.displayData[i]);
1182 arune 605
    }
606
 
607
}
608
 
1190 arune 609
Display.prototype.lcdCenterText = function(text)
610
{
611
    returnText = text;
1195 arune 612
    var displayWidth = 20;  //this.myLCDService.getWidth();
1190 arune 613
    /* pad on both sides with spaces to displayWidth */
614
    returnText = text.pad(displayWidth, " ", 2);
615
 
616
    return returnText;
617
}
618
 
1181 arune 619
Display.prototype.lcdOffline = function()
620
{
621
    /* If we have no interval timer running do nothing */
622
    if (this.myInterval != null)
623
    {
624
        /* Stop the interval */
625
        this.myInterval.stop();
626
    }
627
}
1175 arune 628
 
629
Display.prototype.lcdOnline = function()
630
{
631
    /* If service is not online do nothing */
632
    if (this.myLCDService.isOnline())
633
    {
634
        /* Clear the LCD screen */
635
        this.myLCDService.clearScreen();
636
        /* Set backlight to max */
637
        this.myLCDService.setBacklight(255);
638
 
639
        /* If we have no interval timer running start it */
640
        if (this.myInterval == null)
641
        {
642
            var self = this;
643
 
644
            /* Start interval timer for our printout. Arguments are the callback function and time in milliseconds */
1203 arune 645
            this.myInterval = new Interval(function() { self.timerUpdate() }, 5000);
1175 arune 646
        }
647
 
648
        this.myInterval.start();
1185 arune 649
 
1189 arune 650
        this.timerUpdate();
1175 arune 651
    }
652
}
1245 arune 653
Display.prototype.setLEDtoCalendar = function()
654
{
655
    if (this.exchangeData)
656
    {
657
        var now = new Date();
658
        //for (var i = 0; i < this.exchangeData.meetings.length; i++)
659
        if (this.exchangeData.meetings.length > 0)
660
        {
661
            /* check if current time is before start of first meeting
662
            first meeting is always the next meeting or current meeting */
1247 arune 663
            //var startDate = new Date.parse(this.exchangeData.meetings[0].start);
664
            var startTimeSplit = this.exchangeData.meetings[0].start.split(":");
1252 arune 665
            var endTimeSplit = this.exchangeData.meetings[0].end.split(":");
666
            if (now.getHours() < startTimeSplit[0] ||
667
            now.getHours() > endTimeSplit[0] ||
668
            now.getHours() == startTimeSplit[0] && now.getMinutes() < startTimeSplit[1])
1245 arune 669
            {
1249 arune 670
                if (this.LEDstatus != "green")
1248 arune 671
                {
1249 arune 672
                    this.LEDstatus = "green";
1248 arune 673
                    this.setLEDgreen();
674
                }
1245 arune 675
            }
676
            else
677
            {
1249 arune 678
                if (this.LEDstatus != "red")
1248 arune 679
                {
1249 arune 680
                    this.LEDstatus = "red";
1248 arune 681
                    this.setLEDred();
682
                }
1245 arune 683
            }
684
        }
1246 arune 685
        else
686
        {
1249 arune 687
            if (this.LEDstatus != "green")
1248 arune 688
            {
1249 arune 689
                this.LEDstatus = "green";
1248 arune 690
                this.setLEDgreen();
691
            }
1246 arune 692
        }
1247 arune 693
    }
1245 arune 694
}
695
 
1244 arune 696
Display.prototype.setLEDred = function()
697
{
698
    this.setLED(255, 0, 0);
699
}
700
 
701
Display.prototype.setLEDgreen = function()
702
{
703
    this.setLED(0, 255, 0);
704
}
705
 
1195 arune 706
Display.prototype.setLED = function(red, green, blue)
707
{
1250 arune 708
log("Display:"+this.myId+"> Trying to set leds to red " +red+" green "+green+" blue "+blue+"\n");
1244 arune 709
    if (this.mySoftPwmService.isOnline())
710
    {
711
        this.mySoftPwmService.setPWMValue(255-(red&0xff), 1);
712
        this.mySoftPwmService.setPWMValue(255-(green&0xff), 2);
713
        this.mySoftPwmService.setPWMValue(255-(blue&0xff), 0);
714
    }
1195 arune 715
}
716
 
1238 arune 717
Display.prototype.sendTimeStamp = function()
718
{
719
    var canMessage = new CanNMTMessage("nmt", "Time");
720
    canMessage.send();
721
}
722
 
1175 arune 723
Display.prototype.timerUpdate = function()
724
{
725
    /* If LCD service is not online do nothing */
726
    if (this.myLCDService.isOnline())
727
    {
1203 arune 728
        this.screenSaverCnt++;
729
        this.mainScreenCnt++;
730
        this.exchangeUpdateCnt++;
731
 
1256 arune 732
        if (this.mainScreenCnt > mainScreenTimeout/5 && this.currentMenuItem != this.screenSaverMenuItem)
1203 arune 733
        {
734
            /* Go to main screen (time) */
735
            this.currentMenuItem = this.statusMenuItem;
736
 
737
            /* update the info on display */
738
            this.updateDisplay();
1216 arune 739
 
740
            this.mainScreenCnt = 0;
1203 arune 741
        }
1175 arune 742
 
1203 arune 743
        if (this.screenSaverCnt > screenSaverTimeout/5)
744
        {
1256 arune 745
            /* Go to screensaver menu */
746
            this.currentMenuItem = this.screenSaverMenuItem;
747
 
748
            /* update the info on display */
749
            this.updateDisplay();
1203 arune 750
 
1216 arune 751
            this.screenSaverCnt = 0;
1203 arune 752
        }
753
 
754
        if (this.exchangeUpdateCnt > exchangeUpdateTimeout/5)
755
        {
756
            /* update the info on display */
757
            this.updateDisplay();
758
 
759
            /* do an exchange lookup */
1247 arune 760
            this.exchangeCalendar.lookup(this.shortName);
1203 arune 761
 
762
            this.exchangeUpdateCnt = 0;
763
        }
1248 arune 764
 
765
        this.setLEDtoCalendar();
1175 arune 766
    }
767
}
768