Skip to content

Develop new module

Thomas T. Jarløv edited this page Feb 15, 2019 · 1 revision

You can easily add new modules. The guide below is how to create the skeleton.

We are going to create a module named smile.

Database

Setup the database

If you need a database, create a file in nimhapkg/resources/database/modules/smile_database.nim.

We are going to create 2 tables: a normal table and a template table.

Insert your table requirements, e.g.:

import db_sqlite
proc smileDatabase*(db: DbConn) =
  if not tryExec(db, sql"""
  CREATE TABLE IF NOT EXISTS smile (
    id INTEGER PRIMARY KEY,
    quote TEXT,
    creation timestamp NOT NULL default (STRFTIME('%s', 'now'))
  );"""):
    echo "ERROR: Smile templates table could not be created"

  if not tryExec(db, sql"""
  CREATE TABLE IF NOT EXISTS smile_templates (
    id INTEGER PRIMARY KEY,
    name TEXT,
    mood TEXT,
    creation timestamp NOT NULL default (STRFTIME('%s', 'now'))
  );"""):
    echo "ERROR: Smile templates table could not be created"

Create database

To create the database navigate to nimha.nim and find the proc proc createDbTables() =.

Insert a new db variable and the proc:

var dbSmile = conn("dbSmile.db")

smileDatabase(dbSmile)

Smile procs

If your module uses proc's, create a file: nimhapkg/modules/smile/smile.nim and insert your proc's, e.g.:

proc smileNow*(): string =
  return "Smile now!"

HTML page

We are going to add a small settings page. It is in this page, you should allow the user add and delete templates. See the other modules for inspiration.

Create the file nimhapkg/tmpl/smile.tmpl and insert:

#? stdtmpl | standard
#
#proc genSmile(c: var TData): string =
# result = ""
<head>
  ${genMainHead(c)}
</head>

<body>
  <header>
    ${genMainHeader()}
  </header>

  <main>
    <div id="pageType" data-userid="${c.userid}" data-type="os" style="display: none;"></div>
    <div class="wrapper">
      ${genMainSidebar()}

      <div id="pagewrapper">
        <div id="smile">
          <h1>SMILE</h1>
          <hr>
          <div class="smileTemplates">
            # let smileTemplates = getAllRows(dbSmile, sql"SELECT id, quote FROM smile")
            # for smile in smileTemplates:
              <div>${smile[2]]</div>
            # end for
          </div>
        </div>
      </div>
    </div>
  </main>
  <footer>
    ${genMainFooter()}
  </footer>
  ${genMainNotify()}
</body>
#end proc

Activate smile proc and HTML

Open nimhapkg/mainmodules/nimha_webinterface.nim:

  1. Import smile proc: import ../modules/smile/smile
  2. Add the database: var dbSmile = conn("dbSmile.db")
  3. Include the HTML: include "../tmpl/smile.tmpl"
  4. Insert the route:
  get "/smil":
    createTFD()
    if not c.loggedIn:
      redirect("/login")

    resp genSmile(c)

  get "/smile/proc":
    createTFD()
    if not c.loggedIn:
      redirect("/login")
    return smileNow()

  get "/smile/template":
    createTFD()
    if not c.loggedIn:
      redirect("/login")

    resp(getValue(dbSmile, sql("SELECT mood FROM smile_templates WHERE id = ?"), @"smileid")

Use the templates

If you modules support templates, then they should be added to:

  • nimhapkg/tmpl/cron.tmpl
  • nimhapkg/tmpl/alarm.tmpl

This will allow the cron and the alarm to use your smile_templates table.