diff --git a/src/controllers/controller/task.js b/src/controllers/controller/task.js new file mode 100644 index 0000000..497051a --- /dev/null +++ b/src/controllers/controller/task.js @@ -0,0 +1,61 @@ +import TaskModal from '../../models/controller/task' + +const task = new TaskModal() + +export const create = async (ctx, next) => { + const data = ctx.request.body + const result = await task.create(data) + ctx.body = result + + next() +} + +export const update = async (ctx, next) => { + const data = ctx.request.body + const result = await task.update(data) + ctx.body = result + + next() +} + +export const updateByID = async (ctx, next) => { + const data = ctx.request.body + const id = ctx.params.id + const result = await task.updateByID(data, id) + ctx.body = result + + next() +} + +export const updateByAttr = async (ctx, next) => { + const data = ctx.request.body + const attr = ctx.params.attr + const value = ctx.params.value + const result = await task.updateByAttr(data, attr, value) + ctx.body = result + + next() +} + +export const clearDeleted = async (ctx, next) => { + const result = await task.clearDeleted() + ctx.body = result + + next() +} + +export const getByAttr = async (ctx, next) => { + const attr = ctx.params.attr + const value = ctx.params.value + const result = await task.getByAttr(attr, value) + ctx.body = result + + next() +} + +export const getAll = async (ctx, next) => { + const result = await task.getAll() + ctx.body = result + + next() +} diff --git a/src/controllers/index.js b/src/controllers/index.js index d5cf2e2..8056187 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -4,6 +4,7 @@ import * as auth from './auth' import * as controller from './product/controller' import * as device from './product/device' import * as collection from './file/collection' +import * as task from './controller/task' import * as recordingDataMeta from './file/recording_data_meta' import * as recordingDataRaw from './file/recording_data_raw' import * as recordingDataMini from './file/recording_data_mini' @@ -15,6 +16,7 @@ export default { controller, device, collection, + task, recordingDataMeta, recordingDataRaw, recordingDataMini diff --git a/src/db/database.js b/src/db/database.js index 62c6997..e1bc71e 100644 --- a/src/db/database.js +++ b/src/db/database.js @@ -24,6 +24,7 @@ db.sequelize = sequelize db.tutorials = require('./schema/tutirial.model')(sequelize, Sequelize) db.controllers = require('./schema/controller.model')(sequelize, Sequelize) +db.tasks = require('./schema/task.model')(sequelize, Sequelize) db.devices = require('./schema/device.model')(sequelize, Sequelize) db.collections = require('./schema/collection.model')(sequelize, Sequelize) db.recording_data_metas = require('./schema/recording_data_meta.model')(sequelize, Sequelize) diff --git a/src/db/schema/controller.model.js b/src/db/schema/controller.model.js index cb96c45..b3f984a 100644 --- a/src/db/schema/controller.model.js +++ b/src/db/schema/controller.model.js @@ -28,6 +28,10 @@ module.exports = (sequelize, Sequelize) => { type: Sequelize.STRING, defaultValue: '' }, + mqtt_id: { + type: Sequelize.STRING, + defaultValue: '' + }, mqtt_address: { type: Sequelize.STRING, defaultValue: '' @@ -64,28 +68,41 @@ module.exports = (sequelize, Sequelize) => { scanned: [] } }, + device_scanned: { + type: Sequelize.JSONB, + defaultValue: {} + }, + device_auto_join: { + type: Sequelize.JSONB, + defaultValue: {} + }, // project/task: { // running: [id...], // library: [id...] // } project: { - type: Sequelize.JSONB, - defaultValue: { - running: [], - library: [] - } + type: Sequelize.STRING, + defaultValue: '' }, task: { - type: Sequelize.JSONB, - defaultValue: { - running: [], - library: [] - } + type: Sequelize.STRING, + defaultValue: '' }, + // task: { + // type: Sequelize.JSONB, + // defaultValue: { + // running: [], + // library: [] + // } + // }, software_version: { type: Sequelize.STRING, defaultValue: '' }, + status: { + type: Sequelize.STRING, + defaultValue: '' + }, deleted: { type: Sequelize.BOOLEAN, defaultValue: false diff --git a/src/db/schema/task.model.js b/src/db/schema/task.model.js index 71876c1..49c3196 100644 --- a/src/db/schema/task.model.js +++ b/src/db/schema/task.model.js @@ -1,16 +1,33 @@ module.exports = (sequelize, Sequelize) => { const Task = sequelize.define('task', { name: { - type: Sequelize.STRING - }, - description: { type: Sequelize.STRING, defaultValue: '' }, - configuration: { + describe: { + type: Sequelize.STRING, + defaultValue: '' + }, + status: { + type: Sequelize.STRING, + defaultValue: '' + }, + device_config: { type: Sequelize.JSONB, defaultValue: {} }, + chart_config: { + type: Sequelize.JSONB, + defaultValue: {} + }, + formula_config: { + type: Sequelize.JSONB, + defaultValue: {} + }, + controller_id: { + type: Sequelize.INTEGER, + defaultValue: -1 + }, deleted: { type: Sequelize.BOOLEAN, defaultValue: false diff --git a/src/models/controller/task.js b/src/models/controller/task.js new file mode 100644 index 0000000..459bde5 --- /dev/null +++ b/src/models/controller/task.js @@ -0,0 +1,105 @@ +const db = require('../../db/database') + +// const Op = db.Sequelize.Op + +class TaskModel { + async create (req) { + console.log(req) + return db.tasks.create(req) + } + + async update (req) { + const ret = await db.tasks.update( + req, + { + where: {} + } + ) + return ret + } + + async updateByID (req, id) { + const ret = await db.tasks.update( + req, + { + where: { + id: id + } + } + ) + return ret + } + + async updateByAttr (req, attr, value) { + const whereObj = {} + const attrList = attr.split('-') + const valueList = value.split('-') + attrList.forEach((key, idx) => { + whereObj[key] = valueList[idx] + }) + + const ret = await db.tasks.update( + req, + { + where: whereObj + } + ) + return ret + } + + async clearDeleted () { + const ret = await db.tasks.delete( + { + where: { + deleted: true + } + } + ) + return ret + } + + async getByID (id) { + const ret = await db.tasks.findAll( + { + where: { + id: id + } + } + ) + return ret + } + + async getByName (name) { + const ret = await db.tasks.findAll( + { + where: { + name: name + } + } + ) + return ret + } + + async getByAttr (attr, value) { + const whereObj = {} + const attrList = attr.split('-') + const valueList = value.split('-') + attrList.forEach((key, idx) => { + whereObj[key] = valueList[idx] + }) + + const ret = await db.tasks.findAll( + { + where: whereObj + } + ) + return ret + } + + async getAll () { + const ret = await db.tasks.findAll() + return ret + } +} + +export default TaskModel diff --git a/src/models/product/controller.js b/src/models/product/controller.js index 3ffecb7..2e6c2be 100644 --- a/src/models/product/controller.js +++ b/src/models/product/controller.js @@ -4,12 +4,8 @@ const db = require('../../db/database') class ControllerModel { async create (req) { - return db.controllers.create({ - name: req.name, - mac_address: req.mac_address, - software_version: req.software_version, - wifi_name: req.wifi_name - }) + console.log(req) + return db.controllers.create(req) } async update (req) {