mqtt prep 7

This commit is contained in:
Jokob-sk
2022-11-28 23:30:44 +11:00
parent c790b1ee88
commit 525f6c3bf1
2 changed files with 80 additions and 81 deletions

65
README.md Normal file → Executable file
View File

@@ -7,8 +7,7 @@ Scan the devices connected to your WIFI / LAN and alert you the connection of
unknown devices. It also warns if a "always connected" devices disconnects.
![Main screen][main]
*(Apologies for my English and my limited knowledge of Python, php and
JavaScript)*
# Docker image 🐳
[![Docker](https://img.shields.io/github/workflow/status/jokob-sk/Pi.Alert/docker?label=Build&logo=GitHub)](https://github.com/jokob-sk/Pi.Alert/actions/workflows/docker.yml)
@@ -17,7 +16,6 @@ JavaScript)*
[![Docker Pulls](https://img.shields.io/docker/pulls/jokobsk/pi.alert?label=Pulls&logo=docker&color=0aa8d2&logoColor=fff)](https://hub.docker.com/r/jokobsk/pi.alert)
[![Docker Pushed](https://img.shields.io/badge/dynamic/json?color=0aa8d2&logoColor=fff&label=Pushed&query=last_updated&url=https%3A%2F%2Fhub.docker.com%2Fv2%2Frepositories%2Fjokobsk%2Fpi.alert%2F&logo=docker&link=http://left&link=https://hub.docker.com/repository/docker/jokobsk/pi.alert)](https://hub.docker.com/r/jokobsk/pi.alert)
🥇 Pi.Alert credit goes to [pucherot/Pi.Alert](https://github.com/pucherot/Pi.Alert) <br/>
🐳 Docker Image: [jokobsk/Pi.Alert](https://registry.hub.docker.com/r/jokobsk/pi.alert) <br/>
📄 [Dockerfile](https://github.com/jokob-sk/Pi.Alert/blob/main/Dockerfile) <br/>
📚 [Dockerfile instructions](https://github.com/jokob-sk/Pi.Alert/blob/main//dockerfiles/README.md)
@@ -47,41 +45,30 @@ Up to three scanning methods are used:
examines the DHCP leases (addresses assigned) to find active devices
that were not discovered by the other methods.
## Components
The system consists of two parts:
### Back
In charge of:
- Scan the network searching connected devices using the scanning methods
described
- Store the information in the DB
- Report the changes detected by e-mail and/or other services ([Apprise](https://hub.docker.com/r/caronc/apprise), [Pushsafer](https://www.pushsafer.com/), [NTFY](https://ntfy.sh/), Webhooks ([sample JSON](docs/webhook_json_sample.json)))
- Optional speedtest for Device "Internet"
- DB cleanup tasks via cron
- a pialert-cli that helps to configure login and password
### Notification Service Support
- [Apprise](https://hub.docker.com/r/caronc/apprise)
- [Pushsafer](https://www.pushsafer.com/)
- [NTFY](https://ntfy.sh/)
- Webhooks ([sample JSON](docs/webhook_json_sample.json))
- (❗Experimental) Home Assistant (via [MQTT](https://www.home-assistant.io/integrations/mqtt/) (processing ~5s per device, deleting not supported, use [MQTT Explorer](https://mqtt-explorer.com/) for now))
| ![Report 1][report1] | ![Report 2][report2] |
| -------------------- | -------------------- |
### Front
### Security
There is a configurable login to prevent unauthorized use.
> * Set `PIALERT_WEB_PROTECTION = True` in `pialert.conf` to enable. The default password is `123456`.
> To change password run `/home/pi/pialert/back/pialert-cli`
A web frontend that allows:
- Manage the devices inventory and the characteristics
## Features
- Display in a visual way all the information collected by the back
- Sessions
- Connected devices
- Favorites
- Events
- Presence
- Concurrent devices
- Sessions, Connected devices, Favorites
- Events, Presence, Concurrent devices
- Down alerts
- IP's
- Manual Nmap scans
- Optional speedtest for Device "Internet"
- Manual Nmap scans, Optional speedtest for Device "Internet"
- Simple Network relationship display
- Maintenance tasks and Settings like:
- Status Infos (active scans, database size, backup counter)
@@ -89,9 +76,8 @@ A web frontend that allows:
- Language Selection (english, german, spanish)
- Light/Dark-Mode Switch
- Pause arp-scan
- DB maintenance tools
- DB Backup and Restore
- CSV Export / Import (Experimental)
- DB maintenance, Backup, Restore tools
- CSV Export / Import
- Help/FAQ Section
| ![Screen 1][screen1] | ![Screen 2][screen2] |
@@ -118,24 +104,6 @@ Instructions for [pucherot's original code can be found here](https://github.com
### [Versions History](docs/VERSIONS_HISTORY.md)
### Powered by:
| Product | Objetive |
| ------------- | ------------------------------------------------------- |
| Python | Programming language for the Back |
| PHP | Programming language for the Front-end |
| JavaScript | Programming language for the Front-end |
| Bootstrap | Front-end framework |
| Admin.LTE | Bootstrap template |
| FullCalendar | Calendar component |
| Sqlite | DB engine |
| Lighttpd | Webserver |
| arp-scan | Scan network using arp commands |
| Pi.hole | DNS Server with Ad-block |
| dnsmasq | DHCP Server |
| nmap | Network Scanner |
| zip | Filecompression Tool |
| speedtest-cli | Python SpeedTest https://github.com/sivel/speedtest-cli |
### License
GPL 3.0
[Read more here](LICENSE.txt)
@@ -146,15 +114,12 @@ Instructions for [pucherot's original code can be found here](https://github.com
Source of the selfhosted Fonts
https://github.com/adobe-fonts/source-sans
### Contact
pi.alert.application@gmail.com
***Suggestions and comments are welcome***
### Special thanks 🥇
This code is a collaborative body of work, with special thanks to:
- 🏆 [pucherot/Pi.Alert](https://github.com/pucherot/Pi.Alert) is the original creator od PiAlert
- [leiweibau](https://github.com/leiweibau/Pi.Alert): Things
- [Macleykun](https://github.com/Macleykun): Help with Dockerfile clean-up
- [Final-Hawk](https://github.com/Final-Hawk): Help with NTFY, styling and other fixes

View File

@@ -141,6 +141,12 @@ try:
except NameError:
mqttPassword = ''
try:
mqttQoS = MQTT_QOS
except NameError:
mqttQoS = 0
#===============================================================================
# MAIN
@@ -442,7 +448,6 @@ def update_devices_MAC_vendors (pArg = ''):
except subprocess.CalledProcessError as e:
# An error occured, handle it
print(e.output)
update_output = ""
# DEBUG
# update_args = ['./vendors_db_update.sh']
@@ -562,7 +567,7 @@ def scan_network ():
# Load current scan data
print ('\nProcessing scan results...')
print_log ('Save scanned devices')
save_scanned_devices (arpscan_devices)
save_scanned_devices (arpscan_devices, cycle_interval)
# Print stats
print_log ('Print Stats')
@@ -752,7 +757,8 @@ def read_DHCP_leases ():
# print (sql.rowcount)
#-------------------------------------------------------------------------------
def save_scanned_devices (p_arpscan_devices):
def save_scanned_devices (p_arpscan_devices, p_cycle_interval):
cycle = 1 # always 1, only one cycle supported
# Delete previous scan data
sql.execute ("DELETE FROM CurrentScan WHERE cur_ScanCycle = ?",
(cycle,))
@@ -773,7 +779,7 @@ def save_scanned_devices (p_arpscan_devices):
WHERE cur_MAC = PH_MAC
AND cur_ScanCycle = ? )""",
(cycle,
(int(startTime.strftime('%s')) - 60 * cycle),
(int(startTime.strftime('%s')) - 60 * p_cycle_interval),
cycle) )
# Check Internet connectivity
@@ -1843,6 +1849,7 @@ def send_apprise (html):
#-------------------------------------------------------------------------------
mqtt_connected_to_broker = 0
mqtt_sensors = []
def publish_mqtt(client, topic, message):
status = 1
@@ -1850,7 +1857,7 @@ def publish_mqtt(client, topic, message):
result = client.publish(
topic=topic,
payload=message,
qos=1,
qos=mqttQoS,
retain=True,
)
@@ -1875,6 +1882,27 @@ def create_generic_device(client):
device_sensor(client, deviceId, deviceName, 'sensor', 'unknown', 'wifi-alert')
# #-------------------------------------------------------------------------------
# def create_sensor(client, deviceId, deviceName, sensorType, sensorName, icon):
# global mqtt_sensors
# new_sensor_config = sensor_config(deviceId, deviceName, sensorType, sensorName, icon)
# if new_sensor_config in mqtt_sensors
# mqtt_sensors
# #-------------------------------------------------------------------------------
# class sensor_config:
# def __init__(self, deviceId, deviceName, sensorType, sensorName, icon):
# self.deviceId = deviceId
# self.deviceName = deviceName
# self.sensorType = sensorType
# self.sensorName = sensorName
# self.icon = icon
#-------------------------------------------------------------------------------
class device_sensor:
def __init__(self, client, deviceId, deviceName, sensorType, sensorName, icon):
@@ -1884,23 +1912,30 @@ class device_sensor:
self.sensorName = sensorName
self.icon = icon
message = '{ \
"name":"'+ deviceName +' '+sensorName+'", \
"state_topic":"system-sensors/'+sensorType+'/'+deviceId+'/state", \
"value_template":"{{value_json.'+sensorName+'}}", \
"unique_id":"'+deviceId+'_sensor_'+sensorName+'", \
"device": \
{ \
"identifiers": ["'+deviceId+'_sensor"], \
"manufacturer": "PiAlert", \
"name":"'+deviceName+'" \
}, \
"icon":"mdi:'+icon+'" \
}'
global mqtt_sensors
topic='homeassistant/'+sensorType+'/'+deviceId+'/'+sensorName+'/config'
if (self in mqtt_sensors) == False:
mqtt_sensors.append(self)
message = '{ \
"name":"'+ deviceName +' '+sensorName+'", \
"state_topic":"system-sensors/'+sensorType+'/'+deviceId+'/state", \
"value_template":"{{value_json.'+sensorName+'}}", \
"unique_id":"'+deviceId+'_sensor_'+sensorName+'", \
"device": \
{ \
"identifiers": ["'+deviceId+'_sensor"], \
"manufacturer": "PiAlert", \
"name":"'+deviceName+'" \
}, \
"icon":"mdi:'+icon+'" \
}'
topic='homeassistant/'+sensorType+'/'+deviceId+'/'+sensorName+'/config'
publish_mqtt(client, topic, message)
publish_mqtt(client, topic, message)
#-------------------------------------------------------------------------------
def mqtt_start():
@@ -1966,7 +2001,6 @@ def mqtt_start():
device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'is_new', 'bell-alert-outline')
device_sensor(client, deviceId, deviceNameDisplay, 'sensor', 'vendor', 'cog')
# update device sensors in home assistant
publish_mqtt(client, 'system-sensors/sensor/'+deviceId+'/state',
@@ -1992,8 +2026,8 @@ def mqtt_start():
# qos=1,
# retain=True,
# )
time.sleep(10)
client.loop()
# time.sleep(10)
# client.loop()