liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env node
 
'use strict'
 
const mqtt = require('../')
const pump = require('pump')
const path = require('path')
const fs = require('fs')
const concat = require('concat-stream')
const Writable = require('readable-stream').Writable
const helpMe = require('help-me')({
  dir: path.join(__dirname, '..', 'doc')
})
const minimist = require('minimist')
const split2 = require('split2')
 
function send (args) {
  const client = mqtt.connect(args)
  client.on('connect', function () {
    client.publish(args.topic, args.message, args, function (err) {
      if (err) {
        console.warn(err)
      }
      client.end()
    })
  })
  client.on('error', function (err) {
    console.warn(err)
    client.end()
  })
}
 
function multisend (args) {
  const client = mqtt.connect(args)
  const sender = new Writable({
    objectMode: true
  })
  sender._write = function (line, enc, cb) {
    client.publish(args.topic, line.trim(), args, cb)
  }
 
  client.on('connect', function () {
    pump(process.stdin, split2(), sender, function (err) {
      client.end()
      if (err) {
        throw err
      }
    })
  })
}
 
function start (args) {
  args = minimist(args, {
    string: ['hostname', 'username', 'password', 'key', 'cert', 'ca', 'message', 'clientId', 'i', 'id'],
    boolean: ['stdin', 'retain', 'help', 'insecure', 'multiline'],
    alias: {
      port: 'p',
      hostname: ['h', 'host'],
      topic: 't',
      message: 'm',
      qos: 'q',
      clientId: ['i', 'id'],
      retain: 'r',
      username: 'u',
      password: 'P',
      stdin: 's',
      multiline: 'M',
      protocol: ['C', 'l'],
      help: 'H',
      ca: 'cafile'
    },
    default: {
      host: 'localhost',
      qos: 0,
      retain: false,
      topic: '',
      message: ''
    }
  })
 
  if (args.help) {
    return helpMe.toStdout('publish')
  }
 
  if (args.key) {
    args.key = fs.readFileSync(args.key)
  }
 
  if (args.cert) {
    args.cert = fs.readFileSync(args.cert)
  }
 
  if (args.ca) {
    args.ca = fs.readFileSync(args.ca)
  }
 
  if (args.key && args.cert && !args.protocol) {
    args.protocol = 'mqtts'
  }
 
  if (args.port) {
    if (typeof args.port !== 'number') {
      console.warn('# Port: number expected, \'%s\' was given.', typeof args.port)
      return
    }
  }
 
  if (args['will-topic']) {
    args.will = {}
    args.will.topic = args['will-topic']
    args.will.payload = args['will-message']
    args.will.qos = args['will-qos']
    args.will.retain = args['will-retain']
  }
 
  if (args.insecure) {
    args.rejectUnauthorized = false
  }
 
  args.topic = (args.topic || args._.shift()).toString()
  args.message = (args.message || args._.shift()).toString()
 
  if (!args.topic) {
    console.error('missing topic\n')
    return helpMe.toStdout('publish')
  }
 
  if (args.stdin) {
    if (args.multiline) {
      multisend(args)
    } else {
      process.stdin.pipe(concat(function (data) {
        args.message = data
        send(args)
      }))
    }
  } else {
    send(args)
  }
}
 
module.exports = start
 
if (require.main === module) {
  start(process.argv.slice(2))
}