Commit 1766accb authored by Alexis Lecanu's avatar Alexis Lecanu

fix bug many timer and add auto restart

parent 7336a214
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<string id="Bip5s">Son 5'</string> <string id="Bip5s">Son 5'</string>
<string id="Vibrate">Vibration</string> <string id="Vibrate">Vibration</string>
<string id="Alarms">Alarmes</string> <string id="Alarms">Alarmes</string>
<string id="AutoRestart">Redémarrer</string>
<string id="Enable">Activé</string> <string id="Enable">Activé</string>
<string id="Disable">Désactivé</string> <string id="Disable">Désactivé</string>
<string id="Timer">Cpte. à Reb.</string> <string id="Timer">Cpte. à Reb.</string>
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
<string id="Bip5s">Sound 5'</string> <string id="Bip5s">Sound 5'</string>
<string id="Vibrate">Vibrate</string> <string id="Vibrate">Vibrate</string>
<string id="Alarms">Alarms</string> <string id="Alarms">Alarms</string>
<string id="AutoRestart">Restart</string>
<string id="Enable">Enable</string> <string id="Enable">Enable</string>
<string id="Disable">Disable</string> <string id="Disable">Disable</string>
<string id="Timer">Timer</string> <string id="Timer">Timer</string>
......
using Toybox.Timer;
using Toybox.Lang;
// utility class to wrap Toybox.Timer so that
// we can register and run timers but avoid "too many timers" errors.
var _cron;
static class Cron {
static const CHECK_LOOP_INTERVAL_MS = 100;
protected var t;
protected var jobs;
static function getInstance() {
if (_cron == null) {
_cron = new Cron();
}
return _cron;
}
function initialize() {
self.jobs = {};
self.t = new Timer.Timer();
self.t.start(self.method(:checkTimersLoop), CHECK_LOOP_INTERVAL_MS, true);
}
function register(timerName, everyMs, callback, repeat) {
if (self.jobs.hasKey(timerName)) {
System.println(Lang.format("timer already registered with name $1$", [timerName]));
return;
} else if (everyMs < CHECK_LOOP_INTERVAL_MS) {
System.println(Lang.format("timer loop must be at least $1$ milliseconds", CHECK_LOOP_INTERVAL_MS));
return;
} else if (!(callback instanceof Lang.Method)) {
System.println("callback must be an invokeable method instance");
return;
}
if (everyMs % CHECK_LOOP_INTERVAL_MS != 0) {
System.println(Lang.format("Job repeat time $1$ doesn't schedule roundly with cron loop; check config for job=$2$", [everyMs, timerName]));
}
self.jobs.put(timerName, {
:enabled => true,
:every => everyMs,
:sinceLast => 0,
:callback => callback,
:repeat => repeat
});
}
function isRegistered(timerName) {
return self.jobs.hasKey(timerName);
}
function isEnabled(timerName) {
return self.isRegistered(timerName) && self.jobs.get(timerName)[:enabled];
}
function enable(timerName) {
if (!self.jobs.hasKey(timerName)) {
System.println(Lang.format("no such timer found with name $1$", [name]));
return;
}
self.jobs[timerName][:enabled] = true;
}
function disable(timerName) {
if (!self.jobs.hasKey(timerName)) {
System.println(Lang.format("no such timer found with name $1$", [name]));
return;
}
self.jobs[timerName][:enabled] = false;
}
function unregister(timerName) {
self.jobs.remove(timerName);
}
function checkTimersLoop() {
var jobNames = self.jobs.keys();
for (var i = 0; i < jobNames.size(); i++) {
var jobName = jobNames[i];
var job = self.jobs[jobName];
job[:sinceLast] += CHECK_LOOP_INTERVAL_MS;
if (!job[:enabled] || job[:sinceLast] < job[:every]) {
continue;
}
job[:callback].invoke();
job[:sinceLast] = 0;
if (job[:repeat]) {
continue;
}
self.unregister(jobName);
}
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ class Element { ...@@ -2,6 +2,7 @@ class Element {
var position; var position;
var name; var name;
var time; var time;
var autoRestart;
var beep; var beep;
var beep5s; var beep5s;
var vibrate; var vibrate;
...@@ -18,6 +19,7 @@ class Element { ...@@ -18,6 +19,7 @@ class Element {
self.beep = !extractValue("beep",json).equals("false")?true:false; self.beep = !extractValue("beep",json).equals("false")?true:false;
self.beep5s = !extractValue("beep5s",json).equals("false")?true:false; self.beep5s = !extractValue("beep5s",json).equals("false")?true:false;
self.vibrate = !extractValue("vibrate",json).equals("false")?true:false; self.vibrate = !extractValue("vibrate",json).equals("false")?true:false;
self.autoRestart = extractValue("autoRestart",json).equals("true")?true:false;
} }
function setPosition(position){ function setPosition(position){
...@@ -49,12 +51,14 @@ class Element { ...@@ -49,12 +51,14 @@ class Element {
return beep5s; return beep5s;
}else if(property.equals("vibrate")){ }else if(property.equals("vibrate")){
return vibrate; return vibrate;
}else if(property.equals("autoRestart")){
return autoRestart;
}else{ }else{
return null; return null;
} }
} }
function toJson(){ function toJson(){
return "{\"name\":\""+name+"\",\"time\":\""+time+"\",\"beep\":\""+beep+"\",\"beep5s\":\""+beep5s+"\",\"vibrate\":\""+vibrate+"\"}"; return "{\"name\":\""+name+"\",\"time\":\""+time+"\",\"beep\":\""+beep+"\",\"beep5s\":\""+beep5s+"\",\"vibrate\":\""+vibrate+"\",\"autoRestart\":\""+autoRestart+"\"}";
} }
} }
\ No newline at end of file
...@@ -11,6 +11,7 @@ class TimerApp extends App.AppBase { ...@@ -11,6 +11,7 @@ class TimerApp extends App.AppBase {
function onStart(state) { function onStart(state) {
extract(App.getApp().getProperty("elements")); extract(App.getApp().getProperty("elements"));
//extract("[{\"name\":\"A\",\"time\":\"15\",\"beep\":\"true\",\"beep5s\":\"true\",\"vibrate\":\"true\"},{\"name\":\"B\",\"time\":\"60\",\"beep\":\"true\",\"beep5s\":\"true\",\"vibrate\":\"true\"},{\"name\":\"C\",\"time\":\"60\",\"beep\":\"true\",\"beep5s\":\"true\",\"vibrate\":\"true\"},{\"name\":\"D\",\"time\":\"60\",\"beep\":\"true\",\"beep5s\":\"true\",\"vibrate\":\"true\"}]");
} }
function onStop(state) { function onStop(state) {
......
...@@ -11,8 +11,6 @@ class TimerModel{ ...@@ -11,8 +11,6 @@ class TimerModel{
var counter; var counter;
var status=:Wait; var status=:Wait;
var data; var data;
hidden var refreshTimer = new Timer.Timer();
hidden var displayTimer = new Timer.Timer();
var viewVisible=true; var viewVisible=true;
function initialize(data){ function initialize(data){
...@@ -22,9 +20,8 @@ class TimerModel{ ...@@ -22,9 +20,8 @@ class TimerModel{
function startTimer(){ function startTimer(){
counter = data.time; counter = data.time;
status=:Work; status=:Work;
refreshTimer.stop(); Cron.getInstance().unregister(data.name);
displayTimer.stop(); Cron.getInstance().register(data.name, 1000, method(:refresh), true);
refreshTimer.start(method(:refresh), 1000, true);
startBuzz(); startBuzz();
Ui.requestUpdate(); Ui.requestUpdate();
} }
...@@ -36,15 +33,13 @@ class TimerModel{ ...@@ -36,15 +33,13 @@ class TimerModel{
stopBuzz(); stopBuzz();
} }
} }
refreshTimer.stop(); Cron.getInstance().unregister(data.name);
displayTimer.stop();
Ui.requestUpdate(); Ui.requestUpdate();
} }
function resetTimer() { function resetTimer() {
status=:Reset; status=:Reset;
refreshTimer.stop(); Cron.getInstance().unregister(data.name);
displayTimer.stop();
Ui.requestUpdate(); Ui.requestUpdate();
} }
...@@ -72,6 +67,9 @@ class TimerModel{ ...@@ -72,6 +67,9 @@ class TimerModel{
function end(){ function end(){
stopTimer(false); stopTimer(false);
endBuzz(); endBuzz();
if(data.autoRestart){
startTimer();
}
} }
function startBuzz() { function startBuzz() {
......
...@@ -35,6 +35,10 @@ class MenuItem ...@@ -35,6 +35,10 @@ class MenuItem
} }
function onUpdate (dc){
}
function draw (dc, y, highlight) { function draw (dc, y, highlight) {
if (highlight) { if (highlight) {
setHighlightColor (dc); setHighlightColor (dc);
...@@ -169,6 +173,10 @@ class Menu extends Ui.View { ...@@ -169,6 +173,10 @@ class Menu extends Ui.View {
} }
function onUpdate (dc) { function onUpdate (dc) {
for (var idx = 0; idx < menuArray.size (); idx++) {
menuArray[idx].onUpdate(dc);
}
var width = dc.getWidth (); var width = dc.getWidth ();
var height = dc.getHeight (); var height = dc.getHeight ();
......
...@@ -6,6 +6,7 @@ class ElementMenu extends Menu { ...@@ -6,6 +6,7 @@ class ElementMenu extends Menu {
hidden var data; hidden var data;
hidden var start; hidden var start;
hidden var reset; hidden var reset;
hidden var autoRestart;
hidden var continu; hidden var continu;
hidden var alarms; hidden var alarms;
hidden var remove; hidden var remove;
...@@ -18,6 +19,7 @@ class ElementMenu extends Menu { ...@@ -18,6 +19,7 @@ class ElementMenu extends Menu {
continu = Ui.loadResource(Rez.Strings.Continu); continu = Ui.loadResource(Rez.Strings.Continu);
start = Ui.loadResource(Rez.Strings.Start); start = Ui.loadResource(Rez.Strings.Start);
reset = Ui.loadResource(Rez.Strings.Reset); reset = Ui.loadResource(Rez.Strings.Reset);
autoRestart = Ui.loadResource(Rez.Strings.AutoRestart);
alarms = Ui.loadResource(Rez.Strings.Alarms); alarms = Ui.loadResource(Rez.Strings.Alarms);
remove = Ui.loadResource(Rez.Strings.Remove); remove = Ui.loadResource(Rez.Strings.Remove);
rename = Ui.loadResource(Rez.Strings.Rename); rename = Ui.loadResource(Rez.Strings.Rename);
...@@ -34,6 +36,7 @@ class ElementMenu extends Menu { ...@@ -34,6 +36,7 @@ class ElementMenu extends Menu {
function defaultMenu(){ function defaultMenu(){
var menuItems = new [0]; var menuItems = new [0];
menuItems.add(new TimeMenuItem (:Timer,data)); menuItems.add(new TimeMenuItem (:Timer,data));
menuItems.add(new StateMenuItem (:AutoRestart,autoRestart,data,"autoRestart"));
menuItems.add(new DataMenuItem (:Alarms,alarms,data)); menuItems.add(new DataMenuItem (:Alarms,alarms,data));
menuItems.add(new DataMenuItem (:Remove,remove,data)); menuItems.add(new DataMenuItem (:Remove,remove,data));
menuItems.add(new DataMenuItem (:Up,up,data)); menuItems.add(new DataMenuItem (:Up,up,data));
......
...@@ -72,6 +72,8 @@ class ElementMenuDelegate extends MenuDelegate { ...@@ -72,6 +72,8 @@ class ElementMenuDelegate extends MenuDelegate {
elements.addAll(temps); elements.addAll(temps);
} }
Ui.popView (Ui.SLIDE_LEFT); Ui.popView (Ui.SLIDE_LEFT);
}else if(item.id ==:AutoRestart){
item.data.autoRestart=!item.data.autoRestart;
} }
} }
} }
\ No newline at end of file
...@@ -6,8 +6,12 @@ class ElementMenuItem extends DataMenuItem{ ...@@ -6,8 +6,12 @@ class ElementMenuItem extends DataMenuItem{
function initialize (id, data) { function initialize (id, data) {
DataMenuItem.initialize(id, data.name,data); DataMenuItem.initialize(id, data.name,data);
} }
function onShow () { function onUpdate (dc) {
DataMenuItem.setValue(Utils.timeToString(data.time)); if(models.hasKey(data.name) && models.get(data.name).status == :Work){
DataMenuItem.setValue(Utils.timeToString(models.get(data.name).counter));
}else{
DataMenuItem.setValue(Utils.timeToString(data.time));
}
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment