Exemplo n.º 1
0
// Connect Builds a new ninja connection to the MQTT broker, using the given client ID
func Connect(clientID string) (*Connection, error) {

	log := logger.GetLogger(fmt.Sprintf("%s.connection", clientID))

	conn := Connection{
		log:      log,
		services: []model.ServiceAnnouncement{},
	}

	mqttURL := fmt.Sprintf("%s:%d", config.MustString("mqtt", "host"), config.MustInt("mqtt", "port"))

	log.Infof("Connecting to %s using cid:%s", mqttURL, clientID)

	conn.mqtt = bus.MustConnect(mqttURL, clientID)

	log.Infof("Connected")

	conn.rpc = rpc.NewClient(conn.mqtt, json2.NewClientCodec())
	conn.rpcServer = rpc.NewServer(conn.mqtt, json2.NewCodec())

	// Add service discovery service. Responds to queries about services exposed in this process.
	discoveryService := &discoverService{&conn}
	_, err := conn.exportService(discoveryService, "$discover", &simpleService{*discoveryService.GetServiceAnnouncement()})
	if err != nil {
		log.Fatalf("Could not expose discovery service: %s", err)
	}

	return &conn, nil
}
Exemplo n.º 2
0
func btcdbSetup(dataDir, dbType string) (log btclog.Logger, db btcdb.Db, cleanup func()) {
	// Setup logging
	backendLogger := btclog.NewDefaultBackendLogger()
	log = btclog.NewSubsystemLogger(backendLogger, "")
	btcdb.UseLogger(log)

	// Setup database access
	blockDbNamePrefix := "blocks"
	dbName := blockDbNamePrefix + "_" + dbType
	if dbType == "sqlite" {
		dbName = dbName + ".db"
	}
	dbPath := filepath.Join(dataDir, "mainnet", dbName)

	log.Infof("loading db %v", dbType)
	db, err := btcdb.OpenDB(dbType, dbPath)
	if err != nil {
		log.Warnf("db open failed: %v", err)
		return
	}
	log.Infof("db load complete")

	cleanup = func() {
		db.Close()
		backendLogger.Flush()
	}

	return
}
Exemplo n.º 3
0
func main() {
	log.Println("Starting")

	_, err := zigbee.ConnectToOtaServer(hostname, otasrvrPort)
	if err != nil {
		// handle error
		log.Printf("Error connecting otasrvr %s", err)
	}

	nwkmgrConn, err := zigbee.ConnectToNwkMgrServer(hostname, nwkmgrPort)
	if err != nil {
		// handle error
		log.Printf("Error connecting nwkmgr %s", err)
	}

	gatewayConn, err := zigbee.ConnectToGatewayServer(hostname, gatewayPort)
	if err != nil {
		// handle error
		log.Printf("Error connecting nwkmgr %s", err)
	}

	//gatewayConn.pendingResponses = make(map[uint8]*pendingGatewayResponse)

	//command := &nwkmgr.NwkZigbeeNwkInfoReq{}
	response := &nwkmgr.NwkGetLocalDeviceInfoCnf{}
	err = nwkmgrConn.SendCommand(&nwkmgr.NwkGetLocalDeviceInfoReq{}, response)

	if err != nil {
		log.Fatalf("Failed to get local device info: %s", err)
	}
	log.Println("Local device info: ")
	outJSON(response)

	joinTime := uint32(30)
	permitJoinRequest := &nwkmgr.NwkSetPermitJoinReq{
		PermitJoinTime: &joinTime,
		PermitJoin:     nwkmgr.NwkPermitJoinTypeT_PERMIT_ALL.Enum(),
	}

	permitJoinResponse := &nwkmgr.NwkZigbeeGenericCnf{}

	err = nwkmgrConn.SendCommand(permitJoinRequest, permitJoinResponse)
	if err != nil {
		log.Fatalf("Failed to enable joining: %s", err)
	}
	if permitJoinResponse.Status.String() != "STATUS_SUCCESS" {
		log.Fatalf("Failed to enable joining: %s", permitJoinResponse.Status)
	}
	log.Println("Permit join response: ")
	outJSON(permitJoinResponse)

	deviceListResponse := &nwkmgr.NwkGetDeviceListCnf{}

	err = nwkmgrConn.SendCommand(&nwkmgr.NwkGetDeviceListReq{}, deviceListResponse)
	if err != nil {
		log.Fatalf("Failed to get device list: %s", err)
	}
	log.Printf("Found %d device(s): ", len(deviceListResponse.DeviceList))
	outJSON(deviceListResponse)

	for _, device := range deviceListResponse.DeviceList {
		log.Printf("Got device : %d", device.IeeeAddress)
		for _, endpoint := range device.SimpleDescList {
			log.Printf("Got endpoint : %d", endpoint.EndpointId)

			if containsUInt32(endpoint.InputClusters, 0x06) {
				log.Printf("This endpoint has on/off cluster")

				toggleRequest := &gateway.DevSetOnOffStateReq{
					DstAddress: &gateway.GwAddressStructT{
						AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
						IeeeAddr:    device.IeeeAddress,
					},
					State: gateway.GwOnOffStateT_TOGGLE_STATE.Enum(),
				}

				powerRequest := &gateway.DevGetPowerReq{
					DstAddress: &gateway.GwAddressStructT{
						AddressType: gateway.GwAddressTypeT_UNICAST.Enum(),
						IeeeAddr:    device.IeeeAddress,
					},
				}

				for i := 0; i < 3; i++ {
					log.Println("Toggling on/off device")

					confirmation := &gateway.GwZigbeeGenericCnf{}

					err = gatewayConn.SendCommand(toggleRequest, confirmation)
					if err != nil {
						log.Fatalf("Failed to toggle device: ", err)
					}
					log.Printf("Got on/off confirmation")
					if confirmation.Status.String() != "STATUS_SUCCESS" {
						log.Fatalf("Failed to request the device to toggle. Status:%s", confirmation.Status.String())
					}

					response := &gateway.GwZigbeeGenericRspInd{}
					err = gatewayConn.WaitForSequenceResponse(confirmation.SequenceNumber, response)
					if err != nil {
						log.Fatalf("Failed to get on/off response: ", err)
					}

					log.Printf("Got toggle response from device! Status: %s", response.Status.String())

					//time.Sleep(10000 * time.Millisecond)

					confirmation = &gateway.GwZigbeeGenericCnf{}

					err = gatewayConn.SendCommand(powerRequest, confirmation)
					if err != nil {
						log.Fatalf("Failed to request power: ", err)
					}
					log.Printf("Got power request confirmation")
					if confirmation.Status.String() != "STATUS_SUCCESS" {
						log.Fatalf("Failed to request the power. Status:%s", confirmation.Status.String())
					}

					powerResponse := &gateway.DevGetPowerRspInd{}
					err = gatewayConn.WaitForSequenceResponse(confirmation.SequenceNumber, powerResponse)
					if err != nil {
						log.Fatalf("Failed to get power response: ", err)
					}

					//	log.Printf("Got power level from device! Status: %s", powerResponse.Status.String())

					outJSON(powerResponse)

				}

			}

		}
		//GwOnOffStateT_TOGGLE_STATE
	}

	//time.Sleep(2000 * time.Millisecond)

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)
	log.Infof("Got signal:", <-c)

}