1
0

motor-control-protocol.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const modbusAccess = require('./modbus-access')
  2. const {
  3. parseHexInteger
  4. } = require('./base-utils')
  5. const {
  6. controlButtonRegisters
  7. } = require('./registers')
  8. function getControlButton(key) {
  9. return controlButtonRegisters.find((item) => item.key === key) || null
  10. }
  11. function getControlButtonWriteValue(button) {
  12. if (!button) return 0
  13. return button.writeValue
  14. }
  15. async function writeControlButton(button, options = {}) {
  16. if (!button) return false
  17. const slaveAddress = modbusAccess.getSharedSlaveAddress()
  18. if (slaveAddress === null) return false
  19. const address = parseHexInteger(button.address)
  20. const coilEnabled = Number(getControlButtonWriteValue(button)) !== 0
  21. return modbusAccess.writeSingleCoil(
  22. slaveAddress,
  23. address,
  24. coilEnabled,
  25. options.label || button.name,
  26. options.kind || 'motor-control-write',
  27. {
  28. showModal: options.showModal
  29. }
  30. )
  31. }
  32. function writeControlButtonByKey(key, options = {}) {
  33. return writeControlButton(getControlButton(key), options)
  34. }
  35. function softReset(options = {}) {
  36. return writeControlButtonByKey('reset', {
  37. label: '软复位',
  38. kind: 'motor-control-soft-reset',
  39. showModal: false,
  40. ...options
  41. })
  42. }
  43. module.exports = {
  44. getControlButton,
  45. softReset,
  46. writeControlButton,
  47. writeControlButtonByKey
  48. }