Skip to content

Commit 11df175

Browse files
committed
Modbus Tags with divisor
1 parent be21fb4 commit 11df175

File tree

9 files changed

+44
-16
lines changed

9 files changed

+44
-16
lines changed

client/dist/assets/i18n/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
"device.tag-property-address": "Address (ex. db5.dbb3 db4.dbx2.0 MB7)",
450450
"device.tag-property-min": "Min",
451451
"device.tag-property-max": "Max",
452+
"device.tag-property-divisor": "Divisor",
452453
"device.tag-property-address-offset": "Address offset (1-65536)",
453454
"device.tag-array-id": "Array ID:",
454455
"device.tag-array-value": "Value:",

client/dist/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@
3636
<body>
3737
<app-root>Loading...</app-root>
3838

39-
<script type="text/javascript" src="runtime.a66f828dca56eeb90e02.js"></script><script type="text/javascript" src="polyfills.709d3d016be532a51b50.js"></script><script type="text/javascript" src="scripts.b8e77019b9ff4d559277.js"></script><script type="text/javascript" src="main.4d163029f8285eeacdc6.js"></script></body>
39+
<script type="text/javascript" src="runtime.a66f828dca56eeb90e02.js"></script><script type="text/javascript" src="polyfills.709d3d016be532a51b50.js"></script><script type="text/javascript" src="scripts.b8e77019b9ff4d559277.js"></script><script type="text/javascript" src="main.dffa94ffc86ea13247da.js"></script></body>
4040
</html>

client/dist/main.4d163029f8285eeacdc6.js client/dist/main.dffa94ffc86ea13247da.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/src/app/_models/device.ts

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ export class Tag {
3535
min: string;
3636
/** not used yet */
3737
max: string;
38+
/** Valuie divisor, used by Modbus */
39+
divisor: number;
3840
/** not used yet */
3941
access: string;
4042
/** Options, used for WebAPI */

client/src/app/device/device-list/device-list.component.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ export class DeviceListComponent implements OnInit {
213213

214214
editTag(tag: Tag, checkToAdd: boolean) {
215215
let oldtag = tag.name;
216-
let temptag = JSON.parse(JSON.stringify(tag));
216+
let temptag: Tag = JSON.parse(JSON.stringify(tag));
217217
let dialogRef = this.dialog.open(TagPropertyComponent, {
218218
panelClass: 'dialog-property',
219219
data: { device: this.deviceSelected, tag: temptag, devices: this.devices },
@@ -236,6 +236,7 @@ export class DeviceListComponent implements OnInit {
236236
tag.memaddress = temptag.memaddress;
237237
tag.min = temptag.min;
238238
tag.max = temptag.max;
239+
tag.divisor = temptag.divisor;
239240
if (checkToAdd) {
240241
this.checkToAdd(tag, result.device);
241242
} else if (tag.id !== oldtag) {

client/src/app/device/tag-property/tag-property.component.html

+5-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ <h1 mat-dialog-title style="display:inline-block; cursor:move; padding-top: 15px
6262
<span>{{'device.tag-property-address-offset' | translate}}</span>
6363
<input numberOnly [(ngModel)]="data.tag.address" style="width: 350px" type="text" [disabled]="!memaddress.value">
6464
</div>
65+
<div class="my-form-field" style="display: inline-block;margin-bottom: 10px;padding-right: 10px;">
66+
<span>{{'device.tag-property-divisor' | translate}}</span>
67+
<input numberOnly [(ngModel)]="data.tag.divisor" style="width: 80px" type="text">
68+
</div>
6569
<!-- <div class="my-form-field" style="display: inline-block;margin-bottom: 10px;padding-right: 10px;">
6670
<span>{{'device.tag-property-min' | translate}}</span>
6771
<input numberOnly [(ngModel)]="data.tag.min" style="width: 80px" type="text">
@@ -75,7 +79,7 @@ <h1 mat-dialog-title style="display:inline-block; cursor:move; padding-top: 15px
7579
<span>{{error}}</span>
7680
</div>
7781
</div>
78-
</div>
82+
</div>
7983
</div>
8084
<div *ngSwitchCase="TypeOfDialog.List" style="width: 670px;">
8185
<h1 mat-dialog-title style="display:inline-block; cursor:move; padding-top: 15px" mat-dialog-draggable>

client/src/app/device/tag-property/tag-property.component.ts

+15-10
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
4444
@Inject(MAT_DIALOG_DATA) public data: any) {
4545

4646
this.tagType = TagType;
47-
if (this.data.device.type === DeviceType.OPCUA || this.data.device.type === DeviceType.BACnet || this.data.device.type === DeviceType.WebAPI) {
47+
if (this.isOpcua() || this.isBACnet() || this.isWebApi()) {
4848
this.dialogType = EditTagDialogType.Tree;
4949
this.config.height = '640px';
5050
this.config.width = '1000px';
51-
this.config.type = (this.data.device.type === DeviceType.WebAPI) ? 'todefine' : '';
52-
} else if (this.data.device.type === DeviceType.MQTTclient) {
51+
this.config.type = (this.isWebApi()) ? 'todefine' : '';
52+
} else if (this.isMqtt()) {
5353
this.dialogType = EditTagDialogType.List;
5454
} else {
5555
if (this.isModbus()) {
@@ -71,7 +71,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
7171

7272
ngOnInit() {
7373
if (this.dialogType === EditTagDialogType.Tree) {
74-
if (this.data.device.type === DeviceType.OPCUA || this.data.device.type === DeviceType.BACnet) {
74+
if (this.isOpcua() || this.isBACnet()) {
7575
this.subscriptionBrowse = this.hmiService.onDeviceBrowse.subscribe(values => {
7676
if (this.data.device.name === values.device) {
7777
if (values.error) {
@@ -93,7 +93,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
9393
}
9494
}
9595
});
96-
} else if (this.data.device.type === DeviceType.WebAPI) {
96+
} else if (this.isWebApi()) {
9797
this.hmiService.onDeviceWebApiRequest.subscribe(res => {
9898
if (res.result) {
9999
this.addTreeNodes(res.result);
@@ -103,7 +103,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
103103
this.hmiService.askWebApiProperty(this.data.device.property);
104104
}
105105
this.queryNext(null);
106-
} else if (this.dialogType === EditTagDialogType.List && this.data.device.type === DeviceType.MQTTclient) {
106+
} else if (this.dialogType === EditTagDialogType.List && this.isMqtt()) {
107107
this.subscriptionBrowse = this.hmiService.onDeviceBrowse.subscribe(value => {
108108
if (value.result === 'error') {
109109
this.discoveryError = value.result;
@@ -150,10 +150,10 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
150150

151151
onOkClick(): void {
152152
this.data.nodes = [];
153-
if (this.data.device.type === DeviceType.WebAPI) {
153+
if (this.isWebApi()) {
154154
let result = this.getSelectedTreeNodes(Object.values(this.treetable.nodes), null);
155155
this.data.nodes = result;
156-
} else if (this.data.device.type === DeviceType.MQTTclient) {
156+
} else if (this.isMqtt()) {
157157
let listcheck = {};
158158
Object.values(this.topicsList).forEach((topic:any) => {
159159
if (topic.checked && topic.enabled) {
@@ -175,6 +175,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
175175
this.data.nodes.push(t);
176176
}
177177
});
178+
} else if (this.isModbus()) {
178179
} else {
179180
Object.keys(this.treetable.nodes).forEach((key) => {
180181
let n: Node = this.treetable.nodes[key];
@@ -232,7 +233,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
232233
let node = new Node(n.id, n.name);
233234
node.class = n.class;
234235
node.property = this.getProperty(n);
235-
if (this.data.device.type === DeviceType.BACnet) {
236+
if (this.isBACnet()) {
236237
node.class = Node.strToType(n.class);
237238
node.type = n.type;
238239
var typetext = Object.values(BACnetObjectType)[n.type];
@@ -374,7 +375,7 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
374375

375376
queryNext(node: Node) {
376377
let n = (node) ? { id: node.id } : null;
377-
if (this.data.device.type === DeviceType.BACnet && node) {
378+
if (this.isBACnet() && node) {
378379
n['parent'] = (node.parent) ? node.parent.id : null;
379380
}
380381
this.hmiService.askDeviceBrowse(this.data.device.name, n);
@@ -413,6 +414,10 @@ export class TagPropertyComponent implements OnInit, OnDestroy {
413414
return (this.data.device.type === DeviceType.MQTTclient) ? true : false;
414415
}
415416

417+
isBACnet() {
418+
return (this.data.device.type === DeviceType.BACnet) ? true : false;
419+
}
420+
416421
checkMemAddress(memaddress) {
417422
if (memaddress === '000000' || memaddress === '100000') {
418423
this.data.tag.type = ModbusTagType.Bool;

client/src/assets/i18n/en.json

+1
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
"device.tag-property-address": "Address (ex. db5.dbb3 db4.dbx2.0 MB7)",
450450
"device.tag-property-min": "Min",
451451
"device.tag-property-max": "Max",
452+
"device.tag-property-divisor": "Divisor",
452453
"device.tag-property-address-offset": "Address offset (1-65536)",
453454
"device.tag-array-id": "Array ID:",
454455
"device.tag-array-value": "Value:",

server/runtime/devices/modbus/index.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function MODBUSclient(_data, _logger, _events) {
292292
if (data.tags[sigid]) {
293293
var memaddr = data.tags[sigid].memaddress;
294294
var offset = parseInt(data.tags[sigid].address) - 1; // because settings address from 1 to 65536 but communication start from 0
295-
var val = datatypes[data.tags[sigid].type].formatter(value);
295+
var val = datatypes[data.tags[sigid].type].formatter(convertValue(value, data.tags[sigid].divisor, true));
296296
_writeMemory(parseInt(memaddr), offset, val).then(result => {
297297
logger.info(`'${data.name}' setValue(${sigid}, ${val})`, true);
298298
}, reason => {
@@ -506,7 +506,7 @@ function MODBUSclient(_data, _logger, _events) {
506506
let value = items[itemidx].value;
507507
let tags = items[itemidx].Tags;
508508
tags.forEach(tag => {
509-
result[tag.name] = { id: tag.name, value: value, type: type };
509+
result[tag.name] = { id: tag.name, value: convertValue(value, tag.divisor), type: type };
510510
someval = true;
511511
});
512512
} else {
@@ -589,6 +589,20 @@ function MODBUSclient(_data, _logger, _events) {
589589
return ModbusMemoryAddress.HoldingRegisters;
590590
}
591591
}
592+
const convertValue = function (value, divisor, tosrc = false) {
593+
try {
594+
if (divisor && parseFloat(divisor)) {
595+
if (tosrc) {
596+
return value * parseFloat(divisor);
597+
} else {
598+
return value / parseFloat(divisor);
599+
}
600+
}
601+
} catch (err) {
602+
console.log(err);
603+
}
604+
return value;
605+
}
592606

593607
/**
594608
* Return the Items that are wit address and size in the range start, size

0 commit comments

Comments
 (0)