13 Commits

Author SHA1 Message Date
7794fabaf3 publish cache 4
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:41:39 +01:00
6e6ff4c229 publish cache 3
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:39:53 +01:00
dc2175c298 publish cache 2
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:38:01 +01:00
6d8c5c25db publish cache
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:34:52 +01:00
ca08059e13 add car feedback 6
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:26:52 +01:00
6796bdd905 add car feedback 5
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:24:12 +01:00
6c208e32bf add car feedback 4
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:22:49 +01:00
d2ee8a80c2 add car feedback 3
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:20:13 +01:00
5e0127b571 add car feedback 2
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:17:25 +01:00
311d4cf555 add car feedback
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-06 14:15:03 +01:00
ad043b5921 add raw output 3
Some checks failed
ci/woodpecker/tag/woodpecker Pipeline failed
2025-12-05 15:00:22 +01:00
7c90962de1 add raw output 2
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-05 14:58:46 +01:00
3a4cd499a5 add raw output
All checks were successful
ci/woodpecker/tag/woodpecker Pipeline was successful
2025-12-05 14:44:24 +01:00
4 changed files with 37 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
global: global:
scan_interval: 1 scan_interval: 0.25
log_level: DEBUG log_level: INFO
mqtt: mqtt:
broker: emqx01-anonymous-cluster-internal.broker.svc.cluster.local broker: emqx01-anonymous-cluster-internal.broker.svc.cluster.local
@@ -123,10 +123,11 @@ output:
- name: pv_control - name: pv_control
publish_topic: IoT/PV/Control/State publish_topic: IoT/PV/Control/State
scan_rate: 1 scan_rate: 1
raw_output: true # use only for output device with only one register, name this register 'output'
slave_id: 1 slave_id: 1
registers: registers:
- address: 0x0001 - address: 0x0001
attribute: state attribute: output
name: State name: State
unit: "-" unit: "-"
register_type: holding register_type: holding
@@ -136,10 +137,11 @@ output:
enabled: true enabled: true
publish_topic: IoT/Car/Control/State publish_topic: IoT/Car/Control/State
scan_rate: 1 scan_rate: 1
raw_output: true # use only for output device with only one register, name this register 'output'
slave_id: 5 slave_id: 5
registers: registers:
- address: 0x0001 - address: 0x0001
attribute: state attribute: output
name: State name: State
unit: "-" unit: "-"
register_type: holding register_type: holding
@@ -228,3 +230,17 @@ output:
register_type: input register_type: input
data_type: float32 data_type: float32
adaptor: floatAdaptor adaptor: floatAdaptor
- name: car_feedback
enabled: true
publish_topic: IoT/Car/Feedback/State
scan_rate: 1
raw_output: true # use only for output device with only one register, name this register 'output'
slave_id: 7
registers:
- address: 0x0010
attribute: output
name: State
unit: "-"
register_type: holding
data_type: int32
adaptor: onOffAdaptor

View File

@@ -9,7 +9,7 @@ def floatAdaptor(i):
return float(f"{i:0.2f}") if i else 0.0 return float(f"{i:0.2f}") if i else 0.0
def onOffAdaptor(i): def onOffAdaptor(i):
return bool(i) return 'on' if bool(i) else 'off'
@@ -49,12 +49,13 @@ class FromDevices(AbstractMqttPublisher):
payload[registers.attribute] = value payload[registers.attribute] = value
payload['status'] = "Ok" payload['status'] = "Ok"
payload['cnt'] = cnt
payloadStr = json.dumps(payload) if not device.raw_output else str(payload['output'])
self.publish_with_cache(device.publish_topic, payloadStr)
logger.debug(f"mqtt message sent: {device.publish_topic} -> {payloadStr}")
except Exception as e: except Exception as e:
logger.error(f"Caught exception: {str(e)}") logger.error(f"Caught exception: {str(e)}")
payload['cnt'] = cnt
payloadStr = json.dumps(payload)
self.client.publish(device.publish_topic, payloadStr)
logger.debug(f"mqtt message sent: {device.publish_topic} -> {payloadStr}")
self.killEvent.wait(timeout=float(self.config.global_.scan_interval)) self.killEvent.wait(timeout=float(self.config.global_.scan_interval))

View File

@@ -25,6 +25,8 @@ class AbstractMqttPublisher(threading.Thread):
logger.info(f"mqtt client id: {client_id}") logger.info(f"mqtt client id: {client_id}")
self.client = mqtt.Client(client_id=client_id, userdata=self) self.client = mqtt.Client(client_id=client_id, userdata=self)
self.cache = {}
# consider this flag in the localLoop # consider this flag in the localLoop
self.killBill = False self.killBill = False
self.killEvent = threading.Event() self.killEvent = threading.Event()
@@ -62,3 +64,10 @@ class AbstractMqttPublisher(threading.Thread):
def onMessage(self, topic, payload): def onMessage(self, topic, payload):
logger.warning("mqtt unexpected message received: {} -> {}".format(topic, str(payload))) logger.warning("mqtt unexpected message received: {} -> {}".format(topic, str(payload)))
def publish_with_cache(self, topic, payload):
if topic in self.cache and self.cache[topic] == payload:
logger.debug(f"mqtt message unchanged, not publishing: {topic} -> {payload}")
return
self.cache[topic] = payload
self.client.publish(topic, payload)

View File

@@ -24,6 +24,7 @@ class OutputConfig(BaseModel):
enabled: bool = Field(default=True) enabled: bool = Field(default=True)
scan_rate: Optional[int] = Field(default=60) scan_rate: Optional[int] = Field(default=60)
publish_topic: str publish_topic: str
raw_output: Optional[bool] = Field(default=False)
slave_id: int slave_id: int
registers: List[RegisterConfig] registers: List[RegisterConfig]
@@ -50,7 +51,7 @@ class ModbusConfig(BaseModel):
class GlobalConfig(BaseModel): class GlobalConfig(BaseModel):
"""Global settings""" """Global settings"""
scan_interval: int scan_interval: float
log_level: str log_level: str