Application
Application instance
Module ID
Module logger
Module name
Connected sockets
Endpoints of the Module
Returns the init promise to know if the module init is done or not
Name of the module
Should the Application wait for the Module to initialize
Add an endpoint to the module endpoints
Endpoint to add
addEndpoint(myEndpoint)
Add multiple endpoints
addEndpoints([ endpoint1, endpoint2, ... ])
Handle /addOCUnit POST route: Add a unit (name + slots) to a given module
Query parameters:
- module
- name <unit> (mandatory) - Name of the unit
eg: /addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower
slots
eg: /addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower&slots=6
groupIndex
eg: /addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower&slots=6&groupIndex=2
/addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower&slots=6&groupIndex=-1
Response: { message: 'success', module: ORModuleSchema }
const module = await('/addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower&slots=6&groupIndex=2', { method: 'POST' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module[2][module.module[2].length - 1].slots) // The unit that you just created
}
Handle /changeOCUnitSlots PUT route: Change the number of slots of a unit
Query parameters:
- module
- group <group> (mandatory) - Group of the unit inside the module
eg: /changeOCUnitSlots?name=Aquaponic%20greenhouse&group=2
- unit <unit> (mandatory) - Index of the unit inside the group
eg: /changeOCUnitSlots?name=Aquaponic%20greenhouse&group=2&unit=6
- slots <slots> (mandatory) - New slot count of the unit
eg: /changeOCUnitSlots?name=Aquaponic%20greenhouse&group=2&unit=6&slots=12
Response: { message: 'success', module: ORModuleSchema }
const module = await('/changeOCUnitSlots?name=Aquaponic%20greenhouse&group=2&unit=6&slots=12', { method: 'PUT' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module[2][6].slots) // 12
}
Handle /editOCElement PUT route: Edit the content or the comment of and element
Query parameters:
- module
- group <group> (mandatory) - Group of the unit inside the module
eg: /editOCElement?name=Aquaponic%20greenhouse&group=2
- unit <unit> (mandatory) - Index of the unit inside the group
eg: /editOCElement?name=Aquaponic%20greenhouse&group=2&unit=6
- element <element> (mandatory) - Index of the element inside the unit
eg: /editOCElement?name=Aquaponic%20greenhouse&group=2&unit=6&element=3
- value <value> (facultative) - New value of the element
eg: /editOCElement?name=Aquaponic%20greenhouse&group=2&unit=6&element=3&value=strawberry
- comment <value> (facultative) - New comment of the element
eg: /editOCElement?name=Aquaponic%20greenhouse&group=2&unit=6&element=3&value=strawberry&comment=Comment%20example%20here
Response: { message: 'success', module: ORModuleSchema }
const module = await('/editOCElement?name=Aquaponic%20greenhouse&group=2&unit=6&element=3&value=strawberry&comment=Comment%20example%20here', { method: 'PUT' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module[2][6][3]) // { value: 'strawberry', comment: 'Comment example here' }
}
Handle /getOCModule GET route: Get the occupancy rate information of a given module
Query parameters:
- name
Response: { module: ORModuleSchema }
const module = await('/getOCModule?name=Aquaponic%20greenhouse', { method: 'GET' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module) // { name: '...', units: [ [...], [...], ... ] }
}
Handle /getOCModules GET route: Get all the modules occupancy rate information
Query parameters: none
Response: { modules: ORModuleSchema[] }
const module = await('/getOCModules', { method: 'GET' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module) // [ { name: '...', units: [ [...], [...], ... ] }, { name: '...', units: [ [...], [...], ... ] }, ... ]
}
Ensure that the needed modules exist in the database
If autoInit is set on true, will run the init async method and put the result of the Promise in a variable that can be get by the application. This will allow the Application to be notified at the end of the init method of the Module If autoInit is set on false, will just put a Promise.resolve() in the init variable
Should the Application run the init method
initialize(true/false)
Handle /moveOCUnit PUT route: Move a unit from a group to another
Query parameters:
- module
- group <group> (mandatory) - Group of the unit inside the module
eg: /moveOCUnit?name=Aquaponic%20greenhouse&group=2
- unit <unit> (mandatory) - Index of the unit inside the group
eg: /moveOCUnit?name=Aquaponic%20greenhouse&group=2&unit=6
- to <group> (mandatory) - Index of the new group of the unit
eg: /moveOCUnit?name=Aquaponic%20greenhouse&group=2&unit=6&to=0
Response: { message: 'success', module: ORModuleSchema }
const module = await('/moveOCUnit?name=Aquaponic%20greenhouse&group=2&unit=6&to=0', { method: 'PUT' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module[0][module.module[0].length - 1].slots) // The unit that you just moved
}
Listener for new client socket connection
Client socket
// Application
onSocketJoin((socket) => {
this._modules.forEach((module) => {
module.onSocketJoin(socket)
})
})
// MyModule
public onSocketJoin(socket) {
console.log(`Socket ${socket.id} joined`)
}
// index.ts
const app = new Application({...})
...
app.registerModule(MyModule)
...
// When a socket joins the server
// console: "Socket xZicnuePG5WehCSKAAAB joined"
Listener for disconnected client sockets
Client socket
// Application
onSocketJoin((socket) => {
...
socket.on('disconnected', (socket) => {
this.modules.forEach((module) => {
module.onSocketLeave(socket)
})
})
})
// MyModule
public onSocketLeave(socket) {
console.log(`Socket ${socket.id} leaved`)
}
// index.ts
const app = new Application({...})
...
app.registerModule(MyModule)
...
// When a socket leaves the server
// console: "Socket xZicnuePG5WehCSKAAAB leaved"
Register a task
Schedule of the task: Date, Moment or cron schedule
Action the will be run on schedule
Does auto start (default: true)
The task
export class MyModule extends Module {
constructor(app: Application) {
...
// Perform task every day at 10h00
this.registerTask('* * 10 * * *', this.performMyTask.bind(this), true)
}
public async performMyTask() {
...
}
}
Handle /removeOCUnit DELETE route: Remove a unit if there are only empty elements inside
Query parameters:
- module
- group <group> (mandatory) - Group of the unit inside the module
eg: /removeOCUnit?name=Aquaponic%20greenhouse&group=2
- unit <unit> (mandatory) - Index of the unit inside the group
eg: /removeOCUnit?name=Aquaponic%20greenhouse&group=2&unit=6
Response: { message: 'success', module: ORModuleSchema }
const module = await('/removeOCUnit?name=Aquaponic%20greenhouse&group=2&unit=6', { method: 'DELETE' })
.then((res) => res.json())
if(!module.error) {
console.log(module.module[2][6].length) // Length before the fetch -1
}
Set the wait flag to the given value
New wait flag value
setWait(true/false)
Compute the occupancy rate of all the modules and update them
All the modules
Generated using TypeDoc
Allow the client to get the occupancy rates data and add/edit them