-
-
Save dbleier/bbc2e776a44e038a5bd7 to your computer and use it in GitHub Desktop.
sign connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import can from 'can'; | |
import superMap from 'can-connect/can/super-map/'; | |
import List from 'can/list/'; | |
import Map from 'can/map/'; | |
import tag from 'can-connect/can/tag/'; | |
import 'can/map/define/define'; | |
import config from 'menuboard-manager/config/config/'; | |
//import services from 'menuboard-manager/config/services/'; | |
export const Sign = Map.extend({ | |
define: {} | |
}); | |
Sign.List = List.extend({ | |
Map: Sign | |
}, {}); | |
Sign.connection = superMap({ | |
url: config.attr('env').domain+'get/1', | |
idProp: 'id', | |
Map: Sign, | |
List: Sign.List, | |
name: 'sign' | |
}); | |
tag('sign-model', Sign.connection); | |
export default Sign; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const SignVM = Map.extend({ | |
define: { | |
logger: { | |
get() { | |
return services.getService('logger'); | |
} | |
}, | |
storage: { | |
get() { | |
return services.getService('storage'); | |
} | |
}, | |
clock: { | |
get() { | |
return services.getService('clock'); | |
} | |
}, | |
filesystem: { | |
get() { | |
return services.getService('filesystem'); | |
} | |
}, | |
path: { | |
get() { | |
return config.attr('env').localPath; | |
} | |
}, | |
signPromise: { | |
get() { | |
var self = this, mac = config.attr('mac'), user = config.attr('user'), | |
params = {mac: mac, user: user}; | |
this.attr('logger').debug('MAC = '+mac); | |
this.attr('logger').debug('USER = '+user); | |
if(user == null) { | |
this.attr('logger').debug('Getting User'); | |
User.getList({mac: mac}).then(function (users) { | |
var user = users[0]; | |
if(user.user) { | |
self.attr('logger').info('New User Retrieved: '+user.user); | |
config.attr('user', user.user); | |
} | |
}); | |
} | |
console.log('getting sign',params); | |
return Sign.getList(params); | |
} | |
}, | |
sign: { | |
get(last, set) { | |
var self = this; | |
return this.attr('signPromise').then(function(signs) { | |
var sign = signs[0], actpres, curpres; | |
if(!sign) { | |
self.attr('logger').error('No Signs Retrieved from Server'); | |
set(sign); | |
return; | |
} | |
// 1st check if there is only 1 active presentation, | |
// if only 1 presentation exists at all, use it regardless | |
// whether it is marked active or not (better than a blank screen) | |
if(sign.presentations.length === 1) { | |
// don't need to worry about scheduling or any other multi-presention logic | |
//curpres = self.processPresentation(sign.presentations[0]); | |
//new Presentation({id: curpres.id, data: curpres}).save(); | |
self.attr('logger').debug('Only One Presentation found: '+sign.presentations[0].id); | |
self.loadPresentation(sign, sign.presentations[0]); | |
} else { | |
actpres = sign.presentations.filter(function(pres) { | |
return pres.props.isActive; | |
}); | |
if(!actpres.length) { | |
self.attr('logger').debug('No Active Presentation found'); | |
// no active presentations, throw error... | |
} else if(actpres.length === 1) { | |
// only 1 active presentation, treat like only presentation | |
//curpres = self.processPresentation(actpres[0]); | |
//new Presentation({id: curpres.id, data: curpres}).save(); | |
self.attr('logger').debug('Only One Active Presentation found: '+actpres[0].id); | |
self.loadPresentation(sign, actpres[0]); | |
} else { | |
self.attr('logger').debug('Multiple Active Presentation found'); | |
// multiple presentations, need to check scheduling, etc. | |
let scp = actpres[0], now = self.attr('clock').now(), | |
latestSchedule = later.schedule(later.parse.text( | |
actpres[0].props.schedule.compiled)).prev(1), | |
remaining = actpres.slice(1); | |
self.attr('logger').debug('1st Presentation: '+scp.id+', last Schedule: '+latestSchedule); | |
//remaining.each(function (pres) { | |
actpres.each(function (pres) { | |
if(!pres.props.schedulingOn) { | |
self.attr('logger').debug('Presentation: '+scp.id+', not scheduled'); | |
return; | |
} | |
let schedule = later.parse.text(pres.props.schedule.compiled), | |
scheduled = later.schedule(schedule), | |
prev = scheduled.prev(1), next = scheduled.next(1); | |
self.attr('logger').debug('Presentation: '+pres.id+', last Schedule: '+prev); | |
if(latestSchedule < prev && prev < now) { | |
latestSchedule = prev; | |
scp = pres; | |
} | |
// schedule next occurence of presentation | |
self.loadPresentation(sign, pres, schedule); | |
}); | |
self.attr('logger').debug('Current Presentation: '+scp.id+', Starting at: '+latestSchedule); | |
self.loadPresentation(sign, scp); | |
} | |
} | |
set(sign); | |
}) | |
.catch(function (err) { | |
self.attr('logger').error(err); | |
}); | |
} | |
}, | |
}, | |
loadSlide(slides, i) { | |
var self = this, index = i < slides.length ? i : slides.length - 1, | |
cs = slides[index], | |
next = (index + 1) % slides.length, | |
timer = ignore(function() { | |
setTimeout(function() { | |
self.loadSlide.call(self, slides, next); | |
}, | |
cs.props.duration * 1000 | |
) | |
}); | |
this.attr('currentSlide', cs); | |
if(cs.type === 'video') { | |
$('#'+cs.id)[0].play(); | |
} | |
timer(); | |
}, | |
loadPresentation(sign, pres, schedule) { | |
if(schedule) { | |
later.setTimeout(function () { | |
sign.attr('currentPresentation', pres); | |
}, schedule); | |
} else { | |
sign.attr('currentPresentation', pres); | |
this.loadSlide(pres.slideshows[0].slides, 0); | |
} | |
config.attr('isLFD') && setTimeout(this.downloadPresentationMedia(pres), 10); | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{#if signPromise.isPending}} | |
<p>Sign Loading...</p> | |
{{else}} | |
{{#with sign.currentPresentation}} | |
<style type="text/css" media="screen"> | |
{{#each ./fonts}} | |
@font-face { | |
font-family: {{fontName(font.id)}}; | |
src: url({{localUrl(font.id, font.url, 'font/')}}); | |
} | |
{{/each}} | |
{{css}} | |
</style> | |
<div id="fields"> | |
{{#each ./fields}} | |
<div {$id}="id" {$class}="classes" {$style}="parseCSS('field', cssrule)">{{value}}</div> | |
{{/each}} | |
</div> | |
<div id="slideshows"> | |
{{#each ./slideshows}} | |
<div {$id}="id" {$class}="classes" {$style}="parseCSS('slideshow', cssrule)" data-cycle-slides="> div" > | |
{{#each slides}} | |
{{#eq type 'video'}} | |
<video {$id}="id" {$src}="localUrl(id, url, 'media/s')" type="video/mp4" style="display:{{#eq id currentSlide.id}}block{{else}}none{{/eq}};" /> | |
{{else}} | |
<img {$id}="id" {$src}="localUrl(id, url, 'media/s')" style="display:{{#eq id currentSlide.id}}block{{else}}none{{/eq}};" /> | |
{{/eq}} | |
{{/each}} | |
</div> | |
{{/each}} | |
</div> | |
{{/with}} | |
{{/if}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment