Kaynağa Gözat

Added example folders with HDSv2 demo

TC pushbot 5 4 yıl önce
ebeveyn
işleme
2a130cf9dd

+ 18 - 0
console.go

@@ -129,6 +129,24 @@ func consoleCommandHandler(input string) string {
 			jsonString, _ := json.Marshal(userHandler.GetStoragePool())
 			return string(jsonString)
 		}
+	} else if len(chunk) > 0 && chunk[0] == "scan" {
+		if matchSubfix(chunk, []string{"scan", "all"}, 2, "") {
+			//scan all nearby arozos units
+			fmt.Println("Scanning (Should take around 10s)")
+			hosts := MDNS.Scan(10, "")
+			for _, host := range hosts {
+				fmt.Println(host)
+			}
+			return "OK"
+		} else if matchSubfix(chunk, []string{"scan", "aroz"}, 2, "") || matchSubfix(chunk, []string{"scan", "arozos"}, 2, "") {
+			//scan all nearby arozos units
+			fmt.Println("Scanning nearybe ArozOS Hosts (Should take around 10s)")
+			hosts := MDNS.Scan(10, "arozos.com")
+			for _, host := range hosts {
+				fmt.Println(host)
+			}
+			return "OK"
+		}
 	} else if len(chunk) > 0 && chunk[0] == "find" {
 		if matchSubfix(chunk, []string{"find", "module"}, 3, "list module {modulename}") {
 			//Display all loaded modules

+ 78 - 0
examples/HomeDynamic2/Base/Base.ino

@@ -0,0 +1,78 @@
+/*
+ * Home Dynamic System v2
+ * Designed by tobychui
+ * 
+ * This is the base module for implementing the Home Dynamic v2 Protocol with ArozOS 1.0
+ * For more examples, see other project folders.
+ * 
+ */
+
+#include <ESP8266WiFi.h>        // Include the Wi-Fi library
+#include <ESP8266WiFiMulti.h>   // Include the Wi-Fi-Multi library
+#include <ESP8266mDNS.h>        // Include the mDNS library
+
+ESP8266WiFiMulti wifiMulti;     // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
+
+//Change the properties of the IoT device
+const String DeviceName = "HDSv2-Demo"; //The name of this IoT device
+
+//Change the WiFi Settings
+void WiFiConfig(){
+  wifiMulti.addAP("Toby Room Automation", "homedynamicsystem"); 
+  //Add more WiFi AP here if nessary
+  //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
+  //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
+}
+
+//Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
+void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
+    //Define the domain of the HDSv2 devices
+    MDNS.addDynamicServiceTxt(p_hService, "domain","homedynamic.org");
+    MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2");
+
+    //Define the OEM written values
+    MDNS.addDynamicServiceTxt(p_hService, "uuid","00000000-0000-0000-0000-000000000000");
+    MDNS.addDynamicServiceTxt(p_hService, "model","Generic");
+    MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");
+    MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");
+    MDNS.addDynamicServiceTxt(p_hService, "version_build","0");
+}
+  
+
+void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
+  MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
+}
+
+void setup() {
+  //Use 115200 baudrate on serial monitor if you want to see what is happening to the device
+  Serial.begin(115200);
+  delay(10);
+  Serial.println('\n');
+
+  WiFiConfig();
+  
+  Serial.println("Connecting ...");
+  while (wifiMulti.run() != WL_CONNECTED) {
+    delay(500);
+    Serial.print('.');
+  }
+  Serial.println('\n');
+  Serial.print("Connected to ");
+  Serial.println(WiFi.SSID());
+  Serial.print("IP address:\t");
+  Serial.println(WiFi.localIP());
+
+  MDNS.setHostProbeResultCallback(hostProbeResult);
+  
+  if (!MDNS.begin(DeviceName)) {             // Start the mDNS responder for esp8266.local
+    Serial.println("Error setting up MDNS responder!");
+  }
+
+  //Advertise the port that you are using
+  MDNS.addService("http", "tcp", 12110);
+  Serial.println("mDNS responder started");
+}
+
+void loop() { 
+   MDNS.update();
+ }

+ 31 - 8
mod/network/mdns/mdns.go

@@ -48,7 +48,8 @@ func (m *MDNSHost) Close() {
 
 }
 
-func (m *MDNSHost) Scan(timeout int) []*NetworkHost {
+//Scan with given timeout and domain filter. Use m.Host.Domain for scanning similar typed devices
+func (m *MDNSHost) Scan(timeout int, domainFilter string) []*NetworkHost {
 	// Discover all services on the network (e.g. _workstation._tcp)
 	resolver, err := zeroconf.NewResolver(nil)
 	if err != nil {
@@ -62,14 +63,9 @@ func (m *MDNSHost) Scan(timeout int) []*NetworkHost {
 
 	go func(results <-chan *zeroconf.ServiceEntry) {
 		for entry := range results {
-			if stringInSlice("domain="+m.Host.Domain, entry.Text) {
-				//This is a ArOZ Online Host
-				/*
-					log.Println("HostName", entry.HostName)
-					log.Println("Port", entry.Port)
-					log.Println("AddrIPv4", entry.AddrIPv4)
-				*/
+			if domainFilter == "" {
 
+				//This is a ArOZ Online Host
 				//Split the required information out of the text element
 				TEXT := entry.Text
 				properties := map[string]string{}
@@ -93,6 +89,33 @@ func (m *MDNSHost) Scan(timeout int) []*NetworkHost {
 					MinorVersion: properties["version_minor"],
 				})
 
+			} else {
+				if stringInSlice("domain="+domainFilter, entry.Text) {
+					//This is a ArOZ Online Host
+					//Split the required information out of the text element
+					TEXT := entry.Text
+					properties := map[string]string{}
+					for _, v := range TEXT {
+						kv := strings.Split(v, "=")
+						if len(kv) == 2 {
+							properties[kv[0]] = kv[1]
+						}
+					}
+
+					//log.Println(properties)
+					discoveredHost = append(discoveredHost, &NetworkHost{
+						HostName:     entry.HostName,
+						Port:         entry.Port,
+						IPv4:         entry.AddrIPv4,
+						Domain:       properties["domain"],
+						Model:        properties["model"],
+						UUID:         properties["uuid"],
+						Vendor:       properties["vendor"],
+						BuildVersion: properties["version_build"],
+						MinorVersion: properties["version_minor"],
+					})
+
+				}
 			}
 
 		}

+ 1 - 1
mod/network/neighbour/neighbour.go

@@ -72,7 +72,7 @@ func (d *Discoverer) StartScanning(interval int, scanDuration int) {
 
 func (d *Discoverer) UpdateScan(scanDuration int) {
 	d.LastScanningTime = time.Now().Unix()
-	results := d.Host.Scan(scanDuration)
+	results := d.Host.Scan(scanDuration, d.Host.Host.Domain)
 	d.NearbyHosts = results
 }