feat: add schedule buttons and tweak CSS

This commit is contained in:
alban 2024-02-27 00:25:12 +01:00
parent 718c10b7aa
commit 8162aa3c8e
4 changed files with 177 additions and 60 deletions

View file

@ -2,6 +2,8 @@
<head>
<title>ESP32 timed Switch</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.8.3/angular.js"></script>
<link rel="manifest" href="manifest.json" />
</head>
<body>
<h1>Scheduler</h1>
@ -24,7 +26,7 @@ class Schedule {
// Load from binary data
var view = new DataView(buffer);
for (var i = 0; i < 24; i++) {
this.schedule[i] = view.getUint32(i * 4);
this.schedule[i] = view.getUint16(i * 2);
}
};
@ -53,7 +55,7 @@ class Schedule {
return this.schedule.map(hour => {
if (hour === 0xFFF) { // All segments on
return { displayValue: ' ', class: 'on' };
} else if (hour === 0x000) { // All segments off
} else if (hour === 0x00) { // All segments off
return { displayValue: ' ', class: 'off' };
} else {
return { displayValue: ' ', class: 'partial' };
@ -93,12 +95,12 @@ class Schedule {
serializeToArrayBuffer = function() {
// Convert to binary data
var buffer = new ArrayBuffer(24 * 4);
var buffer = new ArrayBuffer(24 * 2);
var view = new DataView(buffer);
for (var i = 0; i < this.schedule.length; i++) {
view.setUint32(i * 4, this.schedule[i]);
view.setUint16(i * 2, this.schedule[i]);
}
return buffer;
return view;
};
}
@ -108,10 +110,26 @@ class Schedule {
// Start of Angular
var app = angular.module('plugSchedulerApp', []);
app.value('api_host', window.location.host);
app.factory("data", function(){
var sharedData = { api_host: window.location.host}
return {
getSharedData: function () {
return sharedData;
}
};
});
app.controller('PlugScheduleController', ['$http', function($http) {
app.controller('ApiHostController', ['$scope', 'api_host', 'data', function( $scope, api_host, data) {
var ctrl = this;
$scope.data = data.getSharedData();
window.ctrl = $scope
}])
app.controller('PlugScheduleController', ['$scope', '$http', 'data', function( $scope, $http, data) {
var ctrl = this;
ctrl.editMode = false;
$scope.data = data.getSharedData();
ctrl.$onInit = function() {
ctrl.scheduleObj = new Schedule(); // Initialize with a default schedule
@ -120,11 +138,11 @@ app.controller('PlugScheduleController', ['$http', function($http) {
ctrl.getSchedule = function() {
if (ctrl.plugId === undefined) {
throw new Error('plugId is not defined');
return;
}
// Fetch the schedule from the server for a given plug ID
var url = `/api/schedule/${ctrl.plugId}`
var url = `http://${$scope.data.api_host}/api/schedule/${ctrl.plugId}`
$http.get(url, { responseType: 'arraybuffer' }).then(function(response) {
ctrl.scheduleObj.loadFromArrayBuffer(response.data);
ctrl.displaySchedule = ctrl.scheduleObj.serializeForDisplay();
@ -148,7 +166,7 @@ app.controller('PlugScheduleController', ['$http', function($http) {
// Send the updated schedule to the server
ctrl.updateSchedule = function() {
var url = `/api/schedule/${ctrl.plugId}`
var url = `http://${$scope.data.api_host}/api/schedule/${ctrl.plugId}`
var serializedSchedule = ctrl.scheduleObj.serializeToArrayBuffer();
$http.post(url, serializedSchedule, {
@ -162,11 +180,10 @@ app.controller('PlugScheduleController', ['$http', function($http) {
console.log("successfully updated");
ctrl.displaySchedule = ctrl.scheduleObj.serializeForDisplay();
}, function(error) {
console.error('Error updating schedule:', error);
alert('Error updating schedule:', error);
});
};
// check all segments for a specific hour
ctrl.checkAll = function(hourIndex) {
ctrl.editMatrix[hourIndex].forEach(segment => {
@ -180,6 +197,37 @@ app.controller('PlugScheduleController', ['$http', function($http) {
segment.value = 0;
});
};
ctrl.invertAll = function(hourIndex) {
ctrl.editMatrix[hourIndex].forEach(segment => {
var inverted = Math.abs( segment.value - 1)
segment.value = inverted;
});
};
ctrl.checkPattern = function(hourIndex, results) {
for( index = 0; index < ctrl.editMatrix[hourIndex].length; index++){
ctrl.editMatrix[hourIndex][index] = { value : results[index] };
}
};
ctrl.checkHalf = function(hourIndex) {
ctrl.checkPattern( hourIndex, [1,1,1,1,1,1,0,0,0,0,0,0]);
};
ctrl.checkFourth = function(hourIndex) {
ctrl.checkPattern( hourIndex, [1,1,1,0,0,0,1,1,1,0,0,0]);
};
ctrl.checkSixth = function(hourIndex) {
ctrl.checkPattern( hourIndex, [1,1,0,0,1,1,0,0,1,1,0,0]);
};
ctrl.checkTwelveth = function(hourIndex) {
ctrl.checkPattern( hourIndex, [1,0,1,0,1,0,1,0,1,0,1,0]);
};
}]);
app.component('plugScheduleWidget', {
@ -188,22 +236,32 @@ app.component('plugScheduleWidget', {
},
template: `
<div class="plug-schedule-widget">
<h3>Schedule for Plug {{ $ctrl.plugId }}</h3>
<h3>Plug {{ $ctrl.plugId }}</h3>
<div ng-if="!$ctrl.editMode">
<div ng-repeat="hour in $ctrl.displaySchedule track by $index" class="hour-cell" ng-class="hour.class">
{{ hour.displayValue }}
</div>
<button ng-click="$ctrl.modifySchedule()">Modify Schedule</button>
<button class="modify" ng-click="$ctrl.modifySchedule()">Modify</button>
</div>
<div ng-if="$ctrl.editMode">
<table>
<tr>
<td class=></td>
<td class="minutes" ng-repeat="segment in [0,1,2,3,4,5,6,7,8,9,10,11]">{{ segment * 5 }}</td>
</tr>
<tr ng-repeat="hourSegments in $ctrl.editMatrix track by $index">
<td ng-repeat="segment in hourSegments track by $index">
<td class="hours">{{ $index }}</td>
<td class="cell" ng-repeat="segment in hourSegments track by $index">
<input type="checkbox" ng-model="segment.value" ng-true-value="1" ng-false-value="0">
</td>
<td>
<button ng-click="$ctrl.checkAll($index)">Check All</button>
<button ng-click="$ctrl.uncheckAll($index)">Uncheck All</button>
<button ng-click="$ctrl.checkAll($index)">1</button>
<button ng-click="$ctrl.uncheckAll($index)">0</button>
<button ng-click="$ctrl.checkHalf($index)">½</button>
<button ng-click="$ctrl.checkFourth($index)">¼</button>
<button ng-click="$ctrl.checkSixth($index)"></button>
<button ng-click="$ctrl.checkTwelveth($index)">¹⁄₁₂</button>
<button ng-click="$ctrl.invertAll($index)"></button>
</td>
</tr>
</table>
@ -217,21 +275,29 @@ app.component('plugScheduleWidget', {
// End of Angular
</script>
</script>
<div ng-app="plugSchedulerApp">
<plug-schedule-widget plug-id="1"></plug-schedule-widget>
<plug-schedule-widget plug-id="2"></plug-schedule-widget>
<plug-schedule-widget plug-id="3"></plug-schedule-widget>
<plug-schedule-widget plug-id="4"></plug-schedule-widget>
<plug-schedule-widget plug-id="5"></plug-schedule-widget>
<plug-schedule-widget plug-id="6"></plug-schedule-widget>
<plug-schedule-widget plug-id="7"></plug-schedule-widget>
<plug-schedule-widget plug-id="8"></plug-schedule-widget>
<div ng-app="plugSchedulerApp" id="main_container">
<div ng-controller="PlugScheduleController">
<plug-schedule-widget plug-id="1"></plug-schedule-widget>
<plug-schedule-widget plug-id="2"></plug-schedule-widget>
<plug-schedule-widget plug-id="3"></plug-schedule-widget>
<plug-schedule-widget plug-id="4"></plug-schedule-widget>
<plug-schedule-widget plug-id="5"></plug-schedule-widget>
<plug-schedule-widget plug-id="6"></plug-schedule-widget>
<plug-schedule-widget plug-id="7"></plug-schedule-widget>
<plug-schedule-widget plug-id="8"></plug-schedule-widget>
</div>
<!--
<div ng-controller="ApiHostController">
<h3>
Change Host
<input type="text" ng-model="data.api_host" value="{{data.api_host}}" style="margin: 10px;padding: 3px; font-size: 14pt; color: #333340; border-radius: 6px;">
</h3>
</div>
-->
</div>
<style>
* {
font-family: Calibri, Candara, Segoe, Segoe UI, Optima, Arial, sans-serif;
@ -241,27 +307,60 @@ app.component('plugScheduleWidget', {
text-align: center;
color: #aaa;
font-weight: normal;
font-size: 20pt;
}
body {
width: 80%;
margin: auto;
background: #333340;
}
button {
padding: 4px 8px;
margin-left: 5px;
vertical-align: top;
border-radius: 12px;
background: #696983;
border-radius: 6px;
box-shadow: 1px 1px 3px rgba(0,0,0,0.6);
color: eee;
margin-top: 5px;
font-size: 0.8em;
margin-left: 4px;
margin-top: 16px;
padding: 14px;
vertical-align: top;
}
button.modify {
margin: 4px;
}
button.modifiers {
width: 40px;
}
button:hover {
background: #9f9fc4;
box-shadow: inset 1px 1px 3px rgba(0,0,0,0.6);
}
.hour-cell {
display: inline-block;
width: 10px;
height: 25px;
width: 25px;
height: 50px;
margin-top: 4px;
}
.minutes, .hours {
font-size: 0.6em;
color: rgba(255, 255, 255, .5);
padding: 6px;
}
table tr td {
padding: 3px;
}
tr:nth-child(2n) {
box-shadow: inset 1px 1px 4px 1px rgba(0,0,0,0.54);
background: rgba(0, 0, 0, 0.07);
}
tr td.cell {
border-left: 1px solid rgba(61, 61, 78, .61);
}
div#main_container {
margin: auto;
}
.off {
background-color: #eee;