Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
tzechienchu / MQTT Service Singleton
Last active April 9, 2022 16:33
MQTT Service Singleton
var mqtt = require('mqtt');
var uuid = require('node-uuid');
// Import events module
var events = require('events');
var MQTTService = (function () {
var instance;
function init() {
const NOTIFICATION_MESSAGE = 'F9D4494D-62F8-4CBD-BCCA-CC3DB557276A'
var schedule = require('node-schedule');
var ScheduleService = (function () {
var instance;
function init() {
//
// * * * * * *
// ┬ ┬ ┬ ┬ ┬ ┬
// │ │ │ │ │ |
// │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
var kue = require('kue');
// Priority Level
// {
// low: 10
// , normal: 0
// , medium: -5
// , high: -10
// , critical: -15
// };
var CreateJobService = (function () {
@tzechienchu
tzechienchu / GeneratorWithPromise.js
Last active August 17, 2016 03:37
GeneratorWithPromise
var co = require('co')
LogDive.testORM = function(cb) {
var query = {
"where": {"id": "1234567890"},
include: ['photos','comments','likes','owner',{user:'profile'}]
}
co(function*() {
var insts = yield DataTable.find(query);
console.log(insts);
cb(null,insts);
var getCountryCode = promisify(myGoogleMapAPI.getGeoCodeByAddress);
var result = yield (allCountry.map(function(country,index){
var result = getCountryCode(country);
return {country:country,result:result}
}))
var ObjectID = require("bson-objectid");
webappfiles.genMongoId = function(objectId,cb) {
console.log(ObjectID(objectId)); //Correct One
console.log(MongoId(objectId)); //Wrong
cb(null,ObjectID(objectId))
}
@tzechienchu
tzechienchu / forEach
Last active August 17, 2016 03:36
forEach is Synchronous Operation. It Blocks.
forEach is Synchronous Operation. It Blocks.
@tzechienchu
tzechienchu / PromiseAndCallBack.js
Last active August 17, 2016 03:36
PromiseAndCallBack
// 修改后 既支持 callback 回调,又支持 Promise
var getInfo = function(uid, callback) {
return new Promise(function(resolve, reject) {
if (callback) {
resolve = function (ret) {
callback(null, ret);
};
reject = callback;
}
@tzechienchu
tzechienchu / json.js
Created August 10, 2016 01:59 — forked from geuis/json.js
Better error handling for JSON.parse (javascript)
(function(){
var parse = JSON.parse;
JSON = {
stringify: JSON.stringify,
validate: function(str){
@tzechienchu
tzechienchu / asyncloop.js
Created August 23, 2016 02:35
Async Loop
function asyncLoop(iterations, func, callback) {
var index = 0;
var done = false;
var loop = {
next: function() {
if (done) {
return;
}
if (index < iterations) {