Options
All
  • Public
  • Public/Protected
  • All
Menu

Class OccupancyRateModule

Hierarchy

Index

Constructors

constructor

Properties

Protected Readonly _app

Application instance

Protected Readonly _id

_id: number

Module ID

Protected Readonly _log

_log: Debugger

Module logger

Protected Readonly _name

_name: string

Module name

Protected _sockets

_sockets: Socket<DefaultEventsMap, DefaultEventsMap>[]

Connected sockets

Accessors

endpoints

initPromise

  • get initPromise(): Promise<any>
  • Returns the init promise to know if the module init is done or not

    Returns Promise<any>

name

  • get name(): string

wait

  • get wait(): boolean

Methods

Protected addEndpoint

Protected addEndpoints

  • addEndpoints(endpoints: Endpoint[]): void

addUnitHandler

  • addUnitHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /addOCUnit POST route: Add a unit (name + slots) to a given module

    Query parameters: - module (mandatory) - Name of the module eg: /addOCUnit?name=Aquaponic%20greenhouse

    - name <unit> (mandatory) - Name of the unit
        eg: /addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower
    
    • slots (mandatory) - Number of slots of the unit

        eg: /addOCUnit?name=Aquaponic%20greenhouse&name=Plantation%20tower&slots=6
      
    • groupIndex (facultative) - Group of the unit, starts at 0 (-1 means new group)

        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
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

changeUnitSlotsHandler

  • changeUnitSlotsHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /changeOCUnitSlots PUT route: Change the number of slots of a unit

    Query parameters: - module (mandatory) - Name of the module eg: /changeOCUnitSlots?name=Aquaponic%20greenhouse

    - 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
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

editElementHandler

  • editElementHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /editOCElement PUT route: Edit the content or the comment of and element

    Query parameters: - module (mandatory) - Name of the module eg: /editOCElement?name=Aquaponic%20greenhouse

    - 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' }
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

getModuleHandler

  • getModuleHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /getOCModule GET route: Get the occupancy rate information of a given module

    Query parameters: - name (mandatory) - Name of the module eg: /getOCModule?name=Aquaponic%20greenhouse

    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: [ [...], [...], ... ] }
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

getModulesHandler

  • getModulesHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • 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: [ [...], [...], ... ] }, ... ]
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

Protected init

  • init(): Promise<void>

Protected initialize

  • initialize(autoInit: boolean): void
  • 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

    Parameters

    • autoInit: boolean

      Should the Application run the init method

      initialize(true/false)
      

    Returns void

moveUnitHandler

  • moveUnitHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /moveOCUnit PUT route: Move a unit from a group to another

    Query parameters: - module (mandatory) - Name of the module eg: /moveOCUnit?name=Aquaponic%20greenhouse

    - 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
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

onSocketJoin

  • onSocketJoin(socket: Socket<DefaultEventsMap, DefaultEventsMap>): any
  • Listener for new client socket connection

    Parameters

    • socket: Socket<DefaultEventsMap, DefaultEventsMap>

      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"
      

    Returns any

onSocketLeave

  • onSocketLeave(socket: Socket<DefaultEventsMap, DefaultEventsMap>): any
  • Listener for disconnected client sockets

    Parameters

    • socket: Socket<DefaultEventsMap, DefaultEventsMap>

      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"
      

    Returns any

Protected registerTask

  • Register a task

    Parameters

    • schedule: TaskSchedule

      Schedule of the task: Date, Moment or cron schedule

    • action: TaskAction

      Action the will be run on schedule

    • start: boolean = true

      Does auto start (default: true)

    Returns Task

    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() {
                ...
            }
    }
    

removeUnitHandler

  • removeUnitHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /removeOCUnit DELETE route: Remove a unit if there are only empty elements inside

    Query parameters: - module (mandatory) - Name of the module eg: /removeOCUnit?name=Aquaponic%20greenhouse

    - 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
    }
    

    Parameters

    • req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>
    • res: Response<any, Record<string, any>>

    Returns Promise<void>

setWait

  • setWait(wait: boolean): void
  • Set the wait flag to the given value

    Parameters

    • wait: boolean

      New wait flag value

      setWait(true/false)
      

    Returns void

updateOccupancyRates

Generated using TypeDoc