async def _on_boot_notification( self, charge_point_vendor, charge_point_model, **kwargs) -> call_result.BootNotificationPayload: self._data_object.boot_timestamp = utc_datetime() self._session.commit() return call_result.BootNotificationPayload( current_time=utc_datetime().isoformat(), interval=60, status=RegistrationStatus.accepted)
async def _on_status_notification( self, connector_id: int, error_code: ChargePointErrorCode, status: ChargePointStatus, timestamp: str = None, info: str = None, vendor_id: str = None, vendor_error_code: str = None, **kwargs) -> call_result.StatusNotificationPayload: # Get the connector object this notification references connector = self._get_connector(connector_id) # Generate the database object err = ChargePointError( connector=connector, error_code=error_code, status=status, timestamp=timestamp if timestamp else utc_datetime(), info=info, vendor_id=vendor_id, vendor_error_code=vendor_error_code) # TODO: Prevent logging non error status messages # self._session.add(err) # self._session.commit() return call_result.StatusNotificationPayload()
async def send_stop_transaction(self, id): request = call.StopTransactionPayload( meter_stop=123, timestamp=utc_datetime().isoformat(), transaction_id=id, id_tag='150C8596' ) response = await self.call(request) print(response)
async def send_start_transaction(self) -> int: request = call.StartTransactionPayload( connector_id=1, id_tag='150C8596', meter_start=0, timestamp=utc_datetime().isoformat() ) response: call_result.StartTransactionPayload = await self.call(request) print(response) return response.transaction_id
def get_usage(db: Session, kwh: bool = True, count: bool = True, bucket: BucketType = BucketType.Days, chargepoint: str = None, from_timestamp: Optional[datetime] = None, to_timestamp: Optional[datetime] = None) -> List[KeyedTuple]: """ Get the usage of the charging stations """ args = [func.date(models.Transaction.start_timestamp).label('date')] if count: args.append(func.count(models.Transaction.id).label('count')) if kwh: args.append(func.sum(models.Transaction.meter_used).label('kwh')) query = db.query(*args) if chargepoint: # Transaction -> Connector -> ChargePoint query = query.filter( models.Transaction.connector.has( models.Connector.chargepoint.has( models.ChargePoint.identity == chargepoint))) # Apply time filters if from_timestamp: query = query.filter( models.Transaction.start_timestamp > from_timestamp) if to_timestamp: query = query.filter(models.Transaction.end_timestamp < to_timestamp) # Generate date series timeseries = db.query( func.date( func.generate_series( func.min(from_timestamp or models.Transaction.start_timestamp), func.max(to_timestamp or utc_datetime()), timedelta(days=1))).label('date')).subquery() # Group values data = query.group_by('date').subquery() values = [] if count: values.append(func.coalesce(data.c.count, 0).label('count')) if kwh: values.append(func.coalesce(data.c.kwh / 1000.0, 0).label('kWh')) return db.query(timeseries.c.date, *values).\ outerjoin(data, data.c.date == timeseries.c.date).\ order_by(timeseries.c.date).all()
async def _on_authorize(self, id_tag: str, **kwargs) -> call_result.AuthorizePayload: token = self._get_token(id_tag) # Log all authorization requests auth_req = AuthorizationRequest(token_string=id_tag, chargepoint=self._data_object, timestamp=utc_datetime()) self._session.add(auth_req) self._session.commit() # TODO: Add parent tag if exists return call_result.AuthorizePayload( id_tag_info={ 'status': AuthorizationStatus.accepted if token else AuthorizationStatus. blocked })
async def _on_heartbeat(self, **kwargs) -> call_result.HeartbeatPayload: self._data_object.last_heartbeat = utc_datetime() self._session.commit() return call_result.HeartbeatPayload( current_time=utc_datetime().isoformat())
def log_message(message, path: Optional[str] = None): col = db[path if path else 'ocpp_msgs'] col.insert_one({**message, 'timestamp': utc_datetime()})
def log_connect(point_id: str) -> None: col = db['connections'] col.insert_one({'chargepoint_id': point_id, 'timestamp': utc_datetime()})