Skip to content

Commit

Permalink
fix(modbus): fix points creation / changes and modbus client library
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerni10 committed Apr 6, 2023
1 parent db42b91 commit ed663b2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
13 changes: 6 additions & 7 deletions src/frontend/components/points-component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { useEffect } from 'react'
import { Button, Input } from 'reactstrap'

import PropTypes from 'prop-types'
import objectPath from 'object-path'
import { nanoid } from 'nanoid'
import Table from './table/table.jsx'
import TablePagination from './table/table-pagination.jsx'
Expand Down Expand Up @@ -53,11 +52,10 @@ const PointsComponent = ({
* @returns {void}
*/
const handleAddPoint = () => {
const newPoint = Object.entries(schema.points).map(([name]) => name).reduce((previousValue, currentValue) => {
objectPath.set(previousValue, currentValue, '')
return previousValue
}, {})
newPoint.id = nanoid()
const newPoint = { id: nanoid() }
Object.entries(schema.points).forEach(([key, value]) => {
newPoint[key] = value.defaultValue
})
const newAllPoints = [...allPoints]
newAllPoints.unshift(newPoint)
setAllPoints(newAllPoints)
Expand All @@ -71,6 +69,7 @@ const PointsComponent = ({
const pointToUpdate = newAllPoints.find((point) => point.id === id)
pointToUpdate[name] = value
setAllPoints(newAllPoints)
onChange(prefix, newAllPoints)
}

/**
Expand Down Expand Up @@ -167,7 +166,7 @@ const PointsComponent = ({
onPagePressed={(page) => setSelectedPage(page)}
/>
)}
<div className="force-row-display">
<div className="force-row-display mt-3">
<Button className="inline-button" color="primary" onClick={() => document.getElementById('importFile').click()}>
Import
</Button>
Expand Down
8 changes: 5 additions & 3 deletions src/south/south-modbus/south-modbus.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import net from 'node:net'

import modbus from 'jsmodbus'
import { client } from 'jsmodbus'

import SouthConnector from '../south-connector.js'
import getNumberOfWords from './utils.js'
Expand Down Expand Up @@ -209,16 +209,18 @@ export default class SouthModbus extends SouthConnector {
clearTimeout(this.reconnectTimeout)
}
this.socket = new net.Socket()
this.client = new modbus.client.TCP(this.socket, this.slaveId)
this.client = new client.TCP(this.socket, this.slaveId)
this.logger.debug(`Connecting Modbus socket into ${this.host}:${this.port}`)
this.socket.connect(
{ host: this.host, port: this.port },
async () => {
this.logger.info(`Modbus socket connected to ${this.host}:${this.port}`)
await super.connect()
resolve()
},
)
this.socket.on('error', async (error) => {
this.logger.error(error)
this.logger.error(`Modbus socket error: ${error}`)
await this.disconnect()
this.reconnectTimeout = setTimeout(this.connect.bind(this), this.retryInterval)
})
Expand Down
2 changes: 1 addition & 1 deletion src/south/south-modbus/south-modbus.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe('SouthModbus', () => {
mockedEmitter.emit('error', 'connect error')
await flushPromises()
expect(south.disconnect).toHaveBeenCalledTimes(1)
expect(south.logger.error).toHaveBeenCalledWith('connect error')
expect(south.logger.error).toHaveBeenCalledWith('Modbus socket error: connect error')
jest.advanceTimersByTime(configuration.settings.retryInterval)

expect(net.Socket).toHaveBeenCalledTimes(2)
Expand Down

0 comments on commit ed663b2

Please sign in to comment.