Code Monkey home page Code Monkey logo

eensymachines-in / luminapi Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 50.13 MB

This is a collection of multiple docker-orchestrated microservices that run on the cloud for autolumin project. API, web app and the MQTT broker all batteries included. Push this to a suitable infra-cloud and expect autolumin web-side components to be running

Dockerfile 0.52% Go 26.08% JavaScript 48.74% HTML 23.76% CSS 0.90%
website api mqtt-broker docker golang containers internet-of-things

luminapi's Introduction

Luminapi purpose


Ahead of device and user authentication, we now need a microservice api that can handle CRUD schedules for autolumin. Luminapi is to be accessed by all devices running autolumin. authapi was for any device but this has specific purpose for maitaining the schedules of each of the devices.

  1. Refer a database of schedules, filtered by the device ID and send it as json
  2. Modify a schedule in the database, filtered by the device ID and the notify the broker (mosquitto) of the same.
  3. Clean all schedules for a a device ID
  4. When registering a new device a default schedule is injected and responded back to as http response
  5. Endpoint for the devices to communication the status / logs to the cloud.

Database in the cloud with devices and their schedules is the single source of truth

Devices will try to stay sync to changes on the cloud via a broker. Changes the user makes shall be pushed on the server and further onto the devices via the broker. The logs and the readings that the device records on the ground can be sent over to the cloud for user reporting.

Schedule structure :


For a single device this is what the schedule looks like

  • One and only one primary schedule is required
{
    "schedules": [
        {"on":"06:00 PM", "off":"07:15 AM","primary":true, "ids":["IN1","IN2","IN3","IN4"]},
        {"on":"04:30 AM", "off":"10:00 PM","primary":false, "ids":["IN4"]}
    ]
}

Device and Webapp as clients:


While the api microservice is central to both the webapp and the clients, its design needs to be open for extension, closed for modification. Webapp and the device are just end clients that either get the data or set the data. These 2 items in no way shall enforce design constraints and data model.

Api microservice with the core package


The API microservice shall have the core package that would be a shared package containing the business logic of the application. Device / webapp may use it for referencing data models or service functions.

Now the question remains, from the device perspective should this api, communicate further to the authapi and register the device ? or or should it be the device's responsibility to do the same independently ?

Encapsulated authapi


This means any changes to the authapi shall enforce changes to luminapi only, and leave the devices unaffected. Since now the device has dependency on only the luminapi and not on the authapi this reduces the impact of changes. Luminapi in this case shall have to provide all the encapsulating functions and act like a call fwding microservice. This in my opinion is a serious duplication of code and effort. And given the microservices approach its grossly unnecessary.

Peerside authapi


This denotes luminapi is agnostic of authapi and its the device/app that independently verifies the registeration with 2 different microservices. While in this method we can avoid the code duplication, any changes on authapi can and will affect the the clients (webapp and the device). While its easy to reconfing the webapp, pushing changes to the device could be really intricate.

Recommendation :


Peerside design is fine, authapi can be versioned and device code need not change at all.

Registering a new device:


Besides registering itself on the authapi server, the device will also attempt to register itself on this microservice as well. When doing so it receives a set of default schedules to start with. Default schedules are generated on the server side and sent back to the device. _This is to maintain the server side as the single source of truth (SSoT)

luminapi's People

Contributors

kneerunjun avatar

Stargazers

Zan Ye Htet avatar  avatar frankfanslc avatar

Watchers

James Cloos avatar  avatar

Forkers

frankfanslc

luminapi's Issues

UI issues

image
Check this - the ui does not appear correct in the full browser mode

Error check on PATCH schedules

PATCH  http://localhost/api/v1/devices/000000007920365y HTTP/1.1
Content-Type: application/json

{
    "serial":"000000007920365c",
    "scheds" : [
        {"on":"06:00 PM", "off": "06:00 AM", "primary":true, "ids":["IN1","IN2","IN3","IN4"]},
        {"on":"06:01 PM", "off": "06:10 PM", "primary":false, "ids":["IN1","IN2"]}
    ]
}

param serial = 000000007920365y , serial in payload = 000000007920365c
This has to be checked as an error and the request should be denied completion

When blacklisting the device the luminapi registration also needs to be cleared

this.blacklist_device = function(serial, black){
// Patches the device for lock / unlock status
var defered = $q.defer();
authInfo = lclStorage.get_auth()
execute_request({
method :"PATCH",
url:baseURL.auth+"/devices/"+serial+"?black="+black,
headers:{
'Content-Type': "application/json",
'Authorization': "Bearer "+ authInfo.authtok,
}
},defered)
return defered.promise;
}

Watch these lines, when blacklisting the device(s) this though clears the device registration from the authapi database the device registration on luminapi device though is not changed. Ideally this should be cleared as well.
Blacking the device is often used to clear the registration before it gets assigned to a new owner. If the luminapi database isnt cleared the previous schedule and settings would be flow down into new registration. - Ideally a new in default setting shoudl take effect

Dynamic base url for webapp

$provide.provider("baseURL", function(){
            // change this when the subdomain changes and all the services will follow
            this.$get = function(){
                return {
                    auth : "http://auth.eensymachines.in",
                    // TODO: before moving to production change this uri
                    lumin: "http://lumin.eensymachines.in/api/v1/devices",
                    cmds: "http://lumin.eensymachines.in/api/v1/cmds"
                    // lumin: "http://192.168.0.40/api/v1/devices",
                    // cmds: "http://192.168.0.40/api/v1/cmds"
                }
            }
        });

There has to be a better / dynamic way to do this

Volumes for nginx container can be mounted from the environment file

services:
  nginx:
    build: ./nginx
    ports:
      - "80:80"
    volumes: 
      # for local development the volume mapping changes
      # - "/home/niranjan/repos/eensymachines.in/luminapi/nginx/luminapp:/var/www/luminapp"
      - "/root/repos/eensymachines.in/luminapi/nginx/luminapp:/var/www/luminapp"

Its counter productive to change the volume definition everytime. need to pick this from environment file

API support for device commands

We could have provided hardware to restart/ shutdown device. Adding hardware seems to be too mainstream and the whole idea of having an IoT device is to have maximum features operable from the handheld.
API needs to route the command to mqtt broker under a suitable topic so that device picks up and executes the same on priority

Patch schedules

luminapi/handlers.go

Lines 79 to 157 in 4b10c4d

func HandlDevice(c *gin.Context) {
val, exists := c.Get("devreg")
if !exists {
errx.DigestErr(errx.NewErr(errx.ErrConnFailed{}, nil, "failed connection to database", "HandlDevices/dbConnect"), c)
return
}
devreg, _ := val.(*core.DevRegsColl)
val, _ = c.Get("db_close")
dbClose := val.(func())
defer dbClose()
serial := c.Param("serial")
if c.Request.Method == "DELETE" {
if errx.DigestErr(devreg.UnRegister(serial), c) != 0 {
return
}
c.JSON(http.StatusOK, gin.H{})
return
} else if c.Request.Method == "PATCH" {
val, _ = c.Get("mqttclient")
mqttClient, _ := val.(mqtt.Client)
defer mqttClient.Disconnect(250) // this is important to dispose
// read in the the schedules that need to be patched
// If the device is not registered, the middleware will handle it
payload := []scheduling.JSONRelayState{}
if c.ShouldBindJSON(&payload) != nil {
errx.DigestErr(errx.NewErr(errx.ErrJSONBind{}, nil, "failed to read new schedules", "HandlDevice/ShouldBindJSON"), c)
return
}
// ++++++++++++ here we check for any conflicts within the schedules
sojrs := scheduling.SliceOfJSONRelayState(payload)
scheds := []scheduling.Schedule{}
sojrs.ToSchedules(&scheds)
conflicting := []scheduling.JSONRelayState{} // from the payload this will get the conflicting ones
for i, s := range scheds {
if s.Conflicts() > 0 {
// atleast one of the schedules has conflicts
// we will accumulate the conflicting schdules in on temp array
conflicting = append(conflicting, payload[i])
}
}
// accumulated conflicts are then sent back as payload
// TODO: test here from the client
if len(conflicting) > 0 {
c.JSON(http.StatusBadRequest, gin.H{
"message": "One or more schedules have conflicts",
"conflicts": conflicting,
})
log.WithFields(log.Fields{
"conflicting": conflicting,
}).Warn("We have atleast one schedule that has conflicts")
return
}
// ++++++++++++ all the below code will run only if there arent any conflicting schedules
// Conflicts if found then would send back the schedules as is ErrInvalid
if errx.DigestErr(devreg.UpdateSchedules(serial, payload), c) != 0 {
return
}
// marshal the json string - set the topic and then off it goes
mqttText, err := json.Marshal(payload)
if err != nil {
errx.DigestErr(err, c)
}
token := mqttClient.Publish(fmt.Sprintf("%s/schedules", serial), 0, false, string(mqttText))
token.Wait()
// time.Sleep(1 * time.Second)
c.JSON(http.StatusOK, gin.H{})
return
} else if c.Request.Method == "GET" {
// Gets the schedules for a device given the serial of the device
result := []scheduling.JSONRelayState{}
if errx.DigestErr(devreg.GetSchedules(serial, &result), c) != 0 {
return
}
c.JSON(http.StatusOK, result)
return
}
}

Checking for conflicts before submit

Speculation :


If before the submit we check for coflicts would it or not be convenient to the user ? and if it is then would the redundancy be worth the price of re-implementation in js. If the api incase of error already sends back the conflicting schedules, would it make more sense to use that feature to convey to the user the conflicting schedules ?

End goal : We need to indicate to the user of conflicting schedules, and this has been implemented already once on the server side

User interface rudimentary review with a user

Modifying the schedules of a device is not apparently st. fwd Since the logic of applying schedules is a bit convoluted we needed to design the UI so that we insulate the end user from most of the drudgery and present a simple interface, yet get the required details everytime. Representing such data graphically is the next obvious choice.
Since time is not on our side at this juncture of prototype development, we have tried to fit the ui in bootstrap components.

Need to port onto errx

since this is an API that we are building we need to be sure we pass on the err information thats compatible to log and debug

device details has more details than schedules now

API has a face lift and schedules are sent out as device details. We have the relay maps and logs as well in the same payload. Prior to this we were only dealing with array of data as payload, but now we are expecting an object with all the device details

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.