Options
All
  • Public
  • Public/Protected
  • All
Menu

Class MeasureModule

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

getMyFoodMeasuresHandler

  • getMyFoodMeasuresHandler(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>>): Promise<void>
  • Handle /getMyFoodMeasures GET route: Get records stored in the database from my food API

    Query parameters: - since (facultative) - UNIX timestamp of an UTC value that correspond to the minimum date of the measure eg: /getMyFoodMeasures?since=1618319759 (search for entries after date 2021-04-13T13:15:59.000Z)

    - until <timestamp> (facultative) - UNIX timestamp of an UTC value that correspond to the maximum date of the measure
        eg: /getMyFoodMeasures?until=1618320417 (search for entries before date 2021-04-13T13:26:57.000Z)
            /getMyFoodMeasures?since=1618319759?until=1618320417 (search for entries between dates 2021-04-13T13:15:59.000Z and 2021-04-13T13:26:57.000Z)
    
    • sensors ,,... (facultative) - List of sensors to get

       eg: /getMyFoodMeasures&sensors=ph,humidity (only get the ph and humidity sensors values)
       Response: [
           {
               "_id": "60759916f7603e3dcd86b779",
               "captureDate": "2021-04-13T13:11:46.577Z",
               "value": 33.6,
               "sensor": "humidity",
               "__v": 0
           },
           {
               "_id": "60759916f7603e3dcd86b776",
               "captureDate": "2021-04-13T13:11:46.577Z",
               "value": 8.4,
               "sensor": "ph",
               "__v": 0
           }
       ]
      
      • sort <[-]sort1>,<[-]sort2>,... (facultative) - List of ordered fields to sort by (put a "-" before a field name to sort descending) eg: /getMyFoodMeasures?sort=value (order results by value) eg: /getMyFoodMeasures?sort=-sensor,value (order results by sensor name DESC and then by value ASC)

      • limit (facultative) - Max number of results eg: /getMyFoodMeasures?limit=4 (will only return 4 maximum results)

    Response: MeasureSchema[]

    const lastMeasures = await fetch('/getMyFoodMeasures&limit=6')
            .then((res) => res.json())
    
    if(!lastMeasures.error) {
            console.log(lastMeasures) // [ { sensor: '...', captureDate: ..., value: ... }, { sensor: '...', captureDate: ..., value: ... }, ... ]
    }
    

    Parameters

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

    Returns Promise<void>

Protected init

  • init(): Promise<any>

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

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

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

updateMyFoodMeasures

  • Fetch the MyFood public API to get the latest sensor records, store them in the database, and notify the new values to the users through sockets

    Returns Promise<IMeasureSchema[]>

    The new records

Generated using TypeDoc